From 6b357245c3796e786204c027ee22b35a5ea53825 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Sat, 18 Jul 2015 12:21:47 +0000 Subject: [PATCH] Issue #94: Add tests for arch option --- .travis.yml | 7 +++- bin/junest | 15 ++++++-- lib/core.sh | 20 +++++++---- tests/test_cli.sh | 87 ++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 107 insertions(+), 22 deletions(-) diff --git a/.travis.yml b/.travis.yml index d3273b8..570a311 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,9 +4,14 @@ sudo: required install: - PATH=$PWD/bin:$PATH - - junest --fakeroot echo 'Installing junest' + - junest -- echo "Installing JuNest (\$(uname -m))" + - JUNEST_HOME=~/.junest-arm junest -a arm -- echo "Installing JuNest (\$(uname -m))" + # TODO: Remember to enable x86 tests when fixed + #- JUNEST_HOME=~/.junest-x86 junest -a x86 -- echo "Installing JuNest (\$(uname -m))" script: - ./tests/test_all.sh - junest --check ./bin/junest - yes | junest --delete + - JUNEST_HOME=~/.junest-arm junest --check ./bin/junest --skip-root-tests + - yes | JUNEST_HOME=~/.junest-arm junest --delete diff --git a/bin/junest b/bin/junest index d00167e..18a3de8 100755 --- a/bin/junest +++ b/bin/junest @@ -46,6 +46,7 @@ usage() { echo -e "-n, --disable-validation Disable the $NAME image validation" echo -e "-c, --check <${CMD}_script> Validate the env located in ${JUNEST_HOME}" echo -e " using ${CMD}_script. This will alterate the environment." + echo -e "-s, --skip-root-tests Skip the root tests during the validation process." echo echo -e "General options:" echo -e "-h, --help Show this help message" @@ -67,6 +68,14 @@ check_cli(){ die "The build image option must be used exclusively" fi fi + if $OPT_SKIP_ROOT_TEST + then + if $OPT_DELETE || $OPT_HELP || $OPT_VERSION || $OPT_SETUP_FROM_FILE || \ + $OPT_FAKEROOT || $OPT_ROOT + then + die "The skip root tests option must be used with either build image or check options" + fi + fi if $OPT_CHECK then if $OPT_DELETE || $OPT_HELP || $OPT_VERSION || $OPT_SETUP_FROM_FILE || \ @@ -145,6 +154,7 @@ function parse_arguments(){ OPT_DISABLE_VALIDATION=false OPT_CHECK=false CHECK_ARG="" + OPT_SKIP_ROOT_TEST=false OPT_DELETE=false OPT_HELP=false OPT_VERSION=false @@ -159,6 +169,7 @@ function parse_arguments(){ -b|--build-image) OPT_BUILD_IMAGE=true ; shift ;; -n|--disable-validation) OPT_DISABLE_VALIDATION=true ; shift ;; -c|--check) OPT_CHECK=true ; shift ; CHECK_ARG=$1; shift ;; + -s|--skip-root-tests) OPT_SKIP_ROOT_TEST=true ; shift ;; -d|--delete) OPT_DELETE=true ; shift ;; -h|--help) OPT_HELP=true ; shift ;; -v|--version) OPT_VERSION=true ; shift ;; @@ -180,13 +191,13 @@ function execute_operation(){ $OPT_VERSION && version && return if $OPT_BUILD_IMAGE; then - build_image_env $OPT_DISABLE_VALIDATION + build_image_env $OPT_DISABLE_VALIDATION $OPT_SKIP_ROOT_TEST return elif $OPT_DELETE; then delete_env return elif $OPT_CHECK; then - check_env "${JUNEST_HOME}" "${CHECK_ARG}" + check_env "${JUNEST_HOME}" "${CHECK_ARG}" $OPT_SKIP_ROOT_TEST return fi diff --git a/lib/core.sh b/lib/core.sh index 97c81f8..836dd3e 100644 --- a/lib/core.sh +++ b/lib/core.sh @@ -313,6 +313,7 @@ function build_image_env(){ _check_package git local disable_validation=$1 + local skip_root_tests=$2 local maindir=$(TMPDIR=$JUNEST_TEMPDIR mktemp -d -t ${CMD}.XXXXXXXXXX) sudo mkdir -p ${maindir}/root @@ -399,7 +400,7 @@ function build_image_env(){ then mkdir -p ${maindir}/root_test $TAR -zxpf ${imagefile} -C "${maindir}/root_test" - check_env "${maindir}/root_test" "${maindir}/root_test/opt/${CMD}/bin/${CMD}" + check_env "${maindir}/root_test" "${maindir}/root_test/opt/${CMD}/bin/${CMD}" $skip_root_tests fi sudo cp ${maindir}/output/${imagefile} ${ORIGIN_WD} @@ -412,14 +413,21 @@ function build_image_env(){ function check_env(){ local testdir=$1 local cmd=$2 + local skip_root_tests=$3 info "Validating ${NAME} located in ${testdir} using the ${cmd} script..." echo "Server = ${DEFAULT_MIRROR}" >> ${testdir}/etc/pacman.d/mirrorlist JUNEST_HOME=${testdir} ${cmd} -f pacman --noconfirm -Syy # Check most basic executables work - JUNEST_HOME=${testdir} sudo -E ${cmd} -r pacman -Qi pacman 1> /dev/null - JUNEST_HOME=${testdir} sudo -E ${cmd} -r yaourt -V 1> /dev/null - JUNEST_HOME=${testdir} sudo -E ${cmd} -r /opt/proot/proot-$ARCH --help 1> /dev/null + $skip_root_tests || JUNEST_HOME=${testdir} sudo -E ${cmd} -r pacman -Qi pacman 1> /dev/null + JUNEST_HOME=${testdir} ${cmd} -- pacman -Qi pacman 1> /dev/null + JUNEST_HOME=${testdir} ${cmd} -f -- pacman -Qi pacman 1> /dev/null + $skip_root_tests || JUNEST_HOME=${testdir} sudo -E ${cmd} -r yaourt -V 1> /dev/null + JUNEST_HOME=${testdir} ${cmd} -- yaourt -V 1> /dev/null + JUNEST_HOME=${testdir} ${cmd} -f -- yaourt -V 1> /dev/null + $skip_root_tests || JUNEST_HOME=${testdir} sudo -E ${cmd} -r /opt/proot/proot-$ARCH --help 1> /dev/null + JUNEST_HOME=${testdir} ${cmd} -- /opt/proot/proot-$ARCH --help 1> /dev/null + JUNEST_HOME=${testdir} ${cmd} -f -- /opt/proot/proot-$ARCH --help 1> /dev/null local repo_package=tree info "Installing ${repo_package} package from official repo using proot..." @@ -430,13 +438,13 @@ function check_env(){ local repo_package=iftop info "Installing ${repo_package} package from official repo using root..." JUNEST_HOME=${testdir} ${cmd} -f pacman --noconfirm -S ${repo_package} - JUNEST_HOME=${testdir} sudo -E ${cmd} -r iftop -t -s 5 + $skip_root_tests || JUNEST_HOME=${testdir} sudo -E ${cmd} -r iftop -t -s 5 JUNEST_HOME=${testdir} ${cmd} -f pacman --noconfirm -S base-devel local yaourt_package=tcptraceroute info "Installing ${yaourt_package} package from AUR repo using proot..." JUNEST_HOME=${testdir} ${cmd} -f -- yaourt -A --noconfirm -S ${yaourt_package} - JUNEST_HOME=${testdir} sudo -E ${cmd} -r tcptraceroute localhost + $skip_root_tests || JUNEST_HOME=${testdir} sudo -E ${cmd} -r tcptraceroute localhost info "Removing the previous packages..." JUNEST_HOME=${testdir} ${cmd} -f pacman --noconfirm -Rsn tcptraceroute tree iftop diff --git a/tests/test_cli.sh b/tests/test_cli.sh index f78c487..46d38d1 100755 --- a/tests/test_cli.sh +++ b/tests/test_cli.sh @@ -4,6 +4,12 @@ source $(dirname $0)/../bin/junest -h &> /dev/null # Disable the exiterr set +e +function setUp(){ + function is_env_installed(){ + return 0 + } +} + ## Mock functions ## function usage(){ echo "usage" @@ -12,22 +18,24 @@ function version(){ echo "version" } function build_image_env(){ - echo "build_image_env" + local disable_validation=$1 + local skip_root_tests=$2 + echo "build_image_env($disable_validation,$skip_root_tests)" } function check_env(){ - echo "check_env" + local env_home=$1 + local cmd_script=$2 + local skip_root_tests=$3 + echo "check_env($env_home,$cmd_script,$skip_root_tests)" } function delete_env(){ echo "delete_env" } -function is_env_installed(){ - return 0 -} function setup_env_from_file(){ - echo "setup_env_from_file $@" + echo "setup_env_from_file($1)" } function setup_env(){ - echo "setup_env" + echo "setup_env($1)" } function run_env_as_fakeroot(){ local proot_args="$1" @@ -63,15 +71,27 @@ function test_version(){ } function test_build_image_env(){ local output=$(wrap_env -b) - assertEquals $output "build_image_env" + assertEquals $output "build_image_env(false,false)" local output=$(wrap_env --build-image) - assertEquals $output "build_image_env" + assertEquals $output "build_image_env(false,false)" + local output=$(wrap_env -b -s) + assertEquals $output "build_image_env(false,true)" + local output=$(wrap_env -b -n) + assertEquals $output "build_image_env(true,false)" + local output=$(wrap_env -b -n -s) + assertEquals $output "build_image_env(true,true)" + local output=$(wrap_env --build-image --disable-validation --skip-root-tests) + assertEquals $output "build_image_env(true,true)" } function test_check_env(){ - local output=$(wrap_env -c) - assertEquals $output "check_env" - local output=$(wrap_env --check) - assertEquals $output "check_env" + local output=$(wrap_env -c myscript) + assertEquals $output "check_env(${JUNEST_HOME},myscript,false)" + local output=$(wrap_env --check myscript) + assertEquals $output "check_env(${JUNEST_HOME},myscript,false)" + local output=$(wrap_env -c myscript -s) + assertEquals $output "check_env(${JUNEST_HOME},myscript,true)" + local output=$(wrap_env --check myscript --skip-root-tests) + assertEquals $output "check_env(${JUNEST_HOME},myscript,true)" } function test_delete_env(){ local output=$(wrap_env -d) @@ -79,6 +99,45 @@ function test_delete_env(){ local output=$(wrap_env --delete) assertEquals $output "delete_env" } +#function test_setup_env_from_file(){ + #local output=$(wrap_env -i myimage) + #assertEquals $output "setup_env_from_file(myimage)" + #local output=$(wrap_env --setup-from-file myimage) + #assertEquals $output "setup_env_from_file(myimage)" +#} +function test_setup_env_from_file(){ + is_env_installed(){ + return 1 + } + local output=$(wrap_env -i myimage) + assertEquals "$output" "$(echo -e "setup_env_from_file(myimage)\nrun_env_as_user(,)")" + local output=$(wrap_env --setup-from-file myimage) + assertEquals "$output" "$(echo -e "setup_env_from_file(myimage)\nrun_env_as_user(,)")" + + is_env_installed(){ + return 0 + } + $(wrap_env -i myimage 2> /dev/null) + assertEquals 1 $? +} + +function test_setup_env(){ + is_env_installed(){ + return 1 + } + local output=$(wrap_env -a arm) + assertEquals "$output" "$(echo -e "setup_env(arm)\nrun_env_as_user(,)")" + local output=$(wrap_env --arch arm) + assertEquals "$output" "$(echo -e "setup_env(arm)\nrun_env_as_user(,)")" + local output=$(wrap_env) + assertEquals "$output" "$(echo -e "setup_env()\nrun_env_as_user(,)")" + + is_env_installed(){ + return 0 + } + $(wrap_env -a arm 2> /dev/null) + assertEquals 1 $? +} function test_run_env_as_fakeroot(){ local output=$(wrap_env -f) assertEquals $output "run_env_as_fakeroot(,)" @@ -120,6 +179,8 @@ function test_check_cli(){ assertEquals $? 1 $(wrap_env -b -c 2> /dev/null) assertEquals $? 1 + $(wrap_env -d -s 2> /dev/null) + assertEquals $? 1 $(wrap_env -n -v 2> /dev/null) assertEquals $? 1 $(wrap_env -d -r 2> /dev/null)