-
Notifications
You must be signed in to change notification settings - Fork 0
/
templatesUtilsTestDoublesAndOverrideJsonStruct_test.go
305 lines (291 loc) · 9.92 KB
/
templatesUtilsTestDoublesAndOverrideJsonStruct_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package sewansdk
import (
"encoding/json"
"errors"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/helper/schema"
"io/ioutil"
"os"
)
//------------------------------------------------------------------------------
func compareJSONAndMap(jsonFile string,
fileDataMap map[string]interface{}) (string, error) {
var (
err error
jsonData []byte
diffs string
dataStruct interface{}
)
jsonData, err = ioutil.ReadFile(jsonFile)
if jsonFile != "" {
err = os.Remove(jsonFile)
}
if err == nil {
err = json.Unmarshal(jsonData, &dataStruct)
if err == nil {
diffs = cmp.Diff(fileDataMap, dataStruct)
}
}
return diffs, err
}
type TemplaterDummy struct{}
func (templaterFake TemplaterDummy) FetchTemplateFromList(templateName string,
templateList []interface{}) (map[string]interface{}, error) {
return map[string]interface{}{}, nil
}
func (templaterFake TemplaterDummy) validateTemplate(template map[string]interface{}) error {
return nil
}
func (templaterFake TemplaterDummy) updateSchemaFromTemplateOnResourceCreation(d *schema.ResourceData,
template map[string]interface{}) error {
return nil
}
func (templaterFake TemplaterDummy) createVMTemplateOverrideConfig(d *schema.ResourceData,
template map[string]interface{}) (string, error) {
return "", nil
}
type UnexistingTemplateTemplaterFake struct{}
func (templaterFake UnexistingTemplateTemplaterFake) FetchTemplateFromList(templateName string,
templateList []interface{}) (map[string]interface{}, error) {
return map[string]interface{}{}, errors.New("Unavailable template : windows95")
}
func (templaterFake UnexistingTemplateTemplaterFake) validateTemplate(template map[string]interface{}) error {
return nil
}
func (templaterFake UnexistingTemplateTemplaterFake) updateSchemaFromTemplateOnResourceCreation(d *schema.ResourceData,
template map[string]interface{}) error {
return nil
}
func (templaterFake UnexistingTemplateTemplaterFake) createVMTemplateOverrideConfig(d *schema.ResourceData,
template map[string]interface{}) (string, error) {
return "", nil
}
type TemplateFormatErrorTemplaterFake struct{}
func (templaterFake TemplateFormatErrorTemplaterFake) FetchTemplateFromList(templateName string,
templateList []interface{}) (map[string]interface{}, error) {
return map[string]interface{}{}, nil
}
func (templaterFake TemplateFormatErrorTemplaterFake) validateTemplate(template map[string]interface{}) error {
return errors.New("Template missing fields : " + "\"" + NameField + "\" " +
"\"" + OsField + "\" " +
"\"" + RAMField + "\" " +
"\"" + CPUField + "\" " +
"\"" + EnterpriseField + "\" " +
"\"" + DisksField + "\" " +
"\"" + DataCenterField + "\" ")
}
func (templaterFake TemplateFormatErrorTemplaterFake) updateSchemaFromTemplateOnResourceCreation(d *schema.ResourceData,
template map[string]interface{}) error {
return nil
}
func (templaterFake TemplateFormatErrorTemplaterFake) createVMTemplateOverrideConfig(d *schema.ResourceData,
template map[string]interface{}) (string, error) {
return "", nil
}
type existingTemplateNoAdditionalDiskVMMapTemplaterFake struct{}
func (templaterFake existingTemplateNoAdditionalDiskVMMapTemplaterFake) FetchTemplateFromList(templateName string,
templateList []interface{}) (map[string]interface{}, error) {
return map[string]interface{}{
IDField: 82,
NameField: "template1",
SlugField: "centos7-rd-dc1",
RAMField: 1,
CPUField: 1,
OsField: "CentOS",
EnterpriseField: unitTestEnterprise,
DisksField: []interface{}{
map[string]interface{}{NameField: "template1 disk1",
SizeField: 20,
StorageClassField: "storage_enterprise",
SlugField: "template1 disk1 slug",
},
},
NicsField: []interface{}{
map[string]interface{}{VlanNameField: "unit test vlan1",
MacAdressField: "00:50:56:21:7c:ab",
ConnectedField: true,
},
map[string]interface{}{VlanNameField: "unit test vlan2",
MacAdressField: "00:50:56:21:7c:ac",
ConnectedField: true,
},
},
"login": "",
"password": "",
DynamicField: "",
}, nil
}
func (templaterFake existingTemplateNoAdditionalDiskVMMapTemplaterFake) validateTemplate(template map[string]interface{}) error {
return nil
}
func (templaterFake existingTemplateNoAdditionalDiskVMMapTemplaterFake) updateSchemaFromTemplateOnResourceCreation(d *schema.ResourceData,
template map[string]interface{}) error {
d.Set(NameField, "Unit test template no disc add on vm resource")
d.Set(EnterpriseField, unitTestEnterprise)
d.Set(TemplateField, "template1")
d.Set(RAMField, 1)
d.Set(CPUField, 1)
d.Set(DisksField,
[]interface{}{
map[string]interface{}{NameField: "template1 disk1",
SizeField: 20,
StorageClassField: "storage_enterprise",
SlugField: "template1 disk1 slug",
},
},
)
d.Set(NicsField, []interface{}{})
return nil
}
func (templaterFake existingTemplateNoAdditionalDiskVMMapTemplaterFake) createVMTemplateOverrideConfig(d *schema.ResourceData,
template map[string]interface{}) (string, error) {
return "", nil
}
type instanceNumberFieldUnitTestVMInstanceMAPTemplaterFake struct{}
func (templaterFake instanceNumberFieldUnitTestVMInstanceMAPTemplaterFake) FetchTemplateFromList(templateName string,
templateList []interface{}) (map[string]interface{}, error) {
return map[string]interface{}{
IDField: 82,
NameField: "template1",
SlugField: "centos7-rd-dc1",
RAMField: 1,
CPUField: 1,
OsField: "CentOS",
EnterpriseField: unitTestEnterprise,
DisksField: []interface{}{
map[string]interface{}{NameField: "template1 disk1",
SizeField: 20,
StorageClassField: "storage_enterprise",
SlugField: "template1 disk1 slug",
},
},
NicsField: []interface{}{
map[string]interface{}{VlanNameField: "unit test vlan1",
MacAdressField: "00:50:56:21:7c:ab",
ConnectedField: true,
},
map[string]interface{}{VlanNameField: "unit test vlan2",
MacAdressField: "00:50:56:21:7c:ac",
ConnectedField: true,
},
},
"login": "",
"password": "",
DynamicField: "",
}, nil
}
func (templaterFake instanceNumberFieldUnitTestVMInstanceMAPTemplaterFake) validateTemplate(template map[string]interface{}) error {
return nil
}
func (templaterFake instanceNumberFieldUnitTestVMInstanceMAPTemplaterFake) updateSchemaFromTemplateOnResourceCreation(d *schema.ResourceData,
template map[string]interface{}) error {
d.Set(NameField, "instanceNumberFieldUnitTest")
d.Set(EnterpriseField, unitTestEnterprise)
d.Set(TemplateField, "template1")
d.Set(RAMField, 1)
d.Set(CPUField, 1)
d.Set(DisksField,
[]interface{}{
map[string]interface{}{NameField: "template1 disk1",
SizeField: 20,
StorageClassField: "storage_enterprise",
SlugField: "template1 disk1 slug",
},
},
)
d.Set(NicsField, []interface{}{})
return nil
}
func (templaterFake instanceNumberFieldUnitTestVMInstanceMAPTemplaterFake) createVMTemplateOverrideConfig(d *schema.ResourceData,
template map[string]interface{}) (string, error) {
return "", nil
}
type existingTemplateWithAdditionalAndModifiedDisksAndNicsVMMapTemplaterFake struct{}
func (templaterFake existingTemplateWithAdditionalAndModifiedDisksAndNicsVMMapTemplaterFake) FetchTemplateFromList(templateName string,
templateList []interface{}) (map[string]interface{}, error) {
return map[string]interface{}{
IDField: 82,
NameField: "template1",
SlugField: "centos7-rd-dc1",
RAMField: 1,
CPUField: 1,
OsField: "CentOS",
EnterpriseField: unitTestEnterprise,
DisksField: []interface{}{
map[string]interface{}{NameField: "template1 disk1",
SizeField: 20,
StorageClassField: "storage_enterprise",
SlugField: "template1 disk1 slug",
},
},
NicsField: []interface{}{
map[string]interface{}{VlanNameField: "unit test vlan1",
MacAdressField: "00:50:56:21:7c:ab",
ConnectedField: true,
},
map[string]interface{}{VlanNameField: "unit test vlan2",
MacAdressField: "00:50:56:21:7c:ac",
ConnectedField: true,
},
},
"login": "",
"password": "",
DynamicField: "",
}, nil
}
func (templaterFake existingTemplateWithAdditionalAndModifiedDisksAndNicsVMMapTemplaterFake) validateTemplate(template map[string]interface{}) error {
return nil
}
func (templaterFake existingTemplateWithAdditionalAndModifiedDisksAndNicsVMMapTemplaterFake) updateSchemaFromTemplateOnResourceCreation(d *schema.ResourceData,
template map[string]interface{}) error {
d.Set(NameField, "existingTemplateWithAdditionalAndModifiedDisksAndNicsVMMap")
d.Set(EnterpriseField, unitTestEnterprise)
d.Set(TemplateField, "template1")
d.Set(RAMField, 8)
d.Set(CPUField, 4)
d.Set(DisksField,
[]interface{}{
map[string]interface{}{
NameField: "disk 1",
SizeField: 24,
StorageClassField: "storage_enterprise",
SlugField: "",
},
map[string]interface{}{
NameField: "template1 disk1",
SizeField: 25,
StorageClassField: "storage_enterprise",
SlugField: "template1 disk1 slug",
},
},
)
d.Set(NicsField, []interface{}{
map[string]interface{}{
VlanNameField: "non template vlan 1",
MacAdressField: "00:21:21:21:21:21",
ConnectedField: true,
},
map[string]interface{}{
VlanNameField: "non template vlan 2",
MacAdressField: "00:21:21:21:21:22",
ConnectedField: true,
},
},
)
d.Set("vdc: ", "vdc")
d.Set("boot: ", "on disk")
d.Set(StorageClassField, "storage_enterprise")
d.Set("slug: ", "42")
d.Set("token: ", "424242")
d.Set("backup: ", "backup_no_backup")
d.Set("disk_image: ", "")
d.Set("platform_name:", "42")
d.Set("backup_size: ", 42)
d.Set("comment: ", "42")
d.Set("dynamic_field:", "42")
return nil
}
func (templaterFake existingTemplateWithAdditionalAndModifiedDisksAndNicsVMMapTemplaterFake) createVMTemplateOverrideConfig(d *schema.ResourceData,
template map[string]interface{}) (string, error) {
return "", nil
}