From 84a5f22934f03d04fe5bf8b56fd3ef1d819cb601 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Wed, 10 Jul 2024 10:26:40 +0200 Subject: [PATCH] fix(tests): assert destroy errors --- controllers/clickhousegrant_controller.go | 2 +- tests/cassandra_test.go | 2 +- tests/clickhouse_test.go | 2 +- tests/clickhousegrant_test.go | 2 +- tests/clickhouseuser_test.go | 2 +- tests/connectionpool_test.go | 2 +- tests/database_test.go | 2 +- tests/generic_service_handler_test.go | 4 ++-- tests/grafana_test.go | 2 +- tests/kafka_test.go | 2 +- tests/kafka_with_projectvpc_ref_test.go | 2 +- tests/kafkaacl_test.go | 2 +- tests/kafkaconnect_test.go | 2 +- tests/kafkaschema_test.go | 2 +- tests/kafkatopic_test.go | 2 +- tests/kafkschemaregistryaacl_test.go | 2 +- tests/mysql_test.go | 2 +- tests/opensearch_test.go | 2 +- tests/postgresql_test.go | 6 +++--- tests/project_test.go | 2 +- tests/projectvpc_test.go | 4 ++-- tests/redis_test.go | 2 +- tests/service_opts_test.go | 4 ++-- tests/serviceintegration_test.go | 14 +++++++------- tests/serviceintegrationendpoint_test.go | 4 ++-- tests/serviceuser_test.go | 4 ++-- tests/session.go | 20 ++++++++++++++++---- 27 files changed, 55 insertions(+), 43 deletions(-) diff --git a/controllers/clickhousegrant_controller.go b/controllers/clickhousegrant_controller.go index a0ce655a..17a191dd 100644 --- a/controllers/clickhousegrant_controller.go +++ b/controllers/clickhousegrant_controller.go @@ -5,9 +5,9 @@ package controllers import ( "context" "fmt" + "net/http" "slices" "strconv" - "strings" "github.com/aiven/aiven-go-client/v2" avngen "github.com/aiven/go-client-codegen" diff --git a/tests/cassandra_test.go b/tests/cassandra_test.go index 7a992d9f..c3675e14 100644 --- a/tests/cassandra_test.go +++ b/tests/cassandra_test.go @@ -56,7 +56,7 @@ func TestCassandra(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/clickhouse_test.go b/tests/clickhouse_test.go index 63e96300..9825d246 100644 --- a/tests/clickhouse_test.go +++ b/tests/clickhouse_test.go @@ -65,7 +65,7 @@ func TestClickhouse(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/clickhousegrant_test.go b/tests/clickhousegrant_test.go index 853b050e..974a2453 100644 --- a/tests/clickhousegrant_test.go +++ b/tests/clickhousegrant_test.go @@ -133,7 +133,7 @@ func TestClickhouseGrant(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/clickhouseuser_test.go b/tests/clickhouseuser_test.go index d8c9c9c9..4b492a5d 100644 --- a/tests/clickhouseuser_test.go +++ b/tests/clickhouseuser_test.go @@ -33,7 +33,7 @@ func TestClickhouseUser(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/connectionpool_test.go b/tests/connectionpool_test.go index 07bfbedc..7f3079be 100644 --- a/tests/connectionpool_test.go +++ b/tests/connectionpool_test.go @@ -33,7 +33,7 @@ func TestConnectionPool(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/database_test.go b/tests/database_test.go index 434c4721..1ab9f477 100644 --- a/tests/database_test.go +++ b/tests/database_test.go @@ -55,7 +55,7 @@ func TestDatabase(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/generic_service_handler_test.go b/tests/generic_service_handler_test.go index 7ae8307d..3b70a9e4 100644 --- a/tests/generic_service_handler_test.go +++ b/tests/generic_service_handler_test.go @@ -70,7 +70,7 @@ func TestCreateUpdateService(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest @@ -130,7 +130,7 @@ func TestErrorCondition(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/grafana_test.go b/tests/grafana_test.go index ae30124d..6d65262c 100644 --- a/tests/grafana_test.go +++ b/tests/grafana_test.go @@ -55,7 +55,7 @@ func TestGrafana(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/kafka_test.go b/tests/kafka_test.go index 981fcf65..873d5a6c 100644 --- a/tests/kafka_test.go +++ b/tests/kafka_test.go @@ -57,7 +57,7 @@ func TestKafka(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/kafka_with_projectvpc_ref_test.go b/tests/kafka_with_projectvpc_ref_test.go index 93bfc520..94b5c1b5 100644 --- a/tests/kafka_with_projectvpc_ref_test.go +++ b/tests/kafka_with_projectvpc_ref_test.go @@ -60,7 +60,7 @@ func TestKafkaWithProjectVPCRef(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/kafkaacl_test.go b/tests/kafkaacl_test.go index 36d7dde2..35b9984e 100644 --- a/tests/kafkaacl_test.go +++ b/tests/kafkaacl_test.go @@ -77,7 +77,7 @@ func TestKafkaACL(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/kafkaconnect_test.go b/tests/kafkaconnect_test.go index cc751449..07ca29d9 100644 --- a/tests/kafkaconnect_test.go +++ b/tests/kafkaconnect_test.go @@ -55,7 +55,7 @@ func TestKafkaConnect(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/kafkaschema_test.go b/tests/kafkaschema_test.go index 3ce2d69d..7187b9a0 100644 --- a/tests/kafkaschema_test.go +++ b/tests/kafkaschema_test.go @@ -76,7 +76,7 @@ func TestKafkaSchema(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/kafkatopic_test.go b/tests/kafkatopic_test.go index ab24edb1..2014c6d7 100644 --- a/tests/kafkatopic_test.go +++ b/tests/kafkatopic_test.go @@ -85,7 +85,7 @@ func TestKafkaTopic(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/kafkschemaregistryaacl_test.go b/tests/kafkschemaregistryaacl_test.go index c39326d0..67c8ec2e 100644 --- a/tests/kafkschemaregistryaacl_test.go +++ b/tests/kafkschemaregistryaacl_test.go @@ -79,7 +79,7 @@ func TestKafkaSchemaRegistryACL(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/mysql_test.go b/tests/mysql_test.go index 28d90bdd..502aa671 100644 --- a/tests/mysql_test.go +++ b/tests/mysql_test.go @@ -55,7 +55,7 @@ func TestMySQL(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/opensearch_test.go b/tests/opensearch_test.go index 761714a8..994d9025 100644 --- a/tests/opensearch_test.go +++ b/tests/opensearch_test.go @@ -60,7 +60,7 @@ func TestOpenSearch(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/postgresql_test.go b/tests/postgresql_test.go index 48123889..47a103bc 100644 --- a/tests/postgresql_test.go +++ b/tests/postgresql_test.go @@ -77,7 +77,7 @@ func TestPgReadReplica(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest @@ -195,7 +195,7 @@ func TestPgCustomPrefix(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest @@ -287,7 +287,7 @@ func TestPgUpgradeVersion(t *testing.T) { yaml := getPgUpgradeVersionYaml(cfg.Project, pgName, cfg.PrimaryCloudName, startingVersion) s := NewSession(ctx, k8sClient, cfg.Project) - defer s.Destroy() + defer s.Destroy(t) require.NoError(t, s.Apply(yaml)) diff --git a/tests/project_test.go b/tests/project_test.go index cbbf408e..178e5190 100644 --- a/tests/project_test.go +++ b/tests/project_test.go @@ -43,7 +43,7 @@ func TestProject(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/projectvpc_test.go b/tests/projectvpc_test.go index 28454b39..0b1d66c7 100644 --- a/tests/projectvpc_test.go +++ b/tests/projectvpc_test.go @@ -78,7 +78,7 @@ func TestProjectVPCID(t *testing.T) { vpcSession := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer vpcSession.Destroy() + defer vpcSession.Destroy(t) // WHEN // Applies given manifest @@ -115,7 +115,7 @@ func TestProjectVPCID(t *testing.T) { kafkaSession := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer kafkaSession.Destroy() + defer kafkaSession.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/redis_test.go b/tests/redis_test.go index 4429c796..939526ba 100644 --- a/tests/redis_test.go +++ b/tests/redis_test.go @@ -52,7 +52,7 @@ func TestRedis(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/service_opts_test.go b/tests/service_opts_test.go index 255322f1..98be9992 100644 --- a/tests/service_opts_test.go +++ b/tests/service_opts_test.go @@ -49,7 +49,7 @@ func TestServiceTechnicalEmails(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest @@ -121,7 +121,7 @@ func runTest(t *testing.T, scenario TestScenario) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/serviceintegration_test.go b/tests/serviceintegration_test.go index 94258d04..698279eb 100644 --- a/tests/serviceintegration_test.go +++ b/tests/serviceintegration_test.go @@ -35,7 +35,7 @@ func TestServiceIntegrationClickhousePostgreSQL(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest @@ -71,15 +71,15 @@ func TestServiceIntegrationClickhousePostgreSQL(t *testing.T) { assert.Equal(t, pgAvn.CloudName, pg.Spec.CloudName) assert.Equal(t, pgAvn.MaintenanceWindow.DayOfWeek, pg.Spec.MaintenanceWindowDow) assert.Equal(t, pgAvn.MaintenanceWindow.TimeOfDay, pg.Spec.MaintenanceWindowTime) - assert.Equal(t, pgAvn.UserConfig["pg_version"].(string), *pg.Spec.UserConfig.PgVersion) + assert.EqualValues(t, pgAvn.UserConfig["pg_version"], ptrValue(pg.Spec.UserConfig.PgVersion)) // Validates ServiceIntegration siAvn, err := avnClient.ServiceIntegrations.Get(ctx, cfg.Project, si.Status.ID) require.NoError(t, err) assert.Equal(t, "clickhouse_postgresql", siAvn.IntegrationType) assert.Equal(t, siAvn.IntegrationType, si.Spec.IntegrationType) - assert.Equal(t, pgName, *siAvn.SourceService) - assert.Equal(t, chName, *siAvn.DestinationService) + assert.Equal(t, pgName, ptrValue(siAvn.SourceService)) + assert.Equal(t, chName, ptrValue(siAvn.DestinationService)) assert.True(t, siAvn.Active) assert.True(t, siAvn.Enabled) } @@ -107,7 +107,7 @@ func TestServiceIntegrationKafkaLogs(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest @@ -176,7 +176,7 @@ func TestServiceIntegrationKafkaConnect(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest @@ -254,7 +254,7 @@ func TestServiceIntegrationDatadog(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/serviceintegrationendpoint_test.go b/tests/serviceintegrationendpoint_test.go index e54b0847..f4650351 100644 --- a/tests/serviceintegrationendpoint_test.go +++ b/tests/serviceintegrationendpoint_test.go @@ -27,7 +27,7 @@ func TestServiceIntegrationEndpointExternalPostgres(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest @@ -70,7 +70,7 @@ func TestServiceIntegrationEndpoint(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/serviceuser_test.go b/tests/serviceuser_test.go index 303d073e..82afcc06 100644 --- a/tests/serviceuser_test.go +++ b/tests/serviceuser_test.go @@ -67,7 +67,7 @@ func TestServiceUserKafka(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest @@ -189,7 +189,7 @@ func TestServiceUserPg(t *testing.T) { s := NewSession(ctx, k8sClient, cfg.Project) // Cleans test afterward - defer s.Destroy() + defer s.Destroy(t) // WHEN // Applies given manifest diff --git a/tests/session.go b/tests/session.go index 491ed3b2..677a161b 100644 --- a/tests/session.go +++ b/tests/session.go @@ -42,7 +42,7 @@ type Session interface { Apply(src string) error GetRunning(obj client.Object, keys ...string) error GetSecret(keys ...string) (*corev1.Secret, error) - Destroy() + Destroy(t testingT) Delete(o client.Object, exists func() error) error } @@ -150,12 +150,16 @@ func (s *session) GetSecret(keys ...string) (*corev1.Secret, error) { return secret, nil } +type testingT interface { + Errorf(format string, args ...any) +} + // Destroy deletes all applied resources. // Tolerant to "not found" error, // because resource may have been deleted manually -func (s *session) Destroy() { +func (s *session) Destroy(t testingT) { if err := recover(); err != nil { - log.Printf("panicked, deleting resources: %s", err) + t.Errorf("panicked, deleting resources: %s\n%s", err, debug.Stack()) } var wg sync.WaitGroup @@ -166,7 +170,7 @@ func (s *session) Destroy() { defer s.recover() err := s.delete(s.objs[n]) if !(err == nil || isNotFound(err)) { - log.Printf("failed to delete %q: %s", n, err) + t.Errorf("failed to delete %q: %s", n, err) } }(n) } @@ -357,3 +361,11 @@ func parseObjs(src string) (map[string]client.Object, error) { } return objs, nil } + +func ptrValue[T any](v *T) T { + if v == nil { + var empty T + return empty + } + return *v +}