Building and Testing

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

    You need either Docker 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.

    The following targets can be executed outside Docker by setting the variable PRE_TARGET to an empty string (we don’t recommend that):

    • test-unit
    • test-integration
    • validate
    • binary (the webUI is still generated by using Docker)

    ex:

    1. PRE_TARGET= make test-unit
    • go v1.14+
    • environment variable GO111MODULE=on
    • GO111MODULE=off go get -u github.com/containous/go-bindata/...

    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 via:

    1. export GOPATH=~/go

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

    Verify your environment is setup properly by running $ go env. Depending on your OS and environment, you should see an output similar to:

    Build Traefik

    Beforehand, you need to get (the first time) in order to be able to use the go generate command (which is part of the build process).

    1. cd ~/go/src/github.com/traefik/traefik
    2. # Get go-bindata. (Important: the ellipses are required.)
    3. GO111MODULE=off go get github.com/containous/go-bindata/...
    1. # Generate UI static files
    2. rm -rf static/ autogen/; make 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 (traefik) 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. # […]
    3. 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
    4. ---> Making bundle: generate (in .)
    5. removed 'gen.go'
    6. ---> Making bundle: test-unit (in .)
    7. + go test -cover -coverprofile=cover.out .
    8. ok github.com/traefik/traefik 0.005s coverage: 4.1% of statements
    9. 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. # Run every tests ending with "Test", in the MyTest suite
    8. TESTFLAGS="-check.f MyTestSuite.*Test" make test-integration

    More:

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