From 6f8a2aecf132267fd051d28476af8529cd686479 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Wed, 28 Feb 2024 11:13:19 +0100 Subject: [PATCH 1/3] refactor(tests): clean artefacts on panic --- tests/session.go | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/tests/session.go b/tests/session.go index e592fcd9..568bff1f 100644 --- a/tests/session.go +++ b/tests/session.go @@ -27,6 +27,7 @@ import ( ) const ( + suiteTimeout = time.Minute * 20 retryInterval = time.Second * 10 createTimeout = time.Second * 15 waitRunningTimeout = time.Minute * 20 @@ -45,20 +46,23 @@ type Session interface { var _ Session = &session{} type session struct { - k8s client.Client - avn *aiven.Client - ctx context.Context - objs map[string]client.Object - project string + k8s client.Client + avn *aiven.Client + ctx context.Context + cancelCtx func() + objs map[string]client.Object + project string } func NewSession(k8s client.Client, avn *aiven.Client, project string) Session { + ctx, cancel := context.WithTimeout(context.Background(), suiteTimeout) s := &session{ - k8s: k8s, - avn: avn, - ctx: context.Background(), - objs: make(map[string]client.Object), - project: project, + k8s: k8s, + avn: avn, + ctx: ctx, + cancelCtx: cancel, + objs: make(map[string]client.Object), + project: project, } return s } @@ -142,6 +146,12 @@ func (s *session) GetSecret(keys ...string) (*corev1.Secret, error) { // Tolerant to "not found" error, // because resource may have been deleted manually func (s *session) Destroy() { + defer s.cancelCtx() + + if err := recover(); err != nil { + log.Printf("panicked, deleting resources: %s", err) + } + var wg sync.WaitGroup wg.Add(len(s.objs)) for n := range s.objs { @@ -183,15 +193,18 @@ func (s *session) delete(o client.Object) error { return fmt.Errorf("resource %q not applied", o.GetName()) } - err := s.k8s.Delete(s.ctx, o) + // Delete operation doesn't share the context, + // because it shouldn't leave artifacts + ctx := context.Background() + err := s.k8s.Delete(ctx, o) if err != nil { return fmt.Errorf("kubernetes error: %w", err) } // Waits being deleted from kube key := types.NamespacedName{Name: o.GetName(), Namespace: o.GetNamespace()} - return retryForever(s.ctx, fmt.Sprintf("delete %s", o.GetName()), func() (bool, error) { - err := s.k8s.Get(s.ctx, key, o) + return retryForever(ctx, fmt.Sprintf("delete %s", o.GetName()), func() (bool, error) { + err := s.k8s.Get(ctx, key, o) return !isNotFound(err), nil }) } From 839dfe7692bbd00f953257943f3aae2f30103111 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Tue, 19 Dec 2023 13:10:12 +0100 Subject: [PATCH 2/3] ci(tests): run each test as a separate job --- .github/workflows/tests.yml | 21 +++++++++++++++++++-- Makefile | 2 +- tests/session.go | 26 ++++++++++---------------- tests/suite_test.go | 7 +++++++ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3cd11830..1504812b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,9 +21,26 @@ jobs: AIVEN_TOKEN: ${{ secrets.AIVEN_TOKEN }} AIVEN_PROJECT_NAME_PREFIX: ${{ secrets.AIVEN_PROJECT_NAME_PREFIX }} + find_tests: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.find_tests.outputs.matrix }} + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + - id: find_tests + run: | + echo "matrix=$(LIST_ONLY=1 go test ./tests/... -list=. | grep Test | jq -cnR '[inputs | select(length>0)]')" >> $GITHUB_OUTPUT + test: - needs: setup_aiven_project_suffix + needs: [setup_aiven_project_suffix, find_tests] runs-on: ubuntu-latest + strategy: + max-parallel: 5 + matrix: + name: ${{ fromJson(needs.find_tests.outputs.matrix) }} steps: - uses: actions/checkout@v4 with: @@ -31,7 +48,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version-file: go.mod - - run: make test + - run: make run="^${{ matrix.name }}$" test env: AIVEN_TOKEN: ${{ secrets.AIVEN_TOKEN }} AIVEN_PROJECT_NAME: >- diff --git a/Makefile b/Makefile index e7590d15..45763076 100644 --- a/Makefile +++ b/Makefile @@ -108,7 +108,7 @@ test-e2e-preinstalled: check-env-vars check-avn-client ## Run end-to-end tests u test: envtest ## Run tests. To target a specific test, use 'run=TestName make test'. export KUBEBUILDER_ASSETS=$(shell eval ${KUBEBUILDER_ASSETS_CMD}); \ - go test ./tests/... -race -run=$(run) -v $(if $(run), -timeout 20m, -timeout 60m) -parallel 10 -cover -coverpkg=./controllers -covermode=atomic -coverprofile=coverage.out + go test ./tests/... -race -run=$(run) -v $(if $(run), -timeout 30m, -timeout 60m) -parallel 10 -cover -coverpkg=./controllers -covermode=atomic -coverprofile=coverage.out ##@ Build diff --git a/tests/session.go b/tests/session.go index 568bff1f..2f2da85c 100644 --- a/tests/session.go +++ b/tests/session.go @@ -27,7 +27,6 @@ import ( ) const ( - suiteTimeout = time.Minute * 20 retryInterval = time.Second * 10 createTimeout = time.Second * 15 waitRunningTimeout = time.Minute * 20 @@ -46,23 +45,20 @@ type Session interface { var _ Session = &session{} type session struct { - k8s client.Client - avn *aiven.Client - ctx context.Context - cancelCtx func() - objs map[string]client.Object - project string + k8s client.Client + avn *aiven.Client + ctx context.Context + objs map[string]client.Object + project string } func NewSession(k8s client.Client, avn *aiven.Client, project string) Session { - ctx, cancel := context.WithTimeout(context.Background(), suiteTimeout) s := &session{ - k8s: k8s, - avn: avn, - ctx: ctx, - cancelCtx: cancel, - objs: make(map[string]client.Object), - project: project, + k8s: k8s, + avn: avn, + ctx: context.Background(), + objs: make(map[string]client.Object), + project: project, } return s } @@ -146,8 +142,6 @@ func (s *session) GetSecret(keys ...string) (*corev1.Secret, error) { // Tolerant to "not found" error, // because resource may have been deleted manually func (s *session) Destroy() { - defer s.cancelCtx() - if err := recover(); err != nil { log.Printf("panicked, deleting resources: %s", err) } diff --git a/tests/suite_test.go b/tests/suite_test.go index 39d7824a..6f399e3b 100644 --- a/tests/suite_test.go +++ b/tests/suite_test.go @@ -43,6 +43,13 @@ type testConfig struct { } func TestMain(m *testing.M) { + if os.Getenv("LIST_ONLY") != "" { + // For go test ./... -list=. + // Lists test names without running them. + m.Run() + return + } + env, err := setupSuite() if err != nil { log.Fatal(err) From b2bff69081e7a34eb3c0ed864d7bde993b449850 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Thu, 29 Feb 2024 13:32:00 +0100 Subject: [PATCH 3/3] refactor(tests): set test ctx timeout --- .github/workflows/tests.yml | 2 +- tests/cassandra_test.go | 7 +++-- tests/clickhouse_test.go | 9 +++--- tests/clickhouseuser_test.go | 9 +++--- tests/connectionpool_test.go | 9 +++--- tests/database_test.go | 9 +++--- tests/generic_service_handler_test.go | 17 ++++++----- tests/grafana_test.go | 9 +++--- tests/kafka_test.go | 9 +++--- tests/kafka_with_projectvpc_ref_test.go | 9 +++--- tests/kafkaacl_test.go | 9 +++--- tests/kafkaconnect_test.go | 9 +++--- tests/kafkaschema_test.go | 9 +++--- tests/kafkatopic_test.go | 9 +++--- tests/mysql_test.go | 9 +++--- tests/opensearch_test.go | 9 +++--- tests/postgresql_test.go | 23 +++++++++------ tests/project_test.go | 9 +++--- tests/projectvpc_test.go | 15 +++++----- tests/redis_test.go | 9 +++--- tests/service_opts_test.go | 16 +++++++---- tests/serviceintegration_test.go | 38 ++++++++++++++++--------- tests/serviceuser_test.go | 13 +++++---- tests/session.go | 6 ++-- tests/suite_test.go | 22 +++++++++----- 25 files changed, 169 insertions(+), 125 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1504812b..94be2f9b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -56,7 +56,7 @@ jobs: sweep: if: always() - needs: [test, setup_aiven_project_suffix] + needs: [setup_aiven_project_suffix, test] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/tests/cassandra_test.go b/tests/cassandra_test.go index 4d7bfdf6..37368923 100644 --- a/tests/cassandra_test.go +++ b/tests/cassandra_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -49,10 +48,12 @@ func TestCassandra(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("cassandra") yml := getCassandraYaml(cfg.Project, name, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward defer s.Destroy() diff --git a/tests/clickhouse_test.go b/tests/clickhouse_test.go index 6c163703..05251c0b 100644 --- a/tests/clickhouse_test.go +++ b/tests/clickhouse_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -45,12 +44,14 @@ func TestClickhouse(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("clickhouse") yml := getClickhouseYaml(cfg.Project, name, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/clickhouseuser_test.go b/tests/clickhouseuser_test.go index 93ad6f60..2e7ff0ad 100644 --- a/tests/clickhouseuser_test.go +++ b/tests/clickhouseuser_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -55,13 +54,15 @@ func TestClickhouseUser(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + chName := randName("clickhouse-user") userName := randName("clickhouse-user") yml := getClickhouseUserYaml(cfg.Project, chName, userName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/connectionpool_test.go b/tests/connectionpool_test.go index 19a42b65..2a0ff2fb 100644 --- a/tests/connectionpool_test.go +++ b/tests/connectionpool_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -79,15 +78,17 @@ func TestConnectionPool(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + pgName := randName("connection-pool") dbName := randName("connection-pool") userName := randName("connection-pool") poolName := randName("connection-pool") yml := getConnectionPoolYaml(cfg.Project, pgName, dbName, userName, poolName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/database_test.go b/tests/database_test.go index 6e2c5c7c..7d010e51 100644 --- a/tests/database_test.go +++ b/tests/database_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -51,13 +50,15 @@ func TestDatabase(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + pgName := randName("database") dbName := randName("database") yml := getDatabaseYaml(cfg.Project, pgName, dbName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/generic_service_handler_test.go b/tests/generic_service_handler_test.go index 79a19e7d..d1944f61 100644 --- a/tests/generic_service_handler_test.go +++ b/tests/generic_service_handler_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" "time" @@ -58,12 +57,14 @@ func TestCreateUpdateService(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + pgName := randName("generic-handler") ymlCreate := getCreateServiceYaml(cfg.Project, pgName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN @@ -116,12 +117,14 @@ func TestErrorCondition(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + pgName := randName("generic-handler") yml := getErrorConditionYaml(cfg.Project, pgName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/grafana_test.go b/tests/grafana_test.go index 897fc42f..373e847a 100644 --- a/tests/grafana_test.go +++ b/tests/grafana_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -48,12 +47,14 @@ func TestGrafana(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("grafana") yml := getGrafanaYaml(cfg.Project, name, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/kafka_test.go b/tests/kafka_test.go index 0bf76bc3..bf252107 100644 --- a/tests/kafka_test.go +++ b/tests/kafka_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -50,12 +49,14 @@ func TestKafka(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("kafka") yml := getKafkaYaml(cfg.Project, name, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/kafka_with_projectvpc_ref_test.go b/tests/kafka_with_projectvpc_ref_test.go index 0fd9a8c5..3b9871ce 100644 --- a/tests/kafka_with_projectvpc_ref_test.go +++ b/tests/kafka_with_projectvpc_ref_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -52,13 +51,15 @@ func TestKafkaWithProjectVPCRef(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + vpcName := randName("kafka-vpc") kafkaName := randName("kafka-vpc") yml := getKafkaWithProjectVPCRefYaml(cfg.Project, vpcName, kafkaName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/kafkaacl_test.go b/tests/kafkaacl_test.go index d18c9d38..2559b0bb 100644 --- a/tests/kafkaacl_test.go +++ b/tests/kafkaacl_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -68,14 +67,16 @@ func TestKafkaACL(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + kafkaName := randName("kafka-acl") topicName := randName("kafka-acl") aclName := randName("kafka-acl") yml := getKafkaACLYaml(cfg.Project, kafkaName, topicName, aclName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/kafkaconnect_test.go b/tests/kafkaconnect_test.go index 56badb4b..f5b061e1 100644 --- a/tests/kafkaconnect_test.go +++ b/tests/kafkaconnect_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -48,12 +47,14 @@ func TestKafkaConnect(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("kafka-connect") yml := getKafkaConnectYaml(cfg.Project, name, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/kafkaschema_test.go b/tests/kafkaschema_test.go index 628d573f..bbd4f7b8 100644 --- a/tests/kafkaschema_test.go +++ b/tests/kafkaschema_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "encoding/json" "fmt" "testing" @@ -67,14 +66,16 @@ func TestKafkaSchema(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + kafkaName := randName("kafka-schema") schemaName := randName("kafka-schema") subjectName := randName("kafka-schema") yml := getKafkaSchemaYaml(cfg.Project, kafkaName, schemaName, subjectName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/kafkatopic_test.go b/tests/kafkatopic_test.go index 226d0fe9..ec8da332 100644 --- a/tests/kafkatopic_test.go +++ b/tests/kafkatopic_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -74,12 +73,14 @@ func TestKafkaTopic(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + ksName := randName("kafka-topic") yml := getKafkaTopicNameYaml(cfg.Project, ksName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/mysql_test.go b/tests/mysql_test.go index 427ffd64..29734f50 100644 --- a/tests/mysql_test.go +++ b/tests/mysql_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -48,12 +47,14 @@ func TestMySQL(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("mysql") yml := getMySQLYaml(cfg.Project, name, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/opensearch_test.go b/tests/opensearch_test.go index 23db6102..637cb6c1 100644 --- a/tests/opensearch_test.go +++ b/tests/opensearch_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -53,12 +52,14 @@ func TestOpenSearch(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("opensearch") yml := getOpenSearchYaml(cfg.Project, name, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/postgresql_test.go b/tests/postgresql_test.go index e56a893e..82b696b2 100644 --- a/tests/postgresql_test.go +++ b/tests/postgresql_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -69,13 +68,15 @@ func TestPgReadReplica(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + masterName := randName("pg-master") replicaName := randName("pg-replica") yml := getPgReadReplicaYaml(cfg.Project, masterName, replicaName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN @@ -186,12 +187,14 @@ func TestPgCustomPrefix(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + pgName := randName("secret-prefix") yml := getPgCustomPrefixYaml(cfg.Project, pgName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN @@ -277,10 +280,12 @@ func TestPgUpgradeVersion(t *testing.T) { startingVersion := pgVersions[len(pgVersions)-2] targetVersion := pgVersions[len(pgVersions)-1] - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + pgName := randName("upgrade-test") yaml := getPgUpgradeVersionYaml(cfg.Project, pgName, cfg.PrimaryCloudName, startingVersion) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) defer s.Destroy() diff --git a/tests/project_test.go b/tests/project_test.go index 07ef338d..cbbf408e 100644 --- a/tests/project_test.go +++ b/tests/project_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -36,12 +35,14 @@ func TestProject(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("project") yml := getProjectYaml(name) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/projectvpc_test.go b/tests/projectvpc_test.go index afbe92f2..36b336ea 100644 --- a/tests/projectvpc_test.go +++ b/tests/projectvpc_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -70,13 +69,15 @@ func TestProjectVPCID(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + vpcName1 := randName("project-vpc-id") vpcName2 := randName("project-vpc-id") vpcYaml := getProjectVPCsYaml(cfg.Project, vpcName1, cfg.SecondaryCloudName, vpcName2, cfg.TertiaryCloudName) - vpcSession := NewSession(k8sClient, avnClient, cfg.Project) + vpcSession := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer vpcSession.Destroy() // WHEN @@ -111,9 +112,9 @@ func TestProjectVPCID(t *testing.T) { // Creates Kafka with given vpcID kafkaName := randName("project-vpc-id") kafkaYaml := getKafkaForProjectVPCYaml(cfg.Project, vpc1.Status.ID, kafkaName, cfg.SecondaryCloudName) - kafkaSession := NewSession(k8sClient, avnClient, cfg.Project) + kafkaSession := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer kafkaSession.Destroy() // WHEN @@ -142,8 +143,6 @@ func TestProjectVPCID(t *testing.T) { require.NoError(t, kafkaSession.Apply(kafkaYamlUpd)) // This migration takes too long, so we don't wait it's being in the RUNNING state in kube - ctx, cancel := context.WithTimeout(ctx, waitRunningTimeout) - defer cancel() // Gets Aiven object var kafkaAvnUpd *aiven.Service diff --git a/tests/redis_test.go b/tests/redis_test.go index 41b7b890..a3af0b1b 100644 --- a/tests/redis_test.go +++ b/tests/redis_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -45,12 +44,14 @@ func TestRedis(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("redis") yml := getRedisYaml(cfg.Project, name, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/service_opts_test.go b/tests/service_opts_test.go index 0c39311d..255322f1 100644 --- a/tests/service_opts_test.go +++ b/tests/service_opts_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "testing" @@ -42,12 +41,14 @@ func TestServiceTechnicalEmails(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + name := randName("grafana") yml := getTechnicalEmailsYaml(cfg.Project, name, cfg.PrimaryCloudName, true) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN @@ -114,9 +115,12 @@ func runTest(t *testing.T, scenario TestScenario) { createYaml := getYamlWithDisabledOption(baseYaml, scenario.connInfoSecretTargetDisabledChange[0]) updateYaml := getYamlWithDisabledOption(baseYaml, scenario.connInfoSecretTargetDisabledChange[1]) - s := NewSession(k8sClient, avnClient, cfg.Project) + ctx, cancel := testCtx() + defer cancel() + + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/serviceintegration_test.go b/tests/serviceintegration_test.go index 197c9672..b8ccd7c2 100644 --- a/tests/serviceintegration_test.go +++ b/tests/serviceintegration_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "os" "testing" @@ -78,15 +77,17 @@ func TestServiceIntegrationClickhousePostgreSQL(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + chName := randName("clickhouse-postgresql") pgName := randName("clickhouse-postgresql") siName := randName("clickhouse-postgresql") yml := getClickhousePostgreSQLYaml(cfg.Project, chName, pgName, siName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN @@ -193,15 +194,17 @@ func TestServiceIntegrationKafkaLogs(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + ksName := randName("kafka-logs") ktName := randName("kafka-logs") siName := randName("kafka-logs") yml := getKafkaLogsYaml(cfg.Project, ksName, ktName, siName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN @@ -313,15 +316,17 @@ func TestServiceIntegrationKafkaConnect(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + ksName := randName("kafka-connect") kcName := randName("kafka-connect") siName := randName("kafka-connect") yml := getSIKafkaConnectYaml(cfg.Project, ksName, kcName, siName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN @@ -422,14 +427,16 @@ func TestServiceIntegrationDatadog(t *testing.T) { } // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + + defer cancel() pgName := randName("datadog") siName := randName("datadog") yml := getDatadogYaml(cfg.Project, pgName, siName, endpointID, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN @@ -498,12 +505,15 @@ func TestWebhookMultipleUserConfigsDenied(t *testing.T) { t.Parallel() defer recoverPanic(t) + ctx, cancel := testCtx() + defer cancel() + // GIVEN siName := randName("datadog") yml := getWebhookMultipleUserConfigsDeniedYaml(cfg.Project, siName) // WHEN - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) // THEN err := s.Apply(yml) diff --git a/tests/serviceuser_test.go b/tests/serviceuser_test.go index 9ca48e0e..7defd6dc 100644 --- a/tests/serviceuser_test.go +++ b/tests/serviceuser_test.go @@ -1,7 +1,6 @@ package tests import ( - "context" "fmt" "strconv" "testing" @@ -60,11 +59,12 @@ func TestServiceUserKafka(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() kafkaName := randName("service-user") userName := randName("service-user") yml := getServiceUserKafkaYaml(cfg.Project, kafkaName, userName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward defer s.Destroy() @@ -181,13 +181,14 @@ func TestServiceUserPg(t *testing.T) { defer recoverPanic(t) // GIVEN - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() pgName := randName("connection-pool") userName := randName("connection-pool") yml := getServiceUserPgYaml(cfg.Project, pgName, userName, cfg.PrimaryCloudName) - s := NewSession(k8sClient, avnClient, cfg.Project) + s := NewSession(ctx, k8sClient, cfg.Project) - // Cleans test afterwards + // Cleans test afterward defer s.Destroy() // WHEN diff --git a/tests/session.go b/tests/session.go index 2f2da85c..abe0ea4d 100644 --- a/tests/session.go +++ b/tests/session.go @@ -46,17 +46,15 @@ var _ Session = &session{} type session struct { k8s client.Client - avn *aiven.Client ctx context.Context objs map[string]client.Object project string } -func NewSession(k8s client.Client, avn *aiven.Client, project string) Session { +func NewSession(ctx context.Context, k8s client.Client, project string) Session { s := &session{ k8s: k8s, - avn: avn, - ctx: context.Background(), + ctx: ctx, objs: make(map[string]client.Object), project: project, } diff --git a/tests/suite_test.go b/tests/suite_test.go index 6f399e3b..52d2e189 100644 --- a/tests/suite_test.go +++ b/tests/suite_test.go @@ -7,6 +7,7 @@ import ( "os" "runtime/debug" "testing" + "time" "github.com/aiven/aiven-go-client/v2" "github.com/kelseyhightower/envconfig" @@ -34,12 +35,13 @@ const ( ) type testConfig struct { - Token string `envconfig:"AIVEN_TOKEN" required:"true"` - Project string `envconfig:"AIVEN_PROJECT_NAME" required:"true"` - PrimaryCloudName string `envconfig:"AIVEN_CLOUD_NAME" default:"google-europe-west1"` - SecondaryCloudName string `envconfig:"AIVEN_SECONDARY_CLOUD_NAME" default:"google-europe-west2"` - TertiaryCloudName string `envconfig:"AIVEN_TERTIARY_CLOUD_NAME" default:"google-europe-west3"` - DebugLogging bool `envconfig:"ENABLE_DEBUG_LOGGING"` + Token string `envconfig:"AIVEN_TOKEN" required:"true"` + Project string `envconfig:"AIVEN_PROJECT_NAME" required:"true"` + PrimaryCloudName string `envconfig:"AIVEN_CLOUD_NAME" default:"google-europe-west1"` + SecondaryCloudName string `envconfig:"AIVEN_SECONDARY_CLOUD_NAME" default:"google-europe-west2"` + TertiaryCloudName string `envconfig:"AIVEN_TERTIARY_CLOUD_NAME" default:"google-europe-west3"` + DebugLogging bool `envconfig:"ENABLE_DEBUG_LOGGING"` + TestCaseTimeout time.Duration `envconfig:"TEST_CASE_TIMEOUT" default:"20m"` } func TestMain(m *testing.M) { @@ -126,7 +128,9 @@ func setupSuite() (*envtest.Environment, error) { }, } - ctx := context.Background() + ctx, cancel := testCtx() + defer cancel() + err = k8sClient.Create(ctx, secret) if err != nil { return nil, err @@ -158,3 +162,7 @@ func recoverPanic(t *testing.T) { t.Fail() } } + +func testCtx() (context.Context, func()) { + return context.WithTimeout(context.Background(), cfg.TestCaseTimeout) +}