forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpose_test.go
364 lines (308 loc) · 14.3 KB
/
expose_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
package tests_test
import (
"flag"
"fmt"
"strconv"
"time"
"github.com/google/goexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
k8sv1 "k8s.io/api/core/v1"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
"kubevirt.io/kubevirt/pkg/virtctl/expose"
"kubevirt.io/kubevirt/pkg/virtctl/offlinevm"
"kubevirt.io/kubevirt/tests"
)
func newLabeledVM(label string, virtClient kubecli.KubevirtClient) (vm *v1.VirtualMachine) {
vm = tests.NewRandomVMWithEphemeralDiskAndUserdata(tests.RegistryDiskFor(tests.RegistryDiskCirros), "#!/bin/bash\necho 'hello'\n")
vm.Labels = map[string]string{"expose": label}
vm, err := virtClient.VM(tests.NamespaceTestDefault).Create(vm)
Expect(err).ToNot(HaveOccurred())
tests.WaitForSuccessfulVMStartIgnoreWarnings(vm)
vm, err = virtClient.VM(tests.NamespaceTestDefault).Get(vm.ObjectMeta.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return
}
func generateHelloWorldServer(vm *v1.VirtualMachine, virtClient kubecli.KubevirtClient, testPort int, protocol string) {
_, err := tests.LoggedInCirrosExpecter(vm)
Expect(err).ToNot(HaveOccurred())
expecter, _, err := tests.NewConsoleExpecter(virtClient, vm, 10*time.Second)
defer expecter.Close()
Expect(err).ToNot(HaveOccurred())
serverCommand := fmt.Sprintf("screen -d -m nc -klp %d -e echo -e 'Hello World!'\n", testPort)
if protocol == "udp" {
// nc has to be in a while loop in case of UDP, since it exists after one message
serverCommand = fmt.Sprintf("screen -d -m sh -c \"while true\n do nc -uklp %d -e echo -e 'Hello UDP World!'\ndone\n\"\n", testPort)
}
_, err = expecter.ExpectBatch([]expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: "\\$ "},
&expect.BSnd{S: serverCommand},
&expect.BExp{R: "\\$ "},
&expect.BSnd{S: "echo $?\n"},
&expect.BExp{R: "0"},
}, 60*time.Second)
Expect(err).ToNot(HaveOccurred())
}
var _ = Describe("Expose", func() {
flag.Parse()
virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
const testPort = 1500
Context("Expose service on a VM", func() {
var tcpVM *v1.VirtualMachine
tests.BeforeAll(func() {
tcpVM = newLabeledVM("vm", virtClient)
generateHelloWorldServer(tcpVM, virtClient, testPort, "tcp")
})
Context("Expose ClusterIP service", func() {
const servicePort = "27017"
const serviceName = "cluster-ip-vm"
It("Should expose a Cluster IP service on a VM and connect to it", func() {
By("Exposing the service via virtctl command")
virtctl := tests.NewRepeatableVirtctlCommand(expose.COMMAND_EXPOSE, "virtualmachine", "--namespace",
tcpVM.Namespace, tcpVM.Name, "--port", servicePort, "--name", serviceName, "--target-port", strconv.Itoa(testPort))
err := virtctl()
Expect(err).ToNot(HaveOccurred())
By("Getting back the cluster IP given for the service")
svc, err := virtClient.CoreV1().Services(tcpVM.Namespace).Get(serviceName, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
serviceIP := svc.Spec.ClusterIP
By("Starting a pod which tries to reach the VM via ClusterIP")
job := tests.NewHelloWorldJob(serviceIP, servicePort)
job, err = virtClient.CoreV1().Pods(tcpVM.Namespace).Create(job)
Expect(err).ToNot(HaveOccurred())
By("Waiting for the pod to report a successful connection attempt")
getStatus := func() k8sv1.PodPhase {
pod, err := virtClient.CoreV1().Pods(job.Namespace).Get(job.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return pod.Status.Phase
}
Eventually(getStatus, 60, 1).Should(Equal(k8sv1.PodSucceeded))
})
})
Context("Expose NodePort service", func() {
const servicePort = "27017"
const serviceName = "node-port-vm"
const nodePort = "30017"
It("Should expose a NodePort service on a VM and connect to it", func() {
By("Exposing the service via virtctl command")
virtctl := tests.NewRepeatableVirtctlCommand(expose.COMMAND_EXPOSE, "virtualmachine", "--namespace",
tcpVM.Namespace, tcpVM.Name, "--port", servicePort, "--name", serviceName, "--target-port", strconv.Itoa(testPort),
"--type", "NodePort", "--node-port", nodePort)
err := virtctl()
Expect(err).ToNot(HaveOccurred())
By("Getting back the the service")
_, err = virtClient.CoreV1().Services(tcpVM.Namespace).Get(serviceName, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
By("Getting the node IP from all nodes")
nodes, err := virtClient.CoreV1().Nodes().List(k8smetav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(nodes.Items).ToNot(BeEmpty())
for _, node := range nodes.Items {
Expect(node.Status.Addresses).ToNot(BeEmpty())
nodeIP := node.Status.Addresses[0].Address
By("Starting a pod which tries to reach the VM via NodePort")
job := tests.NewHelloWorldJob(nodeIP, nodePort)
job, err = virtClient.CoreV1().Pods(tcpVM.Namespace).Create(job)
Expect(err).ToNot(HaveOccurred())
By("Waiting for the pod to report a successful connection attempt")
getStatus := func() k8sv1.PodPhase {
pod, err := virtClient.CoreV1().Pods(job.Namespace).Get(job.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return pod.Status.Phase
}
Eventually(getStatus, 60, 1).Should(Equal(k8sv1.PodSucceeded))
}
})
})
})
Context("Expose UDP service on a VM", func() {
var udpVM *v1.VirtualMachine
tests.BeforeAll(func() {
udpVM = newLabeledVM("udp-vm", virtClient)
generateHelloWorldServer(udpVM, virtClient, testPort, "udp")
})
Context("Expose ClusterIP UDP service", func() {
const servicePort = "28017"
const serviceName = "cluster-ip-udp-vm"
It("Should expose a ClusterIP service on a VM and connect to it", func() {
By("Exposing the service via virtctl command")
virtctl := tests.NewRepeatableVirtctlCommand(expose.COMMAND_EXPOSE, "virtualmachine", "--namespace",
udpVM.Namespace, udpVM.Name, "--port", servicePort, "--name", serviceName, "--target-port", strconv.Itoa(testPort),
"--protocol", "UDP")
err := virtctl()
Expect(err).ToNot(HaveOccurred())
By("Getting back the cluster IP given for the service")
svc, err := virtClient.CoreV1().Services(udpVM.Namespace).Get(serviceName, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
serviceIP := svc.Spec.ClusterIP
By("Starting a pod which tries to reach the VM via ClusterIP")
job := tests.NewHelloWorldJobUDP(serviceIP, servicePort)
job, err = virtClient.CoreV1().Pods(udpVM.Namespace).Create(job)
Expect(err).ToNot(HaveOccurred())
By("Waiting for the pod to report a successful connection attempt")
getStatus := func() k8sv1.PodPhase {
pod, err := virtClient.CoreV1().Pods(job.Namespace).Get(job.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return pod.Status.Phase
}
Eventually(getStatus, 60, 1).Should(Equal(k8sv1.PodSucceeded))
})
})
Context("Expose NodePort UDP service", func() {
const servicePort = "29017"
const serviceName = "node-port-udp-vm"
const nodePort = "31017"
It("Should expose a NodePort service on a VM and connect to it", func() {
By("Exposing the service via virtctl command")
virtctl := tests.NewRepeatableVirtctlCommand(expose.COMMAND_EXPOSE, "virtualmachine", "--namespace",
udpVM.Namespace, udpVM.Name, "--port", servicePort, "--name", serviceName, "--target-port", strconv.Itoa(testPort),
"--type", "NodePort", "--node-port", nodePort, "--protocol", "UDP")
err := virtctl()
Expect(err).ToNot(HaveOccurred())
By("Getting back the cluster IP given for the service")
svc, err := virtClient.CoreV1().Services(udpVM.Namespace).Get(serviceName, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
serviceIP := svc.Spec.ClusterIP
By("Starting a pod which tries to reach the VM via ClusterIP")
job := tests.NewHelloWorldJobUDP(serviceIP, servicePort)
job, err = virtClient.CoreV1().Pods(udpVM.Namespace).Create(job)
Expect(err).ToNot(HaveOccurred())
By("Getting the node IP from all nodes")
nodes, err := virtClient.CoreV1().Nodes().List(k8smetav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(nodes.Items).ToNot(BeEmpty())
for _, node := range nodes.Items {
Expect(node.Status.Addresses).ToNot(BeEmpty())
nodeIP := node.Status.Addresses[0].Address
By("Starting a pod which tries to reach the VM via NodePort")
job := tests.NewHelloWorldJobUDP(nodeIP, nodePort)
job, err = virtClient.CoreV1().Pods(udpVM.Namespace).Create(job)
Expect(err).ToNot(HaveOccurred())
By("Waiting for the pod to report a successful connection attempt")
getStatus := func() k8sv1.PodPhase {
pod, err := virtClient.CoreV1().Pods(job.Namespace).Get(job.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return pod.Status.Phase
}
Eventually(getStatus, 60, 1).Should(Equal(k8sv1.PodSucceeded))
}
})
})
})
Context("Expose service on a VM replica set", func() {
var vmrs *v1.VirtualMachineReplicaSet
tests.BeforeAll(func() {
By("Creating a VMRS object with 2 replicas")
const numberOfVMs = 2
template := tests.NewRandomVMWithEphemeralDiskAndUserdata(tests.RegistryDiskFor(tests.RegistryDiskCirros), "#!/bin/bash\necho 'hello'\n")
vmrs = tests.NewRandomReplicaSetFromVM(template, int32(numberOfVMs))
vmrs.Labels = map[string]string{"expose": "vmrs"}
By("Start the replica set")
vmrs, err = virtClient.ReplicaSet(tests.NamespaceTestDefault).Create(vmrs)
Expect(err).ToNot(HaveOccurred())
By("Checking the number of ready replicas")
Eventually(func() int {
rs, err := virtClient.ReplicaSet(tests.NamespaceTestDefault).Get(vmrs.ObjectMeta.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return int(rs.Status.ReadyReplicas)
}, 60*time.Second, 1*time.Second).Should(Equal(numberOfVMs))
By("add an 'hello world' server on each VM in the replica set")
// TODO: add label to list options
// check size of list
// remove check for owner
vms, err := virtClient.VM(vmrs.ObjectMeta.Namespace).List(k8smetav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
for _, vm := range vms.Items {
if vm.OwnerReferences != nil {
generateHelloWorldServer(&vm, virtClient, testPort, "tcp")
}
}
})
Context("Expose ClusterIP service", func() {
const servicePort = "27017"
const serviceName = "cluster-ip-vmrs"
It("Should create a ClusterIP service on VMRS and connect to it", func() {
By("Expose a service on the VMRS using virtctl")
virtctl := tests.NewRepeatableVirtctlCommand(expose.COMMAND_EXPOSE, "vmrs", "--namespace",
vmrs.Namespace, vmrs.Name, "--port", servicePort, "--name", serviceName, "--target-port", strconv.Itoa(testPort))
err = virtctl()
Expect(err).ToNot(HaveOccurred())
By("Getting back the cluster IP given for the service")
svc, err := virtClient.CoreV1().Services(vmrs.Namespace).Get(serviceName, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
serviceIP := svc.Spec.ClusterIP
By("Starting a pod which tries to reach the VM via ClusterIP")
job := tests.NewHelloWorldJob(serviceIP, servicePort)
job, err = virtClient.CoreV1().Pods(vmrs.Namespace).Create(job)
Expect(err).ToNot(HaveOccurred())
By("Waiting for the pod to report a successful connection attempt")
getStatus := func() k8sv1.PodPhase {
pod, err := virtClient.CoreV1().Pods(job.Namespace).Get(job.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return pod.Status.Phase
}
Eventually(getStatus, 60, 1).Should(Equal(k8sv1.PodSucceeded))
})
})
})
Context("Expose service on an Offline VM", func() {
const servicePort = "27017"
const serviceName = "cluster-ip-ovm"
var ovm *v1.OfflineVirtualMachine
tests.BeforeAll(func() {
By("Creating an OVM object")
template := tests.NewRandomVMWithEphemeralDiskAndUserdata(tests.RegistryDiskFor(tests.RegistryDiskCirros), "#!/bin/bash\necho 'hello'\n")
template.Labels = map[string]string{"expose": "vmrs"}
ovm = NewRandomOfflineVirtualMachine(template, false)
By("Creating the OVM")
_, err := virtClient.OfflineVirtualMachine(tests.NamespaceTestDefault).Create(ovm)
Expect(err).ToNot(HaveOccurred())
By("Exposing a service on the OVM using virtctl")
virtctl := tests.NewRepeatableVirtctlCommand(expose.COMMAND_EXPOSE, "offlinevirtualmachine", "--namespace",
ovm.Namespace, ovm.Name, "--port", servicePort, "--name", serviceName, "--target-port", strconv.Itoa(testPort))
err = virtctl()
Expect(err).ToNot(HaveOccurred())
By("Calling the start command")
virtctl = tests.NewRepeatableVirtctlCommand(offlinevm.COMMAND_START, "--namespace", ovm.Namespace, ovm.Name)
err = virtctl()
Expect(err).ToNot(HaveOccurred())
By("Getting the status of the OVM")
Eventually(func() bool {
ovm, err = virtClient.OfflineVirtualMachine(ovm.Namespace).Get(ovm.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return ovm.Status.Ready
}, 120*time.Second, 1*time.Second).Should(BeTrue())
By("Getting the running VM")
var vm *v1.VirtualMachine
Eventually(func() bool {
vm, err = virtClient.VM(ovm.Namespace).Get(ovm.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return vm.Status.Phase == v1.Running
}, 120*time.Second, 1*time.Second).Should(BeTrue())
generateHelloWorldServer(vm, virtClient, testPort, "tcp")
})
Context("Expose ClusterIP service", func() {
It("Connect to ClusterIP services that was set when VM was offline", func() {
By("Getting back the cluster IP given for the service")
svc, err := virtClient.CoreV1().Services(ovm.Namespace).Get(serviceName, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
serviceIP := svc.Spec.ClusterIP
By("Starting a pod which tries to reach the VM via ClusterIP")
job := tests.NewHelloWorldJob(serviceIP, servicePort)
job, err = virtClient.CoreV1().Pods(ovm.Namespace).Create(job)
Expect(err).ToNot(HaveOccurred())
By("Waiting for the pod to report a successful connection attempt")
getStatus := func() k8sv1.PodPhase {
pod, err := virtClient.CoreV1().Pods(job.Namespace).Get(job.Name, k8smetav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return pod.Status.Phase
}
Eventually(getStatus, 60, 1).Should(Equal(k8sv1.PodSucceeded))
})
})
})
})