2019-02-26 13:50:07 +00:00
# Building and Testing
Compile and Test Your Own Traefik!
{: .subtitle }
So you want to build your own Traefik binary from the sources?
Let's see how.
## Building
You need either [Docker ](https://github.com/docker/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.
### Method 1: Using `Docker` and `Makefile`
Run make with the `binary` target.
This will create binaries for the Linux platform in the `dist` folder.
2020-01-06 15:58:04 +00:00
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.
2019-02-26 13:50:07 +00:00
```bash
$ make binary
docker build -t traefik-webui -f webui/Dockerfile webui
Sending build context to Docker daemon 2.686MB
Step 1/11 : FROM node:8.15.0
---> 1f6c34f7921c
[...]
Successfully built ce4ff439c06a
Successfully tagged traefik-webui:latest
[...]
docker build -t "traefik-dev:4475--feature-documentation" -f build.Dockerfile .
Sending build context to Docker daemon 279MB
2020-02-26 14:30:06 +00:00
Step 1/10 : FROM golang:1.14-alpine
2019-02-26 13:50:07 +00:00
---> f4bfb3d22bda
[...]
Successfully built 5c3c1a911277
Successfully tagged traefik-dev:4475--feature-documentation
2020-09-16 13:46:04 +00:00
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
2019-02-26 13:50:07 +00:00
---> Making bundle: generate (in .)
removed 'autogen/genstatic/gen.go'
---> Making bundle: binary (in .)
$ ls dist/
traefik*
```
2019-05-10 15:24:06 +00:00
The following targets can be executed outside Docker by setting the variable `PRE_TARGET` to an empty string (we don't recommend that):
2019-03-28 10:42:06 +00:00
- `test-unit`
- `test-integration`
- `validate`
- `binary` (the webUI is still generated by using Docker)
ex:
```bash
PRE_TARGET= make test-unit
```
2019-02-26 13:50:07 +00:00
### Method 2: Using `go`
2019-08-11 10:21:19 +00:00
Requirements:
2020-02-26 14:30:06 +00:00
- `go` v1.14+
2019-08-11 10:21:19 +00:00
- environment variable `GO111MODULE=on`
2019-12-17 13:30:06 +00:00
- [go-bindata ](https://github.com/containous/go-bindata ) `GO111MODULE=off go get -u github.com/containous/go-bindata/...`
2019-02-26 13:50:07 +00:00
!!! tip "Source Directory"
2019-05-10 15:24:06 +00:00
2020-09-16 13:46:04 +00:00
It is recommended that you clone Traefik into the `~/go/src/github.com/traefik/traefik` directory.
2019-02-26 13:50:07 +00:00
This is the official golang workspace hierarchy that will allow dependencies to be properly resolved.
!!! note "Environment"
Set your `GOPATH` and `PATH` variable to be set to `~/go` via:
2019-05-10 15:24:06 +00:00
2019-02-26 13:50:07 +00:00
```bash
export GOPATH=~/go
export PATH=$PATH:$GOPATH/bin
```
2019-05-10 15:24:06 +00:00
2019-02-26 13:50:07 +00:00
For convenience, add `GOPATH` and `PATH` to your `.bashrc` or `.bash_profile`
2019-05-10 15:24:06 +00:00
2019-02-26 13:50:07 +00:00
Verify your environment is setup properly by running `$ go env` .
Depending on your OS and environment, you should see an output similar to:
2019-05-10 15:24:06 +00:00
2019-02-26 13:50:07 +00:00
```bash
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/< yourusername > /go"
GORACE=""
## ... and the list goes on
```
#### Build Traefik
Once you've set up your go environment and cloned the source repository, you can build Traefik.
2019-12-17 13:30:06 +00:00
Beforehand, you need to get [go-bindata ](https://github.com/containous/go-bindata ) (the first time) in order to be able to use the `go generate` command (which is part of the build process).
2019-02-26 13:50:07 +00:00
```bash
2020-09-16 13:46:04 +00:00
cd ~/go/src/github.com/traefik/traefik
2019-02-26 13:50:07 +00:00
# Get go-bindata. (Important: the ellipses are required.)
2019-08-11 10:21:19 +00:00
GO111MODULE=off go get github.com/containous/go-bindata/...
2019-12-12 15:32:06 +00:00
```
2019-02-26 13:50:07 +00:00
2019-12-12 15:32:06 +00:00
```bash
# Generate UI static files
rm -rf static/ autogen/; make generate-webui
2019-02-26 13:50:07 +00:00
2019-12-12 15:32:06 +00:00
# required to merge non-code components into the final binary,
# such as the web dashboard/UI
2019-02-26 13:50:07 +00:00
go generate
2019-12-12 15:32:06 +00:00
```
2019-02-26 13:50:07 +00:00
2019-12-12 15:32:06 +00:00
```bash
2019-02-26 13:50:07 +00:00
# Standard go build
go build ./cmd/traefik
```
2020-09-16 13:46:04 +00:00
You will find the Traefik executable (`traefik`) in the `~/go/src/github.com/traefik/traefik` directory.
2019-02-26 13:50:07 +00:00
## Testing
### Method 1: `Docker` and `make`
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.
```bash
$ make test-unit
docker build -t "traefik-dev:your-feature-branch" -f build.Dockerfile .
# […]
2020-09-16 13:46:04 +00:00
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
2019-02-26 13:50:07 +00:00
---> Making bundle: generate (in .)
removed 'gen.go'
---> Making bundle: test-unit (in .)
+ go test -cover -coverprofile=cover.out .
2020-09-16 13:46:04 +00:00
ok github.com/traefik/traefik 0.005s coverage: 4.1% of statements
2019-02-26 13:50:07 +00:00
Test success
```
For development purposes, you can specify which tests to run by using (only works the `test-integration` target):
```bash
# Run every tests in the MyTest suite
TESTFLAGS="-check.f MyTestSuite" make test-integration
# Run the test "MyTest" in the MyTest suite
TESTFLAGS="-check.f MyTestSuite.MyTest" make test-integration
# Run every tests starting with "My", in the MyTest suite
TESTFLAGS="-check.f MyTestSuite.My" make test-integration
# Run every tests ending with "Test", in the MyTest suite
TESTFLAGS="-check.f MyTestSuite.*Test" make test-integration
```
More: https://labix.org/gocheck
### Method 2: `go`
Unit tests can be run from the cloned directory using `$ go test ./...` which should return `ok` , similar to:
```test
2020-09-16 13:46:04 +00:00
ok _/home/user/go/src/github/traefik/traefik 0.004s
2019-02-26 13:50:07 +00:00
```
Integration tests must be run from the `integration/` directory and require the `-integration` switch: `$ cd integration && go test -integration ./...` .