Skip to content

Commit

Permalink
Refactored existing Cluster test-suite and added Backup test-suite
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekdwivedi3060 committed Jul 17, 2024
1 parent f12e4ca commit 3327e02
Show file tree
Hide file tree
Showing 52 changed files with 2,126 additions and 1,316 deletions.
29 changes: 25 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# # /bin/sh does not support source command needed in make test
# # /bin/sh does not support source command needed in make test-all
#SHELL := /bin/bash

ROOT_DIR=$(shell git rev-parse --show-toplevel)
Expand Down Expand Up @@ -140,10 +140,31 @@ $(GOLANGCI_LINT): $(LOCALBIN)
go-lint: golanci-lint ## Run golangci-lint against code.
$(GOLANGCI_LINT) run

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
.PHONY: all-test
all-test: manifests generate fmt vet envtest ## Run tests.
# KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" cd $(shell pwd)/test; go run github.com/onsi/ginkgo/v2/ginkgo -coverprofile cover.out -show-node-events -v -timeout=12h0m0s -focus=${FOCUS} --junit-report="junit.xml" -- ${ARGS}
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" cd $(shell pwd)/test; go run github.com/onsi/ginkgo/v2/ginkgo -r --keep-going -coverprofile cover.out -show-node-events -v -timeout=12h0m0s --junit-report="junit.xml" -- ${ARGS}

.PHONY: cluster-test
cluster-test: manifests generate fmt vet envtest ## Run tests.
# KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" cd $(shell pwd)/test; go run github.com/onsi/ginkgo/v2/ginkgo -v . ./cluster -coverprofile cover.out -show-node-events -timeout=12h0m0s --junit-report="junit.xml" -- ${ARGS}


.PHONY: backup-service-test
backup-service-test: manifests generate fmt vet envtest ## Run tests.
# KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" cd $(shell pwd)/test; go run github.com/onsi/ginkgo/v2/ginkgo -v . ./backup_service -coverprofile cover.out -show-node-events -timeout=12h0m0s --junit-report="junit.xml" -- ${ARGS}

.PHONY: backup-test
backup-test: manifests generate fmt vet envtest ## Run tests.
# KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" cd $(shell pwd)/test; go run github.com/onsi/ginkgo/v2/ginkgo -v . ./backup -coverprofile cover.out -show-node-events -timeout=12h0m0s --junit-report="junit.xml" -- ${ARGS}

.PHONY: restore-test
restore-test: manifests generate fmt vet envtest ## Run tests.
# KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" cd $(shell pwd)/test; go run github.com/onsi/ginkgo/v2/ginkgo -v . ./restore -coverprofile cover.out -show-node-events -timeout=12h0m0s --junit-report="junit.xml" -- ${ARGS}

##@ Build

