github.com/nalum/terraform@v0.3.2-0.20141223102918-aa2c22ffeff6/terraform/resource_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/config"
     8  )
     9  
    10  func TestInstanceInfo(t *testing.T) {
    11  	cases := []struct {
    12  		Info   *InstanceInfo
    13  		Result string
    14  	}{
    15  		{
    16  			&InstanceInfo{
    17  				Id: "foo",
    18  			},
    19  			"foo",
    20  		},
    21  		{
    22  			&InstanceInfo{
    23  				Id:         "foo",
    24  				ModulePath: rootModulePath,
    25  			},
    26  			"foo",
    27  		},
    28  		{
    29  			&InstanceInfo{
    30  				Id:         "foo",
    31  				ModulePath: []string{"root", "consul"},
    32  			},
    33  			"module.consul.foo",
    34  		},
    35  	}
    36  
    37  	for i, tc := range cases {
    38  		actual := tc.Info.HumanId()
    39  		if actual != tc.Result {
    40  			t.Fatalf("%d: %s", i, actual)
    41  		}
    42  	}
    43  }
    44  
    45  func TestResourceConfigGet(t *testing.T) {
    46  	cases := []struct {
    47  		Config map[string]interface{}
    48  		Vars   map[string]string
    49  		Key    string
    50  		Value  interface{}
    51  	}{
    52  		{
    53  			Config: nil,
    54  			Key:    "foo",
    55  			Value:  nil,
    56  		},
    57  
    58  		{
    59  			Config: map[string]interface{}{
    60  				"foo": "${var.foo}",
    61  			},
    62  			Key:   "foo",
    63  			Value: "${var.foo}",
    64  		},
    65  
    66  		{
    67  			Config: map[string]interface{}{
    68  				"foo": "${var.foo}",
    69  			},
    70  			Vars:  map[string]string{"foo": "bar"},
    71  			Key:   "foo",
    72  			Value: "bar",
    73  		},
    74  
    75  		{
    76  			Config: map[string]interface{}{
    77  				"foo": []interface{}{1, 2, 5},
    78  			},
    79  			Key:   "foo.0",
    80  			Value: 1,
    81  		},
    82  
    83  		{
    84  			Config: map[string]interface{}{
    85  				"foo": []interface{}{1, 2, 5},
    86  			},
    87  			Key:   "foo.5",
    88  			Value: nil,
    89  		},
    90  	}
    91  
    92  	for i, tc := range cases {
    93  		var rawC *config.RawConfig
    94  		if tc.Config != nil {
    95  			var err error
    96  			rawC, err = config.NewRawConfig(tc.Config)
    97  			if err != nil {
    98  				t.Fatalf("err: %s", err)
    99  			}
   100  		}
   101  
   102  		rc := NewResourceConfig(rawC)
   103  		if tc.Vars != nil {
   104  			ctx := NewContext(&ContextOpts{Variables: tc.Vars})
   105  			err := rc.interpolate(
   106  				ctx.walkContext(walkInvalid, rootModulePath),
   107  				nil)
   108  			if err != nil {
   109  				t.Fatalf("err: %s", err)
   110  			}
   111  		}
   112  
   113  		v, _ := rc.Get(tc.Key)
   114  		if !reflect.DeepEqual(v, tc.Value) {
   115  			t.Fatalf("%d bad: %#v", i, v)
   116  		}
   117  	}
   118  }