Skip to content

Commit

Permalink
add tests for prepareForDataSynchronization func
Browse files Browse the repository at this point in the history
Signed-off-by: Shinya Hayashi <[email protected]>
  • Loading branch information
peng225 committed Oct 18, 2024
1 parent d9f469e commit 8817625
Show file tree
Hide file tree
Showing 4 changed files with 351 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ vet: ## Run go vet against code.
.PHONY: mock
mock: mockgen
$(MOCKGEN) -source=internal/ceph/command.go -destination=internal/ceph/command_mock.go -package=ceph
$(MOCKGEN) -source=pkg/controller/proto/controller_grpc.pb.go -destination=pkg/controller/proto/controller_grpc.pb_mock.go -package=proto

.PHONY: test
test: manifests generate fmt vet envtest mock ## Run tests.
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/cybozu-go/mantle
go 1.21

require (
github.com/go-logr/logr v1.2.4
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
Expand Down Expand Up @@ -31,8 +30,10 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/zapr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand Down
132 changes: 132 additions & 0 deletions internal/controller/mantlebackup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"encoding/json"
"fmt"
"slices"
"sync"
"time"
Expand All @@ -12,13 +13,15 @@ import (
"github.com/cybozu-go/mantle/pkg/controller/proto"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
"google.golang.org/grpc"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/kube-openapi/pkg/validation/strfmt"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
)
Expand Down Expand Up @@ -547,3 +550,132 @@ var _ = Describe("searchDiffOriginMantleBackup", func() {
true, "test1"),
)
})

type reporter struct{}

func (g reporter) Errorf(format string, args ...any) {
Fail(fmt.Sprintf(format, args...))
}

func (g reporter) Fatalf(format string, args ...any) {
Fail(fmt.Sprintf(format, args...))
}

func newMantleBackup(
name string,
namespace string,
withDelTimestamp bool,
pvcUID string,
snapID int,
readyToUse metav1.ConditionStatus,
syncedToRemote metav1.ConditionStatus,
) *mantlev1.MantleBackup {
newMB := &mantlev1.MantleBackup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
labelLocalBackupTargetPVCUID: pvcUID,
},
},
Status: mantlev1.MantleBackupStatus{
SnapID: &snapID,
},
}
if withDelTimestamp {
now := metav1.Now()
newMB.SetDeletionTimestamp(&now)
}
meta.SetStatusCondition(&newMB.Status.Conditions, metav1.Condition{
Type: mantlev1.BackupConditionReadyToUse,
Status: readyToUse,
})
meta.SetStatusCondition(&newMB.Status.Conditions, metav1.Condition{
Type: mantlev1.BackupConditionSyncedToRemote,
Status: syncedToRemote,
})
return newMB
}

var _ = Describe("prepareForDataSynchronization", func() {
testPVCUID := "d3b07384-d9a7-4e6b-8a3b-1f4b7b7b7b7b"
testName := "test5"
testNamespace := "test-ns"
testMantleBackup := newMantleBackup(testName, testNamespace, false,
testPVCUID, 5, metav1.ConditionTrue, metav1.ConditionFalse)
DescribeTable("hoge",
func(
primaryBackups []*mantlev1.MantleBackup,
secondaryMantleBackups []*mantlev1.MantleBackup,
isIncremental bool,
isSecondaryMantleBackupReadyToUse bool,
diffFrom *mantlev1.MantleBackup,
) {
var t reporter
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
grpcClient := proto.NewMockMantleServiceClient(mockCtrl)

data, err := json.Marshal(secondaryMantleBackups)
Expect(err).NotTo(HaveOccurred())
grpcClient.EXPECT().ListMantleBackup(gomock.Any(),
&proto.ListMantleBackupRequest{
PvcUID: testPVCUID,
Namespace: testNamespace,
}).Times(1).Return(
&proto.ListMantleBackupResponse{
MantleBackupList: data,
}, nil)

ctrlClient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
Expect(ctrlClient).NotTo(BeNil())
for _, backup := range primaryBackups {
err := ctrlClient.Create(context.Background(), backup)
Expect(err).NotTo(HaveOccurred())
}

mbr := NewMantleBackupReconciler(ctrlClient,
ctrlClient.Scheme(), "test", RolePrimary, nil)

ret, err := mbr.prepareForDataSynchronization(context.Background(),
testMantleBackup, grpcClient)
Expect(err).NotTo(HaveOccurred())
Expect(ret.isIncremental).To(Equal(isIncremental))
Expect(ret.isSecondaryMantleBackupReadyToUse).To(Equal(isSecondaryMantleBackupReadyToUse))
if isIncremental {
Expect(ret.diffFrom).NotTo(BeNil())
Expect(ret.diffFrom.GetName()).To(Equal(diffFrom.GetName()))
} else {
Expect(ret.diffFrom).To(BeNil())
}
},
Entry("No synced MantleBackup exists",
[]*mantlev1.MantleBackup{
testMantleBackup.DeepCopy(),
},
[]*mantlev1.MantleBackup{
newMantleBackup(testName, testNamespace, false, testPVCUID, 5,
metav1.ConditionFalse, metav1.ConditionUnknown),
}, false, false, nil),
Entry("Synced but not reflected to the condition",
[]*mantlev1.MantleBackup{
testMantleBackup.DeepCopy(),
},
[]*mantlev1.MantleBackup{
newMantleBackup(testName, testNamespace, false, testPVCUID, 5,
metav1.ConditionTrue, metav1.ConditionUnknown),
}, false, true, nil),
Entry("Synced MantleBackup exists but deletionTimestamp is set",
[]*mantlev1.MantleBackup{
newMantleBackup("test3", testNamespace, true, testPVCUID, 3,
metav1.ConditionTrue, metav1.ConditionTrue),
testMantleBackup.DeepCopy(),
},
[]*mantlev1.MantleBackup{
newMantleBackup("test3", testNamespace, true, testPVCUID, 3,
metav1.ConditionTrue, metav1.ConditionUnknown),
newMantleBackup(testName, testNamespace, false, testPVCUID, 5,
metav1.ConditionFalse, metav1.ConditionUnknown),
}, false, false, nil),
)
})
Loading

0 comments on commit 8817625

Please sign in to comment.