diff --git a/bin/ch-test b/bin/ch-test index 2788eed42..db7c281fc 100755 --- a/bin/ch-test +++ b/bin/ch-test @@ -88,6 +88,8 @@ EOF bats_vmin=1.2.0 # FIXME: get from configure somehow? +ch_build_unpack_all="argenv quick copy almalinux_8ch debian_11ch hello multistage seccomp" + ### Functions @@ -197,6 +199,9 @@ dir_tar_check_file () { for f in "$@"; do if [[ -f ${CH_TEST_TARDIR}/${f} ]]; then return 0 + elif [[ "$f" == *"chtest"* ]] && [[ -n "$ch_build_unpack_list" ]]; then + # chtest not needed for build-unpack phase + return 0 else missing+=("${CH_TEST_TARDIR}/${f}") fi @@ -398,7 +403,7 @@ test_make () { local tag printf "finding tests compatible with %s phase settings ...\n" "$phase" case $phase in - build|build-images) + build|build-images|build-unpack) for i in $images $examples; do if test_make_check_image "$i"; then echo "found: $i" @@ -461,7 +466,7 @@ test_make () { } test_make_auto () { - local mode + local mode # mode is the test phase mode=$1;shift if [[ $mode != tag ]]; then printf "# Do not edit this file; it's autogenerated\n\n" @@ -495,7 +500,7 @@ test_make_auto () { fatal "test_make_auto: unknown build type" ;; esac - elif [[ $mode == run ]];then + elif [[ $mode == run || $mode == build-unpack ]];then test_make_template_print 'unpack.bats.in' else fatal "test_make_auto: invalid mode '$mode'" @@ -669,6 +674,17 @@ test_one_file () { bats "$one_file" } +test_build_unpack () { + case $phase in + build) + bats "${TMP_}/build_auto.bats" + ;; + run) + bats "${TMP_}/run_auto.bats" + ;; + esac +} + test_rootemu () { if command -v ch-image > /dev/null 2>&1; then bats force-auto.bats @@ -816,10 +832,11 @@ fi if [[ $# == 0 ]]; then usage 1 fi +ch_build_unpack_list="" while [[ $# -gt 0 ]]; do opt=$1; shift case $opt in - all|build|build-images|clean|examples|mk-perm-dirs|rm-perm-dirs|rootemu|run) + all|build|build-images|build-unpack|clean|examples|mk-perm-dirs|rm-perm-dirs|rootemu|run) require_unset phase phase=$opt ;; @@ -855,6 +872,11 @@ while [[ $# -gt 0 ]]; do -h|--help) usage 0 ;; + -i) + phase=build-unpack + ch_build_unpack_list+=" $1" + shift + ;; --img-dir) require_unset imgdir imgdir=$1; shift @@ -945,6 +967,8 @@ while [[ $# -gt 0 ]]; do esac done +export ch_build_unpack_list + printf 'ch-run: %s (%s)\n' "${ch_bin}/ch-run" "$ch_libc" printf 'bats: %s (%s)\n' "$bats" "$bats_version" if [[ $(printf "%s\n%s" "$bats_version" "$bats_vmin" | sort -V | head -1) \ @@ -978,7 +1002,7 @@ if [[ $phase = one-file ]]; then fi printf "%-21s %s" 'phase:' "$phase" -if [[ $phase = one-file ]]; then +if [[ $phase = one-file || $phase = build-unpack ]]; then printf ': %s (%s)' "$one_file" "$ch_one_test" fi if [[ -z $phase ]]; then @@ -1168,6 +1192,24 @@ case $phase in test_build win ;; + build-unpack) + if [[ -z "$ch_build_unpack_list" ]]; then + # Assume “build-unpack” was specified without “-i” being called, + # build and upack all images. + ch_build_unpack_list="$ch_build_unpack_all" + fi + phase=build + dir_tar_mk + builder_check + test_make + test_build_unpack + + phase=run + dir_img_mk + dir_tar_check + test_make + test_build_unpack + ;; clean) dirs_unpriv_rm if [[ -d $TMP_ ]] && [[ -e $TMP_/build_auto.bats ]]; then diff --git a/doc/ch-test.rst b/doc/ch-test.rst index 405e7f541..4bbdff11a 100644 --- a/doc/ch-test.rst +++ b/doc/ch-test.rst @@ -90,8 +90,10 @@ allows trading off thoroughness versus time. Create the filesystem permissions directories. Requires :code:`--perm-dirs`. - :code:`build-images` - Build images from :code:`build` phase, without running the associated tests. + :code:`build-unpack` + Build and unpack images only, without running the associated tests. By + default, build/unpack all images from the *build* phase. If :code:`-i` is + given, build/unpack those images only. :code:`clean` Delete automatically-generated test files, and packed and unpacked image @@ -107,8 +109,8 @@ allows trading off thoroughness versus time. where you must specify the corresponding Dockerfile or :code:`Build` file instead. This is somewhat brittle and typically used for development or debugging. For example, it does not check whether the pre-requisites of - whatever is in the file are satisfied. Often running :code:`build` and - :code:`run` first is sufficient, but this varies. + whatever is in the file are satisfied. Often running :code:`build-unpack` + and first is sufficient, but this varies. If :code:`TEST` is also given, then run only tests with name containing that string, skipping the others. The separator is a literal colon. If the @@ -159,6 +161,10 @@ Additional arguments: :code:`-h`, :code:`--help` Print usage and then exit. + :code:`-i IMG` + Build and unpack specified image (:code:`IMG`); can be repeated for + multiple images. Implies :code:`build-unpack` as the phase. + :code:`--img-dir DIR` Set unpacked images directory to :code:`DIR`. In a multi-node allocation, this directory may not be shared between nodes. Default: diff --git a/test/common.bash b/test/common.bash index c9f7a1d30..23f6a635a 100644 --- a/test/common.bash +++ b/test/common.bash @@ -252,6 +252,16 @@ scope () { return 0 fi fi + if [[ -n $ch_build_unpack_list ]]; then + for image in $ch_build_unpack_list; do + if [[ $BATS_TEST_DESCRIPTION == *"build $image"* \ + || $BATS_TEST_DESCRIPTION == *"builder to archive $image"* \ + || $BATS_TEST_DESCRIPTION == *"unpack $image"* ]]; then + return 0 + fi + done + skip 'per -i' + fi case $1 in # $1 is the test’s scope quick) ;; # always run quick-scope tests