github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/helper/schema/field_reader_diff_test.go (about)

     1  package schema
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  func TestDiffFieldReader_impl(t *testing.T) {
    11  	var _ FieldReader = new(DiffFieldReader)
    12  }
    13  
    14  // https://github.com/hashicorp/terraform/issues/914
    15  func TestDiffFieldReader_MapHandling(t *testing.T) {
    16  	schema := map[string]*Schema{
    17  		"tags": &Schema{
    18  			Type: TypeMap,
    19  		},
    20  	}
    21  	r := &DiffFieldReader{
    22  		Schema: schema,
    23  		Diff: &terraform.InstanceDiff{
    24  			Attributes: map[string]*terraform.ResourceAttrDiff{
    25  				"tags.%": &terraform.ResourceAttrDiff{
    26  					Old: "1",
    27  					New: "2",
    28  				},
    29  				"tags.baz": &terraform.ResourceAttrDiff{
    30  					Old: "",
    31  					New: "qux",
    32  				},
    33  			},
    34  		},
    35  		Source: &MapFieldReader{
    36  			Schema: schema,
    37  			Map: BasicMapReader(map[string]string{
    38  				"tags.%":   "1",
    39  				"tags.foo": "bar",
    40  			}),
    41  		},
    42  	}
    43  
    44  	result, err := r.ReadField([]string{"tags"})
    45  	if err != nil {
    46  		t.Fatalf("ReadField failed: %#v", err)
    47  	}
    48  
    49  	expected := map[string]interface{}{
    50  		"foo": "bar",
    51  		"baz": "qux",
    52  	}
    53  
    54  	if !reflect.DeepEqual(expected, result.Value) {
    55  		t.Fatalf("bad: DiffHandling\n\nexpected: %#v\n\ngot: %#v\n\n", expected, result.Value)
    56  	}
    57  }
    58  
    59  func TestDiffFieldReader_extra(t *testing.T) {
    60  	schema := map[string]*Schema{
    61  		"stringComputed": &Schema{Type: TypeString},
    62  
    63  		"listMap": &Schema{
    64  			Type: TypeList,
    65  			Elem: &Schema{
    66  				Type: TypeMap,
    67  			},
    68  		},
    69  
    70  		"mapRemove": &Schema{Type: TypeMap},
    71  
    72  		"setChange": &Schema{
    73  			Type:     TypeSet,
    74  			Optional: true,
    75  			Elem: &Resource{
    76  				Schema: map[string]*Schema{
    77  					"index": &Schema{
    78  						Type:     TypeInt,
    79  						Required: true,
    80  					},
    81  
    82  					"value": &Schema{
    83  						Type:     TypeString,
    84  						Required: true,
    85  					},
    86  				},
    87  			},
    88  			Set: func(a interface{}) int {
    89  				m := a.(map[string]interface{})
    90  				return m["index"].(int)
    91  			},
    92  		},
    93  
    94  		"setEmpty": &Schema{
    95  			Type:     TypeSet,
    96  			Optional: true,
    97  			Elem: &Resource{
    98  				Schema: map[string]*Schema{
    99  					"index": &Schema{
   100  						Type:     TypeInt,
   101  						Required: true,
   102  					},
   103  
   104  					"value": &Schema{
   105  						Type:     TypeString,
   106  						Required: true,
   107  					},
   108  				},
   109  			},
   110  			Set: func(a interface{}) int {
   111  				m := a.(map[string]interface{})
   112  				return m["index"].(int)
   113  			},
   114  		},
   115  	}
   116  
   117  	r := &DiffFieldReader{
   118  		Schema: schema,
   119  		Diff: &terraform.InstanceDiff{
   120  			Attributes: map[string]*terraform.ResourceAttrDiff{
   121  				"stringComputed": &terraform.ResourceAttrDiff{
   122  					Old:         "foo",
   123  					New:         "bar",
   124  					NewComputed: true,
   125  				},
   126  
   127  				"listMap.0.bar": &terraform.ResourceAttrDiff{
   128  					NewRemoved: true,
   129  				},
   130  
   131  				"mapRemove.bar": &terraform.ResourceAttrDiff{
   132  					NewRemoved: true,
   133  				},
   134  
   135  				"setChange.10.value": &terraform.ResourceAttrDiff{
   136  					Old: "50",
   137  					New: "80",
   138  				},
   139  
   140  				"setEmpty.#": &terraform.ResourceAttrDiff{
   141  					Old: "2",
   142  					New: "0",
   143  				},
   144  			},
   145  		},
   146  
   147  		Source: &MapFieldReader{
   148  			Schema: schema,
   149  			Map: BasicMapReader(map[string]string{
   150  				"listMap.#":     "2",
   151  				"listMap.0.foo": "bar",
   152  				"listMap.0.bar": "baz",
   153  				"listMap.1.baz": "baz",
   154  
   155  				"mapRemove.foo": "bar",
   156  				"mapRemove.bar": "bar",
   157  
   158  				"setChange.#":        "1",
   159  				"setChange.10.index": "10",
   160  				"setChange.10.value": "50",
   161  
   162  				"setEmpty.#":        "2",
   163  				"setEmpty.10.index": "10",
   164  				"setEmpty.10.value": "50",
   165  				"setEmpty.20.index": "20",
   166  				"setEmpty.20.value": "50",
   167  			}),
   168  		},
   169  	}
   170  
   171  	cases := map[string]struct {
   172  		Addr   []string
   173  		Result FieldReadResult
   174  		Err    bool
   175  	}{
   176  		"stringComputed": {
   177  			[]string{"stringComputed"},
   178  			FieldReadResult{
   179  				Value:    "",
   180  				Exists:   true,
   181  				Computed: true,
   182  			},
   183  			false,
   184  		},
   185  
   186  		"listMapRemoval": {
   187  			[]string{"listMap"},
   188  			FieldReadResult{
   189  				Value: []interface{}{
   190  					map[string]interface{}{
   191  						"foo": "bar",
   192  					},
   193  					map[string]interface{}{
   194  						"baz": "baz",
   195  					},
   196  				},
   197  				Exists: true,
   198  			},
   199  			false,
   200  		},
   201  
   202  		"mapRemove": {
   203  			[]string{"mapRemove"},
   204  			FieldReadResult{
   205  				Value: map[string]interface{}{
   206  					"foo": "bar",
   207  				},
   208  				Exists:   true,
   209  				Computed: false,
   210  			},
   211  			false,
   212  		},
   213  
   214  		"setChange": {
   215  			[]string{"setChange"},
   216  			FieldReadResult{
   217  				Value: []interface{}{
   218  					map[string]interface{}{
   219  						"index": 10,
   220  						"value": "80",
   221  					},
   222  				},
   223  				Exists: true,
   224  			},
   225  			false,
   226  		},
   227  
   228  		"setEmpty": {
   229  			[]string{"setEmpty"},
   230  			FieldReadResult{
   231  				Value:  []interface{}{},
   232  				Exists: true,
   233  			},
   234  			false,
   235  		},
   236  	}
   237  
   238  	for name, tc := range cases {
   239  		out, err := r.ReadField(tc.Addr)
   240  		if err != nil != tc.Err {
   241  			t.Fatalf("%s: err: %s", name, err)
   242  		}
   243  		if s, ok := out.Value.(*Set); ok {
   244  			// If it is a set, convert to a list so its more easily checked.
   245  			out.Value = s.List()
   246  		}
   247  		if !reflect.DeepEqual(tc.Result, out) {
   248  			t.Fatalf("%s: bad: %#v", name, out)
   249  		}
   250  	}
   251  }
   252  
   253  func TestDiffFieldReader(t *testing.T) {
   254  	testFieldReader(t, func(s map[string]*Schema) FieldReader {
   255  		return &DiffFieldReader{
   256  			Schema: s,
   257  			Diff: &terraform.InstanceDiff{
   258  				Attributes: map[string]*terraform.ResourceAttrDiff{
   259  					"bool": &terraform.ResourceAttrDiff{
   260  						Old: "",
   261  						New: "true",
   262  					},
   263  
   264  					"int": &terraform.ResourceAttrDiff{
   265  						Old: "",
   266  						New: "42",
   267  					},
   268  
   269  					"float": &terraform.ResourceAttrDiff{
   270  						Old: "",
   271  						New: "3.1415",
   272  					},
   273  
   274  					"string": &terraform.ResourceAttrDiff{
   275  						Old: "",
   276  						New: "string",
   277  					},
   278  
   279  					"stringComputed": &terraform.ResourceAttrDiff{
   280  						Old:         "foo",
   281  						New:         "bar",
   282  						NewComputed: true,
   283  					},
   284  
   285  					"list.#": &terraform.ResourceAttrDiff{
   286  						Old: "0",
   287  						New: "2",
   288  					},
   289  
   290  					"list.0": &terraform.ResourceAttrDiff{
   291  						Old: "",
   292  						New: "foo",
   293  					},
   294  
   295  					"list.1": &terraform.ResourceAttrDiff{
   296  						Old: "",
   297  						New: "bar",
   298  					},
   299  
   300  					"listInt.#": &terraform.ResourceAttrDiff{
   301  						Old: "0",
   302  						New: "2",
   303  					},
   304  
   305  					"listInt.0": &terraform.ResourceAttrDiff{
   306  						Old: "",
   307  						New: "21",
   308  					},
   309  
   310  					"listInt.1": &terraform.ResourceAttrDiff{
   311  						Old: "",
   312  						New: "42",
   313  					},
   314  
   315  					"map.foo": &terraform.ResourceAttrDiff{
   316  						Old: "",
   317  						New: "bar",
   318  					},
   319  
   320  					"map.bar": &terraform.ResourceAttrDiff{
   321  						Old: "",
   322  						New: "baz",
   323  					},
   324  
   325  					"set.#": &terraform.ResourceAttrDiff{
   326  						Old: "0",
   327  						New: "2",
   328  					},
   329  
   330  					"set.10": &terraform.ResourceAttrDiff{
   331  						Old: "",
   332  						New: "10",
   333  					},
   334  
   335  					"set.50": &terraform.ResourceAttrDiff{
   336  						Old: "",
   337  						New: "50",
   338  					},
   339  
   340  					"setDeep.#": &terraform.ResourceAttrDiff{
   341  						Old: "0",
   342  						New: "2",
   343  					},
   344  
   345  					"setDeep.10.index": &terraform.ResourceAttrDiff{
   346  						Old: "",
   347  						New: "10",
   348  					},
   349  
   350  					"setDeep.10.value": &terraform.ResourceAttrDiff{
   351  						Old: "",
   352  						New: "foo",
   353  					},
   354  
   355  					"setDeep.50.index": &terraform.ResourceAttrDiff{
   356  						Old: "",
   357  						New: "50",
   358  					},
   359  
   360  					"setDeep.50.value": &terraform.ResourceAttrDiff{
   361  						Old: "",
   362  						New: "bar",
   363  					},
   364  				},
   365  			},
   366  
   367  			Source: &MapFieldReader{
   368  				Schema: s,
   369  				Map: BasicMapReader(map[string]string{
   370  					"listMap.#":     "2",
   371  					"listMap.0.foo": "bar",
   372  					"listMap.0.bar": "baz",
   373  					"listMap.1.baz": "baz",
   374  				}),
   375  			},
   376  		}
   377  	})
   378  }