diff --git a/pym/bob/invoker.py b/pym/bob/invoker.py index 1884ec4db..a8a397d8c 100644 --- a/pym/bob/invoker.py +++ b/pym/bob/invoker.py @@ -460,6 +460,7 @@ def ensureSandboxUsable(): ret = subprocess.run([getSandboxHelperPath(), "-C"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode if ret != 0: + msg = None knobs = [("/proc/sys/kernel/unprivileged_userns_clone", "1"), ("/proc/sys/kernel/apparmor_restrict_unprivileged_userns", "0")] for knob, expected_val in knobs: @@ -473,8 +474,13 @@ def ensureSandboxUsable(): except OSError: msg = f"Make sure {knob} is {expected_val}." break - else: + + if not msg and any(k.startswith("NONO_") for k, v in os.environ.items()): + msg = f"The nono sandbox does not support nested unprivileged containers. See https://github.com/nolabs-ai/nono/issues/1980." + + if not msg: msg = f"Not sure why that is exactly. Please consider opening a bug report at https://github.com/BobBuildTool/bob." + raise BuildError("Your system does not support unprivileged containers! You need to build without sandbox (--no-sandbox).", help=f"{msg} In case you use Docker, consider using the 'unconfined' seccomp profile." " See https://bob-build-tool.readthedocs.io/en/latest/installation.html#sandbox-capabilities for more details.", diff --git a/test/black-box/commands/run.sh b/test/black-box/commands/run.sh index 43b4874d0..9888a8d42 100755 --- a/test/black-box/commands/run.sh +++ b/test/black-box/commands/run.sh @@ -34,6 +34,10 @@ test_command() for CMD in "${COMMANDS[@]}"; do for SBX in "${SANDBOX_MODES[@]}" ; do + # Nested containers are not available in the nono sandbox + if is_nono && [[ ( $CMD == build || $CMD == dev || $CMD == project ) && -n "$SBX" && $SBX != --no-sandbox ]] ; then + continue + fi test_command "$CMD" $SBX done done diff --git a/test/black-box/fingerprints/run.sh b/test/black-box/fingerprints/run.sh index e9fb1bf76..80f605fb2 100755 --- a/test/black-box/fingerprints/run.sh +++ b/test/black-box/fingerprints/run.sh @@ -6,6 +6,9 @@ if [[ $(uname -o) == Msys ]] ; then skip fi +# Needs working sandbox +"${BOB_ROOT}/bin/bob-namespace-sandbox" -C || skip + # Test the various direct properties of fingerprint scripts # ========================================================= # diff --git a/test/integration/gitea/run.py b/test/integration/gitea/run.py index 0d76c1ef0..424da1b3c 100644 --- a/test/integration/gitea/run.py +++ b/test/integration/gitea/run.py @@ -104,13 +104,13 @@ def testUploadDownload(archiveUrl, env): d = "testUploadDownload" prepare(d, archiveUrl) - res = runBob(["build", "--download=no", "--upload", "root"], d, env) + res = runBob(["build", "--no-sandbox", "--download=no", "--upload", "root"], d, env) assertEqual(res.returncode, 0) assertEqual(readResult(d, "root"), "ok") shutil.rmtree(os.path.join(d, "work")) - res = runBob(["build", "--download=forced", "root"], d, env) + res = runBob(["build", "--no-sandbox", "--download=forced", "root"], d, env) assertEqual(res.returncode, 0) assertEqual(readResult(d, "root"), "ok") @@ -125,7 +125,7 @@ def testManage(archiveUrl, env): d = "testManage" prepare(d, archiveUrl) - res = runBob(["build", "--download=no", "--upload", "root-alpha", "root-bravo"], d, env) + res = runBob(["build", "--no-sandbox", "--download=no", "--upload", "root-alpha", "root-bravo"], d, env) assertEqual(res.returncode, 0) shutil.rmtree(os.path.join(d, "work")) @@ -143,12 +143,12 @@ def testManage(archiveUrl, env): assertEqual(res.returncode, 0) # The retained artifact must still be downloadable... - res = runBob(["build", "--download=forced", "root-alpha"], d, env) + res = runBob(["build", "--no-sandbox", "--download=forced", "root-alpha"], d, env) assertEqual(res.returncode, 0) assertEqual(readResult(d, "root-alpha"), "alpha") # ...but the pruned one must be gone from the archive. - res = runBob(["build", "--download=forced", "root-bravo"], d, env) + res = runBob(["build", "--no-sandbox", "--download=forced", "root-bravo"], d, env) if res.returncode == 0: print("[TEST]:", "Error: pruned artifact could still be downloaded!") sys.exit(3) @@ -224,6 +224,7 @@ def giteaCmd(args): ROOT_URL = http://{GITEA_HOST}:{GITEA_PORT}/ DISABLE_SSH = true OFFLINE_MODE = true + SSH_CREATE_AUTHORIZED_KEYS_FILE = false [database] DB_TYPE = sqlite3 @@ -276,8 +277,7 @@ def giteaCmd(args): try: giteaLog = open(os.path.join(workDir, "gitea.log"), "wb") - giteaProc = subprocess.Popen([gitea, "--config", appIni, "--work-path", workDir, "web"], - stdout=giteaLog, stderr=subprocess.STDOUT) + giteaProc = subprocess.Popen([gitea, "--config", appIni, "--work-path", workDir, "web"]) print("[TEST]:", "Gitea running as pid", giteaProc.pid, "in", workDir) print("[TEST]:", "Waiting for Gitea to get ready...") diff --git a/test/run-tests.sh b/test/run-tests.sh index 53f561cfa..7e4f0679d 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -73,7 +73,6 @@ run_test() [[ ${VERBOSE+true} ]] && echo "$test_name: finished: ok ($((SECONDS-start))s)" || echo "ok ($((SECONDS-start))s)" elif [[ $ret -eq 240 ]] ; then [[ "${VERBOSE+true}" ]] && echo "$test_name: finished: skipped ($((SECONDS-start))s)" || echo "skipped ($((SECONDS-start))s)" - ret=0 elif [[ ${VERBOSE+true} ]] ; then echo "$test_name: finished: FAIL (exit $ret, $((SECONDS-start))s)" ret=1 @@ -94,7 +93,6 @@ run_test() [[ ${VERBOSE+true} ]] && echo "$test_name: finished: ok ($((SECONDS-start))s)" || echo "ok ($((SECONDS-start))s)" elif [[ $ret -eq 240 ]] ; then [[ ${VERBOSE+true} ]] && echo "$test_name: finished: skipped ($((SECONDS-start))s)" || echo "skipped ($((SECONDS-start))s)" - ret=0 elif [[ ${VERBOSE+true} ]] ; then echo "$test_name: finished: FAIL ($((SECONDS-start))s)" ret=1 @@ -112,6 +110,43 @@ run_test() cat "$LOGFILE" >> log.txt rm "$LOGFILE" + return $ret # 240 if the test was skipped +} + +# Run test and treat "skipped" as success +run_test_plain() +{ + local ret + run_test "$1" + ret=$? + [[ $ret -ne 240 ]] || ret=0 + return $ret +} + +# On GitHub Actions, collect the output of each test and emit it atomically as +# a collapsible group. The lock keeps parallel tests from interleaving. +run_test_grouped() +{ + local ret out=$(mktemp) start=$SECONDS + run_test "$1" > "$out" 2>&1 + ret=$? + local duration=$((SECONDS-start)) + # Portable lock (flock is unreliable on MSYS): mkdir is atomic. + until mkdir "$GROUP_LOCK/lock" 2>/dev/null ; do sleep 0.1 ; done + { + if [[ $ret -eq 240 ]] ; then + echo "::group::⏭️ $1 (${duration}s)" + elif [[ $ret -ne 0 ]] ; then + echo "::group::❌ $1 (${duration}s)" + else + echo "::group::✅ $1 (${duration}s)" + fi + cat "$out" + echo "::endgroup::" + } + rmdir "$GROUP_LOCK/lock" + rm -f "$out" + [[ $ret -ne 240 ]] || ret=0 return $ret } @@ -278,11 +313,20 @@ if [[ -n "$RUN_BLACKBOX_PAT" ]] ; then popd > /dev/null fi +# Group output per test on GitHub Actions (only useful in verbose mode) +RUN_TEST=run_test_plain +if [[ ${GITHUB_ACTIONS:-} == true && ${VERBOSE+true} ]] ; then + RUN_TEST=run_test_grouped + export GROUP_LOCK="$(mktemp -d)" + trap 'rm -rf "$GROUP_LOCK" 2>/dev/null' EXIT +fi + # execute all tests, possibly in parallel if [[ ${#RUN_TEST_NAMES[@]} -eq 0 ]] ; then : # No tests matched elif [[ ${RUN_JOBS:-} != 1 ]] ; then - export -f run_test print_verbose + export -f run_test run_test_plain run_test_grouped print_verbose + export RUN_TEST export RUN_PYTHON3 export RUN_PYTHON3_COV export TEST_ENVIRONMENT @@ -291,11 +335,11 @@ elif [[ ${RUN_JOBS:-} != 1 ]] ; then # buffering. This prevents the output from being interleaced. echo "${RUN_TEST_NAMES[@]}" \ | xargs -P ${RUN_JOBS:-$(nproc)} -n 1 \ - bash -o pipefail -c 'run_test "$@" | stdbuf -oL sed -e ""' bash \ + bash -o pipefail -c '$RUN_TEST "$@" | stdbuf -oL sed -e ""' bash \ || : $((FAILED+=$?)) else for i in "${RUN_TEST_NAMES[@]}" ; do - if ! run_test "$i" ; then + if ! $RUN_TEST "$i" ; then : $((FAILED++)) fi done diff --git a/test/test-lib.sh b/test/test-lib.sh index 8deca6bf9..e5401e353 100644 --- a/test/test-lib.sh +++ b/test/test-lib.sh @@ -95,6 +95,12 @@ file_url() fi } +# Running inside the nono sandbox? +is_nono() +{ + [[ -n "${NONO_CAP_FILE:-}" ]] +} + cleanup() { rm -rf work dev .bob-* "$@" @@ -104,16 +110,34 @@ cleanup() # and "no global defaults" debug switches. run_bob() { + local args=("$@") + + # The nono sandbox does not support nested unprivileged containers. The + # "build" command uses the sandbox by default. Disable it unless the test + # explicitly asks for a sandbox mode. + if is_nono ; then + local a has_mode=0 is_build=0 + for a in "$@" ; do + [[ "$a" == *-sandbox ]] && has_mode=1 + done + # Find the command. Skip global "-C DIR" option. + local pos=0 + [[ "$1" == -C ]] && pos=2 + [[ "${args[$pos]:-}" == build || "${args[$pos]:-}" == project ]] && is_build=1 + # Options must precede generator arguments of "project" + [[ $is_build -eq 1 && $has_mode -eq 0 ]] && args=("${args[@]:0:pos+1}" --no-sandbox "${args[@]:pos+1}") + fi + if is_win32 ; then - python "$BOB_ROOT/bob" --debug=pkgck,ngd,audit "$@" + python "$BOB_ROOT/bob" --debug=pkgck,ngd,audit "${args[@]}" else - "$BOB_ROOT/bob" --debug=pkgck,ngd,audit "$@" + "$BOB_ROOT/bob" --debug=pkgck,ngd,audit "${args[@]}" fi } export_run_bob() { - export -f run_bob is_win32 + export -f run_bob is_win32 is_nono export BOB_ROOT TEST_ENVIRONMENT PYTHONPATH } diff --git a/test/unit/test_jenkins_build.py b/test/unit/test_jenkins_build.py index abb26e251..44941b111 100644 --- a/test/unit/test_jenkins_build.py +++ b/test/unit/test_jenkins_build.py @@ -6,8 +6,16 @@ import subprocess import sys +from bob.invoker import getSandboxHelperPath from bob.utils import removePath +def _sandboxWorks(): + # Nested containers are not available e.g. inside the nono sandbox. + if sys.platform != "linux": + return False + return subprocess.run([getSandboxHelperPath(), "-C"], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL).returncode == 0 + class JenkinsBuilds(JenkinsTests): OPTIONS = "" @@ -556,7 +564,7 @@ def testBuild(self): class JenkinsSandboxBuildDisabled(JenkinsSandboxBuilds, TestCase): pass -@skipUnless(sys.platform == "linux", "Sandbox requires Linux") +@skipUnless(_sandboxWorks(), "Sandbox requires working containers") class JenkinsSandboxBuildPartial(JenkinsSandboxBuilds, TestCase): OPTIONS = "--sandbox" OUTSIDE_ISOLATED=0 @@ -565,7 +573,7 @@ class JenkinsSandboxBuildPartial(JenkinsSandboxBuilds, TestCase): INSIDE_STABLE_PATH=1 INSIDE_IMAGE_USED=1 -@skipUnless(sys.platform == "linux", "Sandbox requires Linux") +@skipUnless(_sandboxWorks(), "Sandbox requires working containers") class JenkinsSandboxBuildSlim(JenkinsSandboxBuilds, TestCase): OPTIONS = "--slim-sandbox" OUTSIDE_ISOLATED=1 @@ -574,7 +582,7 @@ class JenkinsSandboxBuildSlim(JenkinsSandboxBuilds, TestCase): INSIDE_STABLE_PATH=0 INSIDE_IMAGE_USED=0 -@skipUnless(sys.platform == "linux", "Sandbox requires Linux") +@skipUnless(_sandboxWorks(), "Sandbox requires working containers") class JenkinsSandboxBuildDev(JenkinsSandboxBuilds, TestCase): OPTIONS = "--dev-sandbox" OUTSIDE_ISOLATED=1 @@ -583,7 +591,7 @@ class JenkinsSandboxBuildDev(JenkinsSandboxBuilds, TestCase): INSIDE_STABLE_PATH=0 INSIDE_IMAGE_USED=1 -@skipUnless(sys.platform == "linux", "Sandbox requires Linux") +@skipUnless(_sandboxWorks(), "Sandbox requires working containers") class JenkinsSandboxBuildStrict(JenkinsSandboxBuilds, TestCase): OPTIONS = "--strict-sandbox" OUTSIDE_ISOLATED=1