forked from kubevirt/containerized-data-importer
/
pvc.go
220 lines (184 loc) · 9.02 KB
/
pvc.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
package framework
import (
"encoding/json"
"fmt"
"strings"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/klog"
"kubevirt.io/containerized-data-importer/pkg/image"
"kubevirt.io/containerized-data-importer/tests/utils"
)
// CreatePVCFromDefinition is a wrapper around utils.CreatePVCFromDefinition
func (f *Framework) CreatePVCFromDefinition(def *k8sv1.PersistentVolumeClaim) (*k8sv1.PersistentVolumeClaim, error) {
return utils.CreatePVCFromDefinition(f.K8sClient, f.Namespace.Name, def)
}
// DeletePVC is a wrapper around utils.DeletePVC
func (f *Framework) DeletePVC(pvc *k8sv1.PersistentVolumeClaim) error {
return utils.DeletePVC(f.K8sClient, f.Namespace.Name, pvc)
}
// WaitForPersistentVolumeClaimPhase is a wrapper around utils.WaitForPersistentVolumeClaimPhase
func (f *Framework) WaitForPersistentVolumeClaimPhase(phase k8sv1.PersistentVolumeClaimPhase, pvcName string) error {
return utils.WaitForPersistentVolumeClaimPhase(f.K8sClient, f.Namespace.Name, phase, pvcName)
}
// CreateExecutorPodWithPVC is a wrapper around utils.CreateExecutorPodWithPVC
func (f *Framework) CreateExecutorPodWithPVC(podName string, pvc *k8sv1.PersistentVolumeClaim) (*k8sv1.Pod, error) {
return utils.CreateExecutorPodWithPVC(f.K8sClient, podName, f.Namespace.Name, pvc)
}
// FindPVC is a wrapper around utils.FindPVC
func (f *Framework) FindPVC(pvcName string) (*k8sv1.PersistentVolumeClaim, error) {
return utils.FindPVC(f.K8sClient, f.Namespace.Name, pvcName)
}
// VerifyPVCIsEmpty verifies a passed in PVC is empty, returns true if the PVC is empty, false if it is not.
func VerifyPVCIsEmpty(f *Framework, pvc *k8sv1.PersistentVolumeClaim) (bool, error) {
executorPod, err := f.CreateExecutorPodWithPVC("verify-pvc-empty", pvc)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
err = f.WaitTimeoutForPodReady(executorPod.Name, utils.PodWaitForTime)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
output, err := f.ExecShellInPod(executorPod.Name, f.Namespace.Name, "ls -1 /pvc | wc -l")
if err != nil {
return false, err
}
found := strings.Compare("0", output) == 0
if !found {
// Could be that a file system was created and it has 'lost+found' directory in it, check again.
output, err := f.ExecShellInPod(executorPod.Name, f.Namespace.Name, "ls -1 /pvc")
if err != nil {
return false, err
}
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: files found: %s\n", string(output))
found = strings.Compare("lost+found", output) == 0
}
return found, nil
}
// CreateAndPopulateSourcePVC Creates and populates a PVC using the provided POD and command
func (f *Framework) CreateAndPopulateSourcePVC(pvcDef *k8sv1.PersistentVolumeClaim, podName string, fillCommand string) *k8sv1.PersistentVolumeClaim {
// Create the source PVC and populate it with a file, so we can verify the clone.
sourcePvc, err := f.CreatePVCFromDefinition(pvcDef)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
pod, err := f.CreatePod(utils.NewPodWithPVC(podName, fillCommand, sourcePvc))
gomega.Expect(err).ToNot(gomega.HaveOccurred())
err = f.WaitTimeoutForPodStatus(pod.Name, k8sv1.PodSucceeded, utils.PodWaitForTime)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
return sourcePvc
}
// VerifyTargetPVCContent is used to check the contents of a PVC and ensure it matches the provided expected data
func (f *Framework) VerifyTargetPVCContent(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim,
expectedData, testBaseDir, testFile string) (bool, error) {
var dest string
var err error
var executorPod *k8sv1.Pod
executorPod, err = utils.CreateExecutorPodWithPVC(f.K8sClient, "verify-pvc-content", namespace.Name, pvc)
volumeMode := pvc.Spec.VolumeMode
if volumeMode != nil && *volumeMode == k8sv1.PersistentVolumeBlock {
dest = testBaseDir
} else {
dest = testBaseDir + testFile
}
gomega.Expect(err).ToNot(gomega.HaveOccurred())
err = utils.WaitTimeoutForPodReady(f.K8sClient, executorPod.Name, namespace.Name, utils.PodWaitForTime)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
output, err := f.ExecShellInPod(executorPod.Name, namespace.Name, "cat "+dest)
if err != nil {
return false, err
}
return strings.Compare(expectedData, output) == 0, nil
}
// VerifyTargetPVCContentMD5 provides a function to check the md5 of data on a PVC and ensure it matches that which is provided
func (f *Framework) VerifyTargetPVCContentMD5(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim, fileName string, expectedHash string, numBytes ...int64) (bool, error) {
if len(numBytes) == 0 {
numBytes = append(numBytes, 0)
}
md5, err := f.GetMD5(namespace, pvc, fileName, numBytes[0])
if err != nil {
return false, err
}
return expectedHash == md5, nil
}
// GetMD5 returns the MD5 of a file on a PVC
func (f *Framework) GetMD5(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim, fileName string, numBytes int64) (string, error) {
var executorPod *k8sv1.Pod
var err error
executorPod, err = utils.CreateExecutorPodWithPVC(f.K8sClient, "get-md5-"+pvc.Name, namespace.Name, pvc)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
err = utils.WaitTimeoutForPodReady(f.K8sClient, executorPod.Name, namespace.Name, utils.PodWaitForTime)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
cmd := "md5sum " + fileName
if numBytes > 0 {
cmd = fmt.Sprintf("head -c %d %s | md5sum", numBytes, fileName)
}
output, err := f.ExecShellInPod(executorPod.Name, namespace.Name, cmd)
if err != nil {
return "", err
}
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: md5sum found %s\n", string(output[:32]))
f.DeletePod(executorPod)
return output[:32], nil
}
// VerifyBlankDisk checks a blank disk on a file mode PVC by validating that the disk.img file is sparse.
func (f *Framework) VerifyBlankDisk(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim) (bool, error) {
var executorPod *k8sv1.Pod
var err error
executorPod, err = utils.CreateExecutorPodWithPVC(f.K8sClient, "verify-blank-disk-"+pvc.Name, namespace.Name, pvc)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
defer f.DeletePod(executorPod)
err = utils.WaitTimeoutForPodReady(f.K8sClient, executorPod.Name, namespace.Name, utils.PodWaitForTime)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
cmd := fmt.Sprintf("tr -d '\\000' <%s/disk.img | grep -q -m 1 ^ || echo \"All zeros\"", utils.DefaultPvcMountPath)
output, err := f.ExecShellInPod(executorPod.Name, namespace.Name, cmd)
if err != nil {
return false, err
}
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: empty file check %s\n", string(output))
return strings.Compare("All zeros", string(output)) == 0, nil
}
// VerifyNotSparse checks a disk image not being sparse after creation/resize.
func (f *Framework) VerifyNotSparse(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim) (bool, error) {
var executorPod *k8sv1.Pod
var err error
executorPod, err = utils.CreateExecutorPodWithPVC(f.K8sClient, "verify-not-sparse-"+pvc.Name, namespace.Name, pvc)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
defer f.DeletePod(executorPod)
err = utils.WaitTimeoutForPodReady(f.K8sClient, executorPod.Name, namespace.Name, utils.PodWaitForTime)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
cmd := fmt.Sprintf("qemu-img info %s/disk.img --output=json", utils.DefaultPvcMountPath)
output, err := f.ExecShellInPod(executorPod.Name, namespace.Name, cmd)
if err != nil {
return false, err
}
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: not sparse output %s\n", string(output))
var info image.ImgInfo
err = json.Unmarshal([]byte(output), &info)
if err != nil {
klog.Errorf("Invalid JSON:\n%s\n", string(output))
}
return info.ActualSize >= info.VirtualSize, nil
}
// VerifyTargetPVCArchiveContent provides a function to check if the number of files extracted from an archive matches the passed in value
func (f *Framework) VerifyTargetPVCArchiveContent(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim, count string) (bool, error) {
var executorPod *k8sv1.Pod
var err error
executorPod, err = utils.CreateExecutorPodWithPVC(f.K8sClient, "verify-pvc-archive", namespace.Name, pvc)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
err = utils.WaitTimeoutForPodReady(f.K8sClient, executorPod.Name, namespace.Name, utils.PodWaitForTime)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
output, err := f.ExecShellInPod(executorPod.Name, namespace.Name, "ls "+utils.DefaultPvcMountPath+" | wc -l")
if err != nil {
return false, err
}
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: file count found %s\n", string(output))
return strings.Compare(count, output) == 0, nil
}
// RunCommandAndCaptureOutput runs a command on a pod that has the passed in PVC mounted and captures the output.
func (f *Framework) RunCommandAndCaptureOutput(pvc *k8sv1.PersistentVolumeClaim, cmd string) (string, error) {
executorPod, err := f.CreateExecutorPodWithPVC("execute-command", pvc)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
err = f.WaitTimeoutForPodReady(executorPod.Name, utils.PodWaitForTime)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
output, err := f.ExecShellInPod(executorPod.Name, f.Namespace.Name, cmd)
if err != nil {
return "", err
}
return output, nil
}