Skip to content

Commit

Permalink
fix: testcase error
Browse files Browse the repository at this point in the history
  • Loading branch information
fengluodb committed Oct 12, 2023
1 parent 9510997 commit eaa8a03
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
39 changes: 35 additions & 4 deletions internal/cli/cmd/cluster/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"
Expand Down
21 changes: 21 additions & 0 deletions internal/cli/testing/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions internal/cli/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const (
KindBackupPolicy = "BackupPolicy"
KindOps = "OpsRequest"
KindBackupSchedule = "BackupSchedule"
KindBackupPolicyTemplate = "BackupPolicyTemplate"
)

// K8S rbac API group
Expand Down

0 comments on commit eaa8a03

Please sign in to comment.