Skip to content

Commit

Permalink
Added restore suite
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekdwivedi3060 committed Jul 31, 2024
1 parent 439cfd3 commit 96fb195
Show file tree
Hide file tree
Showing 11 changed files with 727 additions and 78 deletions.
19 changes: 19 additions & 0 deletions api/v1beta1/aerospikerestore_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
"github.com/aerospike/aerospike-kubernetes-operator/controllers/common"
)

// log is for logging in this package.
Expand Down Expand Up @@ -94,10 +95,24 @@ func (r *AerospikeRestore) ValidateDelete() (admission.Warnings, error) {
}

func (r *AerospikeRestore) validateRestoreConfig() error {
restoreConfigInMap := make(map[string]interface{})

if err := yaml.Unmarshal(r.Spec.Config.Raw, &restoreConfigInMap); err != nil {
return err
}

switch r.Spec.Type {
case Full, Incremental:
var restoreRequest model.RestoreRequest

if _, ok := restoreConfigInMap[common.RoutineKey]; ok {
return fmt.Errorf("routine key is not allowed in restore config for restore type %s", r.Spec.Type)
}

if _, ok := restoreConfigInMap[common.TimeKey]; ok {
return fmt.Errorf("time key is not allowed in restore config for restore type %s", r.Spec.Type)
}

if err := yaml.Unmarshal(r.Spec.Config.Raw, &restoreRequest); err != nil {
return err
}
Expand All @@ -107,6 +122,10 @@ func (r *AerospikeRestore) validateRestoreConfig() error {
case TimeStamp:
var restoreRequest model.RestoreTimestampRequest

if _, ok := restoreConfigInMap[common.SourceKey]; ok {
return fmt.Errorf("source key is not allowed in restore config for restore type %s", r.Spec.Type)
}

if err := yaml.Unmarshal(r.Spec.Config.Raw, &restoreRequest); err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions config/samples/aerospikerestore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ spec:
no-generation: true
no-indexes: true
source:
"path": "/localStorage/test-routine/backup/1719911170537/data/test"
"type": 0

"path": "/localStorage/aerospike-aerospikebackup-test-routine/backup/1722326391329/data/test"
"type": local
7 changes: 7 additions & 0 deletions controllers/common/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const (
BackupServiceConfigYAML = "aerospike-backup-service.yml"
)

// Restore config fields
const (
RoutineKey = "routine"
TimeKey = "time"
SourceKey = "source"
)

const (
HTTPKey = "http"
AerospikeBackupService = "aerospike-backup-service"
Expand Down
2 changes: 1 addition & 1 deletion pkg/backup-service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func (c *Client) GetFullBackups() (map[string][]interface{}, error) {
return backups, nil
}

func (c *Client) GetFullBackupForRoutine(routineName string) ([]interface{}, error) {
func (c *Client) GetFullBackupsForRoutine(routineName string) ([]interface{}, error) {
url := c.API(fmt.Sprintf("/backups/full/%s", routineName))

resp, err := http.Get(url)
Expand Down
3 changes: 3 additions & 0 deletions test/backup/backup_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ var _ = BeforeSuite(
Expect(err).ToNot(HaveOccurred())

By("Deploy Aerospike Cluster")
cascadeDeleteTrue := true
aeroCluster := cluster.CreateDummyAerospikeCluster(aerospikeNsNm, 2)
aeroCluster.Spec.Storage.BlockVolumePolicy.InputCascadeDelete = &cascadeDeleteTrue
aeroCluster.Spec.Storage.FileSystemVolumePolicy.InputCascadeDelete = &cascadeDeleteTrue

err = cluster.DeployCluster(k8sClient, testCtx, aeroCluster)
Expect(err).ToNot(HaveOccurred())
Expand Down
70 changes: 35 additions & 35 deletions test/backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ var _ = Describe(
)

AfterEach(func() {
Expect(deleteBackup(k8sClient, backup)).ToNot(HaveOccurred())
Expect(DeleteBackup(k8sClient, backup)).ToNot(HaveOccurred())
})

Context(
"When doing Invalid operations", func() {
It("Should fail when wrong format backup config is given", func() {
backup, err = newBackup(backupNsNm)
backup, err = NewBackup(backupNsNm)
Expect(err).ToNot(HaveOccurred())

badConfig, gErr := getWrongBackupConfBytes(namePrefix(backupNsNm))
Expect(gErr).ToNot(HaveOccurred())
backup.Spec.Config.Raw = badConfig

err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
})

Expand All @@ -48,7 +48,7 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("name should start with %s", namePrefix(backupNsNm)))
})
Expand All @@ -63,12 +63,12 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
})

It("Should fail when on-demand backup is given at the time of creation", func() {
backup, err = newBackup(backupNsNm)
backup, err = NewBackup(backupNsNm)
Expect(err).ToNot(HaveOccurred())

backup.Spec.OnDemandBackups = []asdbv1beta1.OnDemandBackupSpec{
Expand All @@ -78,15 +78,15 @@ var _ = Describe(
},
}

err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
})

It("Should fail when non-existing routine is given in on-demand backup", func() {
backup, err = newBackup(backupNsNm)
backup, err = NewBackup(backupNsNm)
Expect(err).ToNot(HaveOccurred())

err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

backup, err = getBackupObj(k8sClient, backup.Name, backup.Namespace)
Expand All @@ -104,20 +104,20 @@ var _ = Describe(
})

It("Should fail when backup service is not present", func() {
backup, err = newBackup(backupNsNm)
backup, err = NewBackup(backupNsNm)
Expect(err).ToNot(HaveOccurred())

backup.Spec.BackupService.Name = "wrong-backup-service"

err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
})

It("Should fail when backup service reference is updated", func() {
backup, err = newBackup(backupNsNm)
backup, err = NewBackup(backupNsNm)
Expect(err).ToNot(HaveOccurred())

err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

backup, err = getBackupObj(k8sClient, backup.Name, backup.Namespace)
Expand All @@ -140,7 +140,7 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
})

Expand All @@ -155,7 +155,7 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
})

Expand All @@ -170,7 +170,7 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
})

Expand All @@ -185,7 +185,7 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
})

