Skip to content

Commit

Permalink
test/e2e: pick correct controller pod for testscript (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Oct 9, 2024
1 parent 1d8db00 commit 46a0ae1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
35 changes: 30 additions & 5 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,47 @@ func TestOperator(t *testing.T) {
// Deploying the controller-manager
_, err = kind("make", "deploy", "install", fmt.Sprintf("IMG=%s", img))
require.NoError(t, err)
// Accept the EULA and set the PID
_, err = kind("kubectl", "set", "env",
"-n", nsController, controller,
"MSSQL_ACCEPT_EULA=Y", "MSSQL_PID=Developer")
require.NoError(t, err)
// Restarting the controller-manager, to ensure the latest image is used
_, err = kind("kubectl", "rollout", "restart",
"-n", nsController, controller)
require.NoError(t, err)
// Waiting for the controller-manager to be ready
_, err = kind("kubectl", "rollout", "status",
"-n", nsController, controller, "--timeout", "2m")
"-n", nsController, controller,
"--timeout", "2m")
require.NoError(t, err)
var controllerPod string
for range 5 {
// Wait for the old pods to be deleted
<-time.After(time.Second)
// Getting the controller-manager pod name
output, err := kind("kubectl", "wait", "pod",
"-n", nsController,
"-l", "control-plane=controller-manager",
"--for", "condition=Ready",
"--timeout", "2m",
"-o", "go-template",
"--template", "{{.metadata.name}}",
)
require.NoError(t, err)
pods := strings.Split(output, "\n")
if len(pods) == 1 {
controllerPod = pods[0]
break
}
}
require.NotEmpty(t, controllerPod, "controller-manager pod not found")
// Running the test script
testscript.Run(t, testscript.Params{
Dir: filepath.Join("testdata", "atlas-schema"),
Setup: func(e *testscript.Env) (err error) {
e.Setenv("CONTROLLER_NS", nsController)
e.Setenv("CONTROLLER", controller)
e.Setenv("CONTROLLER", controllerPod)
// Sharing the atlas token with the test
e.Setenv("ATLAS_TOKEN", os.Getenv("ATLAS_TOKEN"))
// Ensure the test in running in the right kube context
Expand Down Expand Up @@ -123,9 +150,7 @@ func TestOperator(t *testing.T) {
},
// kubectl runs kubectl with the namespace set to the test namespace
"kubectl": func(ts *testscript.TestScript, neg bool, args []string) {
err := ts.Exec("kubectl", append([]string{
"--namespace", ts.Getenv("NAMESPACE"),
}, args...)...)
err := ts.Exec("kubectl", append([]string{"-n", ts.Getenv("NAMESPACE")}, args...)...)
if !neg {
ts.Check(err)
} else if err == nil {
Expand Down
37 changes: 35 additions & 2 deletions test/e2e/testdata/atlas-schema/registry.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ kubectl wait --for=condition=ready --timeout=60s -l app=postgres pods
# Create the secret to store ATLAS_TOKEN
kubectl create secret generic atlas-token --from-literal=ATLAS_TOKEN=${ATLAS_TOKEN}

# Sync the $WORK directory to the controller pod
kubectl cp -n ${CONTROLLER_NS} ${WORK} ${CONTROLLER}:/tmp/${NAMESPACE}/
# Push the schemas to the registry
env DEV_URL=postgres://root:pass@postgres.${NAMESPACE}:5433/postgres?sslmode=disable
atlas schema push atlas-operator --tag=registry-v1 --dev-url=${DEV_URL} --url=file:///tmp/${NAMESPACE}/schema-v1.hcl
atlas schema push atlas-operator --tag=registry-v2 --dev-url=${DEV_URL} --url=file:///tmp/${NAMESPACE}/schema-v2.hcl

# Create the schema
kubectl apply -f schema.yaml
kubectl wait --for=condition=ready --timeout=120s AtlasSchema/atlasschema-postgres
Expand All @@ -17,7 +24,7 @@ cmp stdout schema-v1.hcl

kubectl patch -f schema.yaml --type merge --patch-file patch-v2.yaml
# Ensure the controller is aware of the change
kubectl wait --for=condition=ready=false --timeout=60s AtlasSchemas/atlasschema-postgres
kubectl wait --for=condition=ready=false --timeout=60s AtlasSchema/atlasschema-postgres
kubectl wait --for=condition=ready --timeout=120s AtlasSchema/atlasschema-postgres

# Inspect the schema to ensure it's correct
Expand Down Expand Up @@ -94,6 +101,9 @@ spec:
- name: postgres
port: 5432
targetPort: postgres
- name: postgres-dev
port: 5433
targetPort: postgres-dev
type: ClusterIP
---
apiVersion: apps/v1
Expand Down Expand Up @@ -134,4 +144,27 @@ spec:
periodSeconds: 2
timeoutSeconds: 1
exec:
command: [ "pg_isready", "-U", "postgres" ]
command: [ "pg_isready" ]
- name: postgres-dev
image: postgres:15.4
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- all
env:
- name: POSTGRES_PASSWORD
value: pass
- name: POSTGRES_USER
value: root
- name: PGPORT
value: "5433"
ports:
- containerPort: 5433
name: postgres-dev
readinessProbe:
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
exec:
command: [ "pg_isready" ]
3 changes: 0 additions & 3 deletions test/e2e/testdata/atlas-schema/sqlserver.txtar
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Accept the EULA and set the PID
kubectl set env -n ${CONTROLLER_NS} ${CONTROLLER} MSSQL_ACCEPT_EULA=Y MSSQL_PID=Developer

env DB_URL=sqlserver://sa:P%40ssw0rd0995@sqlserver.${NAMESPACE}:1433?database=master
kubectl apply -f database.yaml
kubectl create secret generic sqlserver-credentials --from-literal=url=${DB_URL}
Expand Down

0 comments on commit 46a0ae1

Please sign in to comment.