From a554971c1f0163d88f985c4a71b8dbeaeb73d35c Mon Sep 17 00:00:00 2001 From: Ryotaro Banno Date: Fri, 29 Nov 2024 09:04:11 +0000 Subject: [PATCH] wip: add unit test for GarbageCollectorRunner Signed-off-by: Ryotaro Banno --- .../garbage_collector_runner_test.go | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 internal/controller/garbage_collector_runner_test.go diff --git a/internal/controller/garbage_collector_runner_test.go b/internal/controller/garbage_collector_runner_test.go new file mode 100644 index 0000000..8230681 --- /dev/null +++ b/internal/controller/garbage_collector_runner_test.go @@ -0,0 +1,99 @@ +package controller + +import ( + "time" + + mantlev1 "github.com/cybozu-go/mantle/api/v1" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" +) + +var _ = Describe("garbage collector", func() { + var err error + + Context("isMantleRestoreAlreadyDeleted", func() { + DescribeTable("isMantleRestoreAlreadyDeleted", + func( + ctx SpecContext, + buildMR func(ns string) *mantlev1.MantleRestore, + buildPV func(ns string, mr *mantlev1.MantleRestore) *corev1.PersistentVolume, + expectError, expectDeleted bool, + ) { + runner := NewGarbageCollectorRunner(k8sClient, 1*time.Second) + ns := resMgr.CreateNamespace() + mr := buildMR(ns) + if mr != nil { + err = k8sClient.Create(ctx, mr) + Expect(err).NotTo(HaveOccurred()) + err = k8sClient.Get(ctx, types.NamespacedName{Name: mr.GetName(), Namespace: mr.GetNamespace()}, mr) + Expect(err).NotTo(HaveOccurred()) + } + pv := buildPV(ns, mr) + + deleted, err := runner.isMantleRestoreAlreadyDeleted(ctx, pv) + if expectError { + Expect(err).To(HaveOccurred()) + } else { + Expect(err).NotTo(HaveOccurred()) + } + Expect(deleted).To(Equal(expectDeleted)) + }, + Entry( + "MantleRestore exists", + func(ns string) *mantlev1.MantleRestore { + var mr mantlev1.MantleRestore + mr.SetName("mr") + mr.SetNamespace(ns) + mr.Spec.Backup = "dummy" + return &mr + }, + func(ns string, mr *mantlev1.MantleRestore) *corev1.PersistentVolume { + var pv corev1.PersistentVolume + pv.SetAnnotations(map[string]string{ + PVAnnotationRestoredBy: string(mr.GetUID()), + PVAnnotationRestoredByName: "mr", + PVAnnotationRestoredByNamespace: ns, + }) + return &pv + }, + false, false, + ), + Entry( + "MantleRestore does NOT exist", + func(ns string) *mantlev1.MantleRestore { return nil }, + func(ns string, mr *mantlev1.MantleRestore) *corev1.PersistentVolume { + var pv corev1.PersistentVolume + pv.SetAnnotations(map[string]string{ + PVAnnotationRestoredBy: "dummy", + PVAnnotationRestoredByName: "mr1", + PVAnnotationRestoredByNamespace: ns, + }) + return &pv + }, + false, true, + ), + Entry( + "MantleRestore exists, but its annotation has unexpected MR's UID", + func(ns string) *mantlev1.MantleRestore { + var mr mantlev1.MantleRestore + mr.SetName("mr") + mr.SetNamespace(ns) + mr.Spec.Backup = "dummy" + return &mr + }, + func(ns string, mr *mantlev1.MantleRestore) *corev1.PersistentVolume { + var pv corev1.PersistentVolume + pv.SetAnnotations(map[string]string{ + PVAnnotationRestoredBy: "unexpected-uid", + PVAnnotationRestoredByName: "mr1", + PVAnnotationRestoredByNamespace: ns, + }) + return &pv + }, + false, true, + ), + ) + }) +})