Expand All @@ -201,7 +201,7 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(
ContainSubstring("service field cannot be specified in backup config"))
Expand All @@ -221,7 +221,7 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(
ContainSubstring("backup-policies field cannot be specified in backup config"))
Expand All @@ -240,7 +240,7 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(
ContainSubstring("storage field cannot be specified in backup config"))
Expand All @@ -259,20 +259,20 @@ var _ = Describe(
Expect(mErr).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(
ContainSubstring("secret-agent field cannot be specified in backup config"))
})

It("Should fail when aerospike-cluster name is updated", func() {
backup, err = newBackup(backupNsNm)
backup, err = NewBackup(backupNsNm)
Expect(err).ToNot(HaveOccurred())

err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

err = validateTriggeredBackup(k8sClient, backupServiceName, backupServiceNamespace, backup)
err = validateTriggeredBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

backup, err = getBackupObj(k8sClient, backup.Name, backup.Namespace)
Expand All @@ -297,12 +297,12 @@ var _ = Describe(

Context("When doing Valid operations", func() {
It("Should trigger backup when correct backup config with local storage is given", func() {
backup, err = newBackup(backupNsNm)
backup, err = NewBackup(backupNsNm)
Expect(err).ToNot(HaveOccurred())
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

err = validateTriggeredBackup(k8sClient, backupServiceName, backupServiceNamespace, backup)
err = validateTriggeredBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

})
Expand All @@ -320,17 +320,17 @@ var _ = Describe(

backup = newBackupWithConfig(backupNsNm, configBytes)

err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

err = validateTriggeredBackup(k8sClient, backupServiceName, backupServiceNamespace, backup)
err = validateTriggeredBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())
})

It("Should trigger on-demand backup when given", func() {
backup, err = newBackup(backupNsNm)
backup, err = NewBackup(backupNsNm)
Expect(err).ToNot(HaveOccurred())
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

backup, err = getBackupObj(k8sClient, backup.Name, backup.Namespace)
Expand All @@ -346,7 +346,7 @@ var _ = Describe(
err = updateBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

err = validateTriggeredBackup(k8sClient, backupServiceName, backupServiceNamespace, backup)
err = validateTriggeredBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())
})

Expand All @@ -368,10 +368,10 @@ var _ = Describe(
Expect(err).ToNot(HaveOccurred())

backup = newBackupWithConfig(backupNsNm, configBytes)
err = deployBackup(k8sClient, backup)
err = CreateBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

err = validateTriggeredBackup(k8sClient, backupServiceName, backupServiceNamespace, backup)
err = validateTriggeredBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

backup, err = getBackupObj(k8sClient, backup.Name, backup.Namespace)
Expand All @@ -388,7 +388,7 @@ var _ = Describe(
err = updateBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())

err = validateTriggeredBackup(k8sClient, backupServiceName, backupServiceNamespace, backup)
err = validateTriggeredBackup(k8sClient, backup)
Expect(err).ToNot(HaveOccurred())
})

Expand Down
Loading

0 comments on commit 96fb195

Please sign in to comment.