From 24b56ef7a67ef4b6b22601a60c142b640296d3aa Mon Sep 17 00:00:00 2001 From: Abhilash Shetty Date: Thu, 20 Jun 2024 19:00:19 +0000 Subject: [PATCH] test(ci): adding wrapper for vg and pv operations Signed-off-by: Abhilash Shetty --- tests/lvm_utils.go | 201 ++++++++++++++++++++++++++++++++++++++++ tests/provision_test.go | 14 +++ tests/utils.go | 68 -------------- 3 files changed, 215 insertions(+), 68 deletions(-) create mode 100644 tests/lvm_utils.go diff --git a/tests/lvm_utils.go b/tests/lvm_utils.go new file mode 100644 index 00000000..09ec9427 --- /dev/null +++ b/tests/lvm_utils.go @@ -0,0 +1,201 @@ +/* +Copyright 2021 The OpenEBS Authors + +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 tests + +import ( + "fmt" + "strconv" + "strings" + + "github.com/onsi/gomega" +) + +// This creates loopdevice using the size passed as arg, +// Uses the new loop device to create PV. returns loopdevice name to the caller. +func createPV(size int) string { + fmt.Printf("Creating device\n") + + back_file_args := []string{ + "mktemp", "-t", + "openebs_lvm_localpv_disk_XXXXX", + "--dry-run", + } + file, _, _ := execAtLocal("sudo", nil, back_file_args...) + + file_str := strings.TrimSpace(string(file[:])) + size_str := strconv.Itoa(size) + "G" + device_args := []string{ + "truncate", "-s", + size_str, file_str, + } + _, _, err := execAtLocal("sudo", nil, device_args...) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "create device failed") + + args_loop := []string{ + "losetup", "-f", + file_str, "--show", + } + stdout_loop, _, err := execAtLocal("sudo", nil, args_loop...) + gomega.Expect(err).To(gomega.BeNil(), "loop device create failed") + + stdout_loop_str := strings.TrimSpace(string(stdout_loop[:])) + args_pv := []string{ + "pvcreate", + stdout_loop_str, + } + _, _, err_pv := execAtLocal("sudo", nil, args_pv...) + gomega.Expect(err_pv).To(gomega.BeNil(), "pv create failed") + return stdout_loop_str +} + +// Does pvremove on specified device. Deletes loop device and the file backing loop device. +func removePV(device string) { + fmt.Printf("remove pv\n") + args_pv := []string{ + "pvremove", + device, + "-y", + } + _, _, err_pv := execAtLocal("sudo", nil, args_pv...) + gomega.Expect(err_pv).To(gomega.BeNil(), "pv remove failed") + + args_lo := []string{ + "losetup", + device, + "-O", + "BACK-FILE", + "--noheadings", + } + dev, _, _ := execAtLocal("sudo", nil, args_lo...) + dev_str := strings.TrimSpace(string(dev)) + + args_loop := []string{ + "losetup", "-d", + device, + } + _, _, err_loop := execAtLocal("sudo", nil, args_loop...) + gomega.Expect(err_loop).To(gomega.BeNil(), "loop device remove failed") + + args_file := []string{ + "rm", + "-f", + dev_str, + } + _, _, err_file := execAtLocal("sudo", nil, args_file...) + gomega.Expect(err_file).To(gomega.BeNil(), "file remove failed") +} + +// Creates vg on the specified device, Device passed should be a pv. +func createVg(name string, device string) { + fmt.Printf("Creating vg\n") + args_vg := []string{ + "vgcreate", name, + device, + } + _, _, err_vg := execAtLocal("sudo", nil, args_vg...) + gomega.Expect(err_vg).To(gomega.BeNil(), "vg create failed") +} + +// Takes vg name and pv device, extends vg using the supplied pv. +func extendVg(name string, device string) { + fmt.Printf("extending vg\n") + args_vg := []string{ + "vgextend", name, + device, + } + _, _, err_vg := execAtLocal("sudo", nil, args_vg...) + gomega.Expect(err_vg).To(gomega.BeNil(), "vg extend failed") +} + +// Does vhremove on specified vg with force flag, +// lv will be forcedeleted if vg is not empty. +func removeVg(name string) { + fmt.Printf("Removing vg\n") + args_vg := []string{ + "vgremove", + name, + "-f", + } + _, _, err_vg := execAtLocal("sudo", nil, args_vg...) + gomega.Expect(err_vg).To(gomega.BeNil(), "vg remove failed") +} + +// enable the monitoring on thinpool created for test, on local node which +// is part of single node cluster. +func enableThinpoolMonitoring() { + lv := VOLGROUP + "/" + pvcObj.Spec.VolumeName + + args := []string{ + "lvdisplay", "--columns", + "--options", "pool_lv", + "--noheadings", + lv, + } + stdout, _, err := execAtLocal("sudo", nil, args...) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "display LV") + gomega.Expect(strings.TrimSpace(string(stdout))).To(gomega.Not(gomega.Equal("")), "get thinpool LV") + + thinpool := VOLGROUP + "/" + strings.TrimSpace(string(stdout)) + + args = []string{ + "lvchange", + "--monitor", "y", + thinpool, + } + + _, _, err = execAtLocal("sudo", nil, args...) + gomega.Expect(err).To(gomega.BeNil(), "run lvchange command") +} + +// verify that the thinpool has extended in capacity to an expected size. +func VerifyThinpoolExtend() { + expect_size, _ := strconv.ParseInt(expanded_capacity, 10, 64) + lv := VOLGROUP + "/" + pvcObj.Spec.VolumeName + + args := []string{ + "lvdisplay", "--columns", + "--options", "pool_lv", + "--noheadings", + lv, + } + + //stdout will contain the pool name + stdout, _, err := execAtLocal("sudo", nil, args...) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "display LV") + gomega.Expect(strings.TrimSpace(string(stdout))).To(gomega.Not(gomega.Equal("")), "get thinpool LV") + + thinpool := VOLGROUP + "/" + strings.TrimSpace(string(stdout)) + + args = []string{ + "lvdisplay", "--columns", + "--options", "lv_size", + "--units", "b", + "--noheadings", + thinpool, + } + + // stdout will contain the size + stdout, _, err = execAtLocal("sudo", nil, args...) + gomega.Expect(err).To(gomega.BeNil(), "display thinpool LV") + + // Remove unit suffix from the size. + size_str := strings.TrimSuffix(strings.TrimSpace(string(stdout)), "B") + // This expectation is a factor of the lvm.conf settings we do from ci-test.sh + // and the original volume size. + size_int64, _ := strconv.ParseInt(size_str, 10, 64) + gomega.Expect(size_int64).To(gomega.Equal(expect_size)) +} diff --git a/tests/provision_test.go b/tests/provision_test.go index b3c0f2a6..30c388d2 100644 --- a/tests/provision_test.go +++ b/tests/provision_test.go @@ -190,12 +190,26 @@ func leakProtectionTest() { By("Deleting storage class", deleteStorageClass) } +// This acts as a test just to call the new wrapper functions, +// Once we write tests calling them this will not be required. +// This doesnt test any Openebs component. +func lvmOps() { + device := createPV(6) + createVg("newvgcode3", device) + device_1 := createPV(5) + extendVg("newvgcode3", device_1) + removeVg("newvgcode3") + removePV(device) + removePV(device_1) +} + func volumeCreationTest() { By("Running filesystem volume creation test", fsVolCreationTest) By("Running block volume creation test", blockVolCreationTest) By("Running thin volume creation test", thinVolCreationTest) By("Running leak protection test", leakProtectionTest) By("Running shared volume for two app pods on same node test", sharedVolumeTest) + By("Running Lvm Ops", lvmOps) } func capacityTest() { diff --git a/tests/utils.go b/tests/utils.go index aae02a98..99de47d7 100644 --- a/tests/utils.go +++ b/tests/utils.go @@ -19,8 +19,6 @@ package tests import ( "context" "fmt" - "strconv" - "strings" "time" "github.com/onsi/ginkgo" @@ -748,69 +746,3 @@ func createNodeDaemonSet(ds *appsv1.DaemonSet) { gomega.BeNil(), "creating node plugin daemonset %v", nodeDaemonSet) } - -// enable the monitoring on thinpool created for test, on local node which -// is part of single node cluster. -func enableThinpoolMonitoring() { - lv := VOLGROUP + "/" + pvcObj.Spec.VolumeName - - args := []string{ - "lvdisplay", "--columns", - "--options", "pool_lv", - "--noheadings", - lv, - } - stdout, _, err := execAtLocal("sudo", nil, args...) - gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "display LV") - gomega.Expect(strings.TrimSpace(string(stdout))).To(gomega.Not(gomega.Equal("")), "get thinpool LV") - - thinpool := VOLGROUP + "/" + strings.TrimSpace(string(stdout)) - - args = []string{ - "lvchange", - "--monitor", "y", - thinpool, - } - - _, _, err = execAtLocal("sudo", nil, args...) - gomega.Expect(err).To(gomega.BeNil(), "run lvchange command") -} - -// verify that the thinpool has extended in capacity to an expected size. -func VerifyThinpoolExtend() { - expect_size, _ := strconv.ParseInt(expanded_capacity, 10, 64) - lv := VOLGROUP + "/" + pvcObj.Spec.VolumeName - - args := []string{ - "lvdisplay", "--columns", - "--options", "pool_lv", - "--noheadings", - lv, - } - - //stdout will contain the pool name - stdout, _, err := execAtLocal("sudo", nil, args...) - gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "display LV") - gomega.Expect(strings.TrimSpace(string(stdout))).To(gomega.Not(gomega.Equal("")), "get thinpool LV") - - thinpool := VOLGROUP + "/" + strings.TrimSpace(string(stdout)) - - args = []string{ - "lvdisplay", "--columns", - "--options", "lv_size", - "--units", "b", - "--noheadings", - thinpool, - } - - // stdout will contain the size - stdout, _, err = execAtLocal("sudo", nil, args...) - gomega.Expect(err).To(gomega.BeNil(), "display thinpool LV") - - // Remove unit suffix from the size. - size_str := strings.TrimSuffix(strings.TrimSpace(string(stdout)), "B") - // This expectation is a factor of the lvm.conf settings we do from ci-test.sh - // and the original volume size. - size_int64, _ := strconv.ParseInt(size_str, 10, 64) - gomega.Expect(size_int64).To(gomega.Equal(expect_size)) -}