forked from mikeroyal/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_test.go
525 lines (434 loc) · 18.7 KB
/
vm_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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* 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 2018 Red Hat, Inc.
*
*/
package tests_test
import (
"flag"
"fmt"
"net/http"
"strings"
"time"
expect "github.com/google/goexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
v13 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/json"
v1 "kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
"kubevirt.io/kubevirt/pkg/util/net/dns"
"kubevirt.io/kubevirt/pkg/virtctl/vm"
"kubevirt.io/kubevirt/tests"
)
var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:component]VirtualMachine", func() {
flag.Parse()
virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
BeforeEach(func() {
tests.BeforeTestCleanup()
})
Context("An invalid VirtualMachine given", func() {
It("[test_id:1518]should be rejected on POST", func() {
vmiImage := tests.ContainerDiskFor(tests.ContainerDiskCirros)
template := tests.NewRandomVMIWithEphemeralDiskAndUserdata(vmiImage, "echo Hi\n")
newVMI := NewRandomVirtualMachine(template, false)
newVMI.TypeMeta = v12.TypeMeta{
APIVersion: v1.GroupVersion.String(),
Kind: "VirtualMachine",
}
jsonBytes, err := json.Marshal(newVMI)
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("virtualmachines").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:1519]should reject POST if validation webhoook deems the spec is invalid", func() {
vmiImage := tests.ContainerDiskFor(tests.ContainerDiskCirros)
template := tests.NewRandomVMIWithEphemeralDiskAndUserdata(vmiImage, "echo Hi\n")
// Add a disk that doesn't map to a volume.
// This should get rejected which tells us the webhook validator is working.
template.Spec.Domain.Devices.Disks = append(template.Spec.Domain.Devices.Disks, v1.Disk{
Name: "testdisk",
})
newVMI := NewRandomVirtualMachine(template, false)
newVMI.TypeMeta = v12.TypeMeta{
APIVersion: v1.GroupVersion.String(),
Kind: "VirtualMachine",
}
result := virtClient.RestClient().Post().Resource("virtualmachines").Namespace(tests.NamespaceTestDefault).Body(newVMI).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"))
})
})
Context("A valid VirtualMachine given", func() {
newVirtualMachine := func(running bool) *v1.VirtualMachine {
vmiImage := tests.ContainerDiskFor(tests.ContainerDiskCirros)
template := tests.NewRandomVMIWithEphemeralDiskAndUserdata(vmiImage, "echo Hi\n")
var newVMI *v1.VirtualMachine
var err error
newVMI = NewRandomVirtualMachine(template, running)
newVMI, err = virtClient.VirtualMachine(tests.NamespaceTestDefault).Create(newVMI)
Expect(err).ToNot(HaveOccurred())
return newVMI
}
startVMI := func(vm *v1.VirtualMachine) *v1.VirtualMachine {
By("Starting the VirtualMachineInstance")
Eventually(func() error {
updatedVMI, err := virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
updatedVMI.Spec.Running = true
_, err = virtClient.VirtualMachine(updatedVMI.Namespace).Update(updatedVMI)
return err
}, 300*time.Second, 1*time.Second).ShouldNot(HaveOccurred())
updatedVMI, err := virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
// Observe the VirtualMachineInstance created
Eventually(func() error {
_, err := virtClient.VirtualMachineInstance(updatedVMI.Namespace).Get(updatedVMI.Name, &v12.GetOptions{})
return err
}, 300*time.Second, 1*time.Second).Should(Succeed())
By("VMI has the running condition")
Eventually(func() bool {
vm, err := virtClient.VirtualMachine(updatedVMI.Namespace).Get(updatedVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return vm.Status.Ready
}, 300*time.Second, 1*time.Second).Should(BeTrue())
return updatedVMI
}
stopVMI := func(vm *v1.VirtualMachine) *v1.VirtualMachine {
By("Stopping the VirtualMachineInstance")
Eventually(func() error {
updatedVMI, err := virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
updatedVMI.Spec.Running = false
_, err = virtClient.VirtualMachine(updatedVMI.Namespace).Update(updatedVMI)
return err
}, 300*time.Second, 1*time.Second).ShouldNot(HaveOccurred())
updatedVMI, err := virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
// Observe the VirtualMachineInstance deleted
Eventually(func() bool {
_, err = virtClient.VirtualMachineInstance(updatedVMI.Namespace).Get(updatedVMI.Name, &v12.GetOptions{})
if errors.IsNotFound(err) {
return true
}
return false
}, 300*time.Second, 1*time.Second).Should(BeTrue(), "The vmi did not disappear")
By("VMI has not the running condition")
Eventually(func() bool {
vm, err := virtClient.VirtualMachine(updatedVMI.Namespace).Get(updatedVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return vm.Status.Ready
}, 300*time.Second, 1*time.Second).Should(BeFalse())
return updatedVMI
}
It("[test_id:1520]should update VirtualMachine once VMIs are up", func() {
newVMI := newVirtualMachine(true)
Eventually(func() bool {
vm, err := virtClient.VirtualMachine(tests.NamespaceTestDefault).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return vm.Status.Ready
}, 300*time.Second, 1*time.Second).Should(BeTrue())
})
It("[test_id:1521]should remove VirtualMachineInstance once the VMI is marked for deletion", func() {
newVMI := newVirtualMachine(true)
// Delete it
Expect(virtClient.VirtualMachine(newVMI.Namespace).Delete(newVMI.Name, &v12.DeleteOptions{})).To(Succeed())
// Wait until VMIs are gone
Eventually(func() int {
vmis, err := virtClient.VirtualMachineInstance(newVMI.Namespace).List(&v12.ListOptions{})
Expect(err).ToNot(HaveOccurred())
return len(vmis.Items)
}, 300*time.Second, 2*time.Second).Should(BeZero(), "The VirtualMachineInstance did not disappear")
})
It("[test_id:1522]should remove owner references on the VirtualMachineInstance if it is orphan deleted", func() {
newVMI := newVirtualMachine(true)
Eventually(func() []v12.OwnerReference {
// Check for owner reference
vmi, _ := virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
return vmi.OwnerReferences
}, 300*time.Second, 1*time.Second).ShouldNot(BeEmpty())
// Delete it
orphanPolicy := v12.DeletePropagationOrphan
Expect(virtClient.VirtualMachine(newVMI.Namespace).
Delete(newVMI.Name, &v12.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())
// Wait until the virtual machine is deleted
Eventually(func() bool {
_, err := virtClient.VirtualMachine(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
if errors.IsNotFound(err) {
return true
}
return false
}, 300*time.Second, 1*time.Second).Should(BeTrue())
vmi, err := virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(vmi.OwnerReferences).To(BeEmpty())
Expect(err).ToNot(HaveOccurred())
})
It("[test_id:1523]should recreate VirtualMachineInstance if it gets deleted", func() {
newVMI := startVMI(newVirtualMachine(false))
currentVMI, err := virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(virtClient.VirtualMachineInstance(newVMI.Namespace).Delete(newVMI.Name, &v12.DeleteOptions{})).To(Succeed())
Eventually(func() bool {
vmi, err := virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
if errors.IsNotFound(err) {
return false
}
if vmi.UID != currentVMI.UID {
return true
}
return false
}, 240*time.Second, 1*time.Second).Should(BeTrue())
})
It("[test_id:1524]should recreate VirtualMachineInstance if the VirtualMachineInstance's pod gets deleted", func() {
var firstVMI *v1.VirtualMachineInstance
var curVMI *v1.VirtualMachineInstance
var err error
By("Creating a new VMI")
newVMI := newVirtualMachine(true)
// wait for a running VirtualMachineInstance.
By("Waiting for the VMI's VirtualMachineInstance to start")
Eventually(func() error {
firstVMI, err = virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
if err != nil {
return err
}
if !firstVMI.IsRunning() {
return fmt.Errorf("vmi still isn't running")
}
return nil
}, 120*time.Second, 1*time.Second).Should(Succeed())
// get the pod backing the VirtualMachineInstance
By("Getting the pod backing the VirtualMachineInstance")
pods, err := virtClient.CoreV1().Pods(newVMI.Namespace).List(tests.UnfinishedVMIPodSelector(firstVMI))
Expect(err).ToNot(HaveOccurred())
Expect(len(pods.Items)).To(Equal(1))
firstPod := pods.Items[0]
// Delete the Pod
By("Deleting the VirtualMachineInstance's pod")
Eventually(func() error {
return virtClient.CoreV1().Pods(newVMI.Namespace).Delete(firstPod.Name, &v12.DeleteOptions{})
}, 120*time.Second, 1*time.Second).Should(Succeed())
// Wait on the VMI controller to create a new VirtualMachineInstance
By("Waiting for a new VirtualMachineInstance to spawn")
Eventually(func() bool {
curVMI, err = virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
// verify a new VirtualMachineInstance gets created for the VMI after the Pod is deleted.
if errors.IsNotFound(err) {
return false
} else if string(curVMI.UID) == string(firstVMI.UID) {
return false
} else if !curVMI.IsRunning() {
return false
}
return true
}, 120*time.Second, 1*time.Second).Should(BeTrue())
// sanity check that the test ran correctly by
// verifying a different Pod backs the VMI as well.
By("Verifying a new pod backs the VMI")
pods, err = virtClient.CoreV1().Pods(newVMI.Namespace).List(tests.UnfinishedVMIPodSelector(curVMI))
Expect(err).ToNot(HaveOccurred())
Expect(len(pods.Items)).To(Equal(1))
pod := pods.Items[0]
Expect(pod.Name).ToNot(Equal(firstPod.Name))
})
It("[test_id:1525]should stop VirtualMachineInstance if running set to false", func() {
currVMI := newVirtualMachine(false)
currVMI = startVMI(currVMI)
currVMI = stopVMI(currVMI)
})
It("[test_id:1526]should start and stop VirtualMachineInstance multiple times", func() {
var currVMI *v1.VirtualMachine
currVMI = newVirtualMachine(false)
// Start and stop VirtualMachineInstance multiple times
for i := 0; i < 5; i++ {
By(fmt.Sprintf("Doing run: %d", i))
startVMI(currVMI)
stopVMI(currVMI)
}
})
It("[test_id:1527]should not update the VirtualMachineInstance spec if Running", func() {
newVMI := newVirtualMachine(true)
Eventually(func() bool {
newVMI, err = virtClient.VirtualMachine(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return newVMI.Status.Ready
}, 360*time.Second, 1*time.Second).Should(BeTrue())
By("Updating the VMI template spec")
newVMI, err = virtClient.VirtualMachine(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
updatedVMI := newVMI.DeepCopy()
updatedVMI.Spec.Template.Spec.Domain.Resources.Requests = v13.ResourceList{
v13.ResourceMemory: resource.MustParse("4096Ki"),
}
updatedVMI, err := virtClient.VirtualMachine(updatedVMI.Namespace).Update(updatedVMI)
Expect(err).ToNot(HaveOccurred())
By("Expecting the old VirtualMachineInstance spec still running")
vmi, err := virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
vmiMemory := vmi.Spec.Domain.Resources.Requests.Memory()
vmMemory := newVMI.Spec.Template.Spec.Domain.Resources.Requests.Memory()
Expect(vmiMemory.Cmp(*vmMemory)).To(Equal(0))
By("Restarting the VMI")
newVMI = stopVMI(newVMI)
newVMI = startVMI(newVMI)
By("Expecting updated spec running")
vmi, err = virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
vmiMemory = vmi.Spec.Domain.Resources.Requests.Memory()
vmMemory = updatedVMI.Spec.Template.Spec.Domain.Resources.Requests.Memory()
Expect(vmiMemory.Cmp(*vmMemory)).To(Equal(0))
})
It("[test_id:1528]should survive guest shutdown, multiple times", func() {
By("Creating new VMI, not running")
newVMI := newVirtualMachine(false)
newVMI = startVMI(newVMI)
var vmi *v1.VirtualMachineInstance
for i := 0; i < 3; i++ {
currentVMI, err := virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
By("Getting the running VirtualMachineInstance")
Eventually(func() bool {
vmi, err = virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return vmi.Status.Phase == v1.Running
}, 240*time.Second, 1*time.Second).Should(BeTrue())
By("Obtaining the serial console")
expecter, err := tests.LoggedInCirrosExpecter(vmi)
Expect(err).ToNot(HaveOccurred())
defer expecter.Close()
By("Guest shutdown")
_, err = expecter.ExpectBatch([]expect.Batcher{
&expect.BSnd{S: "sudo poweroff\n"},
&expect.BExp{R: "The system is going down NOW!"},
}, 240*time.Second)
Expect(err).ToNot(HaveOccurred())
By("waiting for the controller to replace the shut-down vmi with a new instance")
Eventually(func() bool {
vmi, err = virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
// Almost there, a new instance should be spawned soon
if errors.IsNotFound(err) {
return false
}
Expect(err).ToNot(HaveOccurred())
// If the UID of the vmi changed we see the new vmi
if vmi.UID != currentVMI.UID {
return true
}
return false
}, 240*time.Second, 1*time.Second).Should(BeTrue(), "No new VirtualMachineInstance instance showed up")
By("VMI should run the VirtualMachineInstance again")
}
})
Context("Using virtctl interface", func() {
It("[test_id:1529]should start a VirtualMachineInstance once", func() {
var vmi *v1.VirtualMachineInstance
var err error
By("getting an VMI")
newVMI := newVirtualMachine(false)
By("Invoking virtctl start")
virtctl := tests.NewRepeatableVirtctlCommand(vm.COMMAND_START, "--namespace", newVMI.Namespace, newVMI.Name)
err = virtctl()
Expect(err).ToNot(HaveOccurred())
By("Getting the status of the VMI")
Eventually(func() bool {
newVMI, err = virtClient.VirtualMachine(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return newVMI.Status.Ready
}, 360*time.Second, 1*time.Second).Should(BeTrue())
By("Getting the running VirtualMachineInstance")
Eventually(func() bool {
vmi, err = virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return vmi.Status.Phase == v1.Running
}, 240*time.Second, 1*time.Second).Should(BeTrue())
By("Ensuring a second invocation should fail")
err = virtctl()
Expect(err).To(HaveOccurred())
})
It("[test_id:1530]should stop a VirtualMachineInstance once", func() {
var err error
By("getting an VMI")
newVMI := newVirtualMachine(true)
By("Invoking virtctl stop")
virtctl := tests.NewRepeatableVirtctlCommand(vm.COMMAND_STOP, "--namespace", newVMI.Namespace, newVMI.Name)
By("Ensuring VMI is running")
Eventually(func() bool {
newVMI, err = virtClient.VirtualMachine(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return newVMI.Status.Ready
}, 360*time.Second, 1*time.Second).Should(BeTrue())
err = virtctl()
Expect(err).ToNot(HaveOccurred())
By("Ensuring VMI is not running")
Eventually(func() bool {
newVMI, err = virtClient.VirtualMachine(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return !newVMI.Status.Ready && !newVMI.Status.Created
}, 360*time.Second, 1*time.Second).Should(BeTrue())
By("Ensuring the VirtualMachineInstance is removed")
Eventually(func() error {
_, err = virtClient.VirtualMachineInstance(newVMI.Namespace).Get(newVMI.Name, &v12.GetOptions{})
// Expect a 404 error
return err
}, 240*time.Second, 1*time.Second).Should(HaveOccurred())
By("Ensuring a second invocation should fail")
err = virtctl()
Expect(err).To(HaveOccurred())
})
})
})
})
// NewRandomVirtualMachine creates new VirtualMachine
func NewRandomVirtualMachine(vmi *v1.VirtualMachineInstance, running bool) *v1.VirtualMachine {
name := vmi.Name
namespace := vmi.Namespace
vm := &v1.VirtualMachine{
ObjectMeta: v12.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1.VirtualMachineSpec{
Running: running,
Template: &v1.VirtualMachineInstanceTemplateSpec{
ObjectMeta: v12.ObjectMeta{
Labels: map[string]string{"name": dns.SanitizeHostname(vmi)},
Name: name,
Namespace: namespace,
},
Spec: vmi.Spec,
},
},
}
return vm
}