Expand Down
5 changes: 3 additions & 2 deletions api/v1beta1/aerospikebackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ type OnDemandSpec struct {

// AerospikeBackupStatus defines the observed state of AerospikeBackup
type AerospikeBackupStatus struct {
OnDemand []OnDemandSpec `json:"onDemand,omitempty"`

BackupService *BackupService `json:"backupService"`
Config runtime.RawExtension `json:"config"`
OnDemand []OnDemandSpec `json:"onDemand,omitempty"`
// TODO: finalize the status and phase
}

Expand Down
10 changes: 9 additions & 1 deletion api/v1beta1/aerospikebackup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (r *AerospikeBackup) ValidateUpdate(old runtime.Object) (admission.Warnings

oldObj := old.(*AerospikeBackup)

if !reflect.DeepEqual(r.Spec.BackupService, oldObj.Spec.BackupService) {
return nil, fmt.Errorf("backup service cannot be updated")
}

if err := r.validateBackupConfig(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -155,6 +159,10 @@ func (r *AerospikeBackup) validateBackupConfig() error {
return err
}

if len(aeroClusters) != 1 {
return fmt.Errorf("only one aerospike cluster is allowed in backup config")
}

if len(config.AerospikeClusters) == 0 {
config.AerospikeClusters = make(map[string]*model.AerospikeCluster)
}
Expand Down Expand Up @@ -205,7 +213,7 @@ func (r *AerospikeBackup) validateBackupConfig() error {
}

if err := config.Validate(); err != nil {
return nil
return err
}

// Validate on-demand backup
Expand Down
6 changes: 6 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions config/crd/bases/asdb.aerospike.com_aerospikebackups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ spec:
status:
description: AerospikeBackupStatus defines the observed state of AerospikeBackup
properties:
backupService:
properties:
name:
description: Backup service name
type: string
namespace:
description: Backup service namespace
type: string
required:
- name
- namespace
type: object
config:
type: object
x-kubernetes-preserve-unknown-fields: true
onDemand:
items:
properties:
Expand All @@ -106,6 +121,9 @@ spec:
- routineName
type: object
type: array
required:
- backupService
- config
type: object
type: object
served: true
Expand Down
6 changes: 3 additions & 3 deletions config/samples/asdb_v1beta1_aerospikebackup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ spec:
backupService:
name: aerospikebackupservice-sample
namespace: aerospike
onDemand:
- id: first-ad-hoc-backup
routineName: test-routine
# onDemand:
# - id: first-ad-hoc-backup
# routineName: test-routine
config:
aerospike-cluster:
test-cluster:
Expand Down
20 changes: 18 additions & 2 deletions controllers/backup/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,22 @@ func (r *SingleBackupReconciler) ScheduleOnDemandBackup() error {
}

func (r *SingleBackupReconciler) reconcileBackup() error {
specHash, err := utils.GetHash(string(r.aeroBackup.Spec.Config.Raw))
if err != nil {
return err
}

statusHash, err := utils.GetHash(string(r.aeroBackup.Status.Config.Raw))
if err != nil {
return err
}

if specHash == statusHash {
r.Log.Info("Backup config not changed",
"name", r.aeroBackup.Name, "namespace", r.aeroBackup.Namespace)
return nil
}

r.Log.Info("Registering backup", "name", r.aeroBackup.Name, "namespace", r.aeroBackup.Namespace)

serviceClient, err := backup_service.GetBackupServiceClient(r.Client, r.aeroBackup.Spec.BackupService)
Expand Down Expand Up @@ -451,9 +467,9 @@ func (r *SingleBackupReconciler) unregisterBackup() error {
}

func (r *SingleBackupReconciler) updateStatus() error {
r.aeroBackup.Status.BackupService = r.aeroBackup.Spec.BackupService
r.aeroBackup.Status.Config = r.aeroBackup.Spec.Config
r.aeroBackup.Status.OnDemand = r.aeroBackup.Spec.OnDemand

r.Log.Info(fmt.Sprintf("Updating status: %+v", r.aeroBackup.Status))

return r.Client.Status().Update(context.Background(), r.aeroBackup)
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ spec:
status:
description: AerospikeBackupStatus defines the observed state of AerospikeBackup
properties:
backupService:
properties:
name:
description: Backup service name
type: string
namespace:
description: Backup service namespace
type: string
required:
- name
- namespace
type: object
config:
type: object
x-kubernetes-preserve-unknown-fields: true
onDemand:
items:
properties:
Expand All @@ -106,6 +121,9 @@ spec:
- routineName
type: object
type: array
required:
- backupService
- config
type: object
type: object
served: true
Expand Down
96 changes: 96 additions & 0 deletions test/backup/backup_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package backup

import (
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8Runtime "k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

asdbv1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1"
asdbv1beta1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1beta1"
"github.com/aerospike/aerospike-kubernetes-operator/test"
backupservice "github.com/aerospike/aerospike-kubernetes-operator/test/backup_service"
"github.com/aerospike/aerospike-kubernetes-operator/test/cluster"
)

var testEnv *envtest.Environment

var k8sClient client.Client

var scheme = k8Runtime.NewScheme()

func TestBackup(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Backup Suite")
}

var _ = BeforeSuite(
func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("Bootstrapping test environment")
var err error

testEnv, _, k8sClient, _, err = test.BootStrapTestEnv(scheme)
Expect(err).NotTo(HaveOccurred())

By("Deploy Backup Service")
backupService, err := backupservice.NewBackupService()
Expect(err).ToNot(HaveOccurred())

backupService.Spec.Service = &asdbv1beta1.Service{
Type: corev1.ServiceTypeLoadBalancer,
}

backupServiceName = backupService.Name
backupServiceNamespace = backupService.Namespace

err = backupservice.DeployBackupService(k8sClient, backupService)
Expect(err).ToNot(HaveOccurred())

By("Deploy Aerospike Cluster")
aeroCluster := cluster.CreateDummyAerospikeCluster(aerospikeNsNm, 2)

err = cluster.DeployCluster(k8sClient, testCtx, aeroCluster)
Expect(err).ToNot(HaveOccurred())
})

var _ = AfterSuite(
func() {
By("Delete Aerospike Cluster")
aeroCluster := asdbv1.AerospikeCluster{
ObjectMeta: metav1.ObjectMeta{
Name: aerospikeNsNm.Name,
Namespace: aerospikeNsNm.Namespace,
},
}

err := cluster.DeleteCluster(k8sClient, testCtx, &aeroCluster)
Expect(err).ToNot(HaveOccurred())

By("Delete Backup Service")
backupService := asdbv1beta1.AerospikeBackupService{
ObjectMeta: metav1.ObjectMeta{
Name: backupServiceName,
Namespace: backupServiceNamespace,
},
}

err = backupservice.DeleteBackupService(k8sClient, &backupService)
Expect(err).ToNot(HaveOccurred())

By("tearing down the test environment")
gexec.KillAndWait(5 * time.Second)
err = testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
},
)
Loading

0 comments on commit 3327e02

Please sign in to comment.