Skip to content

Commit 86c45da

Browse files
committed
ci: isolate unit tests from network
Ensure no unit test can make network requests in continuous integration
1 parent 8219d64 commit 86c45da

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
run: yarn nx run-many -t lint --all
7070

7171
- name: Test
72-
run: yarn nx run-many -t test --all --configuration=ci --detectOpenHandles=false
72+
run: ./bin/run-with-network-isolation.sh yarn nx run-many -t test --all --configuration=ci --detectOpenHandles=false
7373

7474
- name: Upload webapp coverage to Codecov
7575
uses: codecov/codecov-action@57e3a136b779b570ffcdbf80b3bdc90e7fab3de2 # v6.0.0

bin/run-with-network-isolation.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
if [[ $# -eq 0 ]]; then
6+
echo "Usage: $0 <command> [arguments...]" >&2
7+
exit 1
8+
fi
9+
10+
if [[ "$(uname -s)" != "Linux" ]]; then
11+
exec "$@"
12+
fi
13+
14+
if ! command -v unshare >/dev/null 2>&1; then
15+
echo "The 'unshare' command is required on Linux to isolate network access during tests." >&2
16+
exit 1
17+
fi
18+
19+
original_user="$(id -un)"
20+
original_home="${HOME}"
21+
original_path="${PATH}"
22+
23+
if unshare --map-root-user -n true >/dev/null 2>&1; then
24+
exec unshare --map-root-user -n "$@"
25+
fi
26+
27+
if unshare -n true >/dev/null 2>&1; then
28+
exec unshare -n "$@"
29+
fi
30+
31+
if command -v sudo >/dev/null 2>&1; then
32+
if sudo unshare -n true >/dev/null 2>&1; then
33+
if command -v runuser >/dev/null 2>&1; then
34+
exec sudo unshare -n runuser -u "${original_user}" -- env HOME="${original_home}" PATH="${original_path}" "$@"
35+
fi
36+
37+
echo "The 'runuser' command is required for the privileged unshare fallback on this Linux runner." >&2
38+
exit 1
39+
fi
40+
fi
41+
42+
echo "Unable to create a network-isolated namespace with unshare on this Linux runner." >&2
43+
exit 1

0 commit comments

Comments
 (0)