Skip to content

Commit

Permalink
pkg: add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: BornChanger <[email protected]>
  • Loading branch information
BornChanger authored and ti-chi-bot committed Sep 27, 2023
1 parent d30627e commit 558fbed
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/backup/restore/restore_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func (rm *restoreManager) checkTiFlashAndTiKVReplicasFromBackupMeta(r *v1alpha1.
// Check volume number
if (tc.Spec.TiKV.StorageVolumes == nil && metaInfo.KubernetesMeta.TiDBCluster.Spec.TiKV.StorageVolumes != nil) ||
(tc.Spec.TiKV.StorageVolumes != nil && metaInfo.KubernetesMeta.TiDBCluster.Spec.TiKV.StorageVolumes == nil) {
klog.Errorf("additional volumes # not match. either tc or backup has no additional volumes, while the other has")
klog.Errorf("additional volumes # not match. either tc or backup metadata has no additional volumes, while the other has")
return fmt.Errorf("additional volumes mismatched")
} else if len(tc.Spec.TiKV.StorageVolumes) != len(metaInfo.KubernetesMeta.TiDBCluster.Spec.TiKV.StorageVolumes) {
klog.Errorf("additional volumes # not match. tc has %d, and backup has %d", len(tc.Spec.TiKV.StorageVolumes), len(metaInfo.KubernetesMeta.TiDBCluster.Spec.TiKV.StorageVolumes))
Expand Down
8 changes: 4 additions & 4 deletions pkg/backup/restore/restore_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,12 +649,12 @@ func TestVolumeNumMismatchBRRestoreByEBS(t *testing.T) {
}

// Verify invalid tc with mismatch tikv replicas
//generate the restore meta in local nfs, with only 2 tikv replicas
err := os.WriteFile("/tmp/restoremeta", []byte(testutils.ConstructRestore2TiKVMetaStr()), 0644) //nolint:gosec
//generate the restore meta in local nfs, with 3 volumes for each tikv
err := os.WriteFile("/tmp/restoremeta", []byte(testutils.ConstructRestoreTiKVVolumesMetaWithStr()), 0644) //nolint:gosec
g.Expect(err).To(Succeed())

//generate the backup meta in local nfs, tiflash check need backupmeta to validation
err = os.WriteFile("/tmp/backupmeta", []byte(testutils.ConstructRestore2TiKVMetaStr()), 0644) //nolint:gosec
err = os.WriteFile("/tmp/backupmeta", []byte(testutils.ConstructRestoreTiKVVolumesMetaWithStr()), 0644) //nolint:gosec
g.Expect(err).To(Succeed())
defer func() {
err = os.Remove("/tmp/restoremeta")
Expand All @@ -669,6 +669,6 @@ func TestVolumeNumMismatchBRRestoreByEBS(t *testing.T) {
helper.CreateRestore(cases[0].restore)
m := NewRestoreManager(deps)
err := m.Sync(cases[0].restore)
g.Expect(err).Should(MatchError("tikv replica mismatch"))
g.Expect(err).Should(MatchError("additional volumes mismatched"))
})
}
199 changes: 198 additions & 1 deletion pkg/backup/testutils/br.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,19 @@ func ConstructRestoreMetaStr() string {
"version": "",
"tikv": {
"maxFailoverCount": 0,
"replicas": 3
"replicas": 3,
"storageVolumes": [
{
"mountPath": "/var/lib/raft",
"name": "raft",
"storageSize": "50Gi"
},
{
"mountPath": "/var/lib/wal",
"name": "wal",
"storageSize": "50Gi"
}
]
}
},
"status": {
Expand Down Expand Up @@ -491,3 +503,188 @@ func ConstructRestore2TiKVMetaStr() string {
"options": null
}`
}

func ConstructRestoreTiKVVolumesMetaWithStr() string {
return `{
"tikv": {
"replicas": 3,
"stores": [{
"store_id": 1,
"volumes": [{
"volume_id": "vol-0e65f40961a9f6244",
"type": "",
"mount_path": "",
"snapshot_id": "snap-1234567890abcdef0",
"restore_volume_id": "vol-0e65f40961a9f0001"
}]
}, {
"store_id": 2,
"volumes": [{
"volume_id": "vol-0e65f40961a9f6245",
"type": "",
"mount_path": "",
"snapshot_id": "snap-1234567890abcdef1",
"restore_volume_id": "vol-0e65f40961a9f0002"
}]
}]
},
"pd": {
"replicas": 0
},
"tidb": {
"replicas": 0
},
"kubernetes": {
"pvcs": [{
"metadata": {
"name": "tikv-test-tikv-1",
"uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3121",
"resourceVersion": "1957",
"creationTimestamp": null,
"labels": {
"test/label": "retained"
},
"annotations": {
"pv.kubernetes.io/bind-completed": "yes",
"pv.kubernetes.io/bound-by-controller": "yes",
"test/annotation": "retained"
},
"finalizers": ["kubernetes.io/pvc-protection"]
},
"spec": {
"resources": {},
"volumeName": "pv-1"
},
"status": {
"phase": "Bound"
}
}, {
"metadata": {
"name": "tikv-test-tikv-2",
"uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3123",
"resourceVersion": "1959",
"creationTimestamp": null,
"labels": {
"test/label": "retained"
},
"annotations": {
"pv.kubernetes.io/bind-completed": "yes",
"pv.kubernetes.io/bound-by-controller": "yes",
"test/annotation": "retained"
},
"finalizers": ["kubernetes.io/pvc-protection"]
},
"spec": {
"resources": {},
"volumeName": "pv-2"
},
"status": {
"phase": "Bound"
}
}],
"pvs": [{
"metadata": {
"name": "pv-1",
"uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3122",
"resourceVersion": "1958",
"creationTimestamp": null,
"labels": {
"test/label": "retained"
},
"annotations": {
"pv.kubernetes.io/provisioned-by": "ebs.csi.aws.com",
"temporary/volume-id": "vol-0e65f40961a9f6244",
"test/annotation": "retained"
},
"finalizers": ["kubernetes.io/pv-protection"]
},
"spec": {
"csi": {
"driver": "ebs.csi.aws.com",
"volumeHandle": "vol-0e65f40961a9f6244",
"fsType": "ext4"
},
"claimRef": {
"name": "tikv-test-tikv-1",
"uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3121",
"resourceVersion": "1957"
}
},
"status": {
"phase": "Bound"
}
}, {
"metadata": {
"name": "pv-2",
"uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3124",
"resourceVersion": "1960",
"creationTimestamp": null,
"labels": {
"test/label": "retained"
},
"annotations": {
"pv.kubernetes.io/provisioned-by": "ebs.csi.aws.com",
"temporary/volume-id": "vol-0e65f40961a9f6245",
"test/annotation": "retained"
},
"finalizers": ["kubernetes.io/pv-protection"]
},
"spec": {
"csi": {
"driver": "ebs.csi.aws.com",
"volumeHandle": "vol-0e65f40961a9f6245",
"fsType": "ext4"
},
"claimRef": {
"name": "tikv-test-tikv-2",
"uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3123",
"resourceVersion": "1959"
}
},
"status": {
"phase": "Bound"
}
}],
"crd_tidb_cluster": {
"metadata": {
"name": "test",
"creationTimestamp": null
},
"spec": {
"discovery": {},
"version": "",
"tikv": {
"maxFailoverCount": 0,
"replicas": 3,
"storageVolumes": [
{
"mountPath": "/var/lib/raft",
"name": "raft",
"storageSize": "50Gi"
}
]
}
},
"status": {
"pd": {
"synced": false,
"leader": {
"name": "",
"id": "",
"clientURL": "",
"health": false,
"lastTransitionTime": null
}
},
"tikv": {},
"tidb": {},
"pump": {},
"tiflash": {},
"ticdc": {}
}
},
"options": null
},
"options": null
}`
}
4 changes: 4 additions & 0 deletions pkg/backup/testutils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func (h *Helper) CreateTC(namespace, clusterName string, acrossK8s, recoverMode
TiKV: &v1alpha1.TiKVSpec{
BaseImage: "pingcap/tikv",
Replicas: 3,
StorageVolumes: []v1alpha1.StorageVolume{
{MountPath: "/var/lib/raft", Name: "raft", StorageSize: "50Gi"},
{MountPath: "/var/lib/wal", Name: "wal", StorageSize: "50Gi"},
},
},
TiDB: &v1alpha1.TiDBSpec{
TLSClient: &v1alpha1.TiDBTLSClient{Enabled: true},
Expand Down

0 comments on commit 558fbed

Please sign in to comment.