Skip to content

test: Use random ports instead of fixed ones - #356

Merged
sriemer merged 1 commit into
ledgetech:masterfrom
ChrisTX:random-test-ports
Nov 23, 2026
Merged

sriemer merged 1 commit into
ledgetech:masterfrom
ChrisTX:random-test-ports

Conversation

@ChrisTX

@ChrisTX ChrisTX commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Some tests start servers on fixed ports: listen *:8080 and *:8081 in t/14-host-header.t and t/16-http-proxy.t, and --- tcp_listen: 12345 in t/01-basic.t, t/07-keepalive.t, t/15-instance-reuse.t and t/16-http-proxy.t. On a host where another service holds one of these ports, nginx can't start and the suite stops:

t/14-host-header.t TEST 3: Non-default HTTP port is added to Host header - Cannot start nginx using command "nginx … -c …/t/servroot/conf/nginx.conf > /dev/null" (status code 256).

That host was a server with CrowdSec, whose API listens on 8080 by default.

This PR uses Test::Nginx's $TEST_NGINX_RAND_PORT_1 and $TEST_NGINX_RAND_PORT_2 instead. Test::Nginx expands them in the configuration, --- tcp_listen and --- response_body, giving each test block free ports. The 12345 in t/03-requestbody.t is a request body, not a port, so it stays.

Test::Nginx 0.32 doesn't expand these variables in --- error_log. So TESTs 3, 4 and 6 of t/14-host-header.t, which checked the Host header in the debug log, now have the server (or, in TEST 6, the proxy) echo the Host header it received. They compare it in --- response_body, as t/16-http-proxy.t already does with ngx.req.raw_header(). This checks the header as it arrived rather than as it was logged. They still fail if the port goes missing from the Host header: with lib/resty/http.lua changed to send only self.host, all three report got: '127.0.0.1', expected: '127.0.0.1:<port>'. (openresty/test-nginx#191 proposes the expansion in --- error_log, but this PR doesn't need it.)

Tested with nginx 1.30.7 and lua-nginx-module 0.10.32rc8, with another process listening on 8080:

  • serially (prove -r t), Test::Nginx 0.32: all 21 files and 434 tests pass. Without this change, the run stops at t/14-host-header.t.
  • in parallel (TEST_NGINX_RANDOMIZE=1 prove -j32 -r t), five times each with Test::Nginx 0.32 and with its current master: all pass.
  • two copies of the suite at the same time (-j16 each), eight times with Test::Nginx's current master: all pass. With 0.32, a random port occasionally collided with the other copy's: random ports in 0.32 aren't coordinated across processes. That affects the server port of TEST_NGINX_RANDOMIZE just the same, and Test::Nginx's master has fixed it.

🤖 Generated with Claude Code

The tests started servers on the fixed ports 8080, 8081 and 12345, so
the suite could not run on a host where another service holds one of
them, e.g. 8080. Test::Nginx's $TEST_NGINX_RAND_PORT_N variables give
each test block free ports instead.

Test::Nginx doesn't expand these variables in --- error_log, so the
Host header tests of t/14-host-header.t now have the server (or the
proxy) echo the Host header it received and compare it in
@sriemer sriemer self-assigned this Nov 23, 2026
@sriemer sriemer added the bugfix label Nov 23, 2026
@sriemer
sriemer merged commit 99ca361 into ledgetech:master Nov 23, 2026
7 checks passed
@sriemer

sriemer commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@ChrisTX, thanks for your contribution. LGTM. Reviewed by IBM Bob Shell.
It also fixed the incomplete sentence in the commit description from

Test::Nginx doesn't expand these variables in --- error_log, so the
Host header tests of t/14-host-header.t now have the server (or the
proxy) echo the Host header it received and compare it in

to

Test::Nginx doesn't expand these variables in --- error_log, so the
Host header tests of t/14-host-header.t now have the server (or the
proxy) echo the Host header it received and compare it in the response
body via --- response_body instead of matching the debug log.

so that I was able to correct this with a squash merge.

ChrisTX added a commit to ChrisTX/aur-packages that referenced this pull request Nov 23, 2026
- upstream's tests take random ports since ledgetech/lua-resty-http#356,
  merged but not released yet: a workaround directive, so the daily check
  says when the workaround can go

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants