Skip to content

Commit

Permalink
integration tests for Cassandra User + Cassandra cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
ribaraka authored and testisnullus committed Aug 30, 2023
1 parent f9f73a0 commit e38b77d
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 120 deletions.
2 changes: 1 addition & 1 deletion controllers/clusterresources/cassandrauser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (r *CassandraUserReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

if event == models.DeletingEvent {
l.Info("Deleting user", "user", u, "cluster ID", clusterID)
l.Info("Deleting user from a cluster", "cluster ID", clusterID)

err = r.API.DeleteUser(username, clusterID, instaclustr.CassandraBundleUser)
if err != nil {
Expand Down
34 changes: 16 additions & 18 deletions controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,7 @@ func (r *CassandraReconciler) handleDeleteCluster(
"cluster name", cassandra.Spec.Name,
"cluster ID", cassandra.Status.ID,
"kind", cassandra.Kind,
"api Version", cassandra.APIVersion,
"namespace", cassandra.Namespace,
)
"api Version", cassandra.APIVersion)

r.EventRecorder.Eventf(
cassandra, models.Normal, models.Deleted,
Expand All @@ -627,7 +625,7 @@ func (r *CassandraReconciler) handleUsersCreate(
err := r.Get(ctx, req, u)
if err != nil {
if k8serrors.IsNotFound(err) {
l.Error(err, "Cassandra user is not found", "request", req)
l.Error(err, "Cannot create a Cassandra user. The resource is not found", "request", req)
r.EventRecorder.Eventf(c, models.Warning, models.NotFound,
"User is not found, create a new one Cassandra User or provide correct userRef."+
"Current provided reference: %v", uRef)
Expand Down Expand Up @@ -686,24 +684,23 @@ func (r *CassandraReconciler) handleUsersDelete(
err := r.Get(ctx, req, u)
if err != nil {
if k8serrors.IsNotFound(err) {
l.Error(err, "Cassandra user is not found", "request", req)
l.Error(err, "Cannot delete a Cassandra user, the user is not found", "request", req)
r.EventRecorder.Eventf(c, models.Warning, models.NotFound,
"User is not found, create a new one Cassandra User or provide correct userRef."+
"Current provided reference: %v", uRef)
return err
"Cannot delete a Cassandra user, the user %v is not found", req)
return nil
}

l.Error(err, "Cannot get Cassandra user", "user", u.Spec)
l.Error(err, "Cannot get Cassandra user", "user", req)
r.EventRecorder.Eventf(c, models.Warning, models.DeletionFailed,
"Cannot get Cassandra user. user reference: %v", uRef)
"Cannot get Cassandra user. user reference: %v", req)
return err
}

if _, exist := u.Status.ClustersEvents[c.Status.ID]; !exist {
l.Info("User is not existing on the cluster",
"user reference", uRef)
r.EventRecorder.Eventf(c, models.Normal, models.DeletionFailed,
"User is not existing on the cluster. User reference: %v", uRef)
"User is not existing on the cluster. User reference: %v", req)

return nil
}
Expand All @@ -721,7 +718,9 @@ func (r *CassandraReconciler) handleUsersDelete(
return err
}

l.Info("User has been added to the queue for deletion", "username", u.Name)
l.Info("User has been added to the queue for deletion",
"User resource", u.Namespace+"/"+u.Name,
"Cassandra resource", c.Namespace+"/"+c.Name)

return nil
}
Expand All @@ -741,16 +740,15 @@ func (r *CassandraReconciler) handleUsersDetach(
err := r.Get(ctx, req, u)
if err != nil {
if k8serrors.IsNotFound(err) {
l.Error(err, "Cassandra user is not found", "request", req)
l.Error(err, "Cannot detach a Cassandra user, the user is not found", "request", req)
r.EventRecorder.Eventf(c, models.Warning, models.NotFound,
"User resource is not found, please provide correct userRef."+
"Current provided reference: %v", uRef)
return err
"Cannot detach a Cassandra user, the user %v is not found", req)
return nil
}

l.Error(err, "Cannot get Cassandra user", "user", u.Spec)
l.Error(err, "Cannot get Cassandra user", "user", req)
r.EventRecorder.Eventf(c, models.Warning, models.DeletionFailed,
"Cannot get Cassandra user. user reference: %v", uRef)
"Cannot get Cassandra user. user reference: %v", req)
return err
}

Expand Down
61 changes: 27 additions & 34 deletions controllers/clusters/cassandra_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"

Expand All @@ -37,86 +36,80 @@ const newCassandraNodeSize = "CAS-DEV-t4g.small-30"

var _ = Describe("Cassandra Controller", func() {
var (
cassandraResource v1beta1.Cassandra
cassandraYAML v1beta1.Cassandra
c = "cassandra"
ns = "default"
cassandraNS = types.NamespacedName{Name: c, Namespace: ns}
timeout = time.Second * 40
interval = time.Second * 2
ns = "default"

cassandra v1beta1.Cassandra
cassandraManifest v1beta1.Cassandra

timeout = time.Second * 40
interval = time.Second * 2
)

yfile, err := os.ReadFile("datatest/cassandra_v1beta1.yaml")
Expect(err).NotTo(HaveOccurred())

err = yaml.Unmarshal(yfile, &cassandraYAML)
err = yaml.Unmarshal(yfile, &cassandraManifest)
Expect(err).NotTo(HaveOccurred())

cassandraObjMeta := metav1.ObjectMeta{
Name: c,
Namespace: ns,
Annotations: map[string]string{
models.ResourceStateAnnotation: models.CreatingEvent,
},
}

cassandraYAML.ObjectMeta = cassandraObjMeta

ctx := context.Background()

clusterID := cassandraManifest.Spec.Name + openapi.CreatedID
cassandraNamespacedName := types.NamespacedName{Name: cassandraManifest.ObjectMeta.Name, Namespace: ns}

When("apply a cassandra manifest", func() {
It("should create a cassandra resources", func() {
Expect(k8sClient.Create(ctx, &cassandraYAML)).Should(Succeed())
Expect(k8sClient.Create(ctx, &cassandraManifest)).Should(Succeed())
By("sending cassandra specification to the Instaclustr API and get ID of created cluster.")

Eventually(func() bool {
if err := k8sClient.Get(ctx, cassandraNS, &cassandraResource); err != nil {

if err := k8sClient.Get(ctx, cassandraNamespacedName, &cassandra); err != nil {
return false
}

return cassandraResource.Status.ID == openapi.CreatedID
return cassandra.Status.ID == clusterID
}).Should(BeTrue())
})
})

When("changing a node size", func() {
It("should update a cassandra resources", func() {
Expect(k8sClient.Get(ctx, cassandraNS, &cassandraResource)).Should(Succeed())
patch := cassandraResource.NewPatch()
Expect(k8sClient.Get(ctx, cassandraNamespacedName, &cassandra)).Should(Succeed())
patch := cassandra.NewPatch()

cassandraResource.Spec.DataCentres[0].NodeSize = newCassandraNodeSize
cassandra.Spec.DataCentres[0].NodeSize = newCassandraNodeSize

cassandraResource.Annotations = map[string]string{models.ResourceStateAnnotation: models.UpdatingEvent}
Expect(k8sClient.Patch(ctx, &cassandraResource, patch)).Should(Succeed())
cassandra.Annotations = map[string]string{models.ResourceStateAnnotation: models.UpdatingEvent}
Expect(k8sClient.Patch(ctx, &cassandra, patch)).Should(Succeed())

By("sending a resize request to the Instaclustr API. And when the resize is completed, " +
"the status job get new data from the InstAPI and update it in k8s cassandra resource")

Eventually(func() bool {
if err := k8sClient.Get(ctx, cassandraNS, &cassandraResource); err != nil {
if err := k8sClient.Get(ctx, cassandraNamespacedName, &cassandra); err != nil {
return false
}

if len(cassandraResource.Status.DataCentres) == 0 || len(cassandraResource.Status.DataCentres[0].Nodes) == 0 {
if len(cassandra.Status.DataCentres) == 0 || len(cassandra.Status.DataCentres[0].Nodes) == 0 {
return false
}

return cassandraResource.Status.DataCentres[0].Nodes[0].Size == newCassandraNodeSize
return cassandra.Status.DataCentres[0].Nodes[0].Size == newCassandraNodeSize
}, timeout, interval).Should(BeTrue())
})
})

When("delete the cassandra resource", func() {
It("should send delete request to the Instaclustr API", func() {
Expect(k8sClient.Get(ctx, cassandraNS, &cassandraResource)).Should(Succeed())
Expect(k8sClient.Get(ctx, cassandraNamespacedName, &cassandra)).Should(Succeed())

cassandraResource.Annotations = map[string]string{models.ResourceStateAnnotation: models.DeletingEvent}
cassandra.Annotations = map[string]string{models.ResourceStateAnnotation: models.DeletingEvent}

Expect(k8sClient.Delete(ctx, &cassandraResource)).Should(Succeed())
Expect(k8sClient.Delete(ctx, &cassandra)).Should(Succeed())

By("sending delete request to Instaclustr API")
Eventually(func() bool {
err := k8sClient.Get(ctx, cassandraNS, &cassandraResource)
err := k8sClient.Get(ctx, cassandraNamespacedName, &cassandra)
if err != nil && !k8serrors.IsNotFound(err) {
return false
}
Expand Down
3 changes: 3 additions & 0 deletions controllers/clusters/datatest/cassandra_v1beta1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ apiVersion: clusters.instaclustr.com/v1beta1
kind: Cassandra
metadata:
name: cassandra-cluster
namespace: default
annotations:
defaulter: webhook
spec:
name: "Cassandra"
version: "3.11.13"
Expand Down
Loading

0 comments on commit e38b77d

Please sign in to comment.