github.com/heimweh/terraform@v0.7.4/terraform/eval_variable_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestCoerceMapVariable(t *testing.T) {
     9  	cases := map[string]struct {
    10  		Input      *EvalCoerceMapVariable
    11  		ExpectVars map[string]interface{}
    12  	}{
    13  		"a valid map is untouched": {
    14  			Input: &EvalCoerceMapVariable{
    15  				Variables: map[string]interface{}{
    16  					"amap": map[string]interface{}{"foo": "bar"},
    17  				},
    18  				ModulePath: []string{"root"},
    19  				ModuleTree: testModuleInline(t, map[string]string{
    20  					"main.tf": `
    21  						variable "amap" {
    22  							type = "map"
    23  						}
    24  					`,
    25  				}),
    26  			},
    27  			ExpectVars: map[string]interface{}{
    28  				"amap": map[string]interface{}{"foo": "bar"},
    29  			},
    30  		},
    31  		"a list w/ a single map element is coerced": {
    32  			Input: &EvalCoerceMapVariable{
    33  				Variables: map[string]interface{}{
    34  					"amap": []interface{}{
    35  						map[string]interface{}{"foo": "bar"},
    36  					},
    37  				},
    38  				ModulePath: []string{"root"},
    39  				ModuleTree: testModuleInline(t, map[string]string{
    40  					"main.tf": `
    41  						variable "amap" {
    42  							type = "map"
    43  						}
    44  					`,
    45  				}),
    46  			},
    47  			ExpectVars: map[string]interface{}{
    48  				"amap": map[string]interface{}{"foo": "bar"},
    49  			},
    50  		},
    51  		"a list w/ more than one map element is untouched": {
    52  			Input: &EvalCoerceMapVariable{
    53  				Variables: map[string]interface{}{
    54  					"amap": []interface{}{
    55  						map[string]interface{}{"foo": "bar"},
    56  						map[string]interface{}{"baz": "qux"},
    57  					},
    58  				},
    59  				ModulePath: []string{"root"},
    60  				ModuleTree: testModuleInline(t, map[string]string{
    61  					"main.tf": `
    62  						variable "amap" {
    63  							type = "map"
    64  						}
    65  					`,
    66  				}),
    67  			},
    68  			ExpectVars: map[string]interface{}{
    69  				"amap": []interface{}{
    70  					map[string]interface{}{"foo": "bar"},
    71  					map[string]interface{}{"baz": "qux"},
    72  				},
    73  			},
    74  		},
    75  		"list coercion also works in a module": {
    76  			Input: &EvalCoerceMapVariable{
    77  				Variables: map[string]interface{}{
    78  					"amap": []interface{}{
    79  						map[string]interface{}{"foo": "bar"},
    80  					},
    81  				},
    82  				ModulePath: []string{"root", "middle", "bottom"},
    83  				ModuleTree: testModuleInline(t, map[string]string{
    84  					"top.tf": `
    85  						module "middle" {
    86  							source = "./middle"
    87  						}
    88  					`,
    89  					"middle/mid.tf": `
    90  						module "bottom" {
    91  							source = "./bottom"
    92  							amap {
    93  								foo = "bar"
    94  							}
    95  						}
    96  					`,
    97  					"middle/bottom/bot.tf": `
    98  						variable "amap" {
    99  							type = "map"
   100  						}
   101  					`,
   102  				}),
   103  			},
   104  			ExpectVars: map[string]interface{}{
   105  				"amap": map[string]interface{}{"foo": "bar"},
   106  			},
   107  		},
   108  		"coercion only occurs when target var is a map": {
   109  			Input: &EvalCoerceMapVariable{
   110  				Variables: map[string]interface{}{
   111  					"alist": []interface{}{
   112  						map[string]interface{}{"foo": "bar"},
   113  					},
   114  				},
   115  				ModulePath: []string{"root"},
   116  				ModuleTree: testModuleInline(t, map[string]string{
   117  					"main.tf": `
   118  						variable "alist" {
   119  							type = "list"
   120  						}
   121  					`,
   122  				}),
   123  			},
   124  			ExpectVars: map[string]interface{}{
   125  				"alist": []interface{}{
   126  					map[string]interface{}{"foo": "bar"},
   127  				},
   128  			},
   129  		},
   130  	}
   131  
   132  	for tn, tc := range cases {
   133  		_, err := tc.Input.Eval(&MockEvalContext{})
   134  		if err != nil {
   135  			t.Fatalf("%s: Unexpected err: %s", tn, err)
   136  		}
   137  		if !reflect.DeepEqual(tc.Input.Variables, tc.ExpectVars) {
   138  			t.Fatalf("%s: Expected variables:\n\n%#v\n\nGot:\n\n%#v",
   139  				tn, tc.ExpectVars, tc.Input.Variables)
   140  		}
   141  	}
   142  }