test-lib-functions: introduce the 'test_set_port' helper function

Several test scripts run daemons like 'git-daemon' or Apache, and communicate with them through TCP sockets. To have unique ports where these daemons are accessible, the ports are usually the number of the corresponding test scripts, unless the user overrides them via environment variables, and thus all those tests and test libs contain more or less the same bit of one-liner boilerplate code to find out the port. The last patch in this series will make this a bit more complicated. Factor out finding the port for a daemon into the common helper function 'test_set_port' to avoid repeating ourselves. Take special care of test scripts with "low" numbers: - Test numbers below 1024 would result in a port that's only usable as root, so set their port to '10000 + test-nr' to make sure it doesn't interfere with other tests in the test suite. This makes the hardcoded port number in 't0410-partial-clone.sh' unnecessary, remove it. - The shell's arithmetic evaluation interprets numbers with leading zeros as octal values, which means that test number below 1000 and containing the digits 8 or 9 will trigger an error. Remove all leading zeros from the test numbers to prevent this. Note that the 'git p4' tests are unlike the other tests involving daemons in that: - 'lib-git-p4.sh' doesn't use the test's number for unique port as is, but does a bit of additional arithmetic on top [1]. - The port is not overridable via an environment variable. With this patch even 'git p4' tests will use the test's number as default port, and it will be overridable via the P4DPORT environment variable. [1] Commit fc00233071 (git-p4 tests: refactor and cleanup, 2011-08-22) introduced that "unusual" unique port computation without explaining why it was necessary (as opposed to simply using the test number as is). It seems to be just unnecessary complication, and in any case that commit came way before the "test nr as unique port" got "standardized" for other daemons in commits c44132fcf3 (tests: auto-set git-daemon port, 2014-02-10), 3bb486e439 (tests: auto-set LIB_HTTPD_PORT from test name, 2014-02-10), and bf9d7df950 (t/lib-git-svn.sh: improve svnserve tests with parallel make test, 2017-12-01). Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Jan 5, 2019 at 02:08 UTC fa840581805c794e93e03a83a944839f15ba08d0
7 files changed +41 -13
t/lib-git-daemon.sh
+1 -1
@@ -28,7 +28,7 @@ then
28 test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
29 fi
30
31 -LIB_GIT_DAEMON_PORT=${LIB_GIT_DAEMON_PORT-${this_test#t}}
31 +test_set_port LIB_GIT_DAEMON_PORT
32
33 GIT_DAEMON_PID=
34 GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
t/lib-git-p4.sh
+1 -8
@@ -53,14 +53,7 @@ time_in_seconds () {
53 (cd / && "$PYTHON_PATH" -c 'import time; print(int(time.time()))')
54 }
55
56 -# Try to pick a unique port: guess a large number, then hope
57 -# no more than one of each test is running.
58 -#
59 -# This does not handle the case where somebody else is running the
60 -# same tests and has chosen the same ports.
61 -testid=${this_test#t}
62 -git_p4_test_start=9800
63 -P4DPORT=$((10669 + ($testid - $git_p4_test_start)))
56 +test_set_port P4DPORT
57
58 P4PORT=localhost:$P4DPORT
59 P4CLIENT=client
t/lib-git-svn.sh
+1 -1
@@ -13,6 +13,7 @@ fi
13 GIT_DIR=$PWD/.git
14 GIT_SVN_DIR=$GIT_DIR/svn/refs/remotes/git-svn
15 SVN_TREE=$GIT_SVN_DIR/svn-tree
16 +test_set_port SVNSERVE_PORT
17
18 svn >/dev/null 2>&1
19 if test $? -ne 1
@@ -119,7 +120,6 @@ require_svnserve () {
120 }
121
122 start_svnserve () {
122 - SVNSERVE_PORT=${SVNSERVE_PORT-${this_test#t}}
123 svnserve --listen-port $SVNSERVE_PORT \
124 --root "$rawsvnrepo" \
125 --listen-once \
t/lib-httpd.sh
+1 -1
@@ -82,7 +82,7 @@ case $(uname) in
82 esac
83
84 LIB_HTTPD_PATH=${LIB_HTTPD_PATH-"$DEFAULT_HTTPD_PATH"}
85 -LIB_HTTPD_PORT=${LIB_HTTPD_PORT-${this_test#t}}
85 +test_set_port LIB_HTTPD_PORT
86
87 TEST_PATH="$TEST_DIRECTORY"/lib-httpd
88 HTTPD_ROOT_PATH="$PWD"/httpd
t/t0410-partial-clone.sh
-1
@@ -480,7 +480,6 @@ test_expect_success 'gc stops traversal when a missing but promised object is re
480 ! grep "$TREE_HASH" out
481 '
482
483 -LIB_HTTPD_PORT=12345 # default port, 410, cannot be used as non-root
483 . "$TEST_DIRECTORY"/lib-httpd.sh
484 start_httpd
485
t/t5512-ls-remote.sh
+1 -1
@@ -260,7 +260,7 @@ test_lazy_prereq GIT_DAEMON '
260 # This test spawns a daemon, so run it only if the user would be OK with
261 # testing with git-daemon.
262 test_expect_success PIPE,JGIT,GIT_DAEMON 'indicate no refs in standards-compliant empty remote' '
263 - JGIT_DAEMON_PORT=${JGIT_DAEMON_PORT-${this_test#t}} &&
263 + test_set_port JGIT_DAEMON_PORT &&
264 JGIT_DAEMON_PID= &&
265 git init --bare empty.git &&
266 >empty.git/git-daemon-export-ok &&
t/test-lib-functions.sh
+36
@@ -1263,3 +1263,39 @@ test_oid () {
1263 fi &&
1264 eval "printf '%s' \"\${$var}\""
1265 }
1266 +
1267 +# Choose a port number based on the test script's number and store it in
1268 +# the given variable name, unless that variable already contains a number.
1269 +test_set_port () {
1270 + local var=$1 port
1271 +
1272 + if test $# -ne 1 || test -z "$var"
1273 + then
1274 + BUG "test_set_port requires a variable name"
1275 + fi
1276 +
1277 + eval port=\$$var
1278 + case "$port" in
1279 + "")
1280 + # No port is set in the given env var, use the test
1281 + # number as port number instead.
1282 + # Remove not only the leading 't', but all leading zeros
1283 + # as well, so the arithmetic below won't (mis)interpret
1284 + # a test number like '0123' as an octal value.
1285 + port=${this_test#${this_test%%[1-9]*}}
1286 + if test "${port:-0}" -lt 1024
1287 + then
1288 + # root-only port, use a larger one instead.
1289 + port=$(($port + 10000))
1290 + fi
1291 +
1292 + eval $var=$port
1293 + ;;
1294 + *[^0-9]*|0*)
1295 + error >&7 "invalid port number: $port"
1296 + ;;
1297 + *)
1298 + # The user has specified the port.
1299 + ;;
1300 + esac
1301 +}