forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_test.go
295 lines (256 loc) · 14 KB
/
template_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
/*
* 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 (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"kubevirt.io/kubevirt/tests/decorators"
"kubevirt.io/kubevirt/tests/clientcmd"
"kubevirt.io/kubevirt/tests/framework/checks"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
"kubevirt.io/kubevirt/tests/testsuite"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"kubevirt.io/kubevirt/tests/util"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"kubevirt.io/client-go/kubecli"
cd "kubevirt.io/kubevirt/tests/containerdisk"
"kubevirt.io/kubevirt/tests/libstorage"
vmsgen "kubevirt.io/kubevirt/tools/vms-generator/utils"
)
const (
defaultNamePrefix = "testvm-"
defaultCPUCores = "2"
defaultMemory = "2Gi"
)
var _ = Describe("[Serial][sig-compute]Templates", Serial, decorators.SigCompute, func() {
var virtClient kubecli.KubevirtClient
var (
templateParams map[string]string
workDir string
templateFile string
vmName string
)
BeforeEach(func() {
virtClient = kubevirt.Client()
clientcmd.SkipIfNoCmd("oc")
SetDefaultEventuallyTimeout(120 * time.Second)
SetDefaultEventuallyPollingInterval(2 * time.Second)
workDir = GinkgoT().TempDir()
})
Describe("Creating VM from Template", func() {
AssertTestSetupSuccess := func() func() {
return func() {
templateParams = map[string]string{
"NAME": defaultNamePrefix + rand.String(12),
"CPU_CORES": defaultCPUCores,
"MEMORY": defaultMemory,
}
templateFile = ""
ExpectWithOffset(1, templateParams).To(HaveKeyWithValue("NAME", Not(BeEmpty())), "invalid NAME parameter: VirtualMachine name cannot be empty string")
ExpectWithOffset(1, templateParams).To(HaveKeyWithValue("CPU_CORES", MatchRegexp(`^[0-9]+$`)), "invalid CPU_CORES parameter: %q is not unsigned integer", templateParams["CPU_CORES"])
ExpectWithOffset(1, templateParams).To(HaveKeyWithValue("MEMORY", MatchRegexp(`^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$`)), "invalid MEMORY parameter: %q is not valid quantity", templateParams["MEMORY"])
vmName = templateParams["NAME"]
vm, err := virtClient.VirtualMachine(util.NamespaceTestDefault).Get(context.Background(), vmName, metav1.GetOptions{})
ExpectWithOffset(1, k8serrors.IsNotFound(err) || vm.ObjectMeta.DeletionTimestamp != nil).To(BeTrue(), "invalid NAME parameter: VirtualMachine %q already exists", vmName)
}
}
AssertTemplateSetupSuccess := func(template *vmsgen.Template, params map[string]string) func() {
return func() {
ExpectWithOffset(1, template).NotTo(BeNil(), "template object was not provided")
By("Creating the Template JSON file")
var err error
templateFile, err = generateTemplateJson(template, workDir)
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "failed to write template JSON file: %v", err)
ExpectWithOffset(1, templateFile).To(BeAnExistingFile(), "template JSON file %q was not created", templateFile)
if params != nil {
By("Validating template parameters")
for param, value := range params {
switch param {
case "NAME":
ExpectWithOffset(1, value).NotTo(BeEmpty(), "invalid NAME parameter: VirtualMachine name cannot be empty string")
vmName = value
vm, err := virtClient.VirtualMachine(util.NamespaceTestDefault).Get(context.Background(), vmName, metav1.GetOptions{})
ExpectWithOffset(1, k8serrors.IsNotFound(err) || vm.ObjectMeta.DeletionTimestamp != nil).To(BeTrue(), "invalid NAME parameter: VirtualMachine %q already exists", vmName)
case "CPU_CORES":
ExpectWithOffset(1, templateParams).To(HaveKeyWithValue("CPU_CORES", MatchRegexp(`^[0-9]+$`)), "invalid CPU_CORES parameter: %q is not unsigned integer", templateParams["CPU_CORES"])
case "MEMORY":
ExpectWithOffset(1, templateParams).To(HaveKeyWithValue("MEMORY", MatchRegexp(`^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$`)), "invalid MEMORY parameter: %q is not valid quantity", templateParams["MEMORY"])
}
templateParams[param] = value
}
}
}
}
AssertTestCleanupSuccess := func() func() {
return func() {
if vm, err := virtClient.VirtualMachine(util.NamespaceTestDefault).Get(context.Background(), vmName, metav1.GetOptions{}); err == nil && vm.ObjectMeta.DeletionTimestamp == nil {
By("Deleting the VirtualMachine")
ExpectWithOffset(1, virtClient.VirtualMachine(util.NamespaceTestDefault).Delete(context.Background(), vmName, metav1.DeleteOptions{})).To(Succeed(), "failed to delete VirtualMachine %q: %v", vmName, err)
EventuallyWithOffset(1, func() bool {
obj, err := virtClient.VirtualMachine(util.NamespaceTestDefault).Get(context.Background(), vmName, metav1.GetOptions{})
return k8serrors.IsNotFound(err) || obj.ObjectMeta.DeletionTimestamp != nil
}).Should(BeTrue(), "VirtualMachine %q still exists and the deletion timestamp was not set", vmName)
}
if templateFile != "" {
if _, err := os.Stat(templateFile); !errors.Is(err, os.ErrNotExist) {
By("Deleting template JSON file")
ExpectWithOffset(1, os.RemoveAll(filepath.Dir(templateFile))).To(Succeed(), "failed to remove template JSON file %q: %v", templateFile, err)
ExpectWithOffset(1, templateFile).NotTo(BeAnExistingFile(), "template JSON file %q was not removed", templateFile)
}
}
}
}
AssertVMCreationSuccess := func() func() {
return func() {
By("Creating VirtualMachine from Template via oc command")
ocProcessCommand := []string{"oc", "process", "-f", templateFile}
for param, value := range templateParams {
ocProcessCommand = append(ocProcessCommand, "-p", fmt.Sprintf("%s=%s", param, value))
}
out, stderr, err := clientcmd.RunCommandPipe(ocProcessCommand, []string{"oc", "create", "-f", "-"})
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "failed to create VirtualMachine %q via command \"%s | oc create -f -\": %s: %v", vmName, strings.Join(ocProcessCommand, " "), out+stderr, err)
ExpectWithOffset(1, out).To(MatchRegexp(`"?%s"? created\n`, vmName), "command \"%s | oc create -f -\" did not print expected message: %s", strings.Join(ocProcessCommand, " "), out+stderr)
By("Checking if the VirtualMachine exists")
EventuallyWithOffset(1, func() error {
_, err := virtClient.VirtualMachine(util.NamespaceTestDefault).Get(context.Background(), vmName, metav1.GetOptions{})
return err
}).Should(Succeed(), "VirtualMachine %q still does not exist", vmName)
}
}
AssertVMCreationFailure := func() func() {
return func() {
By("Creating VirtualMachine from Template via oc command")
ocProcessCommand := []string{"oc", "process", "-f", templateFile}
for param, value := range templateParams {
ocProcessCommand = append(ocProcessCommand, "-p", fmt.Sprintf("%s=%s", param, value))
}
out, stderr, err := clientcmd.RunCommandPipe(ocProcessCommand, []string{"oc", "create", "-f", "-"})
ExpectWithOffset(1, err).To(HaveOccurred(), "creation of VirtualMachine %q via command \"%s | oc create -f -\" succeeded: %s: %v", vmName, strings.Join(ocProcessCommand, " "), out+stderr, err)
}
}
AssertVMDeletionSuccess := func() func() {
return func() {
By("Deleting the VirtualMachine via oc command")
out, stderr, err := clientcmd.RunCommand("oc", "delete", "vm", vmName)
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "failed to delete VirtualMachine via command \"oc delete vm %s\": %s: %v", vmName, out+stderr, err)
ExpectWithOffset(1, out).To(MatchRegexp(`"?%s"? deleted\n`, vmName), "command \"oc delete vm %s\" did not print expected message: %s", vmName, out)
By("Checking if the VM does not exist anymore")
EventuallyWithOffset(1, func() bool {
vm, err := virtClient.VirtualMachine(util.NamespaceTestDefault).Get(context.Background(), vmName, metav1.GetOptions{})
return k8serrors.IsNotFound(err) || vm.ObjectMeta.DeletionTimestamp != nil
}).Should(BeTrue(), "the VirtualMachine %q still exists and deletion timestamp was not set", vmName)
}
}
AssertVMDeletionFailure := func() func() {
return func() {
By("Deleting the VirtualMachine via oc command")
out, stderr, err := clientcmd.RunCommand("oc", "delete", "vm", vmName)
ExpectWithOffset(1, err).To(HaveOccurred(), "failed to delete VirtualMachine via command \"oc delete vm %s\": %s: %v", vmName, out+stderr, err)
}
}
AssertVMStartSuccess := func(command string) func() {
return func() {
switch command {
case "oc":
By("Starting VirtualMachine via oc command")
patch := `{"spec":{"running":true}}`
out, stderr, err := clientcmd.RunCommand("oc", "patch", "vm", vmName, "--type=merge", "-p", patch)
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "failed schedule VirtualMachine %q start via command \"oc patch vm %s --type=merge -p '%s'\": %s: %v", vmName, vmName, patch, out+stderr, err)
ExpectWithOffset(1, out).To(MatchRegexp(`"?%s"? patched\n`, vmName), "command \"oc patch vm %s --type=merge -p '%s'\" did not print expected message: %s", vmName, patch, out+stderr)
case "virtctl":
By("Starting VirtualMachine via virtctl command")
out, stderr, err := clientcmd.RunCommand("virtctl", "start", vmName)
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "failed to schedule VirtualMachine %q start via command \"virtctl start %s\": %s: %v", vmName, vmName, out+stderr, err)
ExpectWithOffset(1, out).To(ContainSubstring("%s was scheduled to start\n", vmName), "command \"virtctl start %s\" did not print expected message: %s", vmName, out+stderr)
}
By("Checking if the VirtualMachineInstance was created")
EventuallyWithOffset(1, func() error {
_, err := virtClient.VirtualMachineInstance(util.NamespaceTestDefault).Get(context.Background(), vmName, metav1.GetOptions{})
return err
}).Should(Succeed(), "the VirtualMachineInstance %q still does not exist", vmName)
By("Checking if the VirtualMachine has status ready")
EventuallyWithOffset(1, func() bool {
vm, err := virtClient.VirtualMachine(util.NamespaceTestDefault).Get(context.Background(), vmName, metav1.GetOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "failed to fetch VirtualMachine %q: %v", vmName, err)
return vm.Status.Ready
}).Should(BeTrue(), "VirtualMachine %q still does not have status ready", vmName)
By("Checking if the VirtualMachineInstance specs match Template parameters")
vmi, err := virtClient.VirtualMachineInstance(util.NamespaceTestDefault).Get(context.Background(), vmName, metav1.GetOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "failed to fetch VirtualMachine %q: %v", vmName, err)
vmiCPUCores := vmi.Spec.Domain.CPU.Cores
templateParamCPUCores, err := strconv.ParseUint(templateParams["CPU_CORES"], 10, 32)
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "cannot parse CPU_CORES parameter: value %q: %v", templateParams["CPU_CORES"], err)
ExpectWithOffset(1, vmiCPUCores).To(Equal(uint32(templateParamCPUCores)), "VirtualMachineInstance CPU cores (%d) does not match CPU_CORES parameter value: %s", vmiCPUCores, templateParams["CPU_CORES"])
vmiMemory := vmi.Spec.Domain.Resources.Requests["memory"]
templateParamMemory, err := resource.ParseQuantity(templateParams["MEMORY"])
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "cannot parse MEMORY parameter: value %q: %v", templateParams["MEMORY"], err)
ExpectWithOffset(1, vmiMemory).To(Equal(templateParamMemory), "VirtualMachineInstance memory (%s) does not match MEMORY parameter value: %s", vmiMemory.String(), templateParams["MEMORY"])
}
}
AssertTemplateTestSuccess := func() {
It("[test_id:3292]should succeed to create VirtualMachine via oc command", AssertVMCreationSuccess())
It("[test_id:3293]should fail to delete VirtualMachine via oc command", AssertVMDeletionFailure())
When("the VirtualMachine was created", func() {
BeforeEach(AssertVMCreationSuccess())
It("[test_id:3294]should succeed to start the VirtualMachine via oc command", AssertVMStartSuccess("oc"))
It("[test_id:3295]should succeed to delete VirtualMachine via oc command", AssertVMDeletionSuccess())
It("[test_id:3308]should fail to create the same VirtualMachine via oc command", AssertVMCreationFailure())
})
}
BeforeEach(AssertTestSetupSuccess())
AfterEach(AssertTestCleanupSuccess())
Context("with Fedora Template", func() {
BeforeEach(func() {
AssertTemplateSetupSuccess(vmsgen.GetTemplateFedoraWithContainerDisk(cd.ContainerDiskFor(cd.ContainerDiskFedoraTestTooling)), nil)()
})
AssertTemplateTestSuccess()
})
Context("[rfe_id:273][crit:medium][vendor:[email protected]][level:component]with RHEL Template", func() {
BeforeEach(func() {
const OSRhel = "rhel"
checks.SkipIfNoRhelImage(virtClient)
libstorage.CreatePVC(OSRhel, testsuite.GetTestNamespace(nil), "15Gi", libstorage.Config.StorageClassRhel, true)
AssertTemplateSetupSuccess(vmsgen.GetTestTemplateRHEL7(), nil)()
})
AssertTemplateTestSuccess()
})
})
})
func generateTemplateJson(template *vmsgen.Template, generateDirectory string) (string, error) {
data, err := json.Marshal(template)
if err != nil {
return "", fmt.Errorf("failed to generate json for template %q: %v", template.Name, err)
}
jsonFile := filepath.Join(generateDirectory, template.Name+".json")
if err = os.WriteFile(jsonFile, data, 0644); err != nil {
return "", fmt.Errorf("failed to write json to file %q: %v", jsonFile, err)
}
return jsonFile, nil
}