forked from Azure/skewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_test.go
405 lines (385 loc) · 18 KB
/
data_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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package skewer
import (
"context"
"errors"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2022-03-01/compute" //nolint:staticcheck
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
var (
expectedVirtualMachinesCount = 4
expectedAvailabilityZones = []string{"1", "2", "3"}
shouldNotBePresentCapabilityNotFoundErr = "ShouldNotBePresentCapabilityNotFound"
premiumIOCapabilityValueParseErr = "PremiumIOCapabilityValueParse: failed to parse string 'False' as int64, error: 'strconv.ParseInt: parsing \"False\": invalid syntax'" //nolint:lll
x64ArchType = "x64"
)
//nolint:gocyclo,funlen
func Test_Data(t *testing.T) {
dataWrapper, err := newDataWrapper("./testdata/eastus.json")
if err != nil {
t.Error(err)
}
fakeClient := &fakeClient{
skus: dataWrapper.Value,
}
resourceClient, err := newSuccessfulFakeResourceClient([][]compute.ResourceSku{
dataWrapper.Value,
})
if err != nil {
t.Error(err)
}
chunkedResourceClient, err := newSuccessfulFakeResourceClient(chunk(dataWrapper.Value, 10))
if err != nil {
t.Error(err)
}
resourceProviderClient, err := newSuccessfulFakeResourceProviderClient([][]compute.ResourceSku{
dataWrapper.Value,
})
if err != nil {
t.Error(err)
}
chunkedResourceProviderClient, err := newSuccessfulFakeResourceProviderClient(chunk(dataWrapper.Value, 10))
if err != nil {
t.Error(err)
}
ctx := context.Background()
cases := map[string]struct {
newCacheFunc NewCacheFunc
}{
"resourceClient": {
newCacheFunc: func(_ context.Context, _ ...Option) (*Cache, error) {
return NewCache(ctx, WithResourceClient(resourceClient), WithLocation("eastus"))
},
},
"chunkedResourceClient": {
newCacheFunc: func(_ context.Context, _ ...Option) (*Cache, error) {
return NewCache(ctx, WithResourceClient(chunkedResourceClient), WithLocation("eastus"))
},
},
"resourceProviderClient": {
newCacheFunc: func(_ context.Context, _ ...Option) (*Cache, error) {
return NewCache(ctx, WithResourceProviderClient(resourceProviderClient), WithLocation("eastus"))
},
},
"chunkedResourceProviderClient": {
newCacheFunc: func(_ context.Context, _ ...Option) (*Cache, error) {
return NewCache(ctx, WithResourceProviderClient(chunkedResourceProviderClient), WithLocation("eastus"))
},
},
"wrappedClient": {
newCacheFunc: func(_ context.Context, _ ...Option) (*Cache, error) {
return NewCache(ctx, WithClient(fakeClient), WithLocation("eastus"))
},
},
}
for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
cache, err := tc.newCacheFunc(ctx)
if err != nil {
t.Error(err)
}
t.Run("virtual machines", func(t *testing.T) {
t.Run("expect 4 virtual machine skus", func(t *testing.T) {
if len(cache.GetVirtualMachines(ctx)) != expectedVirtualMachinesCount {
t.Errorf("expected %d virtual machine skus but found %d", expectedVirtualMachinesCount, len(cache.GetVirtualMachines(ctx)))
}
})
t.Run("standard_d4s_v3", func(t *testing.T) {
errCapabilityValueNil := &ErrCapabilityValueParse{}
errCapabilityNotFound := &ErrCapabilityNotFound{}
sku, err := cache.Get(ctx, "standard_d4s_v3", VirtualMachines, "eastus")
if err != nil {
t.Errorf("expected to find virtual machine sku standard_d4s_v3")
}
if name := sku.GetName(); !strings.EqualFold(name, "standard_d4s_v3") {
t.Errorf("expected standard_d4s_v3 to have name standard_d4s_v3, got: '%s'", name)
}
if skuFamily := sku.GetFamilyName(); !strings.EqualFold(skuFamily, "standardDSv3Family") {
t.Errorf("expected standard_d4s_v3 to have name standardDSv3Family, got: '%s'", skuFamily)
}
if skuSize := sku.GetSize(); !strings.EqualFold(skuSize, "d4s_v3") {
t.Errorf("expected standard_d4s_v3 to have name d4s_v3 size, got: '%s'", skuSize)
}
if resourceType := sku.GetResourceType(); resourceType != VirtualMachines {
t.Errorf("expected standard_d4s_v3 to have resourceType virtual machine, got: '%s'", resourceType)
}
if cpu, err := sku.VCPU(); cpu != 4 || err != nil {
t.Errorf("expected standard_d4s_v3 to have 4 vCPUs and parse successfully, got value '%d' and error '%s'", cpu, err)
}
if memory, err := sku.Memory(); memory != 16 || err != nil {
t.Errorf("expected standard_d4s_v3 to have 16GB of memory and parse successfully, got value '%f' and error '%s'", memory, err)
}
if quantity, err := sku.GetCapabilityIntegerQuantity("ShouldNotBePresent"); quantity != -1 || !errors.As(err, &errCapabilityNotFound) {
t.Errorf("expected standard_d4s_v3 not to have a non-existent capability, got value '%d' and error '%s'", quantity, err)
}
if quantity, err := sku.GetCapabilityIntegerQuantity("PremiumIO"); quantity != -1 || !errors.As(err, &errCapabilityValueNil) {
t.Errorf("expected standard_d4s_v3 to fail parsing value for boolean premiumIO as int, got value '%d' and error '%s'", quantity, err)
}
if !sku.HasZonalCapability(UltraSSDAvailable) {
t.Errorf("expected standard_d4s_v3 to support ultra ssd")
}
if sku.HasZonalCapability("NotExistingCapability") {
t.Errorf("expected standard_d4s_v3 not to support non-existent capability")
}
if !sku.HasCapability(EphemeralOSDisk) {
t.Errorf("expected standard_d4s_v3 to support ephemeral os")
}
if !sku.IsAcceleratedNetworkingSupported() {
t.Errorf("expected standard_d4s_v3 to support accelerated networking")
}
if cpuArch, err := sku.GetCPUArchitectureType(); err != nil || cpuArch != x64ArchType {
t.Errorf("expected standard_d4s_v3 to have x64 cpuArchitectureType")
}
if !sku.IsPremiumIO() {
t.Errorf("expected standard_d4s_v3 to support PremiumIO")
}
if !sku.IsHyperVGen1Supported() {
t.Errorf("expected standard_d4s_v3 to support hyper v gen1")
}
if !sku.IsHyperVGen2Supported() {
t.Errorf("expected standard_d4s_v3 to support hyper v gen2")
}
if !sku.HasCapability(EncryptionAtHost) {
t.Errorf("expected standard_d4s_v3 to support encryption at host")
}
if !sku.IsAvailable("eastus") {
t.Errorf("expected standard_d4s_v3 to be available in eastus")
}
if sku.IsRestricted("eastus") {
t.Errorf("expected standard_d4s_v3 to be unrestricted in eastus")
}
if sku.IsAvailable("westus2") {
t.Errorf("expected standard_d4s_v3 not to be available in westus2")
}
if sku.IsRestricted("westus2") {
t.Errorf("expected standard_d4s_v3 not to be restricted in westus2")
}
if quantity, err := sku.MaxResourceVolumeMB(); quantity != 32768 || errors.As(err, &errCapabilityNotFound) {
t.Errorf("expected standard_d4s_v3 to have 32768 MB of temporary disk, got value '%d' and error '%s'", quantity, err)
}
if isSupported, err := sku.HasCapabilityWithMinCapacity("MaxResourceVolumeMB", 32768); !isSupported || err != nil {
t.Errorf("expected standard_d4s_v3 to fit 32GB temp disk, got '%t', error: %s", isSupported, err)
}
if isSupported, err := sku.HasCapabilityWithMinCapacity("MaxResourceVolumeMB", 32769); isSupported || err != nil {
t.Errorf("expected standard_d4s_v3 not to fit 32GB +1 byte temp disk, got '%t', error: %s", isSupported, err)
}
hasV1 := !sku.HasCapabilityWithSeparator(HyperVGenerations, "V1")
hasV2 := !sku.HasCapabilityWithSeparator(HyperVGenerations, "V2")
if hasV1 || hasV2 {
t.Errorf("expected standard_d4s_v3 to support hyper-v generation v1 and v2, got v1: '%t' , v2: '%t'", hasV1, hasV2)
}
})
t.Run("standard_d2_v2", func(t *testing.T) {
errCapabilityValueNil := &ErrCapabilityValueParse{}
errCapabilityNotFound := &ErrCapabilityNotFound{}
sku, err := cache.Get(ctx, "Standard_D2_v2", VirtualMachines, "eastus")
if err != nil {
t.Errorf("expected to find virtual machine sku standard_d2_v2")
}
if name := sku.GetName(); !strings.EqualFold(name, "standard_d2_v2") {
t.Errorf("expected standard_d2_v2 to have name standard_d2_v2, got: '%s'", name)
}
if skuFamily := sku.GetFamilyName(); !strings.EqualFold(skuFamily, "standardDv2Family") {
t.Errorf("expected standard_d2_v2 to have name standardDv2Family, got: '%s'", skuFamily)
}
if skuSize := sku.GetSize(); !strings.EqualFold(skuSize, "d2_v2") {
t.Errorf("expected standard_d2_v2 to have name d2_v2 size, got: '%s'", skuSize)
}
if resourceType := sku.GetResourceType(); resourceType != VirtualMachines {
t.Errorf("expected standard_d2_v2 to have resourceType virtual machine, got: '%s'", resourceType)
}
if cpu, err := sku.VCPU(); cpu != 2 || err != nil {
t.Errorf("expected standard_d2_v2 to have 2 vCPUs and parse successfully, got value '%d' and error '%s'", cpu, err)
}
if memory, err := sku.Memory(); memory != 7 || err != nil {
t.Errorf("expected standard_d2_v2 to have 7GB of memory and parse successfully, got value '%f' and error '%s'", memory, err)
}
if quantity, err := sku.GetCapabilityIntegerQuantity("ShouldNotBePresent"); quantity != -1 ||
!errors.As(err, &errCapabilityNotFound) ||
err.Error() != shouldNotBePresentCapabilityNotFoundErr {
t.Errorf("expected standard_d2_v2 not to have a non-existent capability, got value '%d' and error '%s'", quantity, err)
}
if quantity, err := sku.GetCapabilityIntegerQuantity(CapabilityPremiumIO); quantity != -1 ||
!errors.As(err, &errCapabilityValueNil) ||
err.Error() != premiumIOCapabilityValueParseErr {
t.Errorf("expected standard_d2_v2 to fail parsing value for boolean premiumIO as int, got value '%d' and error '%s'", quantity, err)
}
if sku.HasZonalCapability(UltraSSDAvailable) {
t.Errorf("expected standard_d2_v2 not to support ultra ssd")
}
if sku.HasZonalCapability("NotExistingCapability") {
t.Errorf("expected standard_d2_v2 not to support non-existent capability")
}
if sku.HasCapability(EphemeralOSDisk) {
t.Errorf("expected standard_d2_v2 not to support ephemeral os")
}
if !sku.IsAcceleratedNetworkingSupported() {
t.Errorf("expected standard_d2_v2 to support accelerated networking")
}
if cpuArch, err := sku.GetCPUArchitectureType(); err != nil || cpuArch != x64ArchType {
t.Errorf("expected standard_d2_v2 to have x64 cpuArchitectureType")
}
if sku.IsPremiumIO() {
t.Errorf("expected standard_d2_v2 to not support PremiumIO")
}
if !sku.IsHyperVGen1Supported() {
t.Errorf("expected standard_d2_v2 to support hyper v gen1")
}
if sku.IsHyperVGen2Supported() {
t.Errorf("expected standard_d2_v2 not to support hyper v gen2")
}
if sku.HasCapability(EncryptionAtHost) {
t.Errorf("expected standard_d2_v2 not to support encryption at host")
}
if !sku.IsAvailable("eastus") {
t.Errorf("expected standard_d2_v2 to be available in eastus")
}
if sku.IsRestricted("eastus") {
t.Errorf("expected standard_d2_v2 to be unrestricted in eastus")
}
if sku.IsAvailable("westus2") {
t.Errorf("expected standard_d2_v2 not to be available in westus2")
}
if sku.IsRestricted("westus2") {
t.Errorf("expected standard_d2_v2 not to be restricted in westus2")
}
if quantity, err := sku.MaxResourceVolumeMB(); quantity != 102400 || errors.As(err, &errCapabilityNotFound) {
t.Errorf("expected standard_d2_v2 to have 102400 MB of temporary disk, got value '%d' and error '%s'", quantity, err)
}
if isSupported, err := sku.HasCapabilityWithMinCapacity("MemoryGB", 1000); isSupported || err != nil {
t.Errorf("expected standard_d2_v2 not to have 1000GB of memory, got '%t', error: %s", isSupported, err)
}
hasV1 := !sku.HasCapabilityWithSeparator(HyperVGenerations, "V1")
hasV2 := sku.HasCapabilityWithSeparator(HyperVGenerations, "V2")
if hasV1 || hasV2 {
t.Errorf("expected standard_d2_v2 to support hyper-v generation v1 but not v2, got v1: '%t' , v2: '%t'", hasV1, hasV2)
}
})
t.Run("Standard_NV6", func(t *testing.T) {
errCapabilityValueNil := &ErrCapabilityValueParse{}
errCapabilityNotFound := &ErrCapabilityNotFound{}
sku, err := cache.Get(ctx, "Standard_NV6", VirtualMachines, "eastus")
if err != nil {
t.Errorf("expected to find virtual machine sku Standard_NV6")
}
if name := sku.GetName(); !strings.EqualFold(name, "standard_nv6") {
t.Errorf("expected standard_nv6 to have name standard_nv6, got: '%s'", name)
}
if skuFamily := sku.GetFamilyName(); !strings.EqualFold(skuFamily, "standardNVFamily") {
t.Errorf("expected standard_nv6 to have name standardNVFamily, got: '%s'", skuFamily)
}
if skuSize := sku.GetSize(); !strings.EqualFold(skuSize, "nv6") {
t.Errorf("expected standard_nv6 to have name nv6 size, got: '%s'", skuSize)
}
if resourceType := sku.GetResourceType(); resourceType != VirtualMachines {
t.Errorf("expected standard_nv6 to have resourceType virtual machine, got: '%s'", resourceType)
}
if cpu, err := sku.VCPU(); cpu != 6 || err != nil {
t.Errorf("expected standard_nv6 to have 6 vCPUs and parse successfully, got value '%d' and error '%s'", cpu, err)
}
if cpu, err := sku.GPU(); cpu != 1 || err != nil {
t.Errorf("expected standard_nv6 to have 1 GPUs and parse successfully, got value '%d' and error '%s'", cpu, err)
}
if memory, err := sku.Memory(); memory != 56 || err != nil {
t.Errorf("expected standard_nv6 to have 56GB of memory and parse successfully, got value '%f' and error '%s'", memory, err)
}
if quantity, err := sku.GetCapabilityIntegerQuantity("ShouldNotBePresent"); quantity != -1 ||
!errors.As(err, &errCapabilityNotFound) ||
err.Error() != shouldNotBePresentCapabilityNotFoundErr {
t.Errorf("expected standard_nv6 not to have a non-existent capability, got value '%d' and error '%s'", quantity, err)
}
if quantity, err := sku.GetCapabilityIntegerQuantity(CapabilityPremiumIO); quantity != -1 ||
!errors.As(err, &errCapabilityValueNil) ||
err.Error() != premiumIOCapabilityValueParseErr {
t.Errorf("expected standard_nv6 to fail parsing value for boolean premiumIO as int, got value '%d' and error '%s'", quantity, err)
}
if sku.HasZonalCapability(UltraSSDAvailable) {
t.Errorf("expected standard_nv6 not to support ultra ssd")
}
if sku.HasZonalCapability("NotExistingCapability") {
t.Errorf("expected standard_nv6 not to support non-existent capability")
}
if sku.HasCapability(EphemeralOSDisk) {
t.Errorf("expected standard_nv6 not to support ephemeral os")
}
if sku.IsAcceleratedNetworkingSupported() {
t.Errorf("expected standard_nv6 to not support accelerated networking")
}
if cpuArch, err := sku.GetCPUArchitectureType(); err != nil || cpuArch != x64ArchType {
t.Errorf("expected standard_nv6 to have x64 cpuArchitectureType")
}
if sku.IsPremiumIO() {
t.Errorf("expected standard_nv6 to not support PremiumIO")
}
if !sku.IsHyperVGen1Supported() {
t.Errorf("expected standard_nv6 to support hyper v gen1")
}
if sku.IsHyperVGen2Supported() {
t.Errorf("expected standard_nv6 not to support hyper v gen2")
}
if sku.HasCapability(EncryptionAtHost) {
t.Errorf("expected standard_nv6 not to support encryption at host")
}
if !sku.IsAvailable("eastus") {
t.Errorf("expected standard_nv6 to be available in eastus")
}
if sku.IsRestricted("eastus") {
t.Errorf("expected standard_nv6 to be unrestricted in eastus")
}
if sku.IsAvailable("westus2") {
t.Errorf("expected standard_nv6 not to be available in westus2")
}
if sku.IsRestricted("westus2") {
t.Errorf("expected standard_nv6 not to be restricted in westus2")
}
if quantity, err := sku.MaxResourceVolumeMB(); quantity != 389120 || errors.As(err, &errCapabilityNotFound) {
t.Errorf("expected standard_nv6 to have 389120 MB of temporary disk, got value '%d' and error '%s'", quantity, err)
}
if isSupported, err := sku.HasCapabilityWithMinCapacity("MemoryGB", 1000); isSupported || err != nil {
t.Errorf("expected standard_nv6 not to have 1000GB of memory, got '%t', error: %s", isSupported, err)
}
hasV1 := !sku.HasCapabilityWithSeparator(HyperVGenerations, "V1")
hasV2 := sku.HasCapabilityWithSeparator(HyperVGenerations, "V2")
if hasV1 || hasV2 {
t.Errorf("expected standard_nv6 to support hyper-v generation v1 but not v2, got v1: '%t' , v2: '%t'", hasV1, hasV2)
}
})
t.Run("standard_D13_v2_promo", func(t *testing.T) {
errCapabilityNotFound := &ErrCapabilityNotFound{}
sku, err := cache.Get(ctx, "standard_D13_v2_promo", VirtualMachines, "eastus")
if err != nil {
t.Errorf("expected to find virtual machine sku standard_D13_v2_promo")
}
if sku.IsAvailable("eastus") {
t.Errorf("expected standard_D13_v2_promo to be unavailable in eastus")
}
if !sku.IsRestricted("eastus") {
t.Errorf("expected standard_D13_v2_promo to be restricted in eastus")
}
if sku.IsAvailable("westus2") {
t.Errorf("expected standard_D13_v2_promo not to be available in westus2")
}
if sku.IsRestricted("westus2") {
t.Errorf("expected standard_D13_v2_promo not to be restricted in westus2")
}
if cpuArch, err := sku.GetCPUArchitectureType(); !errors.As(err, &errCapabilityNotFound) || cpuArch != "" {
t.Errorf("expected standard_D13_v2_promo to not have cpuArchitectureType, got %s as cpuArchType with error as %s", cpuArch, err)
}
})
})
t.Run("availability zones", func(t *testing.T) {
if diff := cmp.Diff(cache.GetAvailabilityZones(ctx), expectedAvailabilityZones, []cmp.Option{
cmpopts.EquateEmpty(),
cmpopts.SortSlices(func(a, b string) bool {
return a < b
}),
}...); diff != "" {
t.Errorf("expected and actual availability zones mismatch: %s", diff)
}
})
})
}
}