Shoulder.dev Logo Shoulder.dev

Run Tests for fluxcd/flux2 Codebase

Running Tests for Flux

This document provides a step-by-step guide for running tests for the Flux project.

Prerequisites

  • Go 1.18 or later
  • Make
  • Docker

Running Tests

The Makefile provides several targets for running various tests:

1. Unit Tests:

Unit tests are run with the test target:

make test

This target will run all unit tests in the github.com/fluxcd/flux2/v2 module.

2. End-to-End (E2E) Tests:

E2E tests require a Kubernetes cluster. The e2e target runs all E2E tests:

make e2e

This target will set up a Kubernetes cluster using Kind, run the E2E tests, and then clean up the cluster. The setup-kind target can be used to manually set up the Kind cluster:

make setup-kind

You can then run the E2E tests with:

make test-with-kind

The cleanup-kind target can be used to tear down the Kind cluster:

make cleanup-kind

3. Environment Test (Envtest) Suite:

The envtest target runs a suite of tests using the Kubernetes environment test library.

make envtest

This target will create a Kubernetes cluster with the install-envtest target, run the environment tests and then clean it up with the cleanup-envtest target.

4. Other Targets

The following targets are available in the Makefile:

  • install: Installs the Flux binary to $GOPATH/bin.
  • build: Builds the Flux binary.
  • build-dev: Builds the Flux binary with debug symbols.
  • install-dev: Installs the Flux binary with debug symbols to $GOPATH/bin.
  • vet: Runs Go vet to check for potential code issues.
  • fmt: Runs Go fmt to format the code.
  • tidy: Runs Go mod tidy to remove unused dependencies.
  • all: Runs all tests and builds the Flux binary.

Example Test Cases

Here are examples of unit test cases:

File: cmd/flux/trace_test.go

//go:build unit
// +build unit
func TestTraceNoArgs(t *testing.T) {
    cmd := cmdTestCase{
        args:   "trace",
        assert: assertError("either `/` or ` ` is required as an argument"),
    }
    cmd.runTestCmd(t)
}

File: cmd/flux/main_unit_test.go

func TestMain(m *testing.M) {
    log.SetLogger(logr.New(log.NullLogSink{}))

    // Ensure tests print consistent timestamps regardless of timezone
    os.Setenv("TZ", "UTC")

    // Creating the test env manager sets rootArgs client flags
    km, err := NewTestEnvKubeManager(TestEnvClusterMode)
    if err != nil {
        panic(fmt.Errorf("error creating kube manager: '%w'", err))
    }
    testEnv = km
    // rootArgs.kubeconfig = testEnv.kubeConfigPath
    kubeconfigArgs.KubeConfig = &testEnv.kubeConfigPath

    // Run tests
    code := m.Run()

    km.Stop()

    os.Exit(code)
}

These are just examples, and there are many other test cases within the Flux codebase.

Conclusion

This guide provides a comprehensive overview of running tests for the Flux project. By following these steps, you can ensure the stability and reliability of the Flux codebase.