Debian Codegraph @

Makefile

raw
1 # Local convenience targets. The default path needs no docker: a TestMain runs
2 # the ntf tester inside each test binary.
3 #
4 # make test # full suite
5 # make test T=TestName PKG=./test/... # one test
6 #
7 # The test-docker targets run against an external tester instead. Docker modes
8 # and the server-replace workflow: see TESTING.md.
9
10 # Tester image is pinned in ci.yaml (single source of truth); parsed from there.
11 TESTER_IMAGE ?= $(shell sed -n 's|^ *image: *\(synadia/ntf-server:[^ ]*\).*|\1|p' .github/workflows/ci.yaml)
12 ifeq ($(strip $(TESTER_IMAGE)),)
13 $(error could not parse the tester image from .github/workflows/ci.yaml)
14 endif
15 TESTER_NAME ?= nats-tester
16 TESTER_NETWORK ?= nats-tester-net
17 GO_IMAGE ?= golang:1.26-alpine
18
19 # T limits the run to a single test (-run, verbose); PKG limits the packages.
20 TESTER_NATS_URL ?= nats://localhost:4222
21 PKG ?= ./...
22
23 .PHONY: tester-net tester-up tester-up-host tester-down test-tester test test-norace test-docker server-replace server-replace-drop
24
25 # Points the in-process tester at another nats-server via a replace directive in
26 # go_test.mod. V is a version/branch/commit, or a path when it starts with / or .
27 # Never commit the directive. Prefer this over `go get`, which can drop ntf from
28 # the module graph entirely — see TESTING.md.
29 # make server-replace V=main | v2.13.0 | /path/to/nats-server
30 server-replace:
31 @test -n "$(V)" || { echo "usage: make server-replace V=main|v2.13.0|/path/to/nats-server"; exit 1; }
32 go mod edit -modfile=go_test.mod -replace=github.com/nats-io/nats-server/v2=$(if $(filter /% .%,$(V)),$(V),github.com/nats-io/nats-server/v2@$(V))
33 GOFLAGS=-mod=mod go mod tidy -modfile=go_test.mod
34 @echo "now building against:"
35 @go list -m -modfile=go_test.mod github.com/nats-io/nats-server/v2
36
37 # Removes the directive. Does not fully restore go_test.mod; see the warning it
38 # prints below.
39 server-replace-drop:
40 go mod edit -modfile=go_test.mod -dropreplace=github.com/nats-io/nats-server/v2
41 GOFLAGS=-mod=mod go mod tidy -modfile=go_test.mod
42 @echo "now building against:"
43 @go list -m -modfile=go_test.mod github.com/nats-io/nats-server/v2
44 @git diff --quiet -- go_test.mod go_test.sum || { \
45 echo ""; \
46 echo "WARNING: go_test.mod/go_test.sum still differ from HEAD — the replaced"; \
47 echo "module's dependencies stayed upgraded. Do not commit these. For an exact"; \
48 echo "restore (discards other local edits to both files):"; \
49 echo " git checkout -- go_test.mod go_test.sum"; \
50 }
51
52 # -count=1 everywhere: results depend on external state (which tester answers)
53 # that Go's cache key misses, so a hit can replay an in-process result for a
54 # test-docker run. -p=1: the shared container tester does not tolerate
55 # concurrent CreateServer calls from independent test binaries.
56
57 test:
58 TESTER_NATS_URL= go test -modfile=go_test.mod -tags=internal_testing -race -p=1 -count=1 $(if $(T),-v -run '$(T)') $(PKG) --failfast -vet=off
59
60 # test-docker is test against the container started by `make tester-up-host`.
61 test-docker:
62 TESTER_NATS_URL=$(TESTER_NATS_URL) go test -modfile=go_test.mod -tags=internal_testing -race -p=1 -count=1 $(if $(T),-v -run '$(T)') $(PKG) --failfast -vet=off
63
64 # TestNoRace* tests must run with the race detector off.
65 test-norace:
66 TESTER_NATS_URL= go test -modfile=go_test.mod -p=1 -count=1 $(if $(T),-v) -run '$(or $(T),TestNoRace)' $(PKG) --failfast -vet=off
67
68 tester-net:
69 @docker network inspect $(TESTER_NETWORK) >/dev/null 2>&1 || \
70 docker network create $(TESTER_NETWORK)
71
72 # No host port publishing, for sibling-container mode (make test-tester).
73 # Publishing here would break server bring-up: docker-proxy holds 0.0.0.0:<port>
74 # inside the container's netns, racing the tester's port-reservation handover
75 # ("bind: address already in use"). The ephemeral range is left at the kernel
76 # default so TIME_WAIT churn cannot crowd it; narrowing it only matters when
77 # ports must match a published range (see tester-up-host).
78 tester-up: tester-net
79 docker run -d \
80 --name $(TESTER_NAME) \
81 --network $(TESTER_NETWORK) \
82 --restart unless-stopped \
83 -e NATS_ADVERTISE=$(TESTER_NAME) \
84 $(TESTER_IMAGE) serve
85 @echo "Tester up as '$(TESTER_NAME)'. Run the suite: make test-tester"
86
87 # Publishes ports so `go test` on the host reaches the spawned servers;
88 # ip_local_port_range is narrowed to match the published range. On macOS
89 # docker-proxy races the port handover, failing ~5-10% of server creations in
90 # heavy suites — rerun, or use sibling-container mode, which is immune.
91 tester-up-host: tester-net
92 docker run -d \
93 --name $(TESTER_NAME) \
94 --network $(TESTER_NETWORK) \
95 --restart unless-stopped \
96 --sysctl net.ipv4.ip_local_port_range="30000 31000" \
97 -p 4222:4222 \
98 -p 30000-31000:30000-31000 \
99 -e NATS_ADVERTISE=localhost \
100 $(TESTER_IMAGE) serve
101 @echo "Tester up as '$(TESTER_NAME)'. Run: make test-docker [T=...] [PKG=...]"
102 @echo "(plain 'make test' ignores this container and uses the in-process tester)"
103
104 # Removes the container, discarding its logs. To keep it for debugging instead:
105 # docker logs -f $(TESTER_NAME) / docker restart $(TESTER_NAME).
106 tester-down:
107 -docker rm -f $(TESTER_NAME)
108 -docker network rm $(TESTER_NETWORK)
109
110 # Same two passes as the CI `test` job: NoRace first, then the full race suite.
111 test-tester: tester-net
112 docker run --rm \
113 --network $(TESTER_NETWORK) \
114 -v $(CURDIR):/src \
115 -w /src \
116 -e TESTER_NATS_URL=nats://$(TESTER_NAME):4222 \
117 -e CGO_ENABLED=1 \
118 $(GO_IMAGE) sh -c '\
119 apk add --no-cache gcc libc-dev git make >/dev/null && \
120 go test -modfile=go_test.mod -v -run=TestNoRace -p=1 -count=1 ./... --failfast -vet=off && \
121 go test -modfile=go_test.mod -tags=internal_testing -race -v -p=1 -count=1 ./... --failfast -vet=off'