Running Tests for Cilium

This document outlines the steps for running tests for the Cilium project.

Running Integration Tests

The integration-tests target in the Makefile runs the integration tests for Cilium. This target leverages the test-k8s framework, which enables testing Cilium’s functionality in a Kubernetes environment.

integration-tests:
    $(MAKE) -C test-k8s test-all

Note: The integration-tests target relies on the test-k8s submodule. Ensure the submodule is correctly initialized and updated. Refer to the Cilium repository’s documentation for detailed instructions on submodule management.

Running Unit Tests

The Cilium project utilizes Go’s built-in testing framework for unit testing. The go test command is used to execute unit tests.

Example:

go test ./pkg/...

This command runs all unit tests within the pkg directory. You can specify specific packages or files for testing.

Running Tests in a Docker Environment

Cilium offers a build-container target in the Makefile to build a Docker container containing all necessary components for testing. This provides a controlled environment for running tests.

build-container:
    $(MAKE) -C build build-container

Once the container is built, you can run tests within it using the docker run command and specifying the appropriate test targets.

Using the test-docs Target

The test-docs target in the Makefile runs the documentation tests. This ensures the consistency and accuracy of the project’s documentation.

test-docs:
    $(MAKE) -C docs test

Running Specific Test Categories

Cilium provides targets within the Makefile for running specific test categories:

  • test-privileged: Runs tests that require privileged access.
  • tests-privileged: Runs tests for privileged operations.
  • run_bpf_tests: Runs tests related to BPF (Berkeley Packet Filter).

Example:

test-privileged:
    $(MAKE) -C test-k8s test-privileged

Running Individual Tests

You can run individual tests by specifying the test file and function using the go test command.

Example:

go test -run TestMyFunction ./pkg/mypackage/mypackage_test.go

This command runs the TestMyFunction test within the mypackage_test.go file.

Using gofmt and golangci-lint for Code Formatting and Linting

Cilium utilizes gofmt for code formatting and golangci-lint for static code analysis.

gofmt:
    gofmt -w -s $(shell git ls-files '*.go' | grep -vE '^(vendor|test-k8s|cilium-operator)/')

golangci-lint:
    golangci-lint run --timeout 5m --deadline 20m --issues-exit-code 1

Using Makefile for Test Execution

The Makefile provides convenient targets for running different test configurations and functionalities. Refer to the Makefile for a comprehensive list of available targets.

Example:

test-all:
    $(MAKE) -C test-k8s test-all

This example demonstrates the test-all target, which runs all tests within the test-k8s submodule.

Conclusion

This documentation provides a comprehensive overview of running tests for the Cilium project. By utilizing the provided tools and commands, you can efficiently execute various tests, ensuring the quality and stability of the Cilium software.