From 4f82cb826192cbbf34d1f85c73df6ac79659fb5d Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 18 Feb 2021 00:47:55 +0100 Subject: [PATCH] WIP: test makefile --- test/.gitignore | 2 ++ test/Makefile | 70 +++++++++++++++++++++++++++++++++++++++++ test/busybox-x86_64.md5 | 1 + test/run.sh | 14 ++++++--- test/testenter.sh | 26 +++++++++++++++ test/testwrapper.sh | 22 +++++++++++-- 6 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 test/.gitignore create mode 100644 test/Makefile create mode 100644 test/busybox-x86_64.md5 create mode 100755 test/testenter.sh diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 00000000..af5859fd --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +busybox-x86_64 +test_root diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 00000000..206ffb93 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,70 @@ +ARCH ?= x86_64 + +busybox-ver = 1.31.0 +busybox-bin = busybox-$(ARCH) + +testroot = test_root +testroot_bin = $(testroot)/bin +binaries = $(testroot_bin)/cat \ + $(testroot_bin)/date \ + $(testroot_bin)/echo \ + $(testroot_bin)/ls \ + $(testroot_bin)/mkdir \ + $(testroot_bin)/mkfifo \ + $(testroot_bin)/mknod \ + $(testroot_bin)/mount \ + $(testroot_bin)/printf \ + $(testroot_bin)/ps \ + $(testroot_bin)/sh \ + $(testroot_bin)/sleep \ + $(testroot_bin)/top \ + $(testroot_bin)/touch +dirs = $(testroot)/dev \ + $(testroot)/lib \ + $(testroot)/proc \ + $(testroot)/sbin \ + $(testroot)/sys \ + $(testroot)/tmp \ + $(testroot)/usr/local/lib + +$(testroot)/%: $(testroot) + mkdir -p $@ + +$(busybox-bin): + wget 'https://www.busybox.net/downloads/binaries/$(busybox-ver)-defconfig-multiarch-musl/$(busybox-bin)' + md5sum -c $(busybox-bin).md5 + +$(testroot): + mkdir $(testroot) + +$(testroot_bin)/$(busybox-bin): $(testroot_bin) $(busybox-bin) + cp $(busybox-bin) $(testroot_bin)/ + chmod +x $(testroot_bin)/$(busybox-bin) + +$(testroot_bin)/%: $(testroot_bin)/$(busybox-bin) + cd $(testroot_bin); \ + ln -s $(busybox-bin) $(notdir $@) + +$(testroot_bin)/testwrapper.sh: + cp testwrapper.sh $@ + +$(testroot)/sbin/finit: ../src/finit $(testroot)/lib/ld-musl-x86_64.so.1 $(testroot)/usr/local/lib/libite.so.5 $(testroot)/usr/local/lib/libuev.so.2 + cp $< $@ + +$(testroot)/lib/ld-musl-x86_64.so.1: + cp test_root.backup/lib/ld-musl-x86_64.so.1 $@ + +$(testroot)/usr/local/lib/libite.so.5: + cp test_root.backup/usr/local/lib/libite.so.5 $@ + +$(testroot)/usr/local/lib/libuev.so.2: + cp test_root.backup/usr/local/lib/libuev.so.2 $@ + +.PHONY: test +test: $(binaries) $(testroot_bin)/testwrapper.sh $(dirs) $(testroot)/sbin/finit + ./run.sh finit + +.PHONY: clean +clean: + rm -rf $(testroot) + rm -f $(busybox-bin) diff --git a/test/busybox-x86_64.md5 b/test/busybox-x86_64.md5 new file mode 100644 index 00000000..0ecfec84 --- /dev/null +++ b/test/busybox-x86_64.md5 @@ -0,0 +1 @@ +ea7e2353af0067a926d3e021e504290e busybox-x86_64 diff --git a/test/run.sh b/test/run.sh index 399bdb8b..fff9e83e 100755 --- a/test/run.sh +++ b/test/run.sh @@ -1,15 +1,19 @@ #!/bin/sh -set -eux +set -eu TEST_DIR=$(dirname "$0") -TESTS_ROOT="${TEST_DIR}/test_root" +TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" -cp "${TEST_DIR}/testwrapper.sh" "${TESTS_ROOT}/usr/local/bin/" +echo "Hint: Execute './test/testenter.sh \$(pgrep -P $$) /bin/sh; reset' to enter the test namespace" -unshare \ +# Not supported by Busybox unshare: +# --cgroup --time +export PATH=/sbin:/bin +exec unshare \ --user --map-root-user \ --fork --pid --mount-proc \ --mount \ --mount-proc \ - chroot "$TESTS_ROOT" /usr/local/bin/testwrapper.sh "$@" + --uts --ipc --net \ + chroot "$TESTS_ROOT" /bin/testwrapper.sh "$@" diff --git a/test/testenter.sh b/test/testenter.sh new file mode 100755 index 00000000..8c4b11bb --- /dev/null +++ b/test/testenter.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +set -eu + +if [ "$#" -lt 2 ]; then + echo "Usage:" + echo " $0 [target-pid] [command [arguments]]" + exit 1 +fi +target="$1" +shift + +TEST_DIR=$(dirname "$0") +TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" + +export PATH=/sbin:/bin +nsenter \ + --preserve-credentials \ + --user \ + --mount \ + --uts \ + --ipc \ + --net \ + --pid \ + -w -t "$target" \ + chroot "$TESTS_ROOT" "$@" diff --git a/test/testwrapper.sh b/test/testwrapper.sh index ec4be152..19a9afee 100755 --- a/test/testwrapper.sh +++ b/test/testwrapper.sh @@ -1,9 +1,27 @@ #!/bin/sh -set -eux +set -eu -export PATH=/sbin:/bin +mount -n -t tmpfs none /dev +# mount -t devtmpfs none /dev + +# mknod -m 622 /dev/console c 5 1 +# mknod -m 666 /dev/null c 1 3 +# mknod -m 666 /dev/zero c 1 5 +# mknod -m 666 /dev/ptmx c 5 2 +# mknod -m 666 /dev/tty c 5 0 +# mknod -m 444 /dev/random c 1 8 +# mknod -m 444 /dev/urandom c 1 9 +# chown root:tty /dev/{console,ptmx,tty} mount -t proc none /proc +mount -t sysfs none /sys +mount -t tmpfs none /tmp + +# mkdir -p /dev/pts +# mount -t devpts none /dev/pts + +tty=/dev/$(cat /sys/class/tty/console/active) +mkfifo "$tty" exec "$@"