|
| 1 | +package multik8s |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "time" |
| 6 | + |
| 7 | + mantlev1 "github.com/cybozu-go/mantle/api/v1" |
| 8 | + "github.com/cybozu-go/mantle/internal/ceph" |
| 9 | + "github.com/cybozu-go/mantle/internal/controller" |
| 10 | + . "github.com/cybozu-go/mantle/test/e2e/multik8s/testutil" |
| 11 | + "github.com/cybozu-go/mantle/test/util" |
| 12 | + . "github.com/onsi/ginkgo/v2" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | +) |
| 16 | + |
| 17 | +var _ = Describe("Locking", func() { |
| 18 | + namespace := util.GetUniqueName("ns-") |
| 19 | + pvcName := util.GetUniqueName("pvc-") |
| 20 | + backupName0 := util.GetUniqueName("mb-") |
| 21 | + backupName1 := util.GetUniqueName("mb-") |
| 22 | + dummyLockID := "dummy-lock-id" |
| 23 | + var controllerPod string |
| 24 | + var poolName, imageName string |
| 25 | + |
| 26 | + It("should setup environment", func(ctx SpecContext) { |
| 27 | + SetupEnvironment(namespace) |
| 28 | + // Create PVC and MantleBackup in the primary cluster |
| 29 | + CreatePVC(ctx, PrimaryK8sCluster, namespace, pvcName) |
| 30 | + CreateMantleBackup(PrimaryK8sCluster, namespace, pvcName, backupName0) |
| 31 | + WaitMantleBackupSynced(namespace, backupName0) |
| 32 | + var err error |
| 33 | + controllerPod, err = GetControllerPodName(SecondaryK8sCluster) |
| 34 | + Expect(err).NotTo(HaveOccurred()) |
| 35 | + }) |
| 36 | + |
| 37 | + It("should lock the volume in the secondary cluster", func() { |
| 38 | + mb0, err := GetMB(SecondaryK8sCluster, namespace, backupName0) |
| 39 | + Expect(err).NotTo(HaveOccurred()) |
| 40 | + |
| 41 | + pvStored := corev1.PersistentVolume{} |
| 42 | + err = json.Unmarshal([]byte(mb0.Status.PVManifest), &pvStored) |
| 43 | + Expect(err).NotTo(HaveOccurred()) |
| 44 | + poolName = pvStored.Spec.CSI.VolumeAttributes["pool"] |
| 45 | + imageName = pvStored.Spec.CSI.VolumeAttributes["imageName"] |
| 46 | + |
| 47 | + // locked |
| 48 | + _, _, err = Kubectl(SecondaryK8sCluster, nil, "exec", "-n", CephClusterNamespace, controllerPod, "--", |
| 49 | + "rbd", "-p", poolName, "lock", "add", imageName, dummyLockID) |
| 50 | + Expect(err).NotTo(HaveOccurred()) |
| 51 | + }) |
| 52 | + |
| 53 | + It("should create additional backup and wait for the log", func(ctx SpecContext) { |
| 54 | + CreateMantleBackup(PrimaryK8sCluster, namespace, pvcName, backupName1) |
| 55 | + var mb1 *mantlev1.MantleBackup |
| 56 | + Eventually(func() error { |
| 57 | + var err error |
| 58 | + mb1, err = GetMB(SecondaryK8sCluster, namespace, backupName1) |
| 59 | + return err |
| 60 | + }).Should(Succeed()) |
| 61 | + |
| 62 | + err := WaitControllerLog(ctx, SecondaryK8sCluster, |
| 63 | + "the volume is locked by another process.*"+string(mb1.GetUID()), |
| 64 | + 3*time.Minute) |
| 65 | + Expect(err).NotTo(HaveOccurred()) |
| 66 | + }) |
| 67 | + |
| 68 | + It("checks that the jobs are not created", func(ctx SpecContext) { |
| 69 | + mb1, err := GetMB(SecondaryK8sCluster, namespace, backupName1) |
| 70 | + Expect(err).NotTo(HaveOccurred()) |
| 71 | + Expect( |
| 72 | + CheckJobExist(SecondaryK8sCluster, CephClusterNamespace, controller.MantleZeroOutJobPrefix+string(mb1.GetUID())), |
| 73 | + ).To(BeFalse()) |
| 74 | + Expect( |
| 75 | + CheckJobExist(SecondaryK8sCluster, CephClusterNamespace, controller.MantleImportJobPrefix+string(mb1.GetUID())), |
| 76 | + ).To(BeFalse()) |
| 77 | + }) |
| 78 | + |
| 79 | + It("should unlock the volume in the secondary cluster", func() { |
| 80 | + stdout, _, err := Kubectl(SecondaryK8sCluster, nil, "exec", "-n", CephClusterNamespace, controllerPod, "--", |
| 81 | + "rbd", "-p", poolName, "--format", "json", "lock", "ls", imageName) |
| 82 | + Expect(err).NotTo(HaveOccurred()) |
| 83 | + var locks []*ceph.RBDLock |
| 84 | + err = json.Unmarshal([]byte(stdout), &locks) |
| 85 | + Expect(err).NotTo(HaveOccurred()) |
| 86 | + Expect(locks).To(HaveLen(1)) |
| 87 | + |
| 88 | + // unlock |
| 89 | + _, _, err = Kubectl(SecondaryK8sCluster, nil, "exec", "-n", CephClusterNamespace, controllerPod, "--", |
| 90 | + "rbd", "-p", poolName, "lock", "rm", imageName, dummyLockID, locks[0].Locker) |
| 91 | + Expect(err).NotTo(HaveOccurred()) |
| 92 | + }) |
| 93 | + |
| 94 | + It("should resume backup creation and complete it", func() { |
| 95 | + // check that the lock created by Mantle exists |
| 96 | + mb1, err := GetMB(SecondaryK8sCluster, namespace, backupName1) |
| 97 | + Expect(err).NotTo(HaveOccurred()) |
| 98 | + Eventually(func() error { |
| 99 | + stdout, _, err := Kubectl(SecondaryK8sCluster, nil, "exec", "-n", CephClusterNamespace, controllerPod, "--", |
| 100 | + "rbd", "-p", poolName, "--format", "json", "lock", "ls", imageName) |
| 101 | + Expect(err).NotTo(HaveOccurred()) |
| 102 | + var locks []*ceph.RBDLock |
| 103 | + err = json.Unmarshal([]byte(stdout), &locks) |
| 104 | + Expect(err).NotTo(HaveOccurred()) |
| 105 | + Expect(locks).To(HaveLen(1)) |
| 106 | + Expect(locks[0].LockID).To(Equal(string(mb1.GetUID()))) |
| 107 | + return nil |
| 108 | + }).Should(Succeed()) |
| 109 | + |
| 110 | + // wait for completion |
| 111 | + WaitMantleBackupSynced(namespace, backupName1) |
| 112 | + }) |
| 113 | + |
| 114 | + It("should not exist locks after backup completion", func() { |
| 115 | + stdout, _, err := Kubectl(SecondaryK8sCluster, nil, "exec", "-n", CephClusterNamespace, controllerPod, "--", |
| 116 | + "rbd", "-p", poolName, "--format", "json", "lock", "ls", imageName) |
| 117 | + Expect(err).NotTo(HaveOccurred()) |
| 118 | + var locks []*ceph.RBDLock |
| 119 | + err = json.Unmarshal([]byte(stdout), &locks) |
| 120 | + Expect(err).NotTo(HaveOccurred()) |
| 121 | + Expect(locks).To(HaveLen(0)) |
| 122 | + }) |
| 123 | +}) |
0 commit comments