|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "strings" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 30 | + virtv1 "kubevirt.io/api/core/v1" |
| 31 | + |
| 32 | + "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 33 | + "github.com/deckhouse/virtualization/test/e2e/internal/framework" |
| 34 | +) |
| 35 | + |
| 36 | +func GetBlockDevicePath(f *framework.Framework, vm *v1alpha2.VirtualMachine, bdKind v1alpha2.BlockDeviceKind, bdName string) string { |
| 37 | + GinkgoHelper() |
| 38 | + |
| 39 | + serial, ok := GetBlockDeviceSerialNumber(vm, bdKind, bdName) |
| 40 | + Expect(ok).To(BeTrue(), "failed to get block device serial number") |
| 41 | + |
| 42 | + devicePath, err := GetBlockDeviceBySerial(f, vm, serial) |
| 43 | + Expect(err).NotTo(HaveOccurred(), "failed to get device by serial") |
| 44 | + return devicePath |
| 45 | +} |
| 46 | + |
| 47 | +func CreateBlockDeviceFilesystem(f *framework.Framework, vm *v1alpha2.VirtualMachine, bdKind v1alpha2.BlockDeviceKind, bdName, fsType string) { |
| 48 | + GinkgoHelper() |
| 49 | + |
| 50 | + serial, ok := GetBlockDeviceSerialNumber(vm, bdKind, bdName) |
| 51 | + Expect(ok).To(BeTrue(), "failed to get block device serial number") |
| 52 | + |
| 53 | + devicePath, err := GetBlockDeviceBySerial(f, vm, serial) |
| 54 | + Expect(err).NotTo(HaveOccurred(), "failed to get device by serial") |
| 55 | + |
| 56 | + _, err = f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo mkfs.%s %s", fsType, devicePath)) |
| 57 | + Expect(err).NotTo(HaveOccurred()) |
| 58 | +} |
| 59 | + |
| 60 | +func MountBlockDevice(f *framework.Framework, vm *v1alpha2.VirtualMachine, bdKind v1alpha2.BlockDeviceKind, bdName, mountPoint string) { |
| 61 | + GinkgoHelper() |
| 62 | + |
| 63 | + serial, ok := GetBlockDeviceSerialNumber(vm, bdKind, bdName) |
| 64 | + Expect(ok).To(BeTrue(), "failed to get block device serial number") |
| 65 | + |
| 66 | + devicePath, err := GetBlockDeviceBySerial(f, vm, serial) |
| 67 | + Expect(err).NotTo(HaveOccurred(), "failed to get device by serial") |
| 68 | + |
| 69 | + _, err = f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo mount %s %s", devicePath, mountPoint)) |
| 70 | + 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") |
| 85 | + |
| 86 | + 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) |
| 87 | + _, err := f.SSHCommand(vm.Name, vm.Namespace, cmd) |
| 88 | + Expect(err).NotTo(HaveOccurred()) |
| 89 | +} |
| 90 | + |
| 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(), "failed to get device by serial") |
| 99 | + |
| 100 | + // We use dd to ensure the entire disk is read. |
| 101 | + cmdOut, err := f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo dd if=%s bs=4M | sha256sum | awk \"{print \\$1}\"", devicePath)) |
| 102 | + Expect(err).NotTo(HaveOccurred()) |
| 103 | + return strings.TrimSpace(cmdOut) |
| 104 | +} |
| 105 | + |
| 106 | +func GetBlockDeviceBySerial(f *framework.Framework, vm *v1alpha2.VirtualMachine, serial string) (string, error) { |
| 107 | + cmdOut, err := f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo lsblk -o PATH,SERIAL | grep %s | awk \"{print \\$1, \\$2}\"", serial)) |
| 108 | + if err != nil { |
| 109 | + return "", err |
| 110 | + } |
| 111 | + |
| 112 | + cmdLines := strings.Split(strings.TrimSpace(cmdOut), "\n") |
| 113 | + if len(cmdLines) == 0 { |
| 114 | + return "", errors.New("shell out is empty") |
| 115 | + } |
| 116 | + |
| 117 | + columns := strings.Split(strings.TrimSpace(cmdLines[0]), " ") |
| 118 | + if len(columns) != 2 { |
| 119 | + return "", errors.New("shell out columns mismatch") |
| 120 | + } |
| 121 | + |
| 122 | + if columns[1] == serial { |
| 123 | + return columns[0], nil |
| 124 | + } |
| 125 | + |
| 126 | + return "", errors.New("no block device found") |
| 127 | +} |
| 128 | + |
| 129 | +func GetBlockDeviceSerialNumber(vm *v1alpha2.VirtualMachine, bdKind v1alpha2.BlockDeviceKind, bdName string) (string, bool) { |
| 130 | + unstructuredVMI, err := framework.GetClients().DynamicClient().Resource(schema.GroupVersionResource{ |
| 131 | + Group: "internal.virtualization.deckhouse.io", |
| 132 | + Version: "v1", |
| 133 | + Resource: "internalvirtualizationvirtualmachineinstances", |
| 134 | + }).Namespace(vm.Namespace).Get(context.Background(), vm.Name, metav1.GetOptions{}) |
| 135 | + Expect(err).NotTo(HaveOccurred()) |
| 136 | + |
| 137 | + var kvvmi virtv1.VirtualMachineInstance |
| 138 | + err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredVMI.Object, &kvvmi) |
| 139 | + Expect(err).NotTo(HaveOccurred()) |
| 140 | + |
| 141 | + var blockDeviceName string |
| 142 | + switch bdKind { |
| 143 | + case v1alpha2.DiskDevice: |
| 144 | + blockDeviceName = fmt.Sprintf("vd-%s", bdName) |
| 145 | + case v1alpha2.ImageDevice: |
| 146 | + blockDeviceName = fmt.Sprintf("vi-%s", bdName) |
| 147 | + case v1alpha2.ClusterImageDevice: |
| 148 | + blockDeviceName = fmt.Sprintf("cvi-%s", bdName) |
| 149 | + default: |
| 150 | + Fail(fmt.Sprintf("unknown block device kind %q", bdKind)) |
| 151 | + } |
| 152 | + |
| 153 | + for _, disk := range kvvmi.Spec.Domain.Devices.Disks { |
| 154 | + if disk.Name == blockDeviceName { |
| 155 | + return disk.Serial, true |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return "", false |
| 160 | +} |
| 161 | + |
| 162 | +func WriteFile(f *framework.Framework, vm *v1alpha2.VirtualMachine, path, value string) { |
| 163 | + GinkgoHelper() |
| 164 | + |
| 165 | + // Escape single quotes in value to prevent command injection. |
| 166 | + escapedValue := strings.ReplaceAll(value, "'", "'\"'\"'") |
| 167 | + _, err := f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo bash -c \"echo '%s' > %s\"", escapedValue, path)) |
| 168 | + Expect(err).NotTo(HaveOccurred()) |
| 169 | +} |
| 170 | + |
| 171 | +func ReadFile(f *framework.Framework, vm *v1alpha2.VirtualMachine, path string) string { |
| 172 | + GinkgoHelper() |
| 173 | + |
| 174 | + cmdOut, err := f.SSHCommand(vm.Name, vm.Namespace, fmt.Sprintf("sudo cat %s", path)) |
| 175 | + Expect(err).NotTo(HaveOccurred()) |
| 176 | + return strings.TrimSpace(cmdOut) |
| 177 | +} |
0 commit comments