How to Run Tests for the Moby Project

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

Prerequisites

Ensure you have the following installed:

  • Go: The Moby project is written in Go.
  • Docker: You’ll need Docker to run the tests, including integration tests.

Running Unit Tests

  1. Navigate to the project’s root directory.

  2. Run the following command:

    make test-unit
    

    This will execute the unit tests defined in the *_test.go files within the project.

    Example: daemon/runtime_unix_test.go contains unit tests for the daemon package.

    func TestGetRuntime_PreflightCheck(t *testing.T) {
        cfg, err := config.New()
        assert.NilError(t, err)
    
        cfg.Root = t.TempDir()
        cfg.Runtimes = map[string]system.Runtime{
            "path-only": {
                Path: "/usr/local/bin/file-not-found",
            },
            "with-args": {
                Path: "/usr/local/bin/file-not-found",
                Args: []string{"--arg"},
            },
        }
        assert.NilError(t, initRuntimesDir(cfg))
        runtimes, err := setupRuntimes(cfg)
        assert.NilError(t, err, "runtime paths should not be validated during setupRuntimes()")
    
        t.Run("PathOnly", func(t *testing.T) {
            _, _, err := runtimes.Get("path-only")
            assert.NilError(t, err, "custom runtimes without wrapper scripts should not have pre-flight checks")
        })
        t.Run("WithArgs", func(t *testing.T) {
            _, _, err := runtimes.Get("with-args")
            assert.ErrorIs(t, err, fs.ErrNotExist)
        })
    }
    

Running Integration Tests

  1. Navigate to the project’s root directory.

  2. Run the following command to execute all integration tests:

    make test-integration
    

    This command will build the Moby project and run the integration tests.

  3. Alternatively, you can run specific integration tests:

    make test-integration-cli
    

    This command will execute the integration tests for the Docker CLI.

    Example: integration-cli/check_test.go contains integration tests for the CLI.

    func TestDockerHubPullSuite(t *testing.T) {
        ctx := testutil.StartSpan(baseContext, t)
        ensureTestEnvSetup(ctx, t)
        // FIXME. Temporarily turning this off for Windows as GH16039 was breaking
        // Windows to Linux CI @icecrime
        testRequires(t, DaemonIsLinux)
        suite.Run(ctx, t, newDockerHubPullSuite())
    }
    

Running Flaky Integration Tests

The Moby project uses the test-integration-flaky target to run integration tests that may be prone to occasional failures. These tests can be helpful in identifying intermittent issues.

  1. Navigate to the project’s root directory.

  2. Run the following command to execute flaky integration tests:

    make test-integration-flaky
    

Running Specific Tests

You can also run specific tests by specifying their path:

go test path/to/test_file.go

Running Tests with Specific Flags

The go test command supports various flags to modify the test execution:

  • -v: Verbose output, showing individual test results.
  • -run: Filter tests by name pattern.
  • -short: Run tests in a shorter mode, potentially skipping long-running tests.

For example, to run all tests with verbose output:

go test -v ./...

To run only tests that match the pattern “TestFoo” with verbose output:

go test -v -run TestFoo ./...

Understanding Test Structure

Moby’s tests are organized in a modular fashion. You can find tests for specific packages or functionalities within their respective directories.

Additional Resources

For more information on running tests for specific components or areas within the Moby project, refer to the project’s documentation and codebase.