Skip to content

Commit

Permalink
auto generate namespace for integration test to avoid collision
Browse files Browse the repository at this point in the history
  • Loading branch information
bjeevan-ib committed Dec 15, 2023
1 parent f0264dd commit f3cf857
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
20 changes: 10 additions & 10 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ pipeline {
}
stage("Run tests") {
steps {
withCredentials([string(credentialsId: 'GITHUB_TOKEN', variable: 'GitHub_PAT')]) {
sh "echo machine github.com login $GitHub_PAT > ~/.netrc"
sh "echo machine api.github.com login $GitHub_PAT >> ~/.netrc"
withCredentials([string(credentialsId: 'GITHUB_TOKEN', variable: 'GitHub_PAT')]) {
sh "echo machine github.com login $GitHub_PAT > ~/.netrc"
sh "echo machine api.github.com login $GitHub_PAT >> ~/.netrc"
}
dir("$DIRECTORY") {
sh "sudo apt-get update"
sh "sudo apt-get -y install postgresql-client"
sh "echo 'db-controller-name' > .id"
sh "make test"
sh "sudo apt-get -y remove postgresql-client"
}
dir("$DIRECTORY") {
sh "sudo apt-get update"
sh "sudo apt-get -y install postgresql-client"
sh "echo 'db-controller-name' > .id"
sh "make test"
sh "sudo apt-get -y remove postgresql-client"
}
}
}
stage("Build db-controller image") {
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ vet: ## Run go vet against code.
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... --ginkgo.label-filter=!integration -coverprofile cover.out

.PHONY: integration-test
integration-test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./controllers --ginkgo.label-filter=integration -coverprofile cover.out
##@ Build

.PHONY: build
Expand Down
55 changes: 47 additions & 8 deletions controllers/dbc_end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"fmt"
"os"
"strings"
"time"

Expand All @@ -21,15 +22,17 @@ import (
)

const (
timeout = time.Minute * 3
interval = time.Minute * 1
timeout = time.Minute * 10
interval = time.Second * 30
)

var (
falseVal = false
namespace = "bjeevan"
newdbcName = "new-db-test-dbclaim"
newdbcMasterSecretName = "box-3-new-db-test-dbclaim-1ec9b27c-master"
namespace string
newdbcName string
newdbcMasterSecretName string
db1 string
db2 string
ctx = context.Background()
// dBHostname string
)
Expand All @@ -41,6 +44,7 @@ var _ = Describe("db-controller end to end testing", Label("integration"), Order

rawConfig, err := kubeConfig.RawConfig()
if err != nil {
// if we can't read the kubeconfig, we can't run the test
Skip(fmt.Sprintf("This test can only run in %s. Current context is %s", "box-3", "unable to read kubeconfig"))
}

Expand All @@ -66,6 +70,15 @@ var _ = Describe("db-controller end to end testing", Label("integration"), Order
e2e_k8sClient, err = client.New(e2e_cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())
Expect(e2e_k8sClient).ToNot(BeNil())

data, err := os.ReadFile("../.id")
Expect(err).ToNot(HaveOccurred())
namespace = strings.TrimSpace(string(data)) + "-e2e"
newdbcName = namespace + "-db-1"
db1 = "box-3-" + newdbcName + "-1ec9b27c"
newdbcMasterSecretName = db1 + "-master"
db2 = namespace + "-db-2"
createNamespace()
})

logf.Log.Info("Starting test", "timeout", timeout, "interval", interval)
Expand Down Expand Up @@ -103,6 +116,23 @@ var _ = Describe("db-controller end to end testing", Label("integration"), Order
})

var _ = AfterAll(func() {
//delete master secret if it exists
By("deleting the master secret")
e2e_k8sClient.Delete(ctx, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: newdbcMasterSecretName,
Namespace: "db-controller",
},
})

//delete the namespace
By("deleting the namespace")
e2e_k8sClient.Delete(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
})

By("tearing down the test environment")
if e2e_testEnv != nil {
err := e2e_testEnv.Stop()
Expand Down Expand Up @@ -163,7 +193,7 @@ func MigratePostgresToAuroraRDS() {

func MigrateUseExistingToNewRDS() {
key := types.NamespacedName{
Name: "end2end-test-2-dbclaim",
Name: db2,
Namespace: namespace,
}
existingDbClaim := &persistancev1.DatabaseClaim{}
Expand Down Expand Up @@ -194,7 +224,7 @@ func MigrateUseExistingToNewRDS() {

func UseExistingPostgresRDSTest() {
key := types.NamespacedName{
Name: "end2end-test-2-dbclaim",
Name: db2,
Namespace: namespace,
}
dbClaim := &persistancev1.DatabaseClaim{
Expand All @@ -219,7 +249,7 @@ func UseExistingPostgresRDSTest() {
SourceDataFrom: &persistancev1.SourceDataFrom{
Type: persistancev1.SourceDataType("database"),
Database: &persistancev1.Database{
DSN: "postgres://root@box-3-new-db-test-dbclaim-1ec9b27c.cpwy0kesdxhx.us-east-1.rds.amazonaws.com:5432/sample_db?sslmode=require",
DSN: "postgres://root@" + db1 + ".cpwy0kesdxhx.us-east-1.rds.amazonaws.com:5432/sample_db?sslmode=require",
SecretRef: &persistancev1.SecretRef{
Name: "existing-db-master-secret",
Namespace: namespace,
Expand Down Expand Up @@ -337,3 +367,12 @@ func createPostgresRDSTest() {
return e2e_k8sClient.Get(ctx, types.NamespacedName{Name: "newdb-secret", Namespace: namespace}, &corev1.Secret{})
}, timeout, interval).Should(BeNil())
}

func createNamespace() {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}
e2e_k8sClient.Create(ctx, ns)
}

0 comments on commit f3cf857

Please sign in to comment.