Skip to content

Commit

Permalink
refactor(tests): clean artefacts on panic
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Feb 28, 2024
1 parent a2a99bf commit 0cdce9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 26 additions & 13 deletions tests/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
)

const (
suiteTimeout = time.Minute * 20
retryInterval = time.Second * 10
createTimeout = time.Second * 15
waitRunningTimeout = time.Minute * 20
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
})
}
Expand Down

0 comments on commit 0cdce9c

Please sign in to comment.