Building and Testing

Compile and Test Your Own Traefik!

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

Building

You need: - Docker - make - Go - misspell - shellcheck - Tailscale if you are using Docker Desktop

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:

  1. export GOPATH=~/go
  2. export PATH=$PATH:$GOPATH/bin

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:

  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. $ make binary
  2. SHA: 8fddfe118288bb5280eb5e77fa952f52def360b4 cheddar 2024-01-11_03:14:57PM
  3. CGO_ENABLED=0 GOGC=off GOOS=darwin GOARCH=arm64 go build -ldflags "-s -w \
  4. -X github.com/traefik/traefik/v2/pkg/version.Version=8fddfe118288bb5280eb5e77fa952f52def360b4 \
  5. -X github.com/traefik/traefik/v2/pkg/version.Codename=cheddar \
  6. -X github.com/traefik/traefik/v2/pkg/version.BuildDate=2024-01-11_03:14:57PM" \
  7. -installsuffix nocgo -o "./dist/darwin/arm64/traefik" ./cmd/traefik
  8. $ ls dist/
  9. traefik*

You will find the Traefik executable (traefik) in the ./dist 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. GOOS=darwin GOARCH=arm64 go test -cover "-coverprofile=cover.out" -v ./pkg/... ./cmd/...
  3. + go test -cover -coverprofile=cover.out .
  4. ok github.com/traefik/traefik 0.005s coverage: 4.1% of statements
  5. Test success

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

Configuring Tailscale for Docker Desktop user

Create tailscale.secret file in integration directory.

This file need to contains a Tailscale auth key (an ephemeral, but reusable, one is recommended).

Add this section to your tailscale ACLs to auto-approve the routes for the containers in the docker subnet:

  1. "autoApprovers": {
  2. // Allow myself to automatically
  3. // advertize routes for docker networks
  4. "routes": {
  5. "172.31.42.0/24": ["your_tailscale_identity"],
  6. },
  7. },
  1. # Run every tests in the MyTest suite
  2. TESTFLAGS="-test.run TestAccessLogSuite" make test-integration
  3. # Run the test "MyTest" in the MyTest suite
  4. TESTFLAGS="-test.run TestAccessLogSuite -testify.m ^TestAccessLog$" make test-integration