github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/helper/diff/diff_test.go (about)

     1  package diff
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"sort"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/terraform/config"
    11  	"github.com/hashicorp/terraform/config/lang/ast"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func testConfig(
    16  	t *testing.T,
    17  	c map[string]interface{},
    18  	vs map[string]string) *terraform.ResourceConfig {
    19  	rc, err := config.NewRawConfig(c)
    20  	if err != nil {
    21  		t.Fatalf("err: %s", err)
    22  	}
    23  
    24  	if len(vs) > 0 {
    25  		vars := make(map[string]ast.Variable)
    26  		for k, v := range vs {
    27  			vars[k] = ast.Variable{Value: v, Type: ast.TypeString}
    28  		}
    29  
    30  		if err := rc.Interpolate(vars); err != nil {
    31  			t.Fatalf("err: %s", err)
    32  		}
    33  	}
    34  
    35  	return terraform.NewResourceConfig(rc)
    36  }
    37  
    38  func testResourceDiffStr(rd *terraform.InstanceDiff) string {
    39  	var buf bytes.Buffer
    40  
    41  	crud := "UPDATE"
    42  	if rd.RequiresNew() {
    43  		crud = "CREATE"
    44  	}
    45  
    46  	buf.WriteString(fmt.Sprintf(
    47  		"%s\n",
    48  		crud))
    49  
    50  	keyLen := 0
    51  	keys := make([]string, 0, len(rd.Attributes))
    52  	for key, _ := range rd.Attributes {
    53  		keys = append(keys, key)
    54  		if len(key) > keyLen {
    55  			keyLen = len(key)
    56  		}
    57  	}
    58  	sort.Strings(keys)
    59  
    60  	for _, attrK := range keys {
    61  		attrDiff := rd.Attributes[attrK]
    62  
    63  		v := attrDiff.New
    64  		if attrDiff.NewComputed {
    65  			v = "<computed>"
    66  		}
    67  		if attrDiff.NewRemoved {
    68  			v = "<removed>"
    69  		}
    70  
    71  		newResource := ""
    72  		if attrDiff.RequiresNew {
    73  			newResource = " (forces new resource)"
    74  		}
    75  
    76  		inOut := "IN "
    77  		if attrDiff.Type == terraform.DiffAttrOutput {
    78  			inOut = "OUT"
    79  		}
    80  
    81  		buf.WriteString(fmt.Sprintf(
    82  			"  %s %s:%s %#v => %#v%s\n",
    83  			inOut,
    84  			attrK,
    85  			strings.Repeat(" ", keyLen-len(attrK)),
    86  			attrDiff.Old,
    87  			v,
    88  			newResource))
    89  	}
    90  
    91  	return buf.String()
    92  }