Skip to content

Commit b8b9d02

Browse files
authored
Dockerfile, bin(run-tests), tests: set USER (#61)
Better conform with Dockerfile best practices [1]: If a service can run without privileges, use `USER` to change to a non-root user. See also the Docker docs on `USER` [2], and the Alpine Linux wiki page on creating a new user [3]. The commit [4] that added a zig cache significantly speeds up a `RUN zig test foo` command that's added at the bottom of the Dockerfile. But it doesn't seem to be a speedup in production. I suspect that the zig cache isn't found in production, due to differences in how containers are run (see Exercism's terraform and tooling-invoker repos [5][6]). I suspect that this commit may be insufficient to fix the problem, but it's a step in the right direction. Possible follow-up work includes: - Setting zig cache environment variables, or passing zig cache location options to `zig test` - Changing the paths of solution files. - Updating Zig. I saw a recent commit [7] that touches Cache.zig. Closes: #60 Closes: #62 Refs: #63 [1] https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ [2] https://docs.docker.com/engine/reference/builder/#user [3] https://wiki.alpinelinux.org/wiki/Setting_up_a_new_user [4] 82b56af, 2023-08-23, ".dockerignore, Dockerfile: add a zig cache" [5] https://github.com/exercism/terraform/blob/3ff7b3b5a2f6/terraform/tooling_invoker/ami.sh#L68-L88 [6] https://github.com/exercism/tooling-invoker/blob/88a9d826a746/lib/tooling_invoker/job_processor/exec_docker.rb#L150-L162 [7] ziglang/zig@020105d0dde6 2023-08-20, "Cache: Fix findPrefix when paths are slightly out of the ordinary"
1 parent ae6ae53 commit b8b9d02

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ FROM ${REPO}:${IMAGE} AS runner
3131
# hadolint ignore=DL3018
3232
RUN apk add --no-cache jq
3333

34-
COPY --from=builder /opt/zig/ /opt/zig/
35-
COPY --from=builder /root/.cache/zig/ /root/.cache/zig/
34+
RUN addgroup ziggroup \
35+
&& adduser --disabled-password --gecos ziggy --ingroup ziggroup ziggy
36+
COPY --from=builder --chown=ziggy:ziggroup /opt/zig/ /opt/zig/
37+
COPY --from=builder --chown=ziggy:ziggroup /root/.cache/zig/ /home/ziggy/.cache/zig/
3638
ENV PATH=$PATH:/opt/zig
3739

40+
USER ziggy:ziggroup
3841
WORKDIR /opt/test-runner
39-
COPY bin/run.sh bin/run.sh
42+
COPY --chown=ziggy:ziggroup bin/run.sh bin/run.sh
4043
ENTRYPOINT ["/opt/test-runner/bin/run.sh"]

bin/run-tests.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
# ./bin/run-tests.sh
1313

1414
exit_code=0
15+
# Copy the tests dir to a temp dir, because in the container the user lacks
16+
# permissions to write to the tests dir.
17+
tmp_dir='/tmp/exercism-zig-test-runner'
18+
rm -rf "${tmp_dir}"
19+
mkdir -p "${tmp_dir}"
20+
cp -r tests/* "${tmp_dir}"
1521

1622
# Iterate over all test directories
17-
for test_dir in tests/*; do
23+
for test_dir in "${tmp_dir}"/*; do
1824
test_dir_name=$(basename "${test_dir}")
1925
test_dir_path=$(realpath "${test_dir}")
2026
results_file_path="${test_dir_path}/results.json"
@@ -33,8 +39,7 @@ for test_dir in tests/*; do
3339
if ! diff "${results_file_path}.tmp" "${expected_results_file_path}.tmp"; then
3440
exit_code=1
3541
fi
36-
37-
rm -f "${results_file_path}.tmp" "${expected_results_file_path}.tmp"
3842
done
3943

44+
rm -rf "${tmp_dir}"
4045
exit ${exit_code}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"version": 1,
33
"status": "fail",
4-
"message": "1/9 test.year not divisible by 4 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-all-fail/test_example_all_fail.zig:7:5: 0x2247d6 in test.year not divisible by 4 in common year (test)\n try testing.expect(!leap.leap(2015));\n ^\n2/9 test.year divisible by 2, not divisible by 4 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-all-fail/test_example_all_fail.zig:11:5: 0x224896 in test.year divisible by 2, not divisible by 4 in common year (test)\n try testing.expect(!leap.leap(1970));\n ^\n3/9 test.year divisible by 4, not divisible by 100 in leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-all-fail/test_example_all_fail.zig:15:5: 0x2248e4 in test.year divisible by 4, not divisible by 100 in leap year (test)\n try testing.expect(leap.leap(1996));\n ^\n4/9 test.year divisible by 4 and 5 is still a leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-all-fail/test_example_all_fail.zig:19:5: 0x224934 in test.year divisible by 4 and 5 is still a leap year (test)\n try testing.expect(leap.leap(1960));\n ^\n5/9 test.year divisible by 100, not divisible by 400 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-all-fail/test_example_all_fail.zig:23:5: 0x224986 in test.year divisible by 100, not divisible by 400 in common year (test)\n try testing.expect(!leap.leap(2100));\n ^\n6/9 test.year divisible by 100 but not by 3 is still not a leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-all-fail/test_example_all_fail.zig:27:5: 0x2249d6 in test.year divisible by 100 but not by 3 is still not a leap year (test)\n try testing.expect(!leap.leap(1900));\n ^\n7/9 test.year divisible by 400 is leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-all-fail/test_example_all_fail.zig:31:5: 0x224a24 in test.year divisible by 400 is leap year (test)\n try testing.expect(leap.leap(2000));\n ^\n8/9 test.year divisible by 400 but not by 125 is still a leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-all-fail/test_example_all_fail.zig:35:5: 0x224a74 in test.year divisible by 400 but not by 125 is still a leap year (test)\n try testing.expect(leap.leap(2400));\n ^\n9/9 test.year divisible by 200, not divisible by 400 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-all-fail/test_example_all_fail.zig:39:5: 0x224ac6 in test.year divisible by 200, not divisible by 400 in common year (test)\n try testing.expect(!leap.leap(1800));\n ^\n0 passed; 0 skipped; 9 failed."
4+
"message": "1/9 test.year not divisible by 4 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-all-fail/test_example_all_fail.zig:7:5: 0x2247d6 in test.year not divisible by 4 in common year (test)\n try testing.expect(!leap.leap(2015));\n ^\n2/9 test.year divisible by 2, not divisible by 4 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-all-fail/test_example_all_fail.zig:11:5: 0x224896 in test.year divisible by 2, not divisible by 4 in common year (test)\n try testing.expect(!leap.leap(1970));\n ^\n3/9 test.year divisible by 4, not divisible by 100 in leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-all-fail/test_example_all_fail.zig:15:5: 0x2248e4 in test.year divisible by 4, not divisible by 100 in leap year (test)\n try testing.expect(leap.leap(1996));\n ^\n4/9 test.year divisible by 4 and 5 is still a leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-all-fail/test_example_all_fail.zig:19:5: 0x224934 in test.year divisible by 4 and 5 is still a leap year (test)\n try testing.expect(leap.leap(1960));\n ^\n5/9 test.year divisible by 100, not divisible by 400 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-all-fail/test_example_all_fail.zig:23:5: 0x224986 in test.year divisible by 100, not divisible by 400 in common year (test)\n try testing.expect(!leap.leap(2100));\n ^\n6/9 test.year divisible by 100 but not by 3 is still not a leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-all-fail/test_example_all_fail.zig:27:5: 0x2249d6 in test.year divisible by 100 but not by 3 is still not a leap year (test)\n try testing.expect(!leap.leap(1900));\n ^\n7/9 test.year divisible by 400 is leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-all-fail/test_example_all_fail.zig:31:5: 0x224a24 in test.year divisible by 400 is leap year (test)\n try testing.expect(leap.leap(2000));\n ^\n8/9 test.year divisible by 400 but not by 125 is still a leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-all-fail/test_example_all_fail.zig:35:5: 0x224a74 in test.year divisible by 400 but not by 125 is still a leap year (test)\n try testing.expect(leap.leap(2400));\n ^\n9/9 test.year divisible by 200, not divisible by 400 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-all-fail/test_example_all_fail.zig:39:5: 0x224ac6 in test.year divisible by 200, not divisible by 400 in common year (test)\n try testing.expect(!leap.leap(1800));\n ^\n0 passed; 0 skipped; 9 failed."
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"version": 1,
33
"status": "fail",
4-
"message": "1/9 test.year not divisible by 4 in common year... OK\n2/9 test.year divisible by 2, not divisible by 4 in common year... OK\n3/9 test.year divisible by 4, not divisible by 100 in leap year... OK\n4/9 test.year divisible by 4 and 5 is still a leap year... OK\n5/9 test.year divisible by 100, not divisible by 400 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-partial-fail/test_example_partial_fail.zig:23:5: 0x224986 in test.year divisible by 100, not divisible by 400 in common year (test)\n try testing.expect(!leap.leap(2100));\n ^\n6/9 test.year divisible by 100 but not by 3 is still not a leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-partial-fail/test_example_partial_fail.zig:27:5: 0x2249d6 in test.year divisible by 100 but not by 3 is still not a leap year (test)\n try testing.expect(!leap.leap(1900));\n ^\n7/9 test.year divisible by 400 is leap year... OK\n8/9 test.year divisible by 400 but not by 125 is still a leap year... OK\n9/9 test.year divisible by 200, not divisible by 400 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/opt/test-runner/tests/example-partial-fail/test_example_partial_fail.zig:39:5: 0x224ac6 in test.year divisible by 200, not divisible by 400 in common year (test)\n try testing.expect(!leap.leap(1800));\n ^\n6 passed; 0 skipped; 3 failed."
4+
"message": "1/9 test.year not divisible by 4 in common year... OK\n2/9 test.year divisible by 2, not divisible by 4 in common year... OK\n3/9 test.year divisible by 4, not divisible by 100 in leap year... OK\n4/9 test.year divisible by 4 and 5 is still a leap year... OK\n5/9 test.year divisible by 100, not divisible by 400 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-partial-fail/test_example_partial_fail.zig:23:5: 0x224986 in test.year divisible by 100, not divisible by 400 in common year (test)\n try testing.expect(!leap.leap(2100));\n ^\n6/9 test.year divisible by 100 but not by 3 is still not a leap year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-partial-fail/test_example_partial_fail.zig:27:5: 0x2249d6 in test.year divisible by 100 but not by 3 is still not a leap year (test)\n try testing.expect(!leap.leap(1900));\n ^\n7/9 test.year divisible by 400 is leap year... OK\n8/9 test.year divisible by 400 but not by 125 is still a leap year... OK\n9/9 test.year divisible by 200, not divisible by 400 in common year... FAIL (TestUnexpectedResult)\n/opt/zig/lib/std/testing.zig:515:14: 0x2246af in expect (test)\n if (!ok) return error.TestUnexpectedResult;\n ^\n/tmp/exercism-zig-test-runner/example-partial-fail/test_example_partial_fail.zig:39:5: 0x224ac6 in test.year divisible by 200, not divisible by 400 in common year (test)\n try testing.expect(!leap.leap(1800));\n ^\n6 passed; 0 skipped; 3 failed."
55
}

0 commit comments

Comments
 (0)