Skip to content

Commit

Permalink
Shut down all test envs
Browse files Browse the repository at this point in the history
As part of testing pipelines with targets in remote clusters, an
envtest Environment `leafEnv` is created to act as a remote
cluster. But it is left running after the tests, as one could see with
`ps` once `go test` has completed.

Since the controller is run in TestMain, the environment must be shut
down in TestMain -- i.e., not within an individual test. This commit
adds a simple mechanism for keeping track of envs to shut down after
the tests have run, and uses it both for the "main" cluster
environment and the leaf cluster.

Signed-off-by: Michael Bridgen <[email protected]>
  • Loading branch information
squaremo committed Dec 21, 2023
1 parent 2619dfc commit fc68ffb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions controllers/leveltriggered/controller_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestRemoteTargets(t *testing.T) {
if err != nil {
t.Error("starting leaf test env failed", err)
}
envsToStop = append(envsToStop, leafEnv)

user, err := leafEnv.ControlPlane.AddUser(envtest.User{
Name: "leaf-admin",
Expand Down
18 changes: 14 additions & 4 deletions controllers/leveltriggered/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var kubeConfig []byte
var eventRecorder *testEventRecorder
var pipelineReconciler *PipelineReconciler

var envsToStop []*envtest.Environment

type testEvent struct {
object runtime.Object
eventType string
Expand Down Expand Up @@ -100,6 +102,7 @@ func TestMain(m *testing.M) {
if err != nil {
log.Fatalf("starting test env failed: %s", err)
}
envsToStop = append(envsToStop, testEnv)

user, err := testEnv.ControlPlane.AddUser(envtest.User{
Name: "envtest-admin",
Expand Down Expand Up @@ -178,11 +181,18 @@ func TestMain(m *testing.M) {
wg.Wait()
log.Println("manager exited")

err = testEnv.Stop()
if err != nil {
log.Fatalf("stopping test env failed: %s", err)
var failedToStopEnvs bool
for _, env := range envsToStop {
err = env.Stop()
if err != nil {
failedToStopEnvs = true
log.Printf("stopping test env failed: %s\n", err)
}
}
if failedToStopEnvs {
log.Fatalf("failed to stop all test envs")
}
log.Println("test env stopped")

log.Println("test envs stopped")
os.Exit(retCode)
}

0 comments on commit fc68ffb

Please sign in to comment.