|
1 | 1 | package nutanix |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "reflect" |
4 | 5 | "testing" |
5 | | - |
6 | | - "github.com/hashicorp/terraform/terraform" |
7 | 6 | ) |
8 | 7 |
|
9 | 8 | func TestResourceNutanixCategoriesteUpgradeV0(t *testing.T) { |
10 | 9 | cases := map[string]struct { |
11 | | - StateVersion int |
12 | | - Attributes map[string]string |
13 | | - Expected map[string]string |
14 | | - Meta interface{} |
| 10 | + Attributes map[string]interface{} |
| 11 | + Expected map[string]interface{} |
| 12 | + Meta interface{} |
15 | 13 | }{ |
16 | 14 | "v0_without_values": { |
17 | | - StateVersion: 0, |
18 | | - Attributes: map[string]string{ |
19 | | - "categories.%": "0", |
| 15 | + Attributes: map[string]interface{}{ |
| 16 | + "categories": make(map[string]interface{}), |
20 | 17 | }, |
21 | | - Expected: map[string]string{ |
22 | | - "categories.#": "0", |
| 18 | + Expected: map[string]interface{}{ |
| 19 | + "categories": make([]interface{}, 0), |
23 | 20 | }, |
24 | 21 | }, |
25 | 22 | "v0_with_values": { |
26 | | - StateVersion: 0, |
27 | | - Attributes: map[string]string{ |
28 | | - "categories.%": "2", |
29 | | - "categories.os_type": "ubuntu", |
30 | | - "categories.os_version": "18.04", |
| 23 | + Attributes: map[string]interface{}{ |
| 24 | + "categories": map[string]interface{}{ |
| 25 | + "os_type": "ubuntu", |
| 26 | + "os_version": "18.04", |
| 27 | + }, |
31 | 28 | }, |
32 | | - Expected: map[string]string{ |
33 | | - "categories.#": "2", |
34 | | - "categories.0.name": "os_type", |
35 | | - "categories.0.value": "ubuntu", |
36 | | - "categories.1.name": "os_version", |
37 | | - "categories.1.value": "18.04", |
| 29 | + Expected: map[string]interface{}{ |
| 30 | + |
| 31 | + "categories": []interface{}{ |
| 32 | + map[string]interface{}{"name": "os_type", "value": "ubuntu"}, |
| 33 | + map[string]interface{}{"name": "os_version", "value": "18.04"}, |
| 34 | + }, |
38 | 35 | }, |
39 | 36 | }, |
40 | 37 | "v0_categories_not_set": { |
41 | | - StateVersion: 0, |
42 | | - Attributes: map[string]string{ |
| 38 | + Attributes: map[string]interface{}{ |
43 | 39 | "name": "test-name", |
44 | 40 | }, |
45 | | - Expected: map[string]string{ |
| 41 | + Expected: map[string]interface{}{ |
46 | 42 | "name": "test-name", |
47 | 43 | }, |
48 | 44 | }, |
49 | 45 | "v0_multiple_categories": { |
50 | | - StateVersion: 0, |
51 | | - Attributes: map[string]string{ |
52 | | - "categories.%": "3", |
53 | | - "categories.os_type": "ubuntu", |
54 | | - "categories.os_version": "18.04", |
55 | | - "categories.tier": "application", |
56 | | - "categories.test": "test-value", |
| 46 | + Attributes: map[string]interface{}{ |
| 47 | + "categories": map[string]interface{}{ |
| 48 | + "os_type": "ubuntu", |
| 49 | + "os_version": "18.04", |
| 50 | + "tier": "application", |
| 51 | + "test": "test-value", |
| 52 | + }, |
57 | 53 | }, |
58 | | - Expected: map[string]string{ |
59 | | - "categories.#": "3", |
60 | | - "categories.0.name": "os_type", |
61 | | - "categories.0.value": "ubuntu", |
62 | | - "categories.1.name": "os_version", |
63 | | - "categories.1.value": "18.04", |
64 | | - "categories.2.name": "test", |
65 | | - "categories.2.value": "test-value", |
66 | | - "categories.3.name": "tier", |
67 | | - "categories.3.value": "application", |
| 54 | + Expected: map[string]interface{}{ |
| 55 | + "categories": []interface{}{ |
| 56 | + map[string]interface{}{"name": "os_type", |
| 57 | + "value": "ubuntu"}, |
| 58 | + map[string]interface{}{"name": "os_version", |
| 59 | + "value": "18.04"}, |
| 60 | + map[string]interface{}{"name": "test", |
| 61 | + "value": "test-value"}, |
| 62 | + map[string]interface{}{"name": "tier", |
| 63 | + "value": "application"}, |
| 64 | + }, |
68 | 65 | }, |
69 | 66 | }, |
70 | 67 | "v0_already_migrated": { |
71 | | - StateVersion: 0, |
72 | | - Attributes: map[string]string{ |
73 | | - "categories.#": "3", |
74 | | - "categories.0.name": "os_type", |
75 | | - "categories.0.value": "ubuntu", |
76 | | - "categories.1.name": "os_version", |
77 | | - "categories.1.value": "18.04", |
78 | | - "categories.2.name": "tier", |
79 | | - "categories.2.value": "application", |
| 68 | + Attributes: map[string]interface{}{ |
| 69 | + "categories": []interface{}{ |
| 70 | + map[string]interface{}{"name": "os_type", |
| 71 | + "value": "ubuntu"}, |
| 72 | + map[string]interface{}{"name": "os_version", |
| 73 | + "value": "18.04"}, |
| 74 | + map[string]interface{}{"name": "tier", |
| 75 | + "value": "application"}, |
| 76 | + }, |
80 | 77 | }, |
81 | | - Expected: map[string]string{ |
82 | | - "categories.#": "3", |
83 | | - "categories.0.name": "os_type", |
84 | | - "categories.0.value": "ubuntu", |
85 | | - "categories.1.name": "os_version", |
86 | | - "categories.1.value": "18.04", |
87 | | - "categories.2.name": "tier", |
88 | | - "categories.2.value": "application", |
| 78 | + Expected: map[string]interface{}{ |
| 79 | + "categories": []interface{}{ |
| 80 | + map[string]interface{}{"name": "os_type", |
| 81 | + "value": "ubuntu"}, |
| 82 | + map[string]interface{}{"name": "os_version", |
| 83 | + "value": "18.04"}, |
| 84 | + map[string]interface{}{"name": "tier", |
| 85 | + "value": "application"}, |
| 86 | + }, |
89 | 87 | }, |
90 | 88 | }, |
91 | 89 | "v0_empty_value": { |
92 | | - StateVersion: 0, |
93 | | - Attributes: map[string]string{ |
94 | | - "categories.%": "3", |
95 | | - "categories.os_type": "", |
96 | | - "categories.os_version": "", |
97 | | - "categories.tier": "", |
| 90 | + Attributes: map[string]interface{}{ |
| 91 | + "categories": map[string]interface{}{ |
| 92 | + "os_type": "", |
| 93 | + "os_version": "", |
| 94 | + "tier": "", |
| 95 | + "test": "", |
| 96 | + }, |
98 | 97 | }, |
99 | | - Expected: map[string]string{ |
100 | | - "categories.#": "3", |
101 | | - "categories.0.name": "os_type", |
102 | | - "categories.0.value": "", |
103 | | - "categories.1.name": "os_version", |
104 | | - "categories.1.value": "", |
105 | | - "categories.2.name": "tier", |
106 | | - "categories.2.value": "", |
| 98 | + Expected: map[string]interface{}{ |
| 99 | + "categories": []interface{}{ |
| 100 | + map[string]interface{}{"name": "os_type", |
| 101 | + "value": ""}, |
| 102 | + map[string]interface{}{"name": "os_version", |
| 103 | + "value": ""}, |
| 104 | + map[string]interface{}{"name": "test", |
| 105 | + "value": ""}, |
| 106 | + map[string]interface{}{"name": "tier", |
| 107 | + "value": ""}, |
| 108 | + }, |
107 | 109 | }, |
108 | 110 | }, |
109 | 111 | } |
110 | 112 |
|
111 | 113 | for tn, tc := range cases { |
112 | | - is := &terraform.InstanceState{ |
113 | | - ID: "i-abc123", |
114 | | - Attributes: tc.Attributes, |
115 | | - } |
116 | | - is, err := resourceNutanixCategoriesMigrateState( |
117 | | - tc.StateVersion, is, tc.Meta) |
| 114 | + is, err := resourceNutanixCategoriesMigrateState(tc.Attributes, tc.Meta) |
118 | 115 |
|
119 | 116 | if err != nil { |
120 | 117 | t.Fatalf("bad: %s, err: %#v", tn, err) |
121 | 118 | } |
122 | 119 |
|
123 | 120 | for k, v := range tc.Expected { |
124 | | - if is.Attributes[k] != v { |
| 121 | + if !reflect.DeepEqual(is[k], v) { |
125 | 122 | t.Fatalf( |
126 | 123 | "bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v", |
127 | | - tn, k, v, k, is.Attributes[k], is.Attributes) |
| 124 | + tn, k, v, k, is[k], is) |
128 | 125 | } |
129 | 126 | } |
130 | 127 | } |
|
0 commit comments