From 2ef2816a6c56be04122556cceb2d57764efa1568 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Wed, 28 Feb 2024 11:13:19 +0100 Subject: [PATCH] refactor(tests): clean artefacts on panic --- Makefile | 2 +- tests/session.go | 39 ++++++++++++++++++++++++++------------- tests/suite_test.go | 3 +-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 35bb5ef34..41af6f2a4 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 60m) -parallel 10 -cover -coverpkg=./controllers -covermode=atomic -coverprofile=coverage.out ##@ Build diff --git a/tests/session.go b/tests/session.go index e592fcd9e..568bff1f2 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 }) } diff --git a/tests/suite_test.go b/tests/suite_test.go index 39d7824a7..71e2b94bf 100644 --- a/tests/suite_test.go +++ b/tests/suite_test.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "os" "runtime/debug" "testing" @@ -49,7 +48,7 @@ func TestMain(m *testing.M) { } defer teardownSuite(env) - os.Exit(m.Run()) + m.Run() } func teardownSuite(env *envtest.Environment) {