Building and Testing

编译并测试您自己的Traefik!

因此,您想从源代码构建自己的Traefik二进制文件吗? 让我们看看如何.

Building

您需要Dockermake (方法1),或者go (方法2)才能构建Traefik. 要更改其依赖性,需要dep依赖性管理工具.

Method 1: Using Docker and Makefile

使用binary目标运行make. 这将在dist文件夹中为Linux平台创建二进制文件.

$ 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
Step 1/10 : FROM golang:1.13-alpine
 ---> f4bfb3d22bda
[...]
Successfully built 5c3c1a911277
Successfully tagged traefik-dev:4475--feature-documentation
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/containous/traefik/"dist":/go/src/github.com/containous/traefik/"dist"" "traefik-dev:4475--feature-documentation" ./script/make.sh generate binary
---> Making bundle: generate (in .)
removed 'autogen/genstatic/gen.go'

---> Making bundle: binary (in .)

$ ls dist/
traefik*

通过将变量PRE_TARGET设置为空字符串,可以在Docker外部执行以下目标(我们不建议这样做):

  • test-unit
  • test-integration
  • validate
  • binary (仍然使用Docker生成webUI)

ex:

PRE_TARGET= make test-unit

Method 2: Using go

Requirements:

  • go v1.13+
  • 环境变量GO111MODULE=on
  • go-bindata GO111MODULE=off go get -u github.com/containous/go-bindata/...

源目录

建议您将Traefik克隆到~/go/src/github.com/containous/traefik目录中. 这是官方的golang工作空间层次结构,可以正确解决依赖关系.

Environment

通过以下方式将您的GOPATHPATH变量设置为~/go

export GOPATH=~/go
export PATH=$PATH:$GOPATH/bin

为了方便起见,请将GOPATHPATH添加到您的.bashrc.bash_profile

通过运行$ go env验证您的环境是否正确设置. 根据您的操作系统和环境,您应该看到类似于以下内容的输出:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/<yourusername>/go"
GORACE=""
## ... and the list goes on

Build Traefik

设置go环境并克隆源存储库后,即可构建Traefik. 事先,您需要(第一次)获取go-bindata才能使用go generate命令(这是构建过程的一部分).

cd ~/go/src/github.com/containous/traefik

# Get go-bindata. (Important: the ellipses are required.)
GO111MODULE=off go get github.com/containous/go-bindata/...

# Let's build

# generate
# (required to merge non-code components into the final binary, such as the web dashboard and the provider's templates)
go generate

# Standard go build
go build ./cmd/traefik

您可以在~/go/src/github.com/containous/traefik目录中找到Traefik可执行文件( traefik ).

Updating the templates

如果您恰巧要更新提供程序的模板(位于/templates ),则必须运行go generate来更新autogen软件包.

Testing

Method 1: Docker and make

使用test-unit目标运行单元测试. 使用test-integration目标运行集成测试. 使用test目标运行所有测试(单元测试和集成test .

$ make test-unit
docker build -t "traefik-dev:your-feature-branch" -f build.Dockerfile .
# […]
docker run --rm -it -e OS_ARCH_ARG -e OS_PLATFORM_ARG -e TESTFLAGS -v "/home/user/go/src/github/containous/traefik/dist:/go/src/github.com/containous/traefik/dist" "traefik-dev:your-feature-branch" ./script/make.sh generate test-unit
---> Making bundle: generate (in .)
removed 'gen.go'

---> Making bundle: test-unit (in .)
+ go test -cover -coverprofile=cover.out .
ok      github.com/containous/traefik   0.005s  coverage: 4.1% of statements

Test success

出于开发目的,您可以指定要使用运行哪些测试(仅适用于test-integration目标):

# 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

更多:https://labix.org/gocheck

Method 2: go

可以使用$ go test ./...从克隆的目录中运行单元测试,它应该返回ok ,类似于:

ok      _/home/user/go/src/github/containous/traefik    0.004s

集成测试必须从integration/目录运行,并且需要-integration开关: $ cd integration && go test -integration ./...