forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows_test.go
314 lines (279 loc) · 9.36 KB
/
windows_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
/*
* 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"
"os"
"strings"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
v1 "kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
"kubevirt.io/kubevirt/pkg/testutils"
"kubevirt.io/kubevirt/pkg/util/net/dns"
"kubevirt.io/kubevirt/tests"
)
const (
windowsDisk = "windows-disk"
windowsFirmware = "5d307ca9-b3ef-428c-8861-06e72d69f223"
windowsVMIUser = "Administrator"
windowsVMIPassword = "Heslo123"
)
const (
winrmCli = "winrmcli"
winrmCliCmd = "winrm-cli"
)
var _ = Describe("Windows VirtualMachineInstance", func() {
flag.Parse()
virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
var windowsVMI *v1.VirtualMachineInstance
gracePeriod := int64(0)
spinlocks := uint32(8191)
firmware := types.UID(windowsFirmware)
_false := false
windowsVMISpec := v1.VirtualMachineInstanceSpec{
TerminationGracePeriodSeconds: &gracePeriod,
Domain: v1.DomainSpec{
CPU: &v1.CPU{Cores: 2},
Features: &v1.Features{
ACPI: v1.FeatureState{},
APIC: &v1.FeatureAPIC{},
Hyperv: &v1.FeatureHyperv{
Relaxed: &v1.FeatureState{},
VAPIC: &v1.FeatureState{},
Spinlocks: &v1.FeatureSpinlocks{Retries: &spinlocks},
},
},
Clock: &v1.Clock{
ClockOffset: v1.ClockOffset{UTC: &v1.ClockOffsetUTC{}},
Timer: &v1.Timer{
HPET: &v1.HPETTimer{Enabled: &_false},
PIT: &v1.PITTimer{TickPolicy: v1.PITTickPolicyDelay},
RTC: &v1.RTCTimer{TickPolicy: v1.RTCTickPolicyCatchup},
Hyperv: &v1.HypervTimer{},
},
},
Firmware: &v1.Firmware{UUID: firmware},
Resources: v1.ResourceRequirements{
Requests: k8sv1.ResourceList{
k8sv1.ResourceMemory: resource.MustParse("2048Mi"),
},
},
Devices: v1.Devices{
Disks: []v1.Disk{
{
Name: windowsDisk,
DiskDevice: v1.DiskDevice{Disk: &v1.DiskTarget{Bus: "sata"}},
},
},
},
},
Volumes: []v1.Volume{
{
Name: windowsDisk,
VolumeSource: v1.VolumeSource{
Ephemeral: &v1.EphemeralVolumeSource{
PersistentVolumeClaim: &k8sv1.PersistentVolumeClaimVolumeSource{
ClaimName: tests.DiskWindows,
},
},
},
},
},
}
tests.BeforeAll(func() {
tests.SkipIfNoWindowsImage(virtClient)
})
BeforeEach(func() {
tests.BeforeTestCleanup()
windowsVMI = tests.NewRandomVMI()
windowsVMI.Spec = windowsVMISpec
tests.AddExplicitPodNetworkInterface(windowsVMI)
windowsVMI.Spec.Domain.Devices.Interfaces[0].Model = "e1000"
})
It("should succeed to start a vmi", func() {
vmi, err := virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Create(windowsVMI)
Expect(err).To(BeNil())
tests.WaitForSuccessfulVMIStartWithTimeout(vmi, 360)
}, 300)
It("should succeed to stop a running vmi", func() {
By("Starting the vmi")
vmi, err := virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Create(windowsVMI)
Expect(err).To(BeNil())
tests.WaitForSuccessfulVMIStartWithTimeout(vmi, 360)
By("Stopping the vmi")
err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Delete(vmi.Name, &metav1.DeleteOptions{})
Expect(err).To(BeNil())
}, 300)
Context("with winrm connection", func() {
var winrmcliPod *k8sv1.Pod
var cli []string
var output string
var vmiIp string
BeforeEach(func() {
By("Creating winrm-cli pod for the future use")
winrmcliPod = &k8sv1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: winrmCli + rand.String(5)},
Spec: k8sv1.PodSpec{
Containers: []k8sv1.Container{
{
Name: winrmCli,
Image: fmt.Sprintf("%s/%s:%s", tests.KubeVirtRepoPrefix, winrmCli, tests.KubeVirtVersionTag),
Command: []string{"sleep"},
Args: []string{"3600"},
},
},
},
}
winrmcliPod, err = virtClient.CoreV1().Pods(tests.NamespaceTestDefault).Create(winrmcliPod)
Expect(err).ToNot(HaveOccurred())
By("Starting the windows VirtualMachineInstance")
windowsVMI, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Create(windowsVMI)
Expect(err).To(BeNil())
tests.WaitForSuccessfulVMIStartWithTimeout(windowsVMI, 360)
windowsVMI, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Get(windowsVMI.Name, &metav1.GetOptions{})
vmiIp = windowsVMI.Status.Interfaces[0].IP
cli = []string{
winrmCliCmd,
"-hostname",
vmiIp,
"-username",
windowsVMIUser,
"-password",
windowsVMIPassword,
}
})
It("should have correct UUID", func() {
command := append(cli, "wmic csproduct get \"UUID\"")
By(fmt.Sprintf("Running \"%s\" command via winrm-cli", command))
Eventually(func() error {
output, err = tests.ExecuteCommandOnPod(
virtClient,
winrmcliPod,
winrmcliPod.Spec.Containers[0].Name,
command,
)
return err
}, time.Minute*5, time.Second*15).ShouldNot(HaveOccurred())
By("Checking that the Windows VirtualMachineInstance has expected UUID")
Expect(output).Should(ContainSubstring(strings.ToUpper(windowsFirmware)))
}, 360)
It("should have pod IP", func() {
command := append(cli, "ipconfig /all")
By(fmt.Sprintf("Running \"%s\" command via winrm-cli", command))
Eventually(func() error {
output, err = tests.ExecuteCommandOnPod(
virtClient,
winrmcliPod,
winrmcliPod.Spec.Containers[0].Name,
command,
)
return err
}, time.Minute*5, time.Second*15).ShouldNot(HaveOccurred())
By("Checking that the Windows VirtualMachineInstance has expected IP address")
Expect(output).Should(ContainSubstring(vmiIp))
}, 360)
It("should have the domain set properly", func() {
command := append(cli, "wmic nicconfig get dnsdomain")
By(fmt.Sprintf("Running \"%s\" command via winrm-cli", command))
By("fetching /etc/resolv.conf from the VMI Pod")
resolvConf := tests.RunCommandOnVmiPod(windowsVMI, []string{"cat", "/etc/resolv.conf"})
By("extracting the search domain of the VMI")
searchDomains, err := dns.ParseSearchDomains(resolvConf)
Expect(err).ToNot(HaveOccurred())
searchDomain := ""
for _, s := range searchDomains {
if len(searchDomain) < len(s) {
searchDomain = s
}
}
Expect(searchDomain).To(HavePrefix(windowsVMI.Namespace), "should contain a searchdomain with the namespace of the VMI")
By("first making sure that we can execute VMI commands")
Eventually(func() error {
output, err = tests.ExecuteCommandOnPod(
virtClient,
winrmcliPod,
winrmcliPod.Spec.Containers[0].Name,
command,
)
return err
}, time.Minute*5, time.Second*15).ShouldNot(HaveOccurred())
By("repeatedly trying to get the search domain, since it may take some time until the domain is set")
Eventually(func() string {
output, err = tests.ExecuteCommandOnPod(
virtClient,
winrmcliPod,
winrmcliPod.Spec.Containers[0].Name,
command,
)
Expect(err).ToNot(HaveOccurred())
return output
}, time.Minute*1, time.Second*10).Should(MatchRegexp(`DNSDomain[\n\r\t ]+` + searchDomain + `[\n\r\t ]+`))
}, 360)
})
Context("with kubectl command", func() {
var yamlFile string
BeforeEach(func() {
tests.SkipIfNoCmd("kubectl")
yamlFile, err = tests.GenerateVMIJson(windowsVMI)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
if yamlFile != "" {
err = os.Remove(yamlFile)
Expect(err).ToNot(HaveOccurred())
yamlFile = ""
}
})
It("should succeed to start a vmi", func() {
By("Starting the vmi via kubectl command")
_, _, err = tests.RunCommand("kubectl", "create", "-f", yamlFile)
Expect(err).ToNot(HaveOccurred())
tests.WaitForSuccessfulVMIStartWithTimeout(windowsVMI, 360)
})
It("should succeed to stop a vmi", func() {
By("Starting the vmi via kubectl command")
_, _, err = tests.RunCommand("kubectl", "create", "-f", yamlFile)
Expect(err).ToNot(HaveOccurred())
tests.WaitForSuccessfulVMIStartWithTimeout(windowsVMI, 360)
podSelector := tests.UnfinishedVMIPodSelector(windowsVMI)
By("Deleting the vmi via kubectl command")
_, _, err = tests.RunCommand("kubectl", "delete", "-f", yamlFile)
Expect(err).ToNot(HaveOccurred())
By("Checking that the vmi does not exist anymore")
result := virtClient.RestClient().Get().Resource(tests.VMIResource).Namespace(k8sv1.NamespaceDefault).Name(windowsVMI.Name).Do()
Expect(result).To(testutils.HaveStatusCode(http.StatusNotFound))
By("Checking that the vmi pod terminated")
Eventually(func() int {
pods, err := virtClient.CoreV1().Pods(tests.NamespaceTestDefault).List(podSelector)
Expect(err).ToNot(HaveOccurred())
return len(pods.Items)
}, 75, 0.5).Should(Equal(0))
})
})
})