Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

ch-test: build and unpack image only #1899

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
52 changes: 47 additions & 5 deletions bin/ch-test
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should derive this list from the tests we actually have; it should also be responsive to --scope.



### Functions

Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not true — why treat chtest different from any other image?

return 0
else
missing+=("${CH_TEST_TARDIR}/${f}")
fi
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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'"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
;;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -945,6 +967,8 @@ while [[ $# -gt 0 ]]; do
esac
done

export ch_build_unpack_list
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subprocesses won't need this; are you sure it needs to be exported?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my understanding that the export is necessary for this variable to be available to other files, e.g. common.bash, where we decide whether or not to skip a test. I'm basically copying what we do with ch_one_test.


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) \
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a reasonable way to make one-file interact with build-unpack. That is, rather than listing images with -i, build-unpack builds and unpacks all images except if -f is given. I think -f does have some smarts about which tests indicate an image?

printf ': %s (%s)' "$one_file" "$ch_one_test"
fi
if [[ -z $phase ]]; then
Expand Down Expand Up @@ -1168,6 +1192,24 @@ case $phase in
test_build
win
;;
build-unpack)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if rather than making build-unpack a phase, could it be a modifier on build and run? Or, perhaps there's a new phase unpack, and build-unpack implies build-images and unpack? Or, maybe build-images goes away and is replaced by running build with the build-unpack modifier?

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
Expand Down
9 changes: 9 additions & 0 deletions doc/ch-test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ allows trading off thoroughness versus time.
:code:`build-images`
Build images from :code:`build` phase, without running the associated tests.

:code:`build-unpack`
Build and unpack images only. By default, build and unpack all applicable
images. If one or more images have been specified with :code:`-i`, build and
unpack those images only.

:code:`clean`
Delete automatically-generated test files, and packed and unpacked image
directories.
Expand Down Expand Up @@ -159,6 +164,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 to build and
unpack 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:
Expand Down
10 changes: 10 additions & 0 deletions test/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down