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