-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathcommon.go
364 lines (320 loc) · 11.4 KB
/
common.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
/*
Copyright 2017 Mirantis
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.
*/
package e2e
import (
"flag"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
. "github.com/onsi/gomega"
"k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/Mirantis/virtlet/tests/e2e/framework"
. "github.com/Mirantis/virtlet/tests/e2e/ginkgo-ext"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
cephContainerName = "ceph_cluster"
// avoid having the loop device on top of overlay2/aufs when using k-d-c
loopDeviceTestDir = "/dind/virtlet-e2e-tests"
)
var (
vmImageLocation = flag.String("image", defaultVMImageLocation, "VM image URL (*without http(s)://*")
sshUser = flag.String("sshuser", DefaultSSHUser, "default SSH user for VMs")
includeCloudInitTests = flag.Bool("include-cloud-init-tests", false, "include Cloud-Init tests")
includeUnsafeTests = flag.Bool("include-unsafe-tests", false, "include tests that can be unsafe if they're run outside the build container")
memoryLimit = flag.Int("memoryLimit", 160, "default VM memory limit (in MiB)")
junitOutput = flag.String("junitOutput", "", "JUnit XML output file")
controller *framework.Controller
)
// UsingCirros() returns true if cirros image is being used for tests
// (which has some limitations)
func UsingCirros() bool {
return strings.Contains(*vmImageLocation, "cirros")
}
// scheduleWaitSSH schedules SSH interface initialization before the test context starts
func scheduleWaitSSH(vm **framework.VMInterface, ssh *framework.Executor) {
BeforeAll(func() {
*ssh = waitSSH(*vm)
})
AfterAll(func() {
(*ssh).Close()
})
}
func waitSSH(vm *framework.VMInterface) framework.Executor {
var ssh framework.Executor
Eventually(
func() error {
var err error
ssh, err = vm.SSH(*sshUser, SshPrivateKey)
if err != nil {
return err
}
_, err = framework.RunSimple(ssh)
return err
}, 60*5, 3).Should(Succeed())
return ssh
}
func waitVirtletPod(vm *framework.VMInterface) *framework.PodInterface {
var virtletPod *framework.PodInterface
Eventually(
func() error {
var err error
virtletPod, err = vm.VirtletPod()
if err != nil {
return err
}
for _, c := range virtletPod.Pod.Status.Conditions {
if c.Type == v1.PodReady && c.Status == v1.ConditionTrue {
return nil
}
}
return fmt.Errorf("Pod not ready yet: %+v", virtletPod.Pod.Status)
}, 60*5, 3).Should(Succeed())
return virtletPod
}
func checkCPUCount(vm *framework.VMInterface, ssh framework.Executor, cpus int) {
proc := do(framework.RunSimple(ssh, "cat", "/proc/cpuinfo")).(string)
Expect(regexp.MustCompile(`(?m)^processor`).FindAllString(proc, -1)).To(HaveLen(cpus))
cpuStats := do(vm.VirshCommand("domstats", "<domain>", "--vcpu")).(string)
match := regexp.MustCompile(`vcpu\.maximum=(\d+)`).FindStringSubmatch(cpuStats)
Expect(match).To(HaveLen(2))
Expect(strconv.Atoi(match[1])).To(Equal(cpus))
}
func deleteVM(vm *framework.VMInterface) {
virtletPod, err := vm.VirtletPod()
Expect(err).NotTo(HaveOccurred())
domainName, err := vm.DomainName()
Expect(err).NotTo(HaveOccurred())
domainName = domainName[8:21] // extract 5d3f8619-fda4 from virtlet-5d3f8619-fda4-cirros-vm
Expect(vm.Delete(time.Minute * 2)).To(Succeed())
commands := map[string][]string{
"domain": {"list", "--name"},
"volume": {"vol-list", "--pool", "volumes"},
"secret": {"secret-list"},
}
for key, cmd := range commands {
Eventually(func() error {
out, err := framework.RunVirsh(virtletPod, cmd...)
if err != nil {
return err
}
if strings.Contains(out, domainName) {
return fmt.Errorf("%s ~%s~ was not deleted", key, domainName)
}
return nil
}, "3m").Should(Succeed())
}
}
// do asserts that function with multiple return values doesn't fail
// Considering we have func `foo(something) (something, error)`
//
// `x := do(foo(something))` is equivalent to
// val, err := fn(something)
// Expect(err).To(Succeed())
// x = val
//
// The rule is that the function must return at least 2 values,
// and the last value is interpreted as error.
func do(value interface{}, extra ...interface{}) interface{} {
if len(extra) == 0 {
panic("bad usage of do() -- no extra values")
}
lastValue := extra[len(extra)-1]
if lastValue != nil {
err := lastValue.(error)
Expect(err).NotTo(HaveOccurred())
}
return value
}
type VMOptions framework.VMOptions
func (o VMOptions) ApplyDefaults() framework.VMOptions {
res := framework.VMOptions(o)
if res.Image == "" {
res.Image = *vmImageLocation
}
if res.SSHKey == "" && res.SSHKeySource == "" {
res.SSHKey = SshPublicKey
}
if res.VCPUCount == 0 {
res.VCPUCount = 1
}
if res.DiskDriver == "" {
res.DiskDriver = "virtio"
}
if res.Limits == nil {
res.Limits = map[string]string{}
}
if res.Limits["memory"] == "" {
res.Limits["memory"] = fmt.Sprintf("%dMi", *memoryLimit)
}
return res
}
func requireCloudInit() {
if !*includeCloudInitTests {
Skip("Cloud-Init tests are not enabled")
}
}
func includeUnsafe() {
if !*includeUnsafeTests {
Skip("Tests that are unsafe outside the build container are disabled")
}
}
func withLoopbackBlockDevice(virtletNodeName, devPath *string, mkfs bool) {
var nodeExecutor framework.Executor
var filename string
BeforeEach(func() {
var err error
*virtletNodeName, err = controller.VirtletNodeName()
Expect(err).NotTo(HaveOccurred())
nodeExecutor, err = controller.DinDNodeExecutor(*virtletNodeName)
Expect(err).NotTo(HaveOccurred())
_, err = framework.RunSimple(nodeExecutor, "mkdir", "-p", loopDeviceTestDir)
Expect(err).NotTo(HaveOccurred())
filename, err = framework.RunSimple(nodeExecutor, "tempfile", "-d", loopDeviceTestDir, "--prefix", "ve2e-")
Expect(err).NotTo(HaveOccurred())
_, err = framework.RunSimple(nodeExecutor, "dd", "if=/dev/zero", "of="+filename, "bs=1M", "count=1000")
Expect(err).NotTo(HaveOccurred())
if mkfs {
// We use mkfs.ext3 here because mkfs.ext4 on
// the node may be too new for CirrOS, causing
// errors like this in VM's dmesg:
// [ 1.316395] EXT3-fs (vdb): error: couldn't mount because of unsupported optional features (2c0)
// [ 1.320222] EXT4-fs (vdb): couldn't mount RDWR because of unsupported optional features (400)
// [ 1.339594] EXT3-fs (vdc1): error: couldn't mount because of unsupported optional features (240)
// [ 1.342850] EXT4-fs (vdc1): mounted filesystem with ordered data mode. Opts: (null)
_, err = framework.RunSimple(nodeExecutor, "mkfs.ext3", filename)
Expect(err).NotTo(HaveOccurred())
}
_, err = framework.RunSimple(nodeExecutor, "sync")
Expect(err).NotTo(HaveOccurred())
*devPath, err = framework.RunSimple(nodeExecutor, "losetup", "-f", filename, "--show")
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
// The loopback device is detached by itself upon
// success (TODO: check why it happens), so we
// ignore errors here
framework.RunSimple(nodeExecutor, "losetup", "-d", *devPath)
Expect(os.RemoveAll(loopDeviceTestDir)).To(Succeed())
})
}
func withCeph(monitorIP, secret *string, kubeSecret string) {
BeforeAll(func() {
nodeExecutor, err := (*controller).DinDNodeExecutor("kube-master")
Expect(err).NotTo(HaveOccurred())
route, err := framework.RunSimple(nodeExecutor, "route", "-n")
Expect(err).NotTo(HaveOccurred())
match := regexp.MustCompile(`(?:default|0\.0\.0\.0)\s+([\d.]+)`).FindStringSubmatch(route)
Expect(match).To(HaveLen(2))
*monitorIP = match[1]
cephPublicNetwork := *monitorIP + "/16"
container, err := controller.DockerContainer(cephContainerName)
Expect(err).NotTo(HaveOccurred())
container.Delete()
Expect(container.PullImage("docker.io/ceph/daemon:v3.1.0-stable-3.1-mimic-centos-7")).To(Succeed())
Expect(container.Run("docker.io/ceph/daemon:v3.1.0-stable-3.1-mimic-centos-7",
map[string]string{
"MON_IP": *monitorIP,
"CEPH_PUBLIC_NETWORK": cephPublicNetwork,
"CEPH_DEMO_UID": "foo",
"CEPH_DEMO_ACCESS_KEY": "foo",
"CEPH_DEMO_SECRET_KEY": "foo",
"CEPH_DEMO_BUCKET": "foo",
"DEMO_DAEMONS": "osd mds",
},
"host", nil, false, "demo")).To(Succeed())
cephContainerExecutor := container.Executor(false, "")
By("Waiting for ceph cluster")
Eventually(func() error {
_, err := framework.RunSimple(cephContainerExecutor, "ceph", "-s")
return err
}).Should(Succeed())
By("Ceph cluster started")
commands := []string{
// Add rbd pool and volume
`ceph osd pool create libvirt-pool 8 8`,
`rbd create rbd-test-image1 --size 1G --pool libvirt-pool --image-feature layering`,
`rbd create rbd-test-image2 --size 1G --pool libvirt-pool --image-feature layering`,
`rbd create rbd-test-image-pv --size 1G --pool libvirt-pool --image-feature layering`,
// Add user for virtlet
`ceph auth get-key client.admin`,
}
var out string
for _, cmd := range commands {
out = do(framework.RunSimple(cephContainerExecutor, "/bin/bash", "-c", cmd)).(string)
}
if secret != nil {
*secret = out
}
if kubeSecret != "" {
// buf := bytes.NewBufferString(out)
// decoder := base64.NewDecoder(base64.StdEncoding, buf)
// decoded, err := ioutil.ReadAll(decoder)
// Expect(err).NotTo(HaveOccurred())
_, err = controller.Secrets().Create(&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: kubeSecret,
},
Type: "kubernetes.io/rbd",
Data: map[string][]byte{
"key": []byte(out),
},
})
Expect(err).NotTo(HaveOccurred())
}
})
AfterAll(func() {
container, err := controller.DockerContainer(cephContainerName)
Expect(err).NotTo(HaveOccurred())
container.Delete()
if kubeSecret != "" {
Expect(controller.Secrets().Delete(kubeSecret, nil)).To(Succeed())
Eventually(func() error {
if _, err := controller.Secrets().Get(kubeSecret, metav1.GetOptions{}); err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
return fmt.Errorf("secret %s was not deleted", kubeSecret)
}).Should(Succeed())
}
})
}
func makeVMWithMountAndSymlinkScript(nodeName string, PVCs []framework.PVCSpec, podCustomization func(*framework.PodInterface)) *framework.VMInterface {
vm := controller.VM("mount-vm")
Expect(vm.CreateAndWait(VMOptions{
NodeName: nodeName,
// TODO: should also have an option to test using
// ubuntu image with volumes mounted using cloud-init
// userdata 'mounts' section
UserDataScript: "@virtlet-mount-script@",
PVCs: PVCs,
}.ApplyDefaults(), time.Minute*5, podCustomization)).To(Succeed())
_, err := vm.Pod()
Expect(err).NotTo(HaveOccurred())
return vm
}
func expectToBeUsableForFilesystem(ssh framework.Executor, devPath string) {
Eventually(func() error {
_, err := framework.RunSimple(ssh, fmt.Sprintf("sudo /usr/sbin/mkfs.ext2 %s", devPath))
return err
}, 60*5, 3).Should(Succeed())
do(framework.RunSimple(ssh, fmt.Sprintf("sudo mount %s /mnt", devPath)))
out := do(framework.RunSimple(ssh, "ls -l /mnt")).(string)
Expect(out).To(ContainSubstring("lost+found"))
}