Skip to content

Commit b8040f2

Browse files
committed
Add backup PVC related test cases
1 parent eae66ea commit b8040f2

File tree

3 files changed

+510
-11
lines changed

3 files changed

+510
-11
lines changed

pkg/resource-handler/controller/shard/pool_statefulset_test.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,3 +931,193 @@ func TestBuildPoolVolumeClaimTemplates(t *testing.T) {
931931
})
932932
}
933933
}
934+
935+
func TestBuildBackupPVC(t *testing.T) {
936+
scheme := runtime.NewScheme()
937+
_ = multigresv1alpha1.AddToScheme(scheme)
938+
939+
shard := &multigresv1alpha1.Shard{
940+
ObjectMeta: metav1.ObjectMeta{
941+
Name: "test-shard",
942+
Namespace: "default",
943+
UID: "test-uid",
944+
},
945+
}
946+
947+
tests := map[string]struct {
948+
poolSpec multigresv1alpha1.PoolSpec
949+
want *corev1.PersistentVolumeClaim
950+
}{
951+
"defaults": {
952+
poolSpec: multigresv1alpha1.PoolSpec{},
953+
want: &corev1.PersistentVolumeClaim{
954+
ObjectMeta: metav1.ObjectMeta{
955+
Name: "backup-data-test-shard-pool-primary-zone1",
956+
Namespace: "default",
957+
Labels: map[string]string{
958+
"app.kubernetes.io/name": "multigres",
959+
"app.kubernetes.io/instance": "test-shard-pool-primary-zone1",
960+
"app.kubernetes.io/component": PoolComponentName,
961+
"app.kubernetes.io/part-of": "multigres",
962+
"app.kubernetes.io/managed-by": "multigres-operator",
963+
"multigres.com/cell": "zone1",
964+
"multigres.com/database": "",
965+
"multigres.com/tablegroup": "",
966+
},
967+
OwnerReferences: []metav1.OwnerReference{
968+
{
969+
APIVersion: "multigres.com/v1alpha1",
970+
Kind: "Shard",
971+
Name: "test-shard",
972+
UID: "test-uid",
973+
Controller: ptr.To(true),
974+
BlockOwnerDeletion: ptr.To(true),
975+
},
976+
},
977+
},
978+
Spec: corev1.PersistentVolumeClaimSpec{
979+
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
980+
Resources: corev1.VolumeResourceRequirements{
981+
Requests: corev1.ResourceList{
982+
corev1.ResourceStorage: resource.MustParse("10Gi"),
983+
},
984+
},
985+
},
986+
},
987+
},
988+
"inherit storage class": {
989+
poolSpec: multigresv1alpha1.PoolSpec{
990+
Storage: multigresv1alpha1.StorageSpec{
991+
Class: "fast-ssd",
992+
},
993+
},
994+
want: &corev1.PersistentVolumeClaim{
995+
ObjectMeta: metav1.ObjectMeta{
996+
Name: "backup-data-test-shard-pool-primary-zone1",
997+
Namespace: "default",
998+
Labels: map[string]string{
999+
"app.kubernetes.io/name": "multigres",
1000+
"app.kubernetes.io/instance": "test-shard-pool-primary-zone1",
1001+
"app.kubernetes.io/component": PoolComponentName,
1002+
"app.kubernetes.io/part-of": "multigres",
1003+
"app.kubernetes.io/managed-by": "multigres-operator",
1004+
"multigres.com/cell": "zone1",
1005+
"multigres.com/database": "",
1006+
"multigres.com/tablegroup": "",
1007+
},
1008+
OwnerReferences: []metav1.OwnerReference{
1009+
{
1010+
APIVersion: "multigres.com/v1alpha1",
1011+
Kind: "Shard",
1012+
Name: "test-shard",
1013+
UID: "test-uid",
1014+
Controller: ptr.To(true),
1015+
BlockOwnerDeletion: ptr.To(true),
1016+
},
1017+
},
1018+
},
1019+
Spec: corev1.PersistentVolumeClaimSpec{
1020+
StorageClassName: ptr.To("fast-ssd"),
1021+
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
1022+
Resources: corev1.VolumeResourceRequirements{
1023+
Requests: corev1.ResourceList{
1024+
corev1.ResourceStorage: resource.MustParse("10Gi"),
1025+
},
1026+
},
1027+
},
1028+
},
1029+
},
1030+
"override backup storage": {
1031+
poolSpec: multigresv1alpha1.PoolSpec{
1032+
Storage: multigresv1alpha1.StorageSpec{
1033+
Class: "fast-ssd",
1034+
},
1035+
BackupStorage: multigresv1alpha1.StorageSpec{
1036+
Class: "backup-nfs",
1037+
Size: "100Gi",
1038+
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany},
1039+
},
1040+
},
1041+
want: &corev1.PersistentVolumeClaim{
1042+
ObjectMeta: metav1.ObjectMeta{
1043+
Name: "backup-data-test-shard-pool-primary-zone1",
1044+
Namespace: "default",
1045+
Labels: map[string]string{
1046+
"app.kubernetes.io/name": "multigres",
1047+
"app.kubernetes.io/instance": "test-shard-pool-primary-zone1",
1048+
"app.kubernetes.io/component": PoolComponentName,
1049+
"app.kubernetes.io/part-of": "multigres",
1050+
"app.kubernetes.io/managed-by": "multigres-operator",
1051+
"multigres.com/cell": "zone1",
1052+
"multigres.com/database": "",
1053+
"multigres.com/tablegroup": "",
1054+
},
1055+
OwnerReferences: []metav1.OwnerReference{
1056+
{
1057+
APIVersion: "multigres.com/v1alpha1",
1058+
Kind: "Shard",
1059+
Name: "test-shard",
1060+
UID: "test-uid",
1061+
Controller: ptr.To(true),
1062+
BlockOwnerDeletion: ptr.To(true),
1063+
},
1064+
},
1065+
},
1066+
Spec: corev1.PersistentVolumeClaimSpec{
1067+
StorageClassName: ptr.To("backup-nfs"),
1068+
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany},
1069+
Resources: corev1.VolumeResourceRequirements{
1070+
Requests: corev1.ResourceList{
1071+
corev1.ResourceStorage: resource.MustParse("100Gi"),
1072+
},
1073+
},
1074+
},
1075+
},
1076+
},
1077+
}
1078+
1079+
for name, tc := range tests {
1080+
t.Run(name, func(t *testing.T) {
1081+
testScheme := scheme
1082+
if name == "error on controller reference" {
1083+
testScheme = runtime.NewScheme() // Empty scheme triggers SetControllerReference error
1084+
}
1085+
1086+
got, err := BuildBackupPVC(
1087+
shard,
1088+
"primary",
1089+
"zone1",
1090+
tc.poolSpec,
1091+
testScheme,
1092+
)
1093+
1094+
if name == "error on controller reference" {
1095+
if err == nil {
1096+
t.Error("BuildBackupPVC() expected error, got nil")
1097+
}
1098+
return
1099+
}
1100+
1101+
if err != nil {
1102+
t.Errorf("BuildBackupPVC() error = %v", err)
1103+
return
1104+
}
1105+
1106+
if diff := cmp.Diff(tc.want, got); diff != "" {
1107+
t.Errorf("BuildBackupPVC() mismatch (-want +got):\n%s", diff)
1108+
}
1109+
})
1110+
}
1111+
}
1112+
1113+
func TestBuildBackupPVC_Error(t *testing.T) {
1114+
// Separate test for error case to avoid messing with table driven test logic too much
1115+
shard := &multigresv1alpha1.Shard{
1116+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
1117+
}
1118+
// Empty scheme causes SetControllerReference to fail
1119+
_, err := BuildBackupPVC(shard, "pool", "cell", multigresv1alpha1.PoolSpec{}, runtime.NewScheme())
1120+
if err == nil {
1121+
t.Error("BuildBackupPVC() expected error with empty scheme, got nil")
1122+
}
1123+
}

pkg/resource-handler/controller/shard/shard_controller_internal_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ func TestReconcile_InvalidScheme(t *testing.T) {
188188
return r.reconcilePoolHeadlessService(ctx, shard, "pool1", "", poolSpec)
189189
},
190190
},
191+
"PoolBackupPVC": {
192+
setupShard: func() *multigresv1alpha1.Shard {
193+
return &multigresv1alpha1.Shard{
194+
ObjectMeta: metav1.ObjectMeta{
195+
Name: "test-shard",
196+
Namespace: "default",
197+
},
198+
}
199+
},
200+
reconcileFunc: func(r *ShardReconciler, ctx context.Context, shard *multigresv1alpha1.Shard) error {
201+
poolSpec := multigresv1alpha1.PoolSpec{
202+
Cells: []multigresv1alpha1.CellName{"cell1"},
203+
}
204+
return r.reconcilePoolBackupPVC(ctx, shard, "pool1", "cell1", poolSpec)
205+
},
206+
},
191207
}
192208

193209
for name, tc := range tests {

0 commit comments

Comments
 (0)