|
| 1 | +package replica_set_mount_connection_string |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/mongodb/mongodb-kubernetes-operator/test/e2e/util/wait" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "os" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + . "github.com/mongodb/mongodb-kubernetes-operator/test/e2e/util/mongotester" |
| 15 | + |
| 16 | + e2eutil "github.com/mongodb/mongodb-kubernetes-operator/test/e2e" |
| 17 | + "github.com/mongodb/mongodb-kubernetes-operator/test/e2e/mongodbtests" |
| 18 | + setup "github.com/mongodb/mongodb-kubernetes-operator/test/e2e/setup" |
| 19 | +) |
| 20 | + |
| 21 | +func TestMain(m *testing.M) { |
| 22 | + code, err := e2eutil.RunTest(m) |
| 23 | + if err != nil { |
| 24 | + fmt.Println(err) |
| 25 | + } |
| 26 | + os.Exit(code) |
| 27 | +} |
| 28 | + |
| 29 | +// createPythonTestPod creates a pod with a simple python app which connects to a MongoDB database |
| 30 | +// using the connection string referenced within a given secret key. |
| 31 | +func createPythonTestPod(idx int, namespace, secretName, secretKey string) corev1.Pod { |
| 32 | + return corev1.Pod{ |
| 33 | + ObjectMeta: metav1.ObjectMeta{ |
| 34 | + Name: fmt.Sprintf("test-pod-%d", idx), |
| 35 | + Namespace: namespace, |
| 36 | + }, |
| 37 | + Spec: corev1.PodSpec{ |
| 38 | + RestartPolicy: corev1.RestartPolicyNever, |
| 39 | + Containers: []corev1.Container{ |
| 40 | + { |
| 41 | + Name: "python-app", |
| 42 | + Image: "quay.io/mongodb/mongodb-kubernetes-operator-test-app:1.0.0", |
| 43 | + Command: []string{"python", "main.py"}, |
| 44 | + WorkingDir: "/app", |
| 45 | + Env: []corev1.EnvVar{ |
| 46 | + { |
| 47 | + Name: "CONNECTION_STRING", |
| 48 | + ValueFrom: &corev1.EnvVarSource{ |
| 49 | + SecretKeyRef: &corev1.SecretKeySelector{ |
| 50 | + LocalObjectReference: corev1.LocalObjectReference{ |
| 51 | + Name: secretName, |
| 52 | + }, |
| 53 | + Key: secretKey, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestMountConnectionString(t *testing.T) { |
| 65 | + ctx := setup.Setup(t) |
| 66 | + defer ctx.Teardown() |
| 67 | + |
| 68 | + mdb, user := e2eutil.NewTestMongoDB(ctx, "mdb0", "") |
| 69 | + scramUser := mdb.GetScramUsers()[0] |
| 70 | + |
| 71 | + _, err := setup.GeneratePasswordForUser(ctx, user, "") |
| 72 | + if err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + |
| 76 | + tester, err := FromResource(t, mdb) |
| 77 | + if err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + |
| 81 | + t.Run("Create MongoDB Resource", mongodbtests.CreateMongoDBResource(&mdb, ctx)) |
| 82 | + t.Run("Basic tests", mongodbtests.BasicFunctionality(&mdb)) |
| 83 | + t.Run("Keyfile authentication is configured", tester.HasKeyfileAuth(3)) |
| 84 | + t.Run("Test Basic Connectivity", tester.ConnectivitySucceeds()) |
| 85 | + t.Run("Test SRV Connectivity", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI("")), WithoutTls(), WithReplicaSet((mdb.Name)))) |
| 86 | + t.Run("Test Basic Connectivity with generated connection string secret", |
| 87 | + tester.ConnectivitySucceeds(WithURI(mongodbtests.GetConnectionStringForUser(mdb, scramUser)))) |
| 88 | + t.Run("Test SRV Connectivity with generated connection string secret", |
| 89 | + tester.ConnectivitySucceeds(WithURI(mongodbtests.GetSrvConnectionStringForUser(mdb, scramUser)))) |
| 90 | + t.Run("Ensure Authentication", tester.EnsureAuthenticationIsConfigured(3)) |
| 91 | + t.Run("AutomationConfig has the correct version", mongodbtests.AutomationConfigVersionHasTheExpectedVersion(&mdb, 1)) |
| 92 | + |
| 93 | + t.Run("Application Pod can connect to MongoDB using the generated standard connection string.", func(t *testing.T) { |
| 94 | + testPod := createPythonTestPod(0, mdb.Namespace, fmt.Sprintf("%s-admin-%s", mdb.Name, user.Name), "connectionString.standard") |
| 95 | + err := e2eutil.TestClient.Create(context.TODO(), &testPod, &e2eutil.CleanupOptions{ |
| 96 | + TestContext: ctx, |
| 97 | + }) |
| 98 | + assert.NoError(t, err) |
| 99 | + assert.NoError(t, wait.ForPodPhase(t, time.Minute*5, testPod, corev1.PodSucceeded)) |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("Application Pod can connect to MongoDB using the generated secret SRV connection string", func(t *testing.T) { |
| 103 | + testPod := createPythonTestPod(1, mdb.Namespace, fmt.Sprintf("%s-admin-%s", mdb.Name, user.Name), "connectionString.standardSrv") |
| 104 | + err := e2eutil.TestClient.Create(context.TODO(), &testPod, &e2eutil.CleanupOptions{ |
| 105 | + TestContext: ctx, |
| 106 | + }) |
| 107 | + assert.NoError(t, err) |
| 108 | + assert.NoError(t, wait.ForPodPhase(t, time.Minute*5, testPod, corev1.PodSucceeded)) |
| 109 | + }) |
| 110 | +} |
0 commit comments