Building and Testing

    You want to build your own Traefik binary from the sources? Let’s see how.

    You need either and make (Method 1), or Go (Method 2) in order to build Traefik. For changes to its dependencies, the dep dependency management tool is required.

    Run make with the binary target.

    This will create binaries for the Linux platform in the dist folder.

    In case when you run build on CI, you may probably want to run docker in non-interactive mode. To achieve that define DOCKER_NON_INTERACTIVE=true environment variable.

    1. $ make binary
    2. docker build -t traefik-webui -f webui/Dockerfile webui
    3. Sending build context to Docker daemon 2.686MB
    4. Step 1/11 : FROM node:8.15.0
    5. ---> 1f6c34f7921c
    6. [...]
    7. Successfully built ce4ff439c06a
    8. Successfully tagged traefik-webui:latest
    9. [...]
    10. docker build -t "traefik-dev:4475--feature-documentation" -f build.Dockerfile .
    11. Sending build context to Docker daemon 279MB
    12. Step 1/10 : FROM golang:1.16-alpine
    13. ---> f4bfb3d22bda
    14. [...]
    15. Successfully built 5c3c1a911277
    16. Successfully tagged traefik-dev:4475--feature-documentation
    17. docker run -e "TEST_CONTAINER=1" -v "/var/run/docker.sock:/var/run/docker.sock" -it -e OS_ARCH_ARG -e OS_PLATFORM_ARG -e TESTFLAGS -e VERBOSE -e VERSION -e CODENAME -e TESTDIRS -e CI -e CONTAINER=DOCKER -v "/home/ldez/sources/go/src/github.com/traefik/traefik/"dist":/go/src/github.com/traefik/traefik/"dist"" "traefik-dev:4475--feature-documentation" ./script/make.sh generate binary
    18. removed 'autogen/genstatic/gen.go'
    19. ---> Making bundle: binary (in .)
    20. traefik*

    The following targets can be executed outside Docker by setting the variable IN_DOCKER to an empty string (although be aware that some of the tests might fail in that context):

    • test-unit
    • test-integration
    • validate
    • binary (the webUI is still generated by using Docker)
    1. IN_DOCKER= make test-unit

    Requirements:

    • go v1.16+
    • environment variable GO111MODULE=on

    Source Directory

    It is recommended that you clone Traefik into the ~/go/src/github.com/traefik/traefik directory. This is the official golang workspace hierarchy that will allow dependencies to be properly resolved.

    Environment

    Set your GOPATH and PATH variable to be set to ~/go via:

    For convenience, add GOPATH and PATH to your .bashrc or .bash_profile

    1. GOARCH="amd64"
    2. GOBIN=""
    3. GOEXE=""
    4. GOHOSTARCH="amd64"
    5. GOHOSTOS="linux"
    6. GOOS="linux"
    7. GOPATH="/home/<yourusername>/go"
    8. GORACE=""
    9. ## ... and the list goes on

    Build Traefik

    Once you’ve set up your go environment and cloned the source repository, you can build Traefik.

    1. # Generate UI static files
    2. make clean-webui generate-webui
    3. # required to merge non-code components into the final binary,
    4. # such as the web dashboard/UI
    5. go generate

    You will find the Traefik executable () in the ~/go/src/github.com/traefik/traefik directory.

    Testing

    Run unit tests using the test-unit target. Run integration tests using the test-integration target. Run all tests (unit and integration) using the test target.

    1. $ make test-unit
    2. docker build -t "traefik-dev:your-feature-branch" -f build.Dockerfile .
    3. # […]
    4. docker run --rm -it -e OS_ARCH_ARG -e OS_PLATFORM_ARG -e TESTFLAGS -v "/home/user/go/src/github/traefik/traefik/dist:/go/src/github.com/traefik/traefik/dist" "traefik-dev:your-feature-branch" ./script/make.sh generate test-unit
    5. ---> Making bundle: generate (in .)
    6. removed 'gen.go'
    7. ---> Making bundle: test-unit (in .)
    8. + go test -cover -coverprofile=cover.out .
    9. ok github.com/traefik/traefik 0.005s coverage: 4.1% of statements
    10. Test success

    For development purposes, you can specify which tests to run by using (only works the test-integration target):

    1. # Run every tests in the MyTest suite
    2. TESTFLAGS="-check.f MyTestSuite" make test-integration
    3. # Run the test "MyTest" in the MyTest suite
    4. TESTFLAGS="-check.f MyTestSuite.MyTest" make test-integration
    5. # Run every tests starting with "My", in the MyTest suite
    6. TESTFLAGS="-check.f MyTestSuite.My" make test-integration
    7. TESTFLAGS="-check.f MyTestSuite.*Test" make test-integration

    Check gocheck for more information.

    Unit tests can be run from the cloned directory using $ go test ./... which should return ok, similar to: