Skip to content

Commit bed1e75

Browse files
authored
simplify test container usage (#808)
* name * auto-determine container name * build test container earlier to keep logspam together * doc updates
1 parent 48a7bd0 commit bed1e75

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

deploy.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ do
9595
done
9696
shift $((OPTIND-1))
9797

98-
export DOJO_CONTAINER
99-
10098
if [ "$START" == "yes" ]; then
10199
cleanup_container $DOJO_CONTAINER
102100

@@ -142,6 +140,9 @@ if [ "$BUILD_IMAGE" == "yes" ]; then
142140
IMAGE_NAME="$DOJO_CONTAINER"
143141
log_endgroup
144142
fi
143+
log_newgroup "Building test container"
144+
docker build -t "${DOJO_CONTAINER}-test" test/
145+
log_endgroup
145146

146147
PORT_ARGS=()
147148
if [ "$EXPORT_PORTS" == "yes" ]; then
@@ -245,16 +246,11 @@ until curl -Ls "http://${CONTAINER_IP}" | grep -q pwn; do sleep 1; done
245246
log_endgroup
246247

247248
if [ "$TEST" == "yes" ]; then
248-
log_newgroup "Building test container"
249-
docker build -t "${DOJO_CONTAINER}-test" test/
250-
log_endgroup
251-
252249
log_newgroup "Running tests in container"
253250
docker run --rm \
254251
-v /var/run/docker.sock:/var/run/docker.sock \
255252
-v "$PWD:/opt/pwn.college" \
256-
-e DOJO_CONTAINER="$DOJO_CONTAINER" \
257-
-e MOZ_HEADLESS=1 \
253+
--name "${DOJO_CONTAINER}-test" \
258254
"${DOJO_CONTAINER}-test" \
259255
pytest --order-dependencies --timeout=60 -v . "$@"
260256
log_endgroup

docs/agents.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ docker exec -i "$DOJO_CONTAINER" dojo flask
4747
# enter a learner's container (must be started first via a testcase or the web interface)
4848
docker exec -i "$DOJO_CONTAINER" dojo enter USER_ID
4949

50-
# run an inidividual testcase --- environment variables need to be set!
51-
export DOJO_CONTAINER
52-
pytest -v test/test_dojos.py::test_create_dojo
50+
# run an inidividual testcase (needs docker socket)
51+
docker run -v /var/run/docker.sock:/var/run/docker.sock -v $PWD:/opt/pwn.college pytest -v test/test_dojos.py::test_create_dojo
5352
```
5453

5554
### Troubleshooting
@@ -68,9 +67,8 @@ Container start failures show up in the ctfd container logs.
6867
# Run tests without using docker or workspace cache
6968
./deploy.sh -D "" -W "" -t
7069

71-
# Run an individual testcase (needs environment variables)
72-
export DOJO_CONTAINER=$(basename "$PWD")
73-
pytest -v test/test_dojos.py::test_create_dojo
70+
# Run an individual testcase (needs docker socket)
71+
docker run -v /var/run/docker.sock:/var/run/docker.sock -v $PWD:/opt/pwn.college pytest -v test/test_dojos.py::test_create_dojo
7472
```
7573

7674
**Test Script Options:**

test/utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,36 @@
77
import re
88
import os
99

10-
DOJO_CONTAINER = os.getenv("DOJO_CONTAINER", "dojo-test")
10+
def _get_dojo_container():
11+
if os.getenv("DOJO_CONTAINER"):
12+
return os.getenv("DOJO_CONTAINER")
13+
14+
if os.path.exists("/.dockerenv"):
15+
import socket
16+
hostname = socket.gethostname()
17+
18+
def docker_cmd(args):
19+
result = subprocess.run(["docker"] + args, capture_output=True, text=True, check=True)
20+
return result.stdout.strip() if result.returncode == 0 else None
21+
22+
container_name = docker_cmd(["ps", "--filter", f"id={hostname}", "--format", "{{.Names}}"])
23+
if container_name.endswith("-test"):
24+
return container_name[:-5]
25+
26+
all_containers = docker_cmd(["ps", "--format", "{{.Names}}"])
27+
if len(all_containers) == 2:
28+
return next(c for c in all_containers.split('\n') if c and c != container_name)
29+
else:
30+
result = subprocess.run(
31+
["git", "rev-parse", "--show-toplevel"],
32+
capture_output=True, text=True, cwd=os.path.dirname(__file__)
33+
)
34+
if result.returncode == 0:
35+
return os.path.basename(result.stdout.strip())
36+
37+
raise RuntimeError(f"Unable to determine the container the dojo is running in. Please set DOJO_CONTAINER.")
38+
39+
DOJO_CONTAINER = _get_dojo_container()
1140

1241
def _get_container_ip(container_name):
1342
result = subprocess.run(

0 commit comments

Comments
 (0)