From bbc6c6cca58c235af9b57d65f9793c929642c6cd Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Thu, 14 Jul 2022 21:02:23 +0200 Subject: [PATCH 1/6] Migrate PostgreSQL Scaler e2e test to Go Signed-off-by: Zbynek Roubalik --- tests/scalers/postgresql.test.ts | 238 -------------- .../scalers_go/postgresql/postgresql_test.go | 291 ++++++++++++++++++ 2 files changed, 291 insertions(+), 238 deletions(-) delete mode 100644 tests/scalers/postgresql.test.ts create mode 100644 tests/scalers_go/postgresql/postgresql_test.go diff --git a/tests/scalers/postgresql.test.ts b/tests/scalers/postgresql.test.ts deleted file mode 100644 index 909512b53d6..00000000000 --- a/tests/scalers/postgresql.test.ts +++ /dev/null @@ -1,238 +0,0 @@ -import * as fs from 'fs' -import * as sh from 'shelljs' -import * as tmp from 'tmp' -import test from 'ava' -import { createNamespace, waitForDeploymentReplicaCount } from './helpers' - -const testNamespace = 'postgresql-test' -const postgreSQLNamespace = 'postgresql' -const postgreSQLUsername = 'test-user' -const postgreSQLPassword = 'test-password' -const postgreSQLDatabase = 'test_db' -const deploymentName = 'worker' - -test.before(t => { - // install postgresql - createNamespace(postgreSQLNamespace) - const postgreSQLTmpFile = tmp.fileSync() - fs.writeFileSync(postgreSQLTmpFile.name, postgresqlDeploymentYaml.replace('{{POSTGRES_USER}}', postgreSQLUsername) - .replace('{{POSTGRES_PASSWORD}}', postgreSQLPassword) - .replace('{{POSTGRES_DB}}', postgreSQLDatabase) - .replace('{{POSTGRES_DB}}', postgreSQLDatabase)) - - t.is(0, sh.exec(`kubectl apply --namespace ${postgreSQLNamespace} -f ${postgreSQLTmpFile.name}`).code, 'creating a POSTGRES deployment should work.') - // wait for postgresql to load - let postgresqlReadyReplicaCount = '0' - for (let i = 0; i < 30; i++) { - postgresqlReadyReplicaCount = sh.exec(`kubectl get deploy/postgresql -n ${postgreSQLNamespace} -o jsonpath='{.status.readyReplicas}'`).stdout - if (postgresqlReadyReplicaCount != '1') { - sh.exec('sleep 2s') - } - } - t.is('1', postgresqlReadyReplicaCount, 'Postgresql is not in a ready state') - - // create table that used by the job and the worker - const postgresqlPod = sh.exec(`kubectl get po -n ${postgreSQLNamespace} -o jsonpath='{.items[0].metadata.name}'`).stdout - t.not(postgresqlPod, '') - const createTableSQL = `CREATE TABLE task_instance (id serial PRIMARY KEY,state VARCHAR(10));` - sh.exec( `kubectl exec -n ${postgreSQLNamespace} ${postgresqlPod} -- psql -U ${postgreSQLUsername} -d ${postgreSQLDatabase} -c "${createTableSQL}"`) - - sh.config.silent = true - createNamespace(testNamespace) - // deploy streams consumer app, scaled object etc. - const tmpFile = tmp.fileSync() - const base64ConnectionString = Buffer.from(`postgresql://${postgreSQLUsername}:${postgreSQLPassword}@postgresql.${postgreSQLNamespace}.svc.cluster.local:5432/${postgreSQLDatabase}?sslmode=disable`).toString('base64') - fs.writeFileSync(tmpFile.name, deployYaml.replace('{{POSTGRES_CONNECTION_STRING}}', base64ConnectionString).replace('{{DEPLOYMENT_NAME}}', deploymentName)) - t.is( - 0, - sh.exec(`kubectl apply -f ${tmpFile.name} --namespace ${testNamespace}`).code, - 'creating a deployment should work..' - ) -}) - -test.serial('Deployment should have 0 replicas on start', async t => { - t.true(await waitForDeploymentReplicaCount(0, deploymentName, testNamespace, 60, 1000), 'replica count should start out as 0') - -}) - -test.serial(`Deployment should scale to 2 (the max) then back to 0`, async t => { - const tmpFile = tmp.fileSync() - fs.writeFileSync(tmpFile.name, insertRecordsJobYaml) - t.is( - 0, - sh.exec(`kubectl apply -f ${tmpFile.name} --namespace ${testNamespace}`).code, - 'creating job should work.' - ) - - const maxReplicaCount = 2 - t.true(await waitForDeploymentReplicaCount(maxReplicaCount, deploymentName, testNamespace, 120, 1000), 'Replica count should be 0 after 2 minutes') - - t.true(await waitForDeploymentReplicaCount(0, deploymentName, testNamespace, 360, 1000), 'Replica count should be 0 after 5 minutes') -}) - -test.after.always.cb('clean up postgresql deployment', t => { - const resources = [ - 'scaledobject.keda.sh/postgresql-scaledobject', - 'triggerauthentication.keda.sh/keda-trigger-auth-postgresql-secret', - `deployment.apps/${deploymentName}`, - 'secret/postgresql-secrets', - 'job/postgresql-insert-job', - ] - - for (const resource of resources) { - sh.exec(`kubectl delete ${resource} --namespace ${testNamespace}`) - } - sh.exec(`kubectl delete namespace ${testNamespace}`) - - // uninstall postgresql - sh.exec(`kubectl delete --namespace ${postgreSQLNamespace} deploy/postgresql`) - sh.exec(`kubectl delete namespace ${postgreSQLNamespace}`) - - t.end() -}) - -const deployYaml = `apiVersion: apps/v1 -kind: Deployment -metadata: - labels: - app: postgresql-update-worker - name: {{DEPLOYMENT_NAME}} -spec: - replicas: 0 - selector: - matchLabels: - app: postgresql-update-worker - template: - metadata: - labels: - app: postgresql-update-worker - spec: - containers: - - image: ghcr.io/kedacore/tests-postgresql - imagePullPolicy: Always - name: postgresql-processor-test - command: - - /app - - update - env: - - name: TASK_INSTANCES_COUNT - value: "6000" - - name: CONNECTION_STRING - valueFrom: - secretKeyRef: - name: postgresql-secrets - key: postgresql_conn_str ---- -apiVersion: v1 -kind: Secret -metadata: - name: postgresql-secrets -type: Opaque -data: - postgresql_conn_str: {{POSTGRES_CONNECTION_STRING}} ---- -apiVersion: keda.sh/v1alpha1 -kind: TriggerAuthentication -metadata: - name: keda-trigger-auth-postgresql-secret -spec: - secretTargetRef: - - parameter: connection - name: postgresql-secrets - key: postgresql_conn_str ---- -apiVersion: keda.sh/v1alpha1 -kind: ScaledObject -metadata: - name: postgresql-scaledobject -spec: - scaleTargetRef: - name: worker - pollingInterval: 5 - cooldownPeriod: 10 - minReplicaCount: 0 - maxReplicaCount: 2 - triggers: - - type: postgresql - metadata: - targetQueryValue: "4" - query: "SELECT CEIL(COUNT(*) / 5) FROM task_instance WHERE state='running' OR state='queued'" - authenticationRef: - name: keda-trigger-auth-postgresql-secret` - -const insertRecordsJobYaml = `apiVersion: batch/v1 -kind: Job -metadata: - labels: - app: postgresql-insert-job - name: postgresql-insert-job -spec: - template: - metadata: - labels: - app: postgresql-insert-job - spec: - containers: - - image: ghcr.io/kedacore/tests-postgresql - imagePullPolicy: Always - name: postgresql-processor-test - command: - - /app - - insert - env: - - name: TASK_INSTANCES_COUNT - value: "6000" - - name: CONNECTION_STRING - valueFrom: - secretKeyRef: - name: postgresql-secrets - key: postgresql_conn_str - restartPolicy: Never - backoffLimit: 4` - - -const postgresqlDeploymentYaml = `apiVersion: apps/v1 -kind: Deployment -metadata: - labels: - app: postgresql - name: postgresql -spec: - replicas: 1 - selector: - matchLabels: - app: postgresql - template: - metadata: - labels: - app: postgresql - spec: - containers: - - image: postgres:10.5 - name: postgresql - env: - - name: POSTGRES_USER - value: {{POSTGRES_USER}} - - name: POSTGRES_PASSWORD - value: {{POSTGRES_PASSWORD}} - - name: POSTGRES_DB - value: {{POSTGRES_DB}} - ports: - - name: postgresql - protocol: TCP - containerPort: 5432 ---- -apiVersion: v1 -kind: Service -metadata: - labels: - app: postgresql - name: postgresql -spec: - ports: - - port: 5432 - protocol: TCP - targetPort: 5432 - selector: - app: postgresql - type: ClusterIP` diff --git a/tests/scalers_go/postgresql/postgresql_test.go b/tests/scalers_go/postgresql/postgresql_test.go new file mode 100644 index 00000000000..31a81097610 --- /dev/null +++ b/tests/scalers_go/postgresql/postgresql_test.go @@ -0,0 +1,291 @@ +//go:build e2e +// +build e2e + +package postgresql_test + +import ( + "encoding/base64" + "fmt" + "testing" + + "github.com/joho/godotenv" + "github.com/stretchr/testify/assert" + "k8s.io/client-go/kubernetes" + + . "github.com/kedacore/keda/v2/tests/helper" +) + +// Load environment variables from .env file +var _ = godotenv.Load("../../.env") + +const ( + testName = "postgresql-test" +) + +var ( + testNamespace = fmt.Sprintf("%s-ns", testName) + deploymentName = fmt.Sprintf("%s-deployment", testName) + scaledObjectName = fmt.Sprintf("%s-so", testName) + triggerAuthenticationName = fmt.Sprintf("%s-ta", testName) + secretName = fmt.Sprintf("%s-secret", testName) + postgreSQLStatefulSetName = "postgresql" + postgresqlPodName = fmt.Sprintf("%s-0", postgreSQLStatefulSetName) + postgreSQLUsername = "test-user" + postgreSQLPassword = "test-password" + postgreSQLDatabase = "test_db" + postgreSQLConnectionString = fmt.Sprintf("postgresql://%s:%s@postgresql.%s.svc.cluster.local:5432/%s?sslmode=disable", + postgreSQLUsername, postgreSQLPassword, testNamespace, postgreSQLDatabase) + minReplicaCount = 0 + maxReplicaCount = 2 +) + +type templateData struct { + TestNamespace string + DeploymentName string + ScaledObjectName string + TriggerAuthenticationName string + SecretName string + PostgreSQLStatefulSetName string + PostgreSQLConnectionStringBase64 string + PostgreSQLUsername string + PostgreSQLPassword string + PostgreSQLDatabase string + MinReplicaCount int + MaxReplicaCount int +} + +type templateValues map[string]string + +const ( + deploymentTemplate = `apiVersion: apps/v1 + kind: Deployment + metadata: + labels: + app: postgresql-update-worker + name: {{.DeploymentName}} + namespace: {{.TestNamespace}} + spec: + replicas: 0 + selector: + matchLabels: + app: postgresql-update-worker + template: + metadata: + labels: + app: postgresql-update-worker + spec: + containers: + - image: ghcr.io/kedacore/tests-postgresql + imagePullPolicy: Always + name: postgresql-processor-test + command: + - /app + - update + env: + - name: TASK_INSTANCES_COUNT + value: "6000" + - name: CONNECTION_STRING + valueFrom: + secretKeyRef: + name: {{.SecretName}} + key: postgresql_conn_str +` + + secretTemplate = `apiVersion: v1 +kind: Secret +metadata: + name: {{.SecretName}} + namespace: {{.TestNamespace}} +type: Opaque +data: + postgresql_conn_str: {{.PostgreSQLConnectionStringBase64}} +` + + triggerAuthenticationTemplate = `keda.sh/v1alpha1 +kind: TriggerAuthentication +metadata: + name: {{.TriggerAuthenticationName}} + namespace: {{.TestNamespace}} +spec: + secretTargetRef: + - parameter: connection + name: {{.SecretName}} + key: postgresql_conn_str +` + + scaledObjectTemplate = `apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: {{.ScaledObjectName}} + namespace: {{.TestNamespace}} +spec: + scaleTargetRef: + name: {{.DeploymentName}} + pollingInterval: 5 + cooldownPeriod: 10 + minReplicaCount: {{.MinReplicaCount}} + maxReplicaCount: {{.MaxReplicaCount}} + triggers: + - type: postgresql + metadata: + targetQueryValue: "4" + query: "SELECT CEIL(COUNT(*) / 5) FROM task_instance WHERE state='running' OR state='queued'" + authenticationRef: + name: {{.TriggerAuthenticationName}} +` + + postgreSQLDeploymentTemplate = `apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: {{.PostgreSQLStatefulSetName}} + name: {{.PostgreSQLStatefulSetName}} + namespace: {{.TestNamespace}} +spec: + replicas: 1 + selector: + matchLabels: + app: {{.PostgreSQLStatefulSetName}} + template: + metadata: + labels: + app: {{.PostgreSQLStatefulSetName}} + spec: + containers: + - image: postgres:10.5 + name: postgresql + env: + - name: POSTGRES_USER + value: {{.PostgreSQLUsername}} + - name: POSTGRES_PASSWORD + value: {{.PostgreSQLPassword}} + - name: POSTGRES_DB + value: {{.PostgreSQLDatabase}} + ports: + - name: postgresql + protocol: TCP + containerPort: 5432 +` + + postgreSQLServiceTemplate = `apiVersion: v1 +kind: Service +metadata: + labels: + app: {{.PostgreSQLStatefulSetName}} + name: {{.PostgreSQLStatefulSetName}} + namespace: {{.TestNamespace}} +spec: + ports: + - port: 5432 + protocol: TCP + targetPort: 5432 + selector: + app: {{.PostgreSQLStatefulSetName}} + type: ClusterIP +` + + insertRecordsJobTemplate = `apiVersion: batch/v1 +kind: Job +metadata: + labels: + app: postgresql-insert-job + name: postgresql-insert-job + namespace: {{.TestNamespace}} +spec: + template: + metadata: + labels: + app: postgresql-insert-job + spec: + containers: + - image: ghcr.io/kedacore/tests-postgresql + imagePullPolicy: Always + name: postgresql-processor-test + command: + - /app + - insert + env: + - name: TASK_INSTANCES_COUNT + value: "6000" + - name: CONNECTION_STRING + valueFrom: + secretKeyRef: + name: {{.SecretName}} + key: postgresql_conn_str + restartPolicy: Never + backoffLimit: 4 +` +) + +func TestPostreSQLScaler(t *testing.T) { + // Create kubernetes resources for PostgreSQL server + kc := GetKubernetesClient(t) + data, postgreSQLtemplates := getPostgreSQLTemplateData() + CreateKubernetesResources(t, kc, testNamespace, data, postgreSQLtemplates) + + assert.True(t, WaitForStatefulsetReplicaReadyCount(t, kc, postgreSQLStatefulSetName, testNamespace, 1, 60, 3), + "replica count should be %s after 3 minute", 1) + + createTableSQL := "CREATE TABLE task_instance (id serial PRIMARY KEY,state VARCHAR(10));" + out, errOut, err := ExecCommandOnSpecificPod(t, postgresqlPodName, testNamespace, fmt.Sprintf("psql -U %s -d %s -c \"%s\"", postgreSQLUsername, postgreSQLDatabase, createTableSQL)) + t.Logf("Output: %s, Error: %s", out, errOut) + assert.Nil(t, err, "executing a command on PostreSQL Pod should work") + + // Create kubernetes resources for testing + data, templates := getTemplateData() + CreateKubernetesResources(t, kc, testNamespace, data, templates) + assert.True(t, WaitForDeploymentReplicaCount(t, kc, deploymentName, testNamespace, minReplicaCount, 60, 3), + "replica count should be %s after 3 minute", minReplicaCount) + + testScaleUp(t, kc, data) + testScaleDown(t, kc) + + // cleanup + DeleteKubernetesResources(t, kc, testNamespace, data, templates) +} +func testScaleUp(t *testing.T, kc *kubernetes.Clientset, data templateData) { + t.Log("--- testing scale up ---") + templateTriggerJob := templateValues{"insertRecordsJobTemplate": insertRecordsJobTemplate} + KubectlApplyMultipleWithTemplate(t, data, templateTriggerJob) + + assert.True(t, WaitForDeploymentReplicaCount(t, kc, deploymentName, testNamespace, maxReplicaCount, 60, 3), + "replica count should be %s after 3 minutes", maxReplicaCount) +} + +func testScaleDown(t *testing.T, kc *kubernetes.Clientset) { + t.Log("--- testing scale down ---") + + assert.True(t, WaitForDeploymentReplicaCount(t, kc, deploymentName, testNamespace, minReplicaCount, 60, 3), + "replica count should be %s after 3 minutes", minReplicaCount) +} + +var data = templateData{ + TestNamespace: testNamespace, + PostgreSQLStatefulSetName: postgreSQLStatefulSetName, + DeploymentName: deploymentName, + ScaledObjectName: scaledObjectName, + MinReplicaCount: minReplicaCount, + MaxReplicaCount: maxReplicaCount, + TriggerAuthenticationName: triggerAuthenticationName, + SecretName: secretName, + PostgreSQLUsername: postgreSQLUsername, + PostgreSQLPassword: postgreSQLPassword, + PostgreSQLDatabase: postgreSQLDatabase, + PostgreSQLConnectionStringBase64: base64.StdEncoding.EncodeToString([]byte(postgreSQLConnectionString)), +} + +func getPostgreSQLTemplateData() (templateData, map[string]string) { + return data, templateValues{ + "postgreSQLDeploymentTemplate": postgreSQLDeploymentTemplate, + "postgreSQLServiceTemplate": postgreSQLServiceTemplate, + } +} + +func getTemplateData() (templateData, map[string]string) { + return data, templateValues{ + "secretTemplate": secretTemplate, + "deploymentTemplate": deploymentTemplate, + "triggerAuthenticationTemplate": triggerAuthenticationTemplate, + "scaledObjectTemplate": scaledObjectTemplate, + } +} From 7a40c3cbdad2bb515ea1cffcfe69f8519dccffb6 Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Thu, 14 Jul 2022 21:06:55 +0200 Subject: [PATCH 2/6] use StatefulSet Signed-off-by: Zbynek Roubalik --- tests/scalers_go/postgresql/postgresql_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/scalers_go/postgresql/postgresql_test.go b/tests/scalers_go/postgresql/postgresql_test.go index 31a81097610..9ea68790ea5 100644 --- a/tests/scalers_go/postgresql/postgresql_test.go +++ b/tests/scalers_go/postgresql/postgresql_test.go @@ -134,8 +134,8 @@ spec: name: {{.TriggerAuthenticationName}} ` - postgreSQLDeploymentTemplate = `apiVersion: apps/v1 -kind: Deployment + postgreSQLStatefulSetTemplate = `apiVersion: apps/v1 +kind: StatefulSet metadata: labels: app: {{.PostgreSQLStatefulSetName}} @@ -276,8 +276,8 @@ var data = templateData{ func getPostgreSQLTemplateData() (templateData, map[string]string) { return data, templateValues{ - "postgreSQLDeploymentTemplate": postgreSQLDeploymentTemplate, - "postgreSQLServiceTemplate": postgreSQLServiceTemplate, + "postgreSQLStatefulSetTemplate": postgreSQLStatefulSetTemplate, + "postgreSQLServiceTemplate": postgreSQLServiceTemplate, } } From 896ea7100ae285dea196b4e98aab2b3d8e1c6f0e Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Thu, 14 Jul 2022 21:20:33 +0200 Subject: [PATCH 3/6] tabs -> spaces Signed-off-by: Zbynek Roubalik --- .../scalers_go/postgresql/postgresql_test.go | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/tests/scalers_go/postgresql/postgresql_test.go b/tests/scalers_go/postgresql/postgresql_test.go index 9ea68790ea5..f33908f6682 100644 --- a/tests/scalers_go/postgresql/postgresql_test.go +++ b/tests/scalers_go/postgresql/postgresql_test.go @@ -58,37 +58,37 @@ type templateValues map[string]string const ( deploymentTemplate = `apiVersion: apps/v1 - kind: Deployment - metadata: - labels: - app: postgresql-update-worker - name: {{.DeploymentName}} - namespace: {{.TestNamespace}} - spec: - replicas: 0 - selector: - matchLabels: - app: postgresql-update-worker - template: - metadata: - labels: - app: postgresql-update-worker - spec: - containers: - - image: ghcr.io/kedacore/tests-postgresql - imagePullPolicy: Always - name: postgresql-processor-test - command: - - /app - - update - env: - - name: TASK_INSTANCES_COUNT - value: "6000" - - name: CONNECTION_STRING - valueFrom: - secretKeyRef: - name: {{.SecretName}} - key: postgresql_conn_str + kind: Deployment + metadata: + labels: + app: postgresql-update-worker + name: {{.DeploymentName}} + namespace: {{.TestNamespace}} + spec: + replicas: 0 + selector: + matchLabels: + app: postgresql-update-worker + template: + metadata: + labels: + app: postgresql-update-worker + spec: + containers: + - image: ghcr.io/kedacore/tests-postgresql + imagePullPolicy: Always + name: postgresql-processor-test + command: + - /app + - update + env: + - name: TASK_INSTANCES_COUNT + value: "6000" + - name: CONNECTION_STRING + valueFrom: + secretKeyRef: + name: {{.SecretName}} + key: postgresql_conn_str ` secretTemplate = `apiVersion: v1 @@ -172,8 +172,8 @@ kind: Service metadata: labels: app: {{.PostgreSQLStatefulSetName}} - name: {{.PostgreSQLStatefulSetName}} - namespace: {{.TestNamespace}} + name: {{.PostgreSQLStatefulSetName}} + namespace: {{.TestNamespace}} spec: ports: - port: 5432 From d0422095965009af651eea910c0f87f4c1b645d3 Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Thu, 14 Jul 2022 22:10:15 +0200 Subject: [PATCH 4/6] fix tests Signed-off-by: Zbynek Roubalik --- tests/scalers_go/postgresql/postgresql_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/scalers_go/postgresql/postgresql_test.go b/tests/scalers_go/postgresql/postgresql_test.go index f33908f6682..2aec16a2249 100644 --- a/tests/scalers_go/postgresql/postgresql_test.go +++ b/tests/scalers_go/postgresql/postgresql_test.go @@ -143,6 +143,7 @@ metadata: namespace: {{.TestNamespace}} spec: replicas: 1 + serviceName: {{.PostgreSQLStatefulSetName}} selector: matchLabels: app: {{.PostgreSQLStatefulSetName}} @@ -233,7 +234,7 @@ func TestPostreSQLScaler(t *testing.T) { // Create kubernetes resources for testing data, templates := getTemplateData() - CreateKubernetesResources(t, kc, testNamespace, data, templates) + KubectlApplyMultipleWithTemplate(t, data, templates) assert.True(t, WaitForDeploymentReplicaCount(t, kc, deploymentName, testNamespace, minReplicaCount, 60, 3), "replica count should be %s after 3 minute", minReplicaCount) From b4dde08f756bf16b9e88d8359e4640af3aa27606 Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Thu, 14 Jul 2022 23:00:30 +0200 Subject: [PATCH 5/6] fix yaml Signed-off-by: Zbynek Roubalik --- .../scalers_go/postgresql/postgresql_test.go | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/tests/scalers_go/postgresql/postgresql_test.go b/tests/scalers_go/postgresql/postgresql_test.go index 2aec16a2249..cc147de2298 100644 --- a/tests/scalers_go/postgresql/postgresql_test.go +++ b/tests/scalers_go/postgresql/postgresql_test.go @@ -58,37 +58,37 @@ type templateValues map[string]string const ( deploymentTemplate = `apiVersion: apps/v1 - kind: Deployment +kind: Deployment +metadata: + labels: + app: postgresql-update-worker + name: {{.DeploymentName}} + namespace: {{.TestNamespace}} +spec: + replicas: 0 + selector: + matchLabels: + app: postgresql-update-worker + template: metadata: labels: app: postgresql-update-worker - name: {{.DeploymentName}} - namespace: {{.TestNamespace}} spec: - replicas: 0 - selector: - matchLabels: - app: postgresql-update-worker - template: - metadata: - labels: - app: postgresql-update-worker - spec: - containers: - - image: ghcr.io/kedacore/tests-postgresql - imagePullPolicy: Always - name: postgresql-processor-test - command: - - /app - - update - env: - - name: TASK_INSTANCES_COUNT - value: "6000" - - name: CONNECTION_STRING - valueFrom: - secretKeyRef: - name: {{.SecretName}} - key: postgresql_conn_str + containers: + - image: ghcr.io/kedacore/tests-postgresql + imagePullPolicy: Always + name: postgresql-processor-test + command: + - /app + - update + env: + - name: TASK_INSTANCES_COUNT + value: "6000" + - name: CONNECTION_STRING + valueFrom: + secretKeyRef: + name: {{.SecretName}} + key: postgresql_conn_str ` secretTemplate = `apiVersion: v1 @@ -101,7 +101,7 @@ data: postgresql_conn_str: {{.PostgreSQLConnectionStringBase64}} ` - triggerAuthenticationTemplate = `keda.sh/v1alpha1 + triggerAuthenticationTemplate = `apiVersion: keda.sh/v1alpha1 kind: TriggerAuthentication metadata: name: {{.TriggerAuthenticationName}} @@ -173,8 +173,8 @@ kind: Service metadata: labels: app: {{.PostgreSQLStatefulSetName}} - name: {{.PostgreSQLStatefulSetName}} - namespace: {{.TestNamespace}} + name: {{.PostgreSQLStatefulSetName}} + namespace: {{.TestNamespace}} spec: ports: - port: 5432 From d3a1a8899d669504b64162e926bcaceefdd78269 Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Fri, 15 Jul 2022 10:19:55 +0200 Subject: [PATCH 6/6] try to init PostgreSQL table multiple times Signed-off-by: Zbynek Roubalik --- tests/helper/helper.go | 16 ++++++++++++++++ tests/scalers_go/postgresql/postgresql_test.go | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/helper/helper.go b/tests/helper/helper.go index f286c56a5b7..6e1e986ada0 100644 --- a/tests/helper/helper.go +++ b/tests/helper/helper.go @@ -141,6 +141,22 @@ func ExecCommandOnSpecificPod(t *testing.T, podName string, namespace string, co return out, errOut, err } +func WaitForSuccessfulExecCommandOnSpecificPod(t *testing.T, podName string, namespace string, command string, iterations, intervalSeconds int) (bool, string, string, error) { + var out, errOut string + var err error + for i := 0; i < iterations; i++ { + out, errOut, err = ExecCommandOnSpecificPod(t, podName, namespace, command) + t.Logf("Waiting for successful execution of command on Pod; Output: %s, Error: %s", out, errOut) + if err == nil { + return true, out, errOut, err + } + + time.Sleep(time.Duration(intervalSeconds) * time.Second) + } + + return false, out, errOut, err +} + func GetKubernetesClient(t *testing.T) *kubernetes.Clientset { if KubeClient != nil && KubeConfig != nil { return KubeClient diff --git a/tests/scalers_go/postgresql/postgresql_test.go b/tests/scalers_go/postgresql/postgresql_test.go index cc147de2298..3999e0b3298 100644 --- a/tests/scalers_go/postgresql/postgresql_test.go +++ b/tests/scalers_go/postgresql/postgresql_test.go @@ -228,9 +228,9 @@ func TestPostreSQLScaler(t *testing.T) { "replica count should be %s after 3 minute", 1) createTableSQL := "CREATE TABLE task_instance (id serial PRIMARY KEY,state VARCHAR(10));" - out, errOut, err := ExecCommandOnSpecificPod(t, postgresqlPodName, testNamespace, fmt.Sprintf("psql -U %s -d %s -c \"%s\"", postgreSQLUsername, postgreSQLDatabase, createTableSQL)) - t.Logf("Output: %s, Error: %s", out, errOut) - assert.Nil(t, err, "executing a command on PostreSQL Pod should work") + ok, out, errOut, err := WaitForSuccessfulExecCommandOnSpecificPod(t, postgresqlPodName, testNamespace, + fmt.Sprintf("psql -U %s -d %s -c \"%s\"", postgreSQLUsername, postgreSQLDatabase, createTableSQL), 60, 3) + assert.True(t, ok, "executing a command on PostreSQL Pod should work; Output: %s, ErrorOutput: %s, Error: %s", out, errOut, err) // Create kubernetes resources for testing data, templates := getTemplateData()