diff --git a/go.mod b/go.mod index 744df66b848..c36e22e77ab 100644 --- a/go.mod +++ b/go.mod @@ -60,6 +60,7 @@ require ( github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 github.com/onsi/ginkgo/v2 v2.11.0 github.com/onsi/gomega v1.27.8 + github.com/opencontainers/image-spec v1.1.0-rc5 github.com/pashagolub/pgxmock/v2 v2.11.0 github.com/pkg/errors v0.9.1 github.com/pmezard/go-difflib v1.0.0 @@ -310,7 +311,6 @@ require ( github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect github.com/oklog/ulid v1.3.1 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect - github.com/opencontainers/image-spec v1.1.0-rc5 // indirect github.com/opencontainers/runc v1.1.7 // indirect github.com/opencontainers/runtime-spec v1.1.0-rc.3 // indirect github.com/opencontainers/selinux v1.11.0 // indirect diff --git a/internal/cli/cmd/cluster/create_test.go b/internal/cli/cmd/cluster/create_test.go index 4f4854e6445..fc0ff5ce74c 100644 --- a/internal/cli/cmd/cluster/create_test.go +++ b/internal/cli/cmd/cluster/create_test.go @@ -35,11 +35,13 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1" + "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1" "github.com/apecloud/kubeblocks/internal/class" "github.com/apecloud/kubeblocks/internal/cli/cluster" "github.com/apecloud/kubeblocks/internal/cli/testing" "github.com/apecloud/kubeblocks/internal/cli/types" "github.com/apecloud/kubeblocks/internal/cli/util" + "github.com/apecloud/kubeblocks/internal/dataprotection/utils/boolptr" viper "github.com/apecloud/kubeblocks/internal/viperx" ) @@ -467,18 +469,47 @@ var _ = Describe("create", func() { }) It("test build backup config", func() { + backupPolicyTemplate := testing.FakeBackupPolicyTemplate("backupPolicyTemplate-test", testing.ClusterDefName) + backupPolicy := appsv1alpha1.BackupPolicy{ + BackupMethods: []v1alpha1.BackupMethod{ + { + Name: "volume-snapshot", + SnapshotVolumes: boolptr.True(), + }, + { + Name: "xtrabackup", + }, + }, + } + backupPolicyTemplate.Spec.BackupPolicies = append(backupPolicyTemplate.Spec.BackupPolicies, backupPolicy) + dynamic := testing.FakeDynamicClient(backupPolicyTemplate) + o := &CreateOptions{} o.Cmd = NewCreateCmd(o.Factory, o.IOStreams) + o.Dynamic = dynamic + o.ClusterDefRef = testing.ClusterDefName cluster := testing.FakeCluster("clusterName", testing.Namespace) By("test backup is not set") Expect(o.buildBackupConfig(cluster)).To(Succeed()) - By("test backup is with snapshot method") - o.BackupMethod = "snapshot" - Expect(o.Cmd.Flags().Set("backup-method", "snapshot")).To(Succeed()) + By("test backup enable") + o.BackupEnabled = true + Expect(o.Cmd.Flags().Set("backup-enabled", "true")).To(Succeed()) + Expect(o.buildBackupConfig(cluster)).To(Succeed()) + Expect(*o.BackupConfig.Enabled).Should(BeTrue()) + Expect(o.BackupConfig.Method).Should(Equal("volume-snapshot")) + + By("test backup with invalid method") + o.BackupMethod = "invalid-method" + Expect(o.Cmd.Flags().Set("backup-method", "invalid-method")).To(Succeed()) + Expect(o.buildBackupConfig(cluster)).To(HaveOccurred()) + + By("test backup with xtrabackup method") + o.BackupMethod = "xtrabackup" + Expect(o.Cmd.Flags().Set("backup-method", "xtrabackup")).To(Succeed()) Expect(o.buildBackupConfig(cluster)).To(Succeed()) - Expect(o.BackupConfig.Method).Should(Equal("snapshot")) + Expect(o.BackupConfig.Method).Should(Equal("xtrabackup")) By("test backup is with wrong cron expression") o.BackupCronExpression = "wrong-cron-expression" diff --git a/internal/cli/testing/fake.go b/internal/cli/testing/fake.go index bb7f2df29e3..fe06dd34e1a 100644 --- a/internal/cli/testing/fake.go +++ b/internal/cli/testing/fake.go @@ -454,6 +454,27 @@ func FakeBackupSchedule(backupScheduleName, backupPolicyName string) *dpv1alpha1 return backupSchedule } +func FakeBackupPolicyTemplate(backupPolicyTemplateName string, clusterDef string) *appsv1alpha1.BackupPolicyTemplate { + backupPolicyTemplate := &appsv1alpha1.BackupPolicyTemplate{ + TypeMeta: metav1.TypeMeta{ + APIVersion: fmt.Sprintf("%s/%s", types.AppsAPIGroup, types.AppsAPIVersion), + Kind: types.KindBackupPolicyTemplate, + }, + ObjectMeta: metav1.ObjectMeta{ + Name: backupPolicyTemplateName, + Namespace: Namespace, + Labels: map[string]string{ + constant.ClusterDefLabelKey: ClusterDefName, + }, + }, + Spec: appsv1alpha1.BackupPolicyTemplateSpec{ + ClusterDefRef: clusterDef, + Identifier: "fake-identifier", + }, + } + return backupPolicyTemplate +} + func FakeBackupWithCluster(cluster *appsv1alpha1.Cluster, backupName string) *dpv1alpha1.Backup { backup := &dpv1alpha1.Backup{ TypeMeta: metav1.TypeMeta{ diff --git a/internal/cli/types/types.go b/internal/cli/types/types.go index 6f94798376d..09c2b4c6ee8 100644 --- a/internal/cli/types/types.go +++ b/internal/cli/types/types.go @@ -107,6 +107,7 @@ const ( KindBackupPolicy = "BackupPolicy" KindOps = "OpsRequest" KindBackupSchedule = "BackupSchedule" + KindBackupPolicyTemplate = "BackupPolicyTemplate" ) // K8S rbac API group