Skip to content

Commit

Permalink
wip: add unit test for GarbageCollectorRunner
Browse files Browse the repository at this point in the history
Signed-off-by: Ryotaro Banno <[email protected]>
  • Loading branch information
ushitora-anqou committed Nov 29, 2024
1 parent eeec979 commit a554971
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions internal/controller/garbage_collector_runner_test.go
Original file line number Diff line number Diff line change
@@ -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,
),
)
})
})

0 comments on commit a554971

Please sign in to comment.