Skip to content

Commit a399822

Browse files
author
Valeriy Khorunzhin
committed
checksum check
Signed-off-by: Valeriy Khorunzhin <valeriy.khorunzhin@flant.com>
1 parent 42a9197 commit a399822

File tree

2 files changed

+102
-22
lines changed

2 files changed

+102
-22
lines changed

test/e2e/internal/util/block_device.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func CreateBlockDeviceFilesystem(f *framework.Framework, vm *v1alpha2.VirtualMac
5757
Expect(err).NotTo(HaveOccurred())
5858
}
5959

60-
func MountBlockDevice(f *framework.Framework, vm *v1alpha2.VirtualMachine, bdKind v1alpha2.BlockDeviceKind, bdName string) {
60+
func MountBlockDevice(f *framework.Framework, vm *v1alpha2.VirtualMachine, bdKind v1alpha2.BlockDeviceKind, bdName, mountPoint string) {
6161
GinkgoHelper()
6262

6363
serial, ok := GetBlockDeviceSerialNumber(vm, bdKind, bdName)
@@ -66,14 +66,42 @@ func MountBlockDevice(f *framework.Framework, vm *v1alpha2.VirtualMachine, bdKin
6666
devicePath, err := GetBlockDeviceBySerial(f, vm, serial)
6767
Expect(err).NotTo(HaveOccurred(), fmt.Errorf("failed to get device by serial: %w", err))
6868

69-
_, err = f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo mount %s /mnt", devicePath))
69+
_, err = f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo mount %s %s", devicePath, mountPoint))
7070
Expect(err).NotTo(HaveOccurred())
71+
}
72+
73+
func UnmountBlockDevice(f *framework.Framework, vm *v1alpha2.VirtualMachine, mountPoint string) {
74+
GinkgoHelper()
75+
76+
_, err := f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo umount %s", mountPoint))
77+
Expect(err).NotTo(HaveOccurred())
78+
}
79+
80+
func RegisterFstabEntry(f *framework.Framework, vm *v1alpha2.VirtualMachine, bdKind v1alpha2.BlockDeviceKind, bdName string) {
81+
GinkgoHelper()
82+
83+
serial, ok := GetBlockDeviceSerialNumber(vm, bdKind, bdName)
84+
Expect(ok).To(BeTrue(), "failed to get block device serial number")
7185

7286
cmd := fmt.Sprintf(`UUID=$(lsblk -o SERIAL,UUID | grep %s | awk "{print \$2}"); echo "UUID=$UUID /mnt ext4 defaults 0 0" | sudo tee -a /etc/fstab`, serial)
73-
_, err = f.SSHCommand(vm.Name, vm.Namespace, cmd)
87+
_, err := f.SSHCommand(vm.Name, vm.Namespace, cmd)
7488
Expect(err).NotTo(HaveOccurred())
7589
}
7690

91+
func GetBlockDeviceHash(f *framework.Framework, vm *v1alpha2.VirtualMachine, bdKind v1alpha2.BlockDeviceKind, bdName string) string {
92+
GinkgoHelper()
93+
94+
serial, ok := GetBlockDeviceSerialNumber(vm, bdKind, bdName)
95+
Expect(ok).To(BeTrue(), "failed to get block device serial number")
96+
97+
devicePath, err := GetBlockDeviceBySerial(f, vm, serial)
98+
Expect(err).NotTo(HaveOccurred(), fmt.Errorf("failed to get device by serial: %w", err))
99+
100+
cmdOut, err := f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo dd if=%s bs=4M | sha256sum | awk \"{print \\$1}\"", devicePath))
101+
Expect(err).NotTo(HaveOccurred())
102+
return strings.TrimSpace(cmdOut)
103+
}
104+
77105
func GetBlockDeviceBySerial(f *framework.Framework, vm *v1alpha2.VirtualMachine, serial string) (string, error) {
78106
cmdOut, err := f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo lsblk -o PATH,SERIAL | grep %s | awk \"{print \\$1, \\$2}\"", serial))
79107
if err != nil {

test/e2e/vmop/restore.go

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const (
6767
originalMemorySize = "256Mi"
6868
changedCPUCores = 2
6969
changedMemorySize = "512Mi"
70+
mountPoint = "/mnt"
7071
fileDataPath = "/mnt/value"
7172
)
7273

@@ -80,19 +81,26 @@ var _ = Describe("VirtualMachineOperationRestore", label.Slow(), func() {
8081
By("Environment preparation", func() {
8182
t.GenerateResources(restoreMode, restartApprovalMode, runPolicy)
8283
err := f.CreateWithDeferredDeletion(
83-
context.Background(), t.CVI, t.VI, t.VDRoot, t.VDBlank, t.VM, t.VMBDA,
84+
context.Background(), t.CVI, t.VI, t.VDRoot, t.VDBlank, t.VM, t.VMBDA, t.VDBlankWithNoFstabEntry, t.VMBDAWithNoFstabEntry,
8485
)
8586
Expect(err).NotTo(HaveOccurred())
8687
if t.VM.Spec.RunPolicy == v1alpha2.ManualPolicy {
8788
util.UntilObjectPhase(string(v1alpha2.MachineStopped), framework.ShortTimeout, t.VM)
8889
util.StartVirtualMachine(f, t.VM)
8990
}
9091
util.UntilVMAgentReady(crclient.ObjectKeyFromObject(t.VM), framework.LongTimeout)
91-
util.UntilObjectPhase(string(v1alpha2.BlockDeviceAttachmentPhaseAttached), framework.MiddleTimeout, t.VMBDA)
92+
util.UntilObjectPhase(string(v1alpha2.BlockDeviceAttachmentPhaseAttached), framework.MiddleTimeout, t.VMBDA, t.VMBDAWithNoFstabEntry)
9293

9394
util.CreateBlockDeviceFilesystem(f, t.VM, v1alpha2.DiskDevice, t.VDBlank.Name, "ext4")
94-
util.MountBlockDevice(f, t.VM, v1alpha2.DiskDevice, t.VDBlank.Name)
95+
util.MountBlockDevice(f, t.VM, v1alpha2.DiskDevice, t.VDBlank.Name, mountPoint)
96+
util.RegisterFstabEntry(f, t.VM, v1alpha2.DiskDevice, t.VDBlank.Name)
97+
util.WriteFile(f, t.VM, fileDataPath, originalValueOnDisk)
98+
99+
util.CreateBlockDeviceFilesystem(f, t.VM, v1alpha2.DiskDevice, t.VDBlankWithNoFstabEntry.Name, "ext4")
100+
util.MountBlockDevice(f, t.VM, v1alpha2.DiskDevice, t.VDBlankWithNoFstabEntry.Name, mountPoint)
95101
util.WriteFile(f, t.VM, fileDataPath, originalValueOnDisk)
102+
util.UnmountBlockDevice(f, t.VM, mountPoint)
103+
t.BlockDeviceHash = util.GetBlockDeviceHash(f, t.VM, v1alpha2.DiskDevice, t.VDBlankWithNoFstabEntry.Name)
96104

97105
err = f.CreateWithDeferredDeletion(context.Background(), t.VMSnapshot)
98106
Expect(err).NotTo(HaveOccurred())
@@ -140,7 +148,7 @@ var _ = Describe("VirtualMachineOperationRestore", label.Slow(), func() {
140148
t.RestoreVM(t.VM, t.VMOPRestore)
141149
})
142150
By("Check VM after restore", func() {
143-
t.CheckVMAfterRestore(t.VM, t.VDRoot, t.VDBlank, t.VMBDA, t.VMOPRestore)
151+
t.CheckVMAfterRestore(t.VM, t.VDRoot, t.VDBlank, t.VDBlankWithNoFstabEntry, t.VMBDA, t.VMBDAWithNoFstabEntry, t.VMOPRestore)
144152
})
145153
By("After restoration, verify that labels and annotations are preserved on the resources", func() {
146154
err := f.Clients.GenericClient().Get(context.Background(), crclient.ObjectKeyFromObject(t.VDRoot), t.VDRoot)
@@ -157,6 +165,16 @@ var _ = Describe("VirtualMachineOperationRestore", label.Slow(), func() {
157165
Expect(err).NotTo(HaveOccurred())
158166
Expect(t.VMBDA.Annotations[resourceAnnotationName]).To(Equal(resourceAnnotationValue))
159167
Expect(t.VMBDA.Labels[resourceLabelName]).To(Equal(resourceLabelValue))
168+
169+
err = f.Clients.GenericClient().Get(context.Background(), crclient.ObjectKeyFromObject(t.VDBlankWithNoFstabEntry), t.VDBlankWithNoFstabEntry)
170+
Expect(err).NotTo(HaveOccurred())
171+
Expect(t.VDBlankWithNoFstabEntry.Annotations[resourceAnnotationName]).To(Equal(resourceAnnotationValue))
172+
Expect(t.VDBlankWithNoFstabEntry.Labels[resourceLabelName]).To(Equal(resourceLabelValue))
173+
174+
err = f.Clients.GenericClient().Get(context.Background(), crclient.ObjectKeyFromObject(t.VMBDAWithNoFstabEntry), t.VMBDAWithNoFstabEntry)
175+
Expect(err).NotTo(HaveOccurred())
176+
Expect(t.VMBDAWithNoFstabEntry.Annotations[resourceAnnotationName]).To(Equal(resourceAnnotationValue))
177+
Expect(t.VMBDAWithNoFstabEntry.Labels[resourceLabelName]).To(Equal(resourceLabelValue))
160178
})
161179
},
162180
Entry(
@@ -212,16 +230,20 @@ var _ = Describe("VirtualMachineOperationRestore", label.Slow(), func() {
212230
})
213231

214232
type restoreModeTest struct {
215-
CVI *v1alpha2.ClusterVirtualImage
216-
VI *v1alpha2.VirtualImage
217-
VDRoot *v1alpha2.VirtualDisk
218-
VDBlank *v1alpha2.VirtualDisk
219-
VM *v1alpha2.VirtualMachine
220-
VMBDA *v1alpha2.VirtualMachineBlockDeviceAttachment
221-
VMSnapshot *v1alpha2.VirtualMachineSnapshot
222-
VMOPRestore *v1alpha2.VirtualMachineOperation
233+
CVI *v1alpha2.ClusterVirtualImage
234+
VI *v1alpha2.VirtualImage
235+
VDRoot *v1alpha2.VirtualDisk
236+
VDBlank *v1alpha2.VirtualDisk
237+
VDBlankWithNoFstabEntry *v1alpha2.VirtualDisk
238+
VM *v1alpha2.VirtualMachine
239+
VMBDA *v1alpha2.VirtualMachineBlockDeviceAttachment
240+
VMBDAWithNoFstabEntry *v1alpha2.VirtualMachineBlockDeviceAttachment
241+
VMSnapshot *v1alpha2.VirtualMachineSnapshot
242+
VMOPRestore *v1alpha2.VirtualMachineOperation
223243

224244
Framework *framework.Framework
245+
246+
BlockDeviceHash string
225247
}
226248

227249
func newRestoreTest(f *framework.Framework) *restoreModeTest {
@@ -261,6 +283,14 @@ func (t *restoreModeTest) GenerateResources(restoreMode v1alpha2.SnapshotOperati
261283
vdbuilder.WithLabel(resourceLabelName, resourceLabelValue),
262284
)
263285

286+
t.VDBlankWithNoFstabEntry = vdbuilder.New(
287+
vdbuilder.WithName("vd-blank-no-fstab-entry"),
288+
vdbuilder.WithNamespace(t.Framework.Namespace().Name),
289+
vdbuilder.WithPersistentVolumeClaim(nil, ptr.To(resource.MustParse("51Mi"))),
290+
vdbuilder.WithAnnotation(resourceAnnotationName, resourceAnnotationValue),
291+
vdbuilder.WithLabel(resourceLabelName, resourceLabelValue),
292+
)
293+
264294
t.VM = vmbuilder.New(
265295
vmbuilder.WithName("vm"),
266296
vmbuilder.WithNamespace(t.Framework.Namespace().Name),
@@ -276,14 +306,10 @@ func (t *restoreModeTest) GenerateResources(restoreMode v1alpha2.SnapshotOperati
276306
Kind: v1alpha2.DiskDevice,
277307
Name: t.VDRoot.Name,
278308
},
279-
),
280-
vmbuilder.WithBlockDeviceRefs(
281309
v1alpha2.BlockDeviceSpecRef{
282310
Kind: v1alpha2.ClusterImageDevice,
283311
Name: t.CVI.Name,
284312
},
285-
),
286-
vmbuilder.WithBlockDeviceRefs(
287313
v1alpha2.BlockDeviceSpecRef{
288314
Kind: v1alpha2.ImageDevice,
289315
Name: t.VI.Name,
@@ -302,6 +328,15 @@ func (t *restoreModeTest) GenerateResources(restoreMode v1alpha2.SnapshotOperati
302328
vmbdabuilder.WithLabel(resourceLabelName, resourceLabelValue),
303329
)
304330

331+
t.VMBDAWithNoFstabEntry = vmbdabuilder.New(
332+
vmbdabuilder.WithName("vmbda-no-fstab-entry"),
333+
vmbdabuilder.WithNamespace(t.VDBlankWithNoFstabEntry.Namespace),
334+
vmbdabuilder.WithVirtualMachineName(t.VM.Name),
335+
vmbdabuilder.WithBlockDeviceRef(v1alpha2.VMBDAObjectRefKindVirtualDisk, t.VDBlankWithNoFstabEntry.Name),
336+
vmbdabuilder.WithAnnotation(resourceAnnotationName, resourceAnnotationValue),
337+
vmbdabuilder.WithLabel(resourceLabelName, resourceLabelValue),
338+
)
339+
305340
t.VMSnapshot = vmsnapshotbuilder.New(
306341
vmsnapshotbuilder.WithName("vmsnapshot"),
307342
vmsnapshotbuilder.WithNamespace(t.Framework.Namespace().Name),
@@ -327,7 +362,7 @@ func (t *restoreModeTest) RemoveRecoverableResources() {
327362
Expect(err).NotTo(HaveOccurred())
328363
util.UntilObjectPhase(string(v1alpha2.MachineStopped), framework.ShortTimeout, t.VM)
329364

330-
err = t.Framework.Delete(context.Background(), t.VDRoot, t.VDBlank, t.VMBDA)
365+
err = t.Framework.Delete(context.Background(), t.VDRoot, t.VDBlank, t.VMBDA, t.VDBlankWithNoFstabEntry, t.VMBDAWithNoFstabEntry)
331366
Expect(err).NotTo(HaveOccurred())
332367

333368
// Wait for resources to be deleted before proceeding.
@@ -352,13 +387,27 @@ func (t *restoreModeTest) RemoveRecoverableResources() {
352387
Name: t.VMBDA.Name,
353388
}, &vmbdaLocal)
354389
g.Expect(k8serrors.IsNotFound(err)).Should(BeTrue())
390+
391+
var vdBlankWithNoFstabEntryLocal v1alpha2.VirtualDisk
392+
err = t.Framework.Clients.GenericClient().Get(context.Background(), types.NamespacedName{
393+
Namespace: t.VDBlankWithNoFstabEntry.Namespace,
394+
Name: t.VDBlankWithNoFstabEntry.Name,
395+
}, &vdBlankWithNoFstabEntryLocal)
396+
g.Expect(k8serrors.IsNotFound(err)).Should(BeTrue())
397+
398+
var vmbdaWithNoFstabEntryLocal v1alpha2.VirtualMachineBlockDeviceAttachment
399+
err = t.Framework.Clients.GenericClient().Get(context.Background(), types.NamespacedName{
400+
Namespace: t.VMBDAWithNoFstabEntry.Namespace,
401+
Name: t.VMBDAWithNoFstabEntry.Name,
402+
}, &vmbdaWithNoFstabEntryLocal)
403+
g.Expect(k8serrors.IsNotFound(err)).Should(BeTrue())
355404
}, framework.LongTimeout, time.Second).Should(Succeed())
356405
}
357406

358407
func (t *restoreModeTest) CheckVMAfterRestore(
359408
vm *v1alpha2.VirtualMachine,
360-
vdRoot, vdBlank *v1alpha2.VirtualDisk,
361-
vmbda *v1alpha2.VirtualMachineBlockDeviceAttachment,
409+
vdRoot, vdBlank, vdBlankWithNoFstabEntry *v1alpha2.VirtualDisk,
410+
vmbda, vmbdaWithNoFstabEntry *v1alpha2.VirtualMachineBlockDeviceAttachment,
362411
vmopRestore *v1alpha2.VirtualMachineOperation,
363412
) {
364413
GinkgoHelper()
@@ -382,7 +431,10 @@ func (t *restoreModeTest) CheckVMAfterRestore(
382431
t.CheckResourceReadyForRestore(vmopRestore, v1alpha2.VirtualDiskKind, vdRoot.Name)
383432
t.CheckResourceReadyForRestore(vmopRestore, v1alpha2.VirtualDiskKind, vdBlank.Name)
384433
t.CheckResourceReadyForRestore(vmopRestore, v1alpha2.VirtualMachineBlockDeviceAttachmentKind, vmbda.Name)
434+
t.CheckResourceReadyForRestore(vmopRestore, v1alpha2.VirtualDiskKind, vdBlankWithNoFstabEntry.Name)
435+
t.CheckResourceReadyForRestore(vmopRestore, v1alpha2.VirtualMachineBlockDeviceAttachmentKind, vmbdaWithNoFstabEntry.Name)
385436

437+
Expect(util.GetBlockDeviceHash(t.Framework, vm, v1alpha2.DiskDevice, vdBlankWithNoFstabEntry.Name)).To(Equal(t.BlockDeviceHash))
386438
Expect(util.ReadFile(t.Framework, vm, fileDataPath)).To(Equal(changedValueOnDisk))
387439
Expect(vm.Annotations[vmAnnotationName]).To(Equal(vmAnnotationChangedValue))
388440
Expect(vm.Labels[vmLabelName]).To(Equal(vmLabelChangedValue))

0 commit comments

Comments
 (0)