Skip to content

Commit

Permalink
added cassandra user creation/deletion integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
ribaraka authored and testisnullus committed Aug 9, 2023
1 parent 4f54849 commit 3f914d3
Show file tree
Hide file tree
Showing 23 changed files with 1,164 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ test-clusterresources:
test-kafkamanagement:
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./controllers/kafkamanagement -coverprofile cover.out

.PHONY: test-users
test-users:
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./controllers/tests

.PHONY: test
test: manifests generate fmt vet docker-build-server-stub run-server-stub envtest test-clusters test-clusterresources test-kafkamanagement stop-server-stub
test: manifests generate fmt vet docker-build-server-stub run-server-stub envtest test-clusters test-clusterresources test-kafkamanagement test-users stop-server-stub

.PHONY: goimports
goimports:
Expand Down
6 changes: 5 additions & 1 deletion controllers/clusterresources/cassandrauser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func (r *CassandraUserReconciler) Reconcile(ctx context.Context, req ctrl.Reques

for clusterID, event := range u.Status.ClustersEvents {
if event == models.CreatingEvent {
l.Info("Creating user", "user", u, "cluster ID", clusterID)

err = r.API.CreateUser(u.ToInstAPI(username, password), clusterID, instaclustr.CassandraBundleUser)
if err != nil {
l.Error(err, "Cannot create a user for the Cassandra cluster",
Expand All @@ -146,7 +148,7 @@ func (r *CassandraUserReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return models.ReconcileRequeue, nil
}

l.Info("User has been created", "username", username)
l.Info("User has been created", "username", username, "cluster ID", clusterID)
r.EventRecorder.Eventf(u, models.Normal, models.Created,
"User has been created for a cluster. Cluster ID: %s, username: %s",
clusterID, username)
Expand All @@ -167,6 +169,8 @@ func (r *CassandraUserReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

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

err = r.API.DeleteUser(username, clusterID, instaclustr.CassandraBundleUser)
if err != nil {
l.Error(err, "Cannot delete Cassandra user")
Expand Down
4 changes: 4 additions & 0 deletions controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ func (r *CassandraReconciler) handleUsersCreate(
return err
}

l.Info("User has been added to the queue for creation", "username", u.Name)

return nil
}

Expand Down Expand Up @@ -692,6 +694,8 @@ func (r *CassandraReconciler) handleUsersDelete(
return err
}

l.Info("User has been added to the queue for deletion", "username", u.Name)

return nil
}

Expand Down
2 changes: 0 additions & 2 deletions controllers/clusters/datatest/postgresql_v1beta1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ metadata:
testAnnotation: test
spec:
name: "testPostgre"
clusterConfigurations:
idle_in_transaction_session_timeout: "1"
version: "14.5.0"
dataCentres:
- region: "US_EAST_1"
Expand Down
191 changes: 191 additions & 0 deletions controllers/tests/cassandra_plus_users_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tests

import (
"context"
"os"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/utils/strings/slices"

clusterresource "github.com/instaclustr/operator/apis/clusterresources/v1beta1"
"github.com/instaclustr/operator/apis/clusters/v1beta1"
openapi "github.com/instaclustr/operator/pkg/instaclustr/mock/server/go"
"github.com/instaclustr/operator/pkg/models"
)

var _ = Describe("Basic Cassandra User controller + Basic Cassandra cluster controllers flow", func() {
var (
ns = "default"

user clusterresource.CassandraUser
userManifest clusterresource.CassandraUser

secret v1.Secret
secretManifest v1.Secret

cassandra v1beta1.Cassandra
cassandraManifest v1beta1.Cassandra

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

ctx := context.Background()

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

err = yaml.Unmarshal(cassandraUserYAML, &userManifest)
Expect(err).NotTo(HaveOccurred())

cassandraUserNS := types.NamespacedName{Name: userManifest.ObjectMeta.Name, Namespace: ns}

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

err = yaml.Unmarshal(secretYAML, &secretManifest)
Expect(err).NotTo(HaveOccurred())

secretNS := types.NamespacedName{Name: secretManifest.ObjectMeta.Name, Namespace: ns}

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

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

cassandraNS := types.NamespacedName{Name: cassandraManifest.ObjectMeta.Name, Namespace: ns}

When("apply a secret and a cassandra user manifests", func() {
It("should create both resources and they've got to have a link with themselves through finalizer", func() {
Expect(k8sClient.Create(ctx, &secretManifest)).Should(Succeed())
Expect(k8sClient.Create(ctx, &userManifest)).Should(Succeed())

Eventually(func() bool {
if err := k8sClient.Get(ctx, cassandraUserNS, &user); err != nil {
return false
}

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

if user.Finalizers == nil {
return false
}

uniqFinalizer := user.GetDeletionFinalizer()

return slices.Contains(user.Finalizers, uniqFinalizer) || slices.Contains(secret.Finalizers, uniqFinalizer)
}).Should(BeTrue())
})
})

When("apply a cassandra manifest", func() {
cassandraManifest.Annotations = map[string]string{models.ResourceStateAnnotation: models.CreatingEvent}
It("should create a cassandra resource", func() {
Expect(k8sClient.Create(ctx, &cassandraManifest)).Should(Succeed())

By("sending Cassandra specification to the Instaclustr API and get ID of a created cluster")

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

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

When("add the user to a Cassandra UserReference", func() {
newUsers := []*v1beta1.UserReference{{
Namespace: userManifest.Namespace,
Name: userManifest.Name,
}}
cassandraNS := types.NamespacedName{Name: cassandraManifest.ObjectMeta.Name, Namespace: ns}
userNS := types.NamespacedName{Name: userManifest.ObjectMeta.Name, Namespace: ns}

It("should create the user for the cluster", func() {

Expect(k8sClient.Get(ctx, cassandraNS, &cassandra)).Should(Succeed())

patch := cassandra.NewPatch()
cassandra.Spec.UserRefs = newUsers

Expect(k8sClient.Patch(ctx, &cassandra, patch)).Should(Succeed())

By("going to Cassandra(cluster) controller predicate and put user entity to creation state. " +
"Finally creates the user for the corresponded cluster")

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

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

if state, exist := user.Status.ClustersEvents[clusterID]; exist && state != models.Created {
return false
}

return true
}, timeout, interval).Should(BeTrue())
})
})

When("remove the user from the Cassandra UserReference", func() {
It("should delete the user for the cluster", func() {
Expect(k8sClient.Get(ctx, cassandraNS, &cassandra)).Should(Succeed())

patch := cassandra.NewPatch()
cassandra.Spec.UserRefs = []*v1beta1.UserReference{}

Expect(k8sClient.Patch(ctx, &cassandra, patch)).Should(Succeed())

By("going to Cassandra(cluster) controller predicate and put user entity to deletion state. " +
"Finally deletes the user for the corresponded cluster")

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

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

if _, exist := user.Status.ClustersEvents[clusterID]; exist {
return false
}

return true
}, timeout, interval).Should(BeTrue())
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: clusterresources.instaclustr.com/v1beta1
kind: CassandraUser
metadata:
name: cassandrauser-sample
namespace: default
spec:
secretRef:
name: "secret-sample"
namespace: "default"
54 changes: 54 additions & 0 deletions controllers/tests/datatest/clusters_v1beta1_cassandra.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
apiVersion: clusters.instaclustr.com/v1beta1
kind: Cassandra
metadata:
name: cassandra-sample
namespace: default
spec:
name: "Cassandra"
version: "3.11.13"
bundledUseOnly: true
dataCentres:
- name: "AWS_cassandra"
region: "US_EAST_1"
cloudProvider: "AWS_VPC"
continuousBackup: true
nodesNumber: 2
replicationFactor: 2
privateIpBroadcastForDiscovery: true
network: "172.16.0.0/19"
tags:
"tag": "testTag"
clientToClusterEncryption: true
nodeSize: "CAS-DEV-t4g.small-5"
# accountName: "asdf"
# cloudProviderSettings:
# - customVirtualNetworkId: "vpc-12345678"
# diskEncryptionKey: "123e4567-e89b-12d3-a456-426614174000"
# resourceGroup: "asdfadfsdfas"
# - name: "Second Data Centre"
# region: "US_EAST_1"
# cloudProvider: "AWS_VPC"
# continuousBackup: true
# nodesNumber: 2
# replicationFactor: 2
# privateIpBroadcastForDiscovery: true
# nodeSize: "CAS-DEV-t4g.small-5"
# network: "172.16.0.0/19"
# tags:
# "tag": "testTag"
# clientToClusterEncryption: true
# accountName: "asdf"
# cloudProviderSettings:
# - customVirtualNetworkId: "vpc-12345678"
# diskEncryptionKey: "123e4567-e89b-12d3-a456-426614174000"
# resourceGroup: "asdfadfsdfas"
pciCompliance: true
luceneEnabled: true
passwordAndUserAuth: true
privateNetworkCluster: true
slaTier: "NON_PRODUCTION"
# twoFactorDelete:
# - email: "emailTest"
# phone: "phoneTest"
# spark:
# - version: "2.3.2"
8 changes: 8 additions & 0 deletions controllers/tests/datatest/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: secret-sample
namespace: default
data:
password: NDgxMzU5ODM1NzlmMDU0ZTlhY2I4ZjcxMTMzMzQ1MjM3ZQo=
username: b2xvbG8K
Loading

0 comments on commit 3f914d3

Please sign in to comment.