github.com/tomaszheflik/terraform@v0.7.3-0.20160827060421-32f990b41594/terraform/variables_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestVariables(t *testing.T) {
     9  	cases := map[string]struct {
    10  		Module   string
    11  		Env      map[string]string
    12  		Override map[string]interface{}
    13  		Error    bool
    14  		Expected map[string]interface{}
    15  	}{
    16  		"config only": {
    17  			"vars-basic",
    18  			nil,
    19  			nil,
    20  			false,
    21  			map[string]interface{}{
    22  				"a": "foo",
    23  				"b": []interface{}{},
    24  				"c": map[string]interface{}{},
    25  			},
    26  		},
    27  
    28  		"env vars": {
    29  			"vars-basic",
    30  			map[string]string{
    31  				"TF_VAR_a": "bar",
    32  				"TF_VAR_b": `["foo", "bar"]`,
    33  				"TF_VAR_c": `{"foo" = "bar"}`,
    34  			},
    35  			nil,
    36  			false,
    37  			map[string]interface{}{
    38  				"a": "bar",
    39  				"b": []interface{}{"foo", "bar"},
    40  				"c": map[string]interface{}{
    41  					"foo": "bar",
    42  				},
    43  			},
    44  		},
    45  
    46  		"override": {
    47  			"vars-basic",
    48  			nil,
    49  			map[string]interface{}{
    50  				"a": "bar",
    51  				"b": []interface{}{"foo", "bar"},
    52  				"c": map[string]interface{}{
    53  					"foo": "bar",
    54  				},
    55  			},
    56  			false,
    57  			map[string]interface{}{
    58  				"a": "bar",
    59  				"b": []interface{}{"foo", "bar"},
    60  				"c": map[string]interface{}{
    61  					"foo": "bar",
    62  				},
    63  			},
    64  		},
    65  
    66  		"override partial map": {
    67  			"vars-basic",
    68  			map[string]string{
    69  				"TF_VAR_c": `{"foo" = "a", "bar" = "baz"}`,
    70  			},
    71  			map[string]interface{}{
    72  				"c": map[string]interface{}{
    73  					"foo": "bar",
    74  				},
    75  			},
    76  			false,
    77  			map[string]interface{}{
    78  				"a": "foo",
    79  				"b": []interface{}{},
    80  				"c": map[string]interface{}{
    81  					"foo": "bar",
    82  					"bar": "baz",
    83  				},
    84  			},
    85  		},
    86  	}
    87  
    88  	for name, tc := range cases {
    89  		if name != "override partial map" {
    90  			continue
    91  		}
    92  
    93  		// Wrapped in a func so we can get defers to work
    94  		func() {
    95  			// Set the env vars
    96  			for k, v := range tc.Env {
    97  				defer tempEnv(t, k, v)()
    98  			}
    99  
   100  			m := testModule(t, tc.Module)
   101  			actual, err := Variables(m, tc.Override)
   102  			if (err != nil) != tc.Error {
   103  				t.Fatalf("%s: err: %s", name, err)
   104  			}
   105  			if err != nil {
   106  				return
   107  			}
   108  
   109  			if !reflect.DeepEqual(actual, tc.Expected) {
   110  				t.Fatalf("%s: expected: %#v\n\ngot: %#v", name, tc.Expected, actual)
   111  			}
   112  		}()
   113  	}
   114  }