1 | #!/bin/bash |
---|
2 | |
---|
3 | # Usage: wait_pid_gone ${FILE} |
---|
4 | # |
---|
5 | # Wait until $FILE disappears, but no longer than $TEST_LOCK_WAIT |
---|
6 | # seconds |
---|
7 | function wait_pid_gone |
---|
8 | { |
---|
9 | local pid_file="${1}" |
---|
10 | local pid_wait=0 |
---|
11 | while [ -e "${pid_file}" ]; do |
---|
12 | if [ "$((pid_wait++))" -gt "${TEST_LOCK_WAIT}" ]; then |
---|
13 | return 1 |
---|
14 | fi |
---|
15 | sleep 1 |
---|
16 | done |
---|
17 | } |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | # Usage: verbose_log [...] |
---|
22 | # |
---|
23 | # If VERBOSE is not empty, write a log message prefixed with the name |
---|
24 | # of the calling function. The function is defined to a no-op |
---|
25 | # otherwise. |
---|
26 | if [ -n "${VERBOSE}" ]; then |
---|
27 | function verbose_log |
---|
28 | { |
---|
29 | echo "${FUNCNAME[1]}: ${@}" |
---|
30 | } |
---|
31 | else |
---|
32 | function verbose_log |
---|
33 | { |
---|
34 | return |
---|
35 | } |
---|
36 | fi |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | # Usage: wait_ready COMMAND [TIMEOUT] [STEP] |
---|
41 | # |
---|
42 | # Wait until COMMAND terminates with success (zero exit code), or |
---|
43 | # until the TIMEOUT (in milliseconds) expires. TIMEOUT defaults to |
---|
44 | # $TEST_SERVICE_MAX_WAIT if unset. A TIMEOUT of zero means to try |
---|
45 | # once. |
---|
46 | # |
---|
47 | # COMMAND is retried every STEP milliseconds, the default is |
---|
48 | # $TEST_SERVICE_WAIT. Note that the last try may happen a little after |
---|
49 | # TIMEOUT expires if STEP does not evenly divide it. |
---|
50 | function wait_ready |
---|
51 | { |
---|
52 | local command="${1}" |
---|
53 | if [ -z "${2}" ]; then |
---|
54 | local -i timeout="${TEST_SERVICE_MAX_WAIT}" |
---|
55 | else |
---|
56 | local -i timeout="${2}" |
---|
57 | fi |
---|
58 | local -i step="${3}" |
---|
59 | [ ${step} -gt 0 ] || step="${TEST_SERVICE_WAIT}" |
---|
60 | # convert step to seconds because that's what "sleep" needs |
---|
61 | local sec_step="$((${step} / 1000)).$((${step} % 1000))" |
---|
62 | |
---|
63 | verbose_log "Waiting for \"${command}\" ..." |
---|
64 | local -i waited=0 |
---|
65 | until eval "${command}"; do |
---|
66 | if [ "${waited}" -ge "${timeout}" ]; then |
---|
67 | echo "${FUNCNAME[0]}: Timed out waiting for \"${command}\"" \ |
---|
68 | "to succeed (waited ${waited} ms)." >&2 |
---|
69 | return 1 |
---|
70 | fi |
---|
71 | waited=$((waited + step)); |
---|
72 | sleep "${sec_step}" |
---|
73 | verbose_log "waiting (${waited} ms)" |
---|
74 | done |
---|
75 | verbose_log "done (waited ${waited} ms)" |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | # Usage: netns_reexec ${@} |
---|
81 | # |
---|
82 | # If USE_TEST_NAMESPACE is set and MGS_NETNS_ACTIVE is not, exec the |
---|
83 | # running command inside a new namespace with active loopback |
---|
84 | # interface and MGS_NETNS_ACTIVE defined. This function can be used to |
---|
85 | # isolate each testcase inside its own network namespace. Since |
---|
86 | # MGS_NETNS_ACTIVE is used to track status, there's no harm in calling |
---|
87 | # it multiple times (e.g. in the test-* script and runtests). |
---|
88 | # |
---|
89 | # Note that once the network is up, the reexec is wrapped in another |
---|
90 | # user namespace to get rid of pseudo "root" access. The reason for |
---|
91 | # this is that Apache tries to switch permissions to a non-root user |
---|
92 | # when apparently started as root, and fails because no such user |
---|
93 | # exists inside the namespace. Changing to a non-root user beforehand |
---|
94 | # avoids that issue. |
---|
95 | function netns_reexec |
---|
96 | { |
---|
97 | if [ -n "${USE_TEST_NAMESPACE}" ] && [ -z "${MGS_NETNS_ACTIVE}" ]; then |
---|
98 | exec "${UNSHARE}" --net --ipc -r /bin/bash -c \ |
---|
99 | "export MGS_NETNS_ACTIVE=1; ip link set up lo; exec ${UNSHARE} --user ${0} ${@}" |
---|
100 | fi |
---|
101 | return 0 |
---|
102 | } |
---|