github.com/mingfang/terraform@v0.11.12-beta1/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  		"bools: config only": {
    88  			"vars-basic-bool",
    89  			nil,
    90  			nil,
    91  			false,
    92  			map[string]interface{}{
    93  				"a": "1",
    94  				"b": "0",
    95  			},
    96  		},
    97  
    98  		"bools: override with string": {
    99  			"vars-basic-bool",
   100  			nil,
   101  			map[string]interface{}{
   102  				"a": "foo",
   103  				"b": "bar",
   104  			},
   105  			false,
   106  			map[string]interface{}{
   107  				"a": "foo",
   108  				"b": "bar",
   109  			},
   110  		},
   111  
   112  		"bools: override with env": {
   113  			"vars-basic-bool",
   114  			map[string]string{
   115  				"TF_VAR_a": "false",
   116  				"TF_VAR_b": "true",
   117  			},
   118  			nil,
   119  			false,
   120  			map[string]interface{}{
   121  				"a": "false",
   122  				"b": "true",
   123  			},
   124  		},
   125  
   126  		"bools: override with bool": {
   127  			"vars-basic-bool",
   128  			nil,
   129  			map[string]interface{}{
   130  				"a": false,
   131  				"b": true,
   132  			},
   133  			false,
   134  			map[string]interface{}{
   135  				"a": "0",
   136  				"b": "1",
   137  			},
   138  		},
   139  
   140  		"override map with string": {
   141  			"vars-basic",
   142  			map[string]string{
   143  				"TF_VAR_c": `{"foo" = "a", "bar" = "baz"}`,
   144  			},
   145  			map[string]interface{}{
   146  				"c": "bar",
   147  			},
   148  			true,
   149  			nil,
   150  		},
   151  	}
   152  
   153  	for name, tc := range cases {
   154  		// Wrapped in a func so we can get defers to work
   155  		t.Run(name, func(t *testing.T) {
   156  			// Set the env vars
   157  			for k, v := range tc.Env {
   158  				defer tempEnv(t, k, v)()
   159  			}
   160  
   161  			m := testModule(t, tc.Module)
   162  			actual, err := Variables(m, tc.Override)
   163  			if (err != nil) != tc.Error {
   164  				t.Fatalf("%s: err: %s", name, err)
   165  			}
   166  			if err != nil {
   167  				return
   168  			}
   169  
   170  			if !reflect.DeepEqual(actual, tc.Expected) {
   171  				t.Fatalf("%s\n\nexpected: %#v\n\ngot: %#v", name, tc.Expected, actual)
   172  			}
   173  		})
   174  	}
   175  }