refactor integration tests

Aperta
#632 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
62/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go
Ambito
testing

Direzione di ricerca

Start with tests/integration/suite_test.go and its TestIntegration() entrypoint, plus tests/integration/helpers.go and the testhelpers.TestCase abstraction. First goal: split TestIntegration() so individual cases run via go test -run TestName. Second: separate setup from assertions in helpers like testWorkerDeploymentCreation so failures are locatable. Done means each integration case is independently runnable and assertions live in explicitly named functions.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

There are a number of issues with the integration tests that make debugging them difficult:

  1. There is no way to run only one of the integration tests.

This is due to the fact that there is only one go test entrypoint for the integration tests: TestIntegration() in tests/integration/suite_test.go. This monster test method spawns sub- testing.T.Run() invocations for lots of integration tests but because of the way the integration testing framework is built, there's no way for go test tool to filter out named tests.

The ideal experience would be to be able to run a single integration test using the standard go test -run TestName command.

  1. Test assertions and expectations are hidden in various helper functions. These helper functions do not indicate that they assert anything at all, which makes reading an integration test and figuring out what it is actually testing a tedious exercise.

For example, take the testWorkerDeploymentCreation helper function in tests/integration/helpers.go:

// testWorkerDeploymentCreation tests the creation of a WorkerDeployment and waits for the expected status
func testWorkerDeploymentCreation(
	ctx context.Context,
	t *testing.T,
	k8sClient client.Client,
	mgr manager.Manager,
	ts *temporaltest.TestServer,
	tc testhelpers.TestCase,
) {
	twd := tc.GetTWD()
	expectedStatus := tc.GetExpectedStatus()

	t.Log("Creating a Connection")
	temporalConnection := &temporaliov1alpha1.Connection{
		ObjectMeta: metav1.ObjectMeta{
			Name:      twd.Spec.WorkerOptions.ConnectionRef.Name,
			Namespace: twd.Namespace,
		},
		Spec: temporaliov1alpha1.ConnectionSpec{
			HostPort: ts.GetFrontendHostPort(),
		},
	}
	if err := k8sClient.Create(ctx, temporalConnection); err != nil {
		t.Fatalf("failed to create Connection: %v", err)
	}

	env := testhelpers.TestEnv{
		K8sClient:                  k8sClient,
		Mgr:                        mgr,
		Ts:                         ts,
		Connection:                 temporalConnection,
		ExistingDeploymentReplicas: tc.GetExistingDeploymentReplicas(),
		ExistingDeploymentImages:   tc.GetExistingDeploymentImages(),
		ExpectedDeploymentReplicas: tc.GetExpectedDeploymentReplicas(),
	}

	makePreliminaryStatusTrue(ctx, t, env, twd, tc.GetPreviouslyCurrentImages())

	// verify that temporal state matches the preliminary status, to confirm that makePreliminaryStatusTrue worked
	verifyTemporalStateMatchesStatusEventually(t, ctx, ts, twd, twd.Status, 30*time.Second, 5*time.Second)

	// apply post-status setup function
	if f := tc.GetSetupFunc(); f != nil {
		f(t, ctx, tc, env)
	}

	// Apply any test-specific mutations to the TWD before it is created.
	if f := tc.GetTWDMutatorFunc(); f != nil {
		f(twd)
	}

	t.Log("Creating a WorkerDeployment")
	if err := k8sClient.Create(ctx, twd); err != nil {
		t.Fatalf("failed to create WorkerDeployment: %v", err)
	}

	// k8sClient.Create strips the status subresource, so the TWD starts with an empty
	// status. Not guaranteed to precede the controller's first reconcile, but seeding it
	// here helps test cases with pre-existing target/deprecated versions converge faster
	// and avoid flaking against the eventually timeouts below.
	if twd.Status.TargetVersion.BuildID != "" {
		if err := k8sClient.Status().Update(ctx, twd); err != nil {
			t.Fatalf("failed to pre-apply TWD status: %v", err)
		}
	}

	// Hook: runs after TWD creation but before waiting for the target Deployment.
	// Use this to assert blocking behaviour and then unblock the rollout.
	if f := tc.GetPostTWDCreateFunc(); f != nil {
		f(t, ctx, tc, env)
	}

	t.Log("Waiting for the controller to reconcile")
	expectedDeploymentName := k8s.ComputeVersionedDeploymentName(twd.Name, k8s.ComputeBuildID(twd))

	// only wait for and create the deployment if it is expected
	if expectedStatus.TargetVersion.Status != temporaliov1alpha1.VersionStatusNotRegistered {
		waitForExpectedTargetDeployment(t, twd, env, 30*time.Second)
		workerStopFuncs := applyDeployment(t, ctx, k8sClient, expectedDeploymentName, twd.Namespace)
		defer handleStopFuncs(workerStopFuncs)
	}

	if wait := tc.GetWaitTime(); wait != nil {
		time.Sleep(*wait)
	}
	verifyWorkerDeploymentStatusEventually(t, ctx, env, twd.Name, twd.Namespace, expectedStatus, 30*time.Second, 5*time.Second)
	verifyTemporalStateMatchesStatusEventually(t, ctx, ts, twd, *expectedStatus, 30*time.Second, 5*time.Second)

	// apply post-expected-status validation function
	if f := tc.GetValidatorFunc(); f != nil {
		tc.GetValidatorFunc()(t, ctx, tc, env)
	}
}

The above function is performing an awful lot of setup actions in addition to asserting a dozen or more things. It's not easy to read and when an error occurs in one of the verifyXXX functions, that error is written into the output of the integration testing framework's stdout along with the entire controller log, making it virtually impossible to spot what exactly happened.

My long-term recommendation is to migrate to a more declarative manner of running functional integration tests (using something like gdt/kube would be cool :) ). In the meantime, however, there are some concrete things we can do to make the existing integration testing framework easier to work with. First and foremost, splitting the one TestIntegration function out into many smaller testing functions would allow us to run a specific test using go test -run. Secondly, cleaning up the helper functions to decouple assertions from setup/construction would make the tests themselves easier to debug.

Lingua principale
Go
Stelle
198
Fork
84
Merge medio
2g 18h
PR unite (30g)
35

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di temporalio/temporal-worker-controller

Tutte le issue di temporalio/temporal-worker-controller

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.