forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
replicaset_test.go
441 lines (376 loc) · 18.3 KB
/
replicaset_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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2017 Red Hat, Inc.
*
*/
package tests_test
import (
"fmt"
"net/http"
"strings"
"time"
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
autov1 "k8s.io/api/autoscaling/v1"
"k8s.io/apimachinery/pkg/api/errors"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/tests"
cd "kubevirt.io/kubevirt/tests/containerdisk"
)
var _ = Describe("[rfe_id:588][crit:medium][vendor:[email protected]][level:component]VirtualMachineInstanceReplicaSet", func() {
var err error
var virtClient kubecli.KubevirtClient
BeforeEach(func() {
virtClient, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)
tests.BeforeTestCleanup()
})
doScale := func(name string, scale int32) {
By(fmt.Sprintf("Scaling to %d", scale))
rs, err := virtClient.ReplicaSet(tests.NamespaceTestDefault).Patch(name, types.JSONPatchType, []byte(fmt.Sprintf("[{ \"op\": \"replace\", \"path\": \"/spec/replicas\", \"value\": %v }]", scale)))
Expect(err).ToNot(HaveOccurred())
By("Checking the number of replicas")
Eventually(func() int32 {
rs, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).Get(name, v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return rs.Status.Replicas
}, 90*time.Second, time.Second).Should(Equal(int32(scale)))
vmis, err := virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).List(&v12.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(tests.NotDeleted(vmis)).To(HaveLen(int(scale)))
}
doScaleWithScaleSubresource := func(name string, scale int32) {
// Status updates can conflict with our desire to change the spec
By(fmt.Sprintf("Scaling to %d", scale))
var s *autov1.Scale
err = tests.RetryIfModified(func() error {
s, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).GetScale(name, v12.GetOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
s.Spec.Replicas = scale
s, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).UpdateScale(name, s)
return err
})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
By("Checking the number of replicas")
EventuallyWithOffset(1, func() int32 {
s, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).GetScale(name, v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return s.Status.Replicas
}, 90*time.Second, time.Second).Should(Equal(int32(scale)))
vmis, err := virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).List(&v12.ListOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
ExpectWithOffset(1, tests.NotDeleted(vmis)).To(HaveLen(int(scale)))
}
doScaleWithHPA := func(name string, min int32, max int32, expected int32) {
// Status updates can conflict with our desire to change the spec
By(fmt.Sprintf("Scaling to %d", min))
hpa := &autov1.HorizontalPodAutoscaler{
ObjectMeta: v12.ObjectMeta{
Name: name,
},
Spec: autov1.HorizontalPodAutoscalerSpec{
ScaleTargetRef: autov1.CrossVersionObjectReference{
Name: name,
Kind: v1.VirtualMachineInstanceReplicaSetGroupVersionKind.Kind,
APIVersion: v1.VirtualMachineInstanceReplicaSetGroupVersionKind.GroupVersion().String(),
},
MinReplicas: &min,
MaxReplicas: max,
},
}
hpa, err := virtClient.AutoscalingV1().HorizontalPodAutoscalers(tests.NamespaceTestDefault).Create(hpa)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
var s *autov1.Scale
By("Checking the number of replicas")
EventuallyWithOffset(1, func() int32 {
s, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).GetScale(name, v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return s.Status.Replicas
}, 90*time.Second, time.Second).Should(Equal(int32(expected)))
vmis, err := virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).List(&v12.ListOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
ExpectWithOffset(1, tests.NotDeleted(vmis)).To(HaveLen(int(min)))
err = virtClient.AutoscalingV1().HorizontalPodAutoscalers(tests.NamespaceTestDefault).Delete(name, &v12.DeleteOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
}
newReplicaSet := func() *v1.VirtualMachineInstanceReplicaSet {
By("Create a new VirtualMachineInstance replica set")
template := tests.NewRandomVMIWithEphemeralDiskAndUserdata(cd.ContainerDiskFor(cd.ContainerDiskCirros), "#!/bin/bash\necho 'hello'\n")
newRS := tests.NewRandomReplicaSetFromVMI(template, int32(0))
newRS, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).Create(newRS)
Expect(err).ToNot(HaveOccurred())
return newRS
}
table.DescribeTable("[rfe_id:588][crit:medium][vendor:[email protected]][level:component]should scale", func(startScale int, stopScale int) {
newRS := newReplicaSet()
doScale(newRS.ObjectMeta.Name, int32(startScale))
doScale(newRS.ObjectMeta.Name, int32(stopScale))
doScale(newRS.ObjectMeta.Name, int32(0))
},
table.Entry("[test_id:1405]to three, to two and then to zero replicas", 3, 2),
table.Entry("[test_id:1406]to five, to six and then to zero replicas", 5, 6),
)
table.DescribeTable("[rfe_id:588][crit:medium][vendor:[email protected]][level:component]should scale with scale subresource", func(startScale int, stopScale int) {
newRS := newReplicaSet()
doScaleWithScaleSubresource(newRS.ObjectMeta.Name, int32(startScale))
doScaleWithScaleSubresource(newRS.ObjectMeta.Name, int32(stopScale))
doScaleWithScaleSubresource(newRS.ObjectMeta.Name, int32(0))
},
table.Entry("[test_id:1407]to three, to two and then to zero replicas", 3, 2),
table.Entry("[test_id:1408]to five, to six and then to zero replicas", 5, 6),
)
table.DescribeTable("[rfe_id:588][crit:medium][vendor:[email protected]][level:component]should scale with the horizontal pod autoscaler", func(startScale int, stopScale int) {
tests.SkipIfVersionBelow("HPA only works with CRs with multiple versions starting from 1.13", "1.13")
template := tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskCirros))
newRS := tests.NewRandomReplicaSetFromVMI(template, int32(1))
newRS, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).Create(newRS)
Expect(err).ToNot(HaveOccurred())
doScaleWithHPA(newRS.ObjectMeta.Name, int32(startScale), int32(startScale), int32(startScale))
doScaleWithHPA(newRS.ObjectMeta.Name, int32(stopScale), (int32(stopScale)), int32(stopScale))
doScaleWithHPA(newRS.ObjectMeta.Name, int32(1), int32(1), int32(1))
},
table.Entry("[test_id:1409]to three, to two and then to one replicas", 3, 2),
table.Entry("[test_id:1410]to five, to six and then to one replicas", 5, 6),
)
It("[test_id:1411]should be rejected on POST if spec is invalid", func() {
newRS := newReplicaSet()
newRS.TypeMeta = v12.TypeMeta{
APIVersion: v1.StorageGroupVersion.String(),
Kind: "VirtualMachineInstanceReplicaSet",
}
jsonBytes, err := json.Marshal(newRS)
Expect(err).To(BeNil())
// change the name of a required field (like domain) so validation will fail
jsonString := strings.Replace(string(jsonBytes), "domain", "not-a-domain", -1)
result := virtClient.RestClient().Post().Resource("virtualmachineinstancereplicasets").Namespace(tests.NamespaceTestDefault).Body([]byte(jsonString)).SetHeader("Content-Type", "application/json").Do()
// Verify validation failed.
statusCode := 0
result.StatusCode(&statusCode)
Expect(statusCode).To(Equal(http.StatusUnprocessableEntity))
})
It("[test_id:1412]should reject POST if validation webhoook deems the spec is invalid", func() {
newRS := newReplicaSet()
newRS.TypeMeta = v12.TypeMeta{
APIVersion: v1.GroupVersion.String(),
Kind: "VirtualMachineInstanceReplicaSet",
}
// Add a disk that doesn't map to a volume.
// This should get rejected which tells us the webhook validator is working.
newRS.Spec.Template.Spec.Domain.Devices.Disks = append(newRS.Spec.Template.Spec.Domain.Devices.Disks, v1.Disk{
Name: "testdisk",
})
result := virtClient.RestClient().Post().Resource("virtualmachineinstancereplicasets").Namespace(tests.NamespaceTestDefault).Body(newRS).Do()
// Verify validation failed.
statusCode := 0
result.StatusCode(&statusCode)
Expect(statusCode).To(Equal(http.StatusUnprocessableEntity))
reviewResponse := &v12.Status{}
body, _ := result.Raw()
err = json.Unmarshal(body, reviewResponse)
Expect(err).To(BeNil())
Expect(len(reviewResponse.Details.Causes)).To(Equal(1))
Expect(reviewResponse.Details.Causes[0].Field).To(Equal("spec.template.spec.domain.devices.disks[2].name"))
})
It("[test_id:1413]should update readyReplicas once VMIs are up", func() {
newRS := newReplicaSet()
doScale(newRS.ObjectMeta.Name, 2)
By("checking the number of ready replicas in the returned yaml")
Eventually(func() int {
rs, err := virtClient.ReplicaSet(tests.NamespaceTestDefault).Get(newRS.ObjectMeta.Name, v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return int(rs.Status.ReadyReplicas)
}, 120*time.Second, 1*time.Second).Should(Equal(2))
})
It("[test_id:1414]should return the correct data when using server-side printing", func() {
tests.SkipIfVersionBelow("server-side printing is only enabled by default from 1.11 on", "1.11")
newRS := newReplicaSet()
doScale(newRS.ObjectMeta.Name, 2)
By("waiting until all VMIs are ready")
Eventually(func() int {
rs, err := virtClient.ReplicaSet(tests.NamespaceTestDefault).Get(newRS.ObjectMeta.Name, v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return int(rs.Status.ReadyReplicas)
}, 120*time.Second, 1*time.Second).Should(Equal(2))
By("checking the output of server-side table printing")
rawTable, err := virtClient.RestClient().Get().
RequestURI(fmt.Sprintf("/apis/kubevirt.io/v1alpha3/namespaces/%s/virtualmachineinstancereplicasets/%s", tests.NamespaceTestDefault, newRS.ObjectMeta.Name)).
SetHeader("Accept", "application/json;as=Table;v=v1beta1;g=meta.k8s.io, application/json").
DoRaw()
Expect(err).ToNot(HaveOccurred())
table := &v1beta1.Table{}
Expect(json.Unmarshal(rawTable, table)).To(Succeed())
Expect(table.ColumnDefinitions[0].Name).To(Equal("Name"))
Expect(table.ColumnDefinitions[0].Type).To(Equal("string"))
Expect(table.ColumnDefinitions[1].Name).To(Equal("Desired"))
Expect(table.ColumnDefinitions[1].Type).To(Equal("integer"))
Expect(table.ColumnDefinitions[2].Name).To(Equal("Current"))
Expect(table.ColumnDefinitions[2].Type).To(Equal("integer"))
Expect(table.ColumnDefinitions[3].Name).To(Equal("Ready"))
Expect(table.ColumnDefinitions[3].Type).To(Equal("integer"))
Expect(table.ColumnDefinitions[4].Name).To(Equal("Age"))
Expect(table.ColumnDefinitions[4].Type).To(Equal("date"))
Expect(table.Rows[0].Cells[0].(string)).To(Equal(newRS.ObjectMeta.Name))
Expect(int(table.Rows[0].Cells[1].(float64))).To(Equal(2))
Expect(int(table.Rows[0].Cells[2].(float64))).To(Equal(2))
Expect(int(table.Rows[0].Cells[3].(float64))).To(Equal(2))
})
It("[test_id:1415]should remove VMIs once they are marked for deletion", func() {
newRS := newReplicaSet()
// Create a replicaset with two replicas
doScale(newRS.ObjectMeta.Name, 2)
// Delete it
By("Deleting the VirtualMachineInstance replica set")
Expect(virtClient.ReplicaSet(newRS.ObjectMeta.Namespace).Delete(newRS.ObjectMeta.Name, &v12.DeleteOptions{})).To(Succeed())
// Wait until VMIs are gone
By("Waiting until all VMIs are gone")
Eventually(func() int {
vmis, err := virtClient.VirtualMachineInstance(newRS.ObjectMeta.Namespace).List(&v12.ListOptions{})
Expect(err).ToNot(HaveOccurred())
return len(vmis.Items)
}, 120*time.Second, 1*time.Second).Should(BeZero())
})
It("[test_id:1416]should remove owner references on the VirtualMachineInstance if it is orphan deleted", func() {
// Cascade=false delete fails in ocp 3.11 with CRDs that contain multiple versions.
tests.SkipIfOpenShiftAndBelowOrEqualVersion("cascade=false delete does not work with CRD multi version support in ocp 3.11", "1.11.0")
newRS := newReplicaSet()
// Create a replicaset with two replicas
doScale(newRS.ObjectMeta.Name, 2)
// Check for owner reference
vmis, err := virtClient.VirtualMachineInstance(newRS.ObjectMeta.Namespace).List(&v12.ListOptions{})
Expect(vmis.Items).To(HaveLen(2))
for _, vmi := range vmis.Items {
Expect(vmi.OwnerReferences).ToNot(BeEmpty())
}
// Delete it
By("Deleting the VirtualMachineInstance replica set with the 'orphan' deletion strategy")
orphanPolicy := v12.DeletePropagationOrphan
Expect(virtClient.ReplicaSet(newRS.ObjectMeta.Namespace).
Delete(newRS.ObjectMeta.Name, &v12.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())
// Wait until the replica set is deleted
By("Waiting until the replica set got deleted")
Eventually(func() bool {
_, err := virtClient.ReplicaSet(newRS.ObjectMeta.Namespace).Get(newRS.ObjectMeta.Name, v12.GetOptions{})
if errors.IsNotFound(err) {
return true
}
return false
}, 60*time.Second, 1*time.Second).Should(BeTrue())
By("Checking if two VMIs are orphaned and still exist")
vmis, err = virtClient.VirtualMachineInstance(newRS.ObjectMeta.Namespace).List(&v12.ListOptions{})
Expect(vmis.Items).To(HaveLen(2))
By("Checking a VirtualMachineInstance owner references")
for _, vmi := range vmis.Items {
Expect(vmi.OwnerReferences).To(BeEmpty())
}
Expect(err).ToNot(HaveOccurred())
})
It("[test_id:1417]should not scale when paused and scale when resume", func() {
rs := newReplicaSet()
// pause controller
By("Pausing the replicaset")
_, err := virtClient.ReplicaSet(rs.Namespace).Patch(rs.Name, types.JSONPatchType, []byte("[{ \"op\": \"add\", \"path\": \"/spec/paused\", \"value\": true }]"))
Expect(err).ToNot(HaveOccurred())
Eventually(func() v1.VirtualMachineInstanceReplicaSetConditionType {
rs, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).Get(rs.ObjectMeta.Name, v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
if len(rs.Status.Conditions) > 0 {
return rs.Status.Conditions[0].Type
}
return ""
}, 10*time.Second, 1*time.Second).Should(Equal(v1.VirtualMachineInstanceReplicaSetReplicaPaused))
// set new replica count while still being paused
By("Updating the number of replicas")
rs.Spec.Replicas = tests.NewInt32(2)
_, err = virtClient.ReplicaSet(rs.ObjectMeta.Namespace).Update(rs)
Expect(err).ToNot(HaveOccurred())
// make sure that we don't scale up
By("Checking that the replicaset do not scale while it is paused")
Consistently(func() int32 {
rs, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).Get(rs.ObjectMeta.Name, v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
// Make sure that no failure happened, so that ensure that we don't scale because we are paused
Expect(rs.Status.Conditions).To(HaveLen(1))
return rs.Status.Replicas
}, 3*time.Second, 1*time.Second).Should(Equal(int32(0)))
// resume controller
By("Resuming the replicaset")
_, err = virtClient.ReplicaSet(rs.Namespace).Patch(rs.Name, types.JSONPatchType, []byte("[{ \"op\": \"replace\", \"path\": \"/spec/paused\", \"value\": false }]"))
Expect(err).ToNot(HaveOccurred())
// Paused condition should disappear
By("Checking that the pause condition disappeared from the replicaset")
Eventually(func() int {
rs, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).Get(rs.ObjectMeta.Name, v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return len(rs.Status.Conditions)
}, 10*time.Second, 1*time.Second).Should(Equal(0))
// Replicas should be created
By("Checking that the missing replicas are now created")
Eventually(func() int32 {
rs, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).Get(rs.ObjectMeta.Name, v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return rs.Status.Replicas
}, 10*time.Second, 1*time.Second).Should(Equal(int32(2)))
})
It("[test_id:1418]should remove the finished VM", func() {
By("Creating new replica set")
rs := newReplicaSet()
doScale(rs.ObjectMeta.Name, int32(2))
vmis, err := virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).List(&v12.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(vmis.Items).ToNot(BeEmpty())
vmi := vmis.Items[0]
pods, err := virtClient.CoreV1().Pods(tests.NamespaceTestDefault).List(tests.UnfinishedVMIPodSelector(&vmi))
Expect(err).ToNot(HaveOccurred())
Expect(len(pods.Items)).To(Equal(1))
pod := pods.Items[0]
By("Deleting one of the RS VMS pods")
err = virtClient.CoreV1().Pods(tests.NamespaceTestDefault).Delete(pod.Name, &v12.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
By("Checking that the VM disappeared")
Eventually(func() bool {
_, err := virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vmi.Name, &v12.GetOptions{})
if errors.IsNotFound(err) {
return true
}
return false
}, 120*time.Second, time.Second).Should(BeTrue())
By("Checking number of RS VM's")
vmis, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).List(&v12.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(len(vmis.Items)).Should(Equal(2))
})
It("[test_id:4121]should create and verify kubectl/oc output for vm replicaset", func() {
k8sClient := tests.GetK8sCmdClient()
tests.SkipIfNoCmd(k8sClient)
newRS := newReplicaSet()
doScale(newRS.ObjectMeta.Name, 2)
result, _, _ := tests.RunCommand(k8sClient, "get", "virtualmachineinstancereplicaset")
Expect(result).ToNot(BeNil())
resultFields := strings.Fields(result)
expectedHeader := []string{"NAME", "DESIRED", "CURRENT", "READY", "AGE"}
columnHeaders := resultFields[:len(expectedHeader)]
// Verify the generated header is same as expected
Expect(columnHeaders).To(Equal(expectedHeader))
// Name will be there in all the cases, so verify name
Expect(resultFields[len(expectedHeader)]).To(Equal(newRS.Name))
})
})