Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky controller-manager tests #1445

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ func TestRepoSyncReconcilerDeploymentLifecycle(t *testing.T) {
errCh := startControllerManager(ctx, t, fakeClient, testReconciler)

// Wait for manager to exit before returning
defer func() {
cancel()
t.Log("waiting for controller-manager to stop")
for err := range errCh {
require.NoError(t, err)
}
}()
defer stopControllerManager(t, cancel, errCh)

watchCtx, watchCancel := context.WithTimeout(ctx, 10*time.Second)
defer watchCancel()
Expand Down Expand Up @@ -187,13 +181,7 @@ func TestReconcileInvalidRepoSyncLifecycle(t *testing.T) {
errCh := startControllerManager(ctx, t, fakeClient, testReconciler)

// Wait for manager to exit before returning
defer func() {
cancel()
t.Log("waiting for controller-manager to stop")
for err := range errCh {
require.NoError(t, err)
}
}()
defer stopControllerManager(t, cancel, errCh)

t.Log("watching for RepoSync status update")
watchCtx, watchCancel := context.WithTimeout(ctx, 10*time.Second)
Expand Down Expand Up @@ -260,13 +248,7 @@ func TestReconcileRepoSyncLifecycleValidToInvalid(t *testing.T) {
errCh := startControllerManager(ctx, t, fakeClient, testReconciler)

// Wait for manager to exit before returning
defer func() {
cancel()
t.Log("waiting for controller-manager to stop")
for err := range errCh {
require.NoError(t, err)
}
}()
defer stopControllerManager(t, cancel, errCh)

reconcilerKey := core.NsReconcilerObjectKey(rs.Namespace, rs.Name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/go-logr/logr/testr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -78,13 +79,7 @@ func TestRootSyncReconcilerDeploymentLifecycle(t *testing.T) {
errCh := startControllerManager(ctx, t, fakeClient, testReconciler)

// Wait for manager to exit before returning
defer func() {
cancel()
t.Log("waiting for controller-manager to stop")
for err := range errCh {
require.NoError(t, err)
}
}()
defer stopControllerManager(t, cancel, errCh)

watchCtx, watchCancel := context.WithTimeout(ctx, 10*time.Second)
defer watchCancel()
Expand Down Expand Up @@ -201,13 +196,7 @@ func TestReconcileInvalidRootSyncLifecycle(t *testing.T) {
errCh := startControllerManager(ctx, t, fakeClient, testReconciler)

// Wait for manager to exit before returning
defer func() {
cancel()
t.Log("waiting for controller-manager to stop")
for err := range errCh {
require.NoError(t, err)
}
}()
defer stopControllerManager(t, cancel, errCh)

t.Log("watching for RootSync status update")
watchCtx, watchCancel := context.WithTimeout(ctx, 10*time.Second)
Expand Down Expand Up @@ -274,13 +263,7 @@ func TestReconcileRootSyncLifecycleValidToInvalid1(t *testing.T) {
errCh := startControllerManager(ctx, t, fakeClient, testReconciler)

// Wait for manager to exit before returning
defer func() {
cancel()
t.Log("waiting for controller-manager to stop")
for err := range errCh {
require.NoError(t, err)
}
}()
defer stopControllerManager(t, cancel, errCh)

reconcilerKey := core.RootReconcilerObjectKey(rs.Name)

Expand Down Expand Up @@ -517,13 +500,7 @@ func testDriftProtection(t *testing.T, fakeClient *syncerFake.Client, testReconc
errCh := startControllerManager(ctx, t, fakeClient, testReconciler)

// Wait for manager to exit before returning
defer func() {
cancel()
t.Log("waiting for controller-manager to stop")
for err := range errCh {
require.NoError(t, err)
}
}()
defer stopControllerManager(t, cancel, errCh)

key := objKeyFunc(client.ObjectKeyFromObject(syncObj))

Expand Down Expand Up @@ -650,6 +627,16 @@ func startControllerManager(ctx context.Context, t *testing.T, fakeClient *synce
return errCh
}

func stopControllerManager(t *testing.T, cancel context.CancelFunc, errCh <-chan error) {
cancel()
t.Log("waiting for controller-manager to stop")
for err := range errCh {
// Error/Assert instead of Fatal/Require, to avoid panic when called async.
// Go tests that panic in a defer can be flakey.
assert.NoError(t, err)
}
}

func logObjectYAMLIfFailed(t *testing.T, fakeClient *syncerFake.Client, obj client.Object) {
if t.Failed() {
err := fakeClient.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)
Expand Down