forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageupload_test.go
119 lines (96 loc) · 3.32 KB
/
imageupload_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
package tests_test
import (
"fmt"
"io"
"net/http"
"os"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/tests"
)
const (
uploadProxyService = "svc/cdi-uploadproxy"
uploadProxyPort = 443
localUploadProxyPort = 18443
imagePath = "/tmp/alpine.iso"
)
var _ = Describe("ImageUpload", func() {
tests.FlagParse()
namespace := tests.NamespaceTestDefault
dvName := "alpine-dv"
pvcSize := "100Mi"
virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)
BeforeEach(func() {
By("Getting CDI HTTP import server pod")
pods, err := virtClient.CoreV1().Pods(tests.KubeVirtInstallNamespace).List(metav1.ListOptions{LabelSelector: "kubevirt.io=cdi-http-import-server"})
Expect(err).ToNot(HaveOccurred())
Expect(pods.Items).ToNot(BeEmpty())
stopChan := make(chan struct{})
err = tests.ForwardPorts(&pods.Items[0], []string{"65432:80"}, stopChan, 10*time.Second)
Expect(err).ToNot(HaveOccurred())
By("Downloading alpine image")
r, err := http.Get("http://localhost:65432/images/alpine.iso")
Expect(err).ToNot(HaveOccurred())
defer r.Body.Close()
file, err := os.Create(imagePath)
Expect(err).ToNot(HaveOccurred())
defer file.Close()
_, err = io.Copy(file, r.Body)
Expect(err).ToNot(HaveOccurred())
})
Context("Upload an image and start a VMI with PVC", func() {
It("Should succeed", func() {
By("Setting up port forwarding")
portMapping := fmt.Sprintf("%d:%d", localUploadProxyPort, uploadProxyPort)
_, kubectlCmd, err := tests.CreateCommandWithNS(tests.ContainerizedDataImporterNamespace, "kubectl", "port-forward", uploadProxyService, portMapping)
Expect(err).ToNot(HaveOccurred())
err = kubectlCmd.Start()
Expect(err).ToNot(HaveOccurred())
time.Sleep(2 * time.Second)
Expect(kubectlCmd.ProcessState).To(BeNil())
defer func() {
kubectlCmd.Process.Kill()
kubectlCmd.Wait()
}()
By("Upload image")
virtctlCmd := tests.NewRepeatableVirtctlCommand("image-upload",
"dv", dvName,
"--namespace", namespace,
"--image-path", imagePath,
"--size", pvcSize,
"--uploadproxy-url", fmt.Sprintf("https://127.0.0.1:%d", localUploadProxyPort),
"--wait-secs", "30",
"--storage-class", tests.Config.StorageClassLocal,
"--insecure")
err = virtctlCmd()
if err != nil {
fmt.Printf("UploadImage Error: %+v\n", err)
Expect(err).ToNot(HaveOccurred())
}
By("Get DataVolume")
_, err = virtClient.CdiClient().CdiV1alpha1().DataVolumes(namespace).Get(dvName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
By("Start VM")
vmi := tests.NewRandomVMIWithPVC(dvName)
vmi, err = virtClient.VirtualMachineInstance(namespace).Create(vmi)
Expect(err).ToNot(HaveOccurred())
tests.WaitForSuccessfulVMIStartIgnoreWarnings(vmi)
vmi, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vmi.ObjectMeta.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
})
})
AfterEach(func() {
err := virtClient.CoreV1().PersistentVolumeClaims(namespace).Delete(dvName, &metav1.DeleteOptions{})
if errors.IsNotFound(err) {
return
}
Expect(err).ToNot(HaveOccurred())
err = os.Remove(imagePath)
Expect(err).ToNot(HaveOccurred())
})
})