Add tests for wrappers

This commit is contained in:
Filippo Squillace 2020-12-29 13:09:35 +01:00
parent 3d16ee2583
commit 2aeb23b882
4 changed files with 74 additions and 43 deletions

View file

@ -68,12 +68,6 @@ usage() {
echo -e " b[uild] Build a $NAME image (must run in ArchLinux)"
echo -e " -n, --disable-check Disable the $NAME image check"
echo
echo -e " c[reate-wrappers] Create wrappers in ${JUNEST_HOME}/usr/bin_wrappers of the executables under"
echo -e " ${JUNEST_HOME}/usr/bin to be run directly from the host."
echo -e " Use the variable JUNEST_ARGS to define how the wrapper will call $NAME (default "ns --fakeroot")."
echo -e " Use PATH variable to point directly to the bin_wrappers directory from the host."
echo -e " -r, --recreate-existing Instead of skipping existing wrappers recreate them"
echo
}
version() {
@ -87,7 +81,6 @@ function parse_arguments(){
ACT_NAMESPACE=false
ACT_PROOT=false
ACT_GROOT=false
ACT_WRAPPERS=false
ACT_ROOT=false
ACT_HELP=false
ACT_VERSION=false
@ -99,7 +92,6 @@ function parse_arguments(){
p|proot) ACT_PROOT=true ; shift ;;
g|groot) ACT_GROOT=true ; shift ;;
r|root) ACT_ROOT=true ; shift ;;
c|create-wrappers) ACT_WRAPPERS=true ; shift ;;
-h|--help) ACT_HELP=true ; shift ;;
-V|--version) ACT_VERSION=true ; shift ;;
*) ACT_NAMESPACE=true ;;
@ -123,33 +115,9 @@ function parse_arguments(){
elif $ACT_ROOT
then
_parse_root_opts "$@"
elif $ACT_WRAPPERS
then
_parse_wrappers_opts "$@"
fi
}
function _parse_wrappers_opts() {
# Options:
OPT_RECREATE_EXISTING=false
while [[ -n "$1" ]]
do
case "$1" in
-r|--recreate-existing) OPT_RECREATE_EXISTING=true ; shift ;;
--) shift ; break ;;
-*) die "Invalid option $1" ;;
*) break ;;
esac
done
ARGS=()
for arg in "$@"
do
ARGS+=("$arg")
done
}
function _parse_root_opts() {
# Options:
BACKEND_ARGS=""
@ -280,7 +248,7 @@ function execute_operation() {
else
setup_env $ARCH_ARG
fi
create_wrappers false
create_wrappers
fi
return
@ -292,11 +260,6 @@ function execute_operation() {
die "Error: The image is still not installed in $JUNEST_HOME. Run this first: $CMD setup"
fi
if $ACT_WRAPPERS; then
create_wrappers $OPT_RECREATE_EXISTING
return
fi
local run_env
if $ACT_NAMESPACE; then
if $OPT_FAKEROOT; then
@ -316,12 +279,9 @@ function execute_operation() {
run_env=run_env_as_chroot
fi
$run_env "$BACKEND_COMMAND" "${BACKEND_ARGS}" $OPT_NO_COPY_FILES "${ARGS[@]}"
# TODO use the run_env exit status
# Call create_wrappers in case new bin files have been created
create_wrappers false
trap "create_wrappers" EXIT QUIT TERM KILL
$run_env "$BACKEND_COMMAND" "${BACKEND_ARGS}" $OPT_NO_COPY_FILES "${ARGS[@]}"
}
function main() {

View file

@ -16,3 +16,6 @@ $JUNEST_SCRIPT proot -- "$CHECK_SCRIPT" --skip-aur-tests --use-sudo
$JUNEST_SCRIPT ns --fakeroot -- "$CHECK_SCRIPT" --skip-aur-tests
$JUNEST_SCRIPT ns -- "$CHECK_SCRIPT" --use-sudo
sudo -E $JUNEST_SCRIPT groot -- "$CHECK_SCRIPT" --run-root-tests --skip-aur-tests
# Test the wrappers work
$JUNEST_HOME/usr/bin_wrappers/pacman --help

View file

@ -0,0 +1,67 @@
#!/bin/bash
source "$(dirname $0)/../utils/utils.sh"
source "$(dirname $0)/../../lib/core/wrappers.sh"
# Disable the exiterr
set +e
function oneTimeSetUp(){
setUpUnitTests
}
function setUp(){
junestSetUp
}
function tearDown(){
junestTearDown
}
function test_create_wrappers_empty_bin(){
assertCommandSuccess create_wrappers
assertEquals "" "$(cat $STDOUTF)"
assertTrue "bin_wrappers does not exist" "[ -e $JUNEST_HOME/usr/bin_wrappers ]"
}
function test_create_wrappers_not_executable_file(){
touch $JUNEST_HOME/usr/bin/myfile
assertCommandSuccess create_wrappers
assertEquals "" "$(cat $STDOUTF)"
assertTrue "bin_wrappers should exist" "[ -e $JUNEST_HOME/usr/bin_wrappers ]"
assertTrue "myfile wrapper should not exist" "[ ! -x $JUNEST_HOME/usr/bin_wrappers/myfile ]"
}
function test_create_wrappers_executable_file(){
touch $JUNEST_HOME/usr/bin/myfile
chmod +x $JUNEST_HOME/usr/bin/myfile
assertCommandSuccess create_wrappers
assertEquals "" "$(cat $STDOUTF)"
assertTrue "bin_wrappers should exist" "[ -e $JUNEST_HOME/usr/bin_wrappers ]"
assertTrue "myfile wrapper should exist" "[ -x $JUNEST_HOME/usr/bin_wrappers/myfile ]"
}
function test_create_wrappers_already_exist(){
touch $JUNEST_HOME/usr/bin/myfile
chmod +x $JUNEST_HOME/usr/bin/myfile
mkdir -p $JUNEST_HOME/usr/bin_wrappers
touch $JUNEST_HOME/usr/bin_wrappers/myfile
chmod +x $JUNEST_HOME/usr/bin_wrappers/myfile
assertCommandSuccess create_wrappers
assertEquals "" "$(cat $STDOUTF)"
assertTrue "bin_wrappers should exist" "[ -e $JUNEST_HOME/usr/bin_wrappers ]"
assertTrue "myfile wrapper should exist" "[ -x $JUNEST_HOME/usr/bin_wrappers/myfile ]"
assertEquals "" "$(touch $JUNEST_HOME/usr/bin_wrappers/myfile)"
}
function test_create_wrappers_executable_no_longer_exist(){
mkdir -p $JUNEST_HOME/usr/bin_wrappers
touch $JUNEST_HOME/usr/bin_wrappers/myfile
chmod +x $JUNEST_HOME/usr/bin_wrappers/myfile
assertCommandSuccess create_wrappers
assertEquals "" "$(cat $STDOUTF)"
assertTrue "bin_wrappers should exist" "[ -e $JUNEST_HOME/usr/bin_wrappers ]"
assertTrue "myfile wrapper should not exist" "[ ! -x $JUNEST_HOME/usr/bin_wrappers/myfile ]"
}
source $(dirname $0)/../utils/shunit2

View file

@ -11,6 +11,7 @@ function cwdTearDown(){
function junestSetUp(){
JUNEST_HOME=$(TMPDIR=/tmp mktemp -d -t junest-home.XXXXXXXXXX)
mkdir -p ${JUNEST_HOME}/usr/bin
mkdir -p ${JUNEST_HOME}/etc/junest
echo "JUNEST_ARCH=x86_64" > ${JUNEST_HOME}/etc/junest/info
mkdir -p ${JUNEST_HOME}/etc/ca-certificates