Traefik 构建和测试文档
构建和测试
编译并测试你自己的 Traefik!
你想从源代码构建你自己的 Traefik 二进制文件? 让我们看看如何做。
构建
你需要:
- Docker
make- Go
- misspell
- shellcheck
- 如果你使用 Docker Desktop,则需要 Tailscale
源目录
建议将 Traefik 克隆到
~/go/src/github.com/traefik/traefik目录。 这是官方 golang 工作区层次结构,将允许正确解析依赖项。
环境
通过以下方式设置
GOPATH和PATH变量为~/go:bashexport GOPATH=~/go export PATH=$PATH:$GOPATH/bin为方便起见,将
GOPATH和PATH添加到你的.bashrc或.bash_profile通过运行
$ go env验证你的环境是否已正确设置。 根据你的操作系统和环境,你应该看到类似以下的输出:GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/home/<yourusername>/go" GORACE="" ## ... 等等
构建 Traefik
设置好 go 环境并克隆源存储库后,你可以构建 Traefik。
bash
$ make binary
SHA: 8fddfe118288bb5280eb5e77fa952f52def360b4 cheddar 2024-01-11_03:14:57PM
CGO_ENABLED=0 GOGC=off GOOS=darwin GOARCH=arm64 go build -ldflags "-s -w \
-X github.com/traefik/traefik/v2/pkg/version.Version=8fddfe118288bb5280eb5e77fa952f52def360b4 \
-X github.com/traefik/traefik/v2/pkg/version.Codename=cheddar \
-X github.com/traefik/traefik/v2/pkg/version.BuildDate=2024-01-11_03:14:57PM" \
-installsuffix nocgo -o "./dist/darwin/arm64/traefik" ./cmd/traefik
$ ls dist/
traefik*你将在 ./dist 目录中找到 Traefik 可执行文件(traefik)。
测试
使用 test-unit 目标运行单元测试。 使用 test-integration 目标运行集成测试。 使用 test 目标运行所有测试(单元和集成)。
bash
$ make test-unit
GOOS=darwin GOARCH=arm64 go test -cover "-coverprofile=cover.out" -v ./pkg/... ./cmd/...
+ go test -cover -coverprofile=cover.out .
ok github.com/traefik/traefik 0.005s coverage: 4.1% of statements
Test success出于开发目的,你可以使用以下方式指定要运行的测试(仅适用于 test-integration 目标):
为 Docker Desktop 用户配置 Tailscale
在
integration目录中创建tailscale.secret文件。此文件需要包含 Tailscale 认证密钥 (推荐使用临时但可重用的密钥)。
将此部分添加到你的 tailscale ACL 中以自动批准 docker 子网中容器的路由:
json"autoApprovers": { // 允许我自动 // 通告 docker 网络的路由 "routes": { "172.31.42.0/24": ["your_tailscale_identity"], }, },
bash
# 在 MyTest 套件中运行每个测试
TESTFLAGS="-test.run TestAccessLogSuite" make test-integration
# 在 MyTest 套件中运行测试 "MyTest"
TESTFLAGS="-test.run TestAccessLogSuite -testify.m ^TestAccessLog$" make test-integration