Skip to content

Commit

Permalink
add test for export() to make sure diff-from annot is not set in full…
Browse files Browse the repository at this point in the history
… backup

Signed-off-by: Ryotaro Banno <[email protected]>
  • Loading branch information
ushitora-anqou committed Nov 6, 2024
1 parent 18581a0 commit 89271a7
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions internal/controller/mantlebackup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
corev1 "k8s.io/api/core/v1"
aerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -1125,3 +1126,105 @@ var _ = Describe("SetSynchronizing", func() {
Entry("case 6", m1, &m0, m2, &m0, true),
)
})

var _ = Describe("export", func() {
var mockCtrl *gomock.Controller
var grpcClient *proto.MockMantleServiceClient
var mbr *MantleBackupReconciler
var ns string

BeforeEach(func() {
var t reporter
mockCtrl = gomock.NewController(t)
grpcClient = proto.NewMockMantleServiceClient(mockCtrl)

mbr = NewMantleBackupReconciler(
k8sClient,
scheme.Scheme,
resMgr.ClusterID,
RolePrimary,
&PrimarySettings{
Client: grpcClient,
ExportDataStorageClass: resMgr.StorageClassName,
},
"dummy image",
"",
nil,
)

ns = resMgr.CreateNamespace()
})

AfterEach(func() {
if mockCtrl != nil {
mockCtrl.Finish()
}
})

It("should set correct annotations after export() is called", func(ctx SpecContext) {
pvc := corev1.PersistentVolumeClaim{
Spec: corev1.PersistentVolumeClaimSpec{
Resources: corev1.VolumeResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceStorage: resource.MustParse("1Gi"),
},
},
},
}
pvcManifest, err := json.Marshal(pvc)
Expect(err).NotTo(HaveOccurred())

pv := corev1.PersistentVolume{
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
VolumeAttributes: map[string]string{
"pool": "dummy",
"imageName": "dummy",
},
},
},
},
}
pvManifest, err := json.Marshal(pv)
Expect(err).NotTo(HaveOccurred())

target := &mantlev1.MantleBackup{
ObjectMeta: metav1.ObjectMeta{
Name: "target",
Namespace: ns,
},
Spec: mantlev1.MantleBackupSpec{
PVC: "dummy",
Expire: "1d",
},
}
err = k8sClient.Create(context.Background(), target)
Expect(err).NotTo(HaveOccurred())
err = updateStatus(context.Background(), k8sClient, target, func() error {
target.Status.PVManifest = string(pvManifest)
target.Status.PVCManifest = string(pvcManifest)
return nil
})
Expect(err).NotTo(HaveOccurred())

grpcClient.EXPECT().SetSynchronizing(gomock.Any(), gomock.Any()).
Times(1).Return(&proto.SetSynchronizingResponse{}, nil)

prepareResult := &dataSyncPrepareResult{
isIncremental: false,
isSecondaryMantleBackupReadyToUse: true,
diffFrom: nil,
}

ret, err := mbr.export(context.Background(), target, prepareResult)
Expect(err).NotTo(HaveOccurred())
Expect(ret.Requeue).To(BeTrue())

err = k8sClient.Get(context.Background(),
types.NamespacedName{Name: target.GetName(), Namespace: target.GetNamespace()}, target)
Expect(err).NotTo(HaveOccurred())
_, ok := target.GetAnnotations()[annotDiffFrom]
Expect(ok).To(BeFalse())
})
})

0 comments on commit 89271a7

Please sign in to comment.