Skip to content

Commit

Permalink
fix(tests): assert destroy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Jul 10, 2024
1 parent aa12ace commit cf57b7f
Show file tree
Hide file tree
Showing 27 changed files with 54 additions and 43 deletions.
2 changes: 1 addition & 1 deletion controllers/clickhousegrant_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/clickhousegrant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/clickhouseuser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/connectionpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/generic_service_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/grafana_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/kafka_with_projectvpc_ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/kafkaacl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/kafkaconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/kafkaschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/kafkatopic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/kafkschemaregistryaacl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/opensearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion tests/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/projectvpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/service_opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions tests/serviceintegration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,15 +71,14 @@ 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)

// 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)
}
Expand Down Expand Up @@ -107,7 +106,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
Expand Down Expand Up @@ -176,7 +175,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
Expand Down Expand Up @@ -254,7 +253,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
Expand Down
4 changes: 2 additions & 2 deletions tests/serviceintegrationendpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/serviceuser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions tests/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}

0 comments on commit cf57b7f

Please sign in to comment.