Skip to content

Commit

Permalink
Added Backup Service test-suite
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekdwivedi3060 committed Jul 11, 2024
1 parent e667d35 commit fc74de5
Show file tree
Hide file tree
Showing 11 changed files with 572 additions and 17 deletions.
5 changes: 2 additions & 3 deletions api/v1beta1/aerospikebackup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilRuntime "k8s.io/apimachinery/pkg/util/runtime"
clientGoScheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
Expand Down
3 changes: 2 additions & 1 deletion api/v1beta1/aerospikebackupservice_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ limitations under the License.
package v1beta1

import (
"github.com/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/yaml"

"github.com/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
)

// log is for logging in this package.
Expand Down
3 changes: 2 additions & 1 deletion api/v1beta1/aerospikerestore_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import (
"reflect"
"time"

"github.com/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/yaml"

"github.com/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
)

// log is for logging in this package.
Expand Down
4 changes: 2 additions & 2 deletions config/samples/asdb_v1beta1_aerospikebackupservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ metadata:
name: aerospikebackupservice-sample
namespace: aerospike
spec:
image: aerospike.jfrog.io/ecosystem-container-prod-local/aerospike-backup-service:0.3.0
image: aerospike.jfrog.io/ecosystem-container-prod-local/aerospike-backup-service:1.0.0
config:
service:
http:
Expand All @@ -27,7 +27,7 @@ spec:
storage:
local:
path: /localStorage
type: 0
type: local
secrets:
- secretName: auth-secret
volumeMount:
Expand Down
18 changes: 11 additions & 7 deletions controllers/backup-service/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (r *SingleBackupServiceReconciler) Reconcile() (result ctrl.Result, recErr
}
}()

if !r.aeroBackupService.ObjectMeta.DeletionTimestamp.IsZero() {
// Stop reconciliation as the Aerospike Backup service is being deleted
return reconcile.Result{}, nil
}

// Set the status to AerospikeClusterInProgress before starting any operations
if err := r.setStatusPhase(asdbv1beta1.AerospikeBackupServiceInProgress); err != nil {
return reconcile.Result{}, err
Expand Down Expand Up @@ -273,11 +278,9 @@ func (r *SingleBackupServiceReconciler) reconcileDeployment() error {
return err
}
}

return r.waitForDeploymentToBeReady()
}

return nil
return r.waitForDeploymentToBeReady()
}

func getBackupServiceName(aeroBackupService *asdbv1beta1.AerospikeBackupService) types.NamespacedName {
Expand Down Expand Up @@ -568,7 +571,7 @@ func (r *SingleBackupServiceReconciler) getBackupServiceConfig() (*serviceConfig
func (r *SingleBackupServiceReconciler) waitForDeploymentToBeReady() error {
const (
podStatusTimeout = 2 * time.Minute
podStatusRetryInterval = time.Second * 5
podStatusRetryInterval = 5 * time.Second
)

r.Log.Info(
Expand All @@ -583,7 +586,8 @@ func (r *SingleBackupServiceReconciler) waitForDeploymentToBeReady() error {
}

if len(podList.Items) == 0 {
return false, fmt.Errorf("no pod found for deployment")
r.Log.Info("No pod found for deployment")
return false, nil
}

for idx := range podList.Items {
Expand All @@ -609,7 +613,7 @@ func (r *SingleBackupServiceReconciler) waitForDeploymentToBeReady() error {
}

if deploy.Status.Replicas != *deploy.Spec.Replicas {
return false, fmt.Errorf("deployment status is not updated")
return false, nil
}

return true, nil
Expand All @@ -628,7 +632,7 @@ func (r *SingleBackupServiceReconciler) setStatusPhase(phase asdbv1beta1.Aerospi
r.aeroBackupService.Status.Phase = phase

if err := r.Client.Status().Update(context.Background(), r.aeroBackupService); err != nil {
r.Log.Error(err, fmt.Sprintf("Failed to set restore status to %s", phase))
r.Log.Error(err, fmt.Sprintf("Failed to set backup service status to %s", phase))
return err
}
}
Expand Down
5 changes: 5 additions & 0 deletions controllers/restore/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type SingleRestoreReconciler struct {
}

func (r *SingleRestoreReconciler) Reconcile() (result ctrl.Result, recErr error) {
if !r.aeroRestore.ObjectMeta.DeletionTimestamp.IsZero() {
// Stop reconciliation as the Aerospike restore is being deleted
return reconcile.Result{}, nil
}

if err := r.setStatusPhase(asdbv1beta1.AerospikeRestoreInProgress); err != nil {
return ctrl.Result{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
)

require (
github.com/abhishekdwivedi3060/aerospike-backup-service v0.0.0-20240604175608-042db47ee091
github.com/abhishekdwivedi3060/aerospike-backup-service v0.0.0-20240709182036-38038c5c38c7
github.com/aerospike/aerospike-client-go/v7 v7.4.0
github.com/deckarep/golang-set/v2 v2.3.1
github.com/sirupsen/logrus v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/abhishekdwivedi3060/aerospike-backup-service v0.0.0-20240604175608-042db47ee091 h1:eY5MT18Ocf5cCHohbZ1qcdophb/aOqr066lwSM3zGPk=
github.com/abhishekdwivedi3060/aerospike-backup-service v0.0.0-20240604175608-042db47ee091/go.mod h1:CMA+bHRLvL/Kj/aLlbu95iNnlPnvP67q62X81b5e2G4=
github.com/abhishekdwivedi3060/aerospike-backup-service v0.0.0-20240709182036-38038c5c38c7 h1:BP+LMehEXEsUE2HGT/Wn1ib0ZarBLHjjzg+x6XGIJ1s=
github.com/abhishekdwivedi3060/aerospike-backup-service v0.0.0-20240709182036-38038c5c38c7/go.mod h1:CMA+bHRLvL/Kj/aLlbu95iNnlPnvP67q62X81b5e2G4=
github.com/aerospike/aerospike-client-go/v7 v7.4.0 h1:g8/7v8RHhQhTArhW3C7Au7o+u8j8x5eySZL6MXfpHKU=
github.com/aerospike/aerospike-client-go/v7 v7.4.0/go.mod h1:pPKnWiS8VDJcH4IeB1b8SA2TWnkjcVLHwAAJ+BHfGK8=
github.com/aerospike/aerospike-management-lib v1.4.0 h1:wT0l3kwzXv5DV5Cd+hD0BQq3hjSIyaPX1HaUb1304TI=
Expand Down
106 changes: 106 additions & 0 deletions test/backup_service/backup_service_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package backupservice

import (
"context"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8Runtime "k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-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/controllers/common"
)

var cfg *rest.Config

var testEnv *envtest.Environment

var k8sClient client.Client

var scheme = k8Runtime.NewScheme()

var testCtx = context.TODO()

var pkgLog = ctrl.Log.WithName("test")

const (
name = "backup-service"
namespace = "test"
)

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

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

By("Bootstrapping test environment")
t := true
testEnv = &envtest.Environment{
UseExistingCluster: &t,
}
var err error

cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = clientgoscheme.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = asdbv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = asdbv1beta1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(
cfg, client.Options{Scheme: scheme},
)
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

sa := corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: common.AerospikeBackupService,
Namespace: namespace,
},
}

err = k8sClient.Create(testCtx, &sa)
if err != nil && !errors.IsAlreadyExists(err) {
Fail(err.Error())
}
})

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

0 comments on commit fc74de5

Please sign in to comment.