mirror of
https://github.com/fsquillace/junest.git
synced 2026-01-23 02:34:30 +00:00
Issue #174: 🎨 Change structure of core modules
This change separate different parts of the code into several categories: `chroot`, `proot`, `build`, `common`, `setup`. This should simplify the maintenance of the code and will help introducing the user namespace module in a easier way.
This commit is contained in:
parent
54a09245d2
commit
ddc7ede70a
20 changed files with 1189 additions and 1031 deletions
21
tests/checkstyle/checkstyle.sh
Executable file
21
tests/checkstyle/checkstyle.sh
Executable file
|
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
source "$(dirname $0)/../utils/utils.sh"
|
||||
|
||||
# Disable the exiterr
|
||||
set +e
|
||||
|
||||
function oneTimeSetUp(){
|
||||
setUpUnitTests
|
||||
}
|
||||
|
||||
function test_check_no_tabs(){
|
||||
assertCommandFailOnStatus 1 grep -R "$(printf '\t')" $(dirname $0)/../../bin/*
|
||||
assertEquals "" "$(cat $STDOUTF)"
|
||||
assertEquals "" "$(cat $STDERRF)"
|
||||
assertCommandFailOnStatus 1 grep -R "$(printf '\t')" $(dirname $0)/../../lib/*
|
||||
assertEquals "" "$(cat $STDOUTF)"
|
||||
assertEquals "" "$(cat $STDERRF)"
|
||||
}
|
||||
|
||||
source $(dirname $0)/../utils/shunit2
|
||||
52
tests/unit-tests/test-chroot.sh
Executable file
52
tests/unit-tests/test-chroot.sh
Executable file
|
|
@ -0,0 +1,52 @@
|
|||
#!/bin/bash
|
||||
|
||||
JUNEST_ROOT=$(readlink -f $(dirname $0)/../..)
|
||||
|
||||
source "$JUNEST_ROOT/tests/utils/utils.sh"
|
||||
|
||||
source "$JUNEST_ROOT/lib/utils/utils.sh"
|
||||
source "$JUNEST_ROOT/lib/core/common.sh"
|
||||
source "$JUNEST_ROOT/lib/core/chroot.sh"
|
||||
|
||||
# Disable the exiterr
|
||||
set +e
|
||||
|
||||
function oneTimeSetUp(){
|
||||
setUpUnitTests
|
||||
}
|
||||
|
||||
function setUp(){
|
||||
cwdSetUp
|
||||
junestSetUp
|
||||
}
|
||||
|
||||
function tearDown(){
|
||||
junestTearDown
|
||||
cwdTearDown
|
||||
}
|
||||
|
||||
function test_run_env_as_root_different_arch(){
|
||||
echo "JUNEST_ARCH=XXX" > ${JUNEST_HOME}/etc/junest/info
|
||||
assertCommandFailOnStatus 104 run_env_as_root pwd
|
||||
}
|
||||
|
||||
function _test_run_env_as_root() {
|
||||
chroot_cmd() {
|
||||
[ "$JUNEST_ENV" != "1" ] && return 1
|
||||
echo $@
|
||||
}
|
||||
|
||||
assertCommandSuccess run_env_as_root $@
|
||||
}
|
||||
|
||||
function test_run_env_as_root_cmd(){
|
||||
_test_run_env_as_root pwd
|
||||
assertEquals "$JUNEST_HOME /bin/sh --login -c pwd" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
function test_run_env_as_classic_root_no_cmd(){
|
||||
_test_run_env_as_root
|
||||
assertEquals "$JUNEST_HOME /bin/sh --login -c /bin/sh --login" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
source $JUNEST_ROOT/tests/utils/shunit2
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
#!/bin/bash
|
||||
source "$(dirname $0)/utils.sh"
|
||||
source "$(dirname $0)/../utils/utils.sh"
|
||||
|
||||
source $(dirname $0)/../../bin/junest -h &> /dev/null
|
||||
|
||||
|
|
@ -178,4 +178,4 @@ function test_check_cli(){
|
|||
assertCommandFail cli -d args
|
||||
}
|
||||
|
||||
source $(dirname $0)/shunit2
|
||||
source $(dirname $0)/../utils/shunit2
|
||||
|
|
|
|||
171
tests/unit-tests/test-common.sh
Executable file
171
tests/unit-tests/test-common.sh
Executable file
|
|
@ -0,0 +1,171 @@
|
|||
#!/bin/bash
|
||||
|
||||
JUNEST_ROOT=$(readlink -f $(dirname $0)/../..)
|
||||
|
||||
source "$JUNEST_ROOT/tests/utils/utils.sh"
|
||||
|
||||
source "$JUNEST_ROOT/lib/utils/utils.sh"
|
||||
source "$JUNEST_ROOT/lib/core/common.sh"
|
||||
|
||||
# Disable the exiterr
|
||||
set +e
|
||||
|
||||
function oneTimeSetUp(){
|
||||
setUpUnitTests
|
||||
junestSetUp
|
||||
}
|
||||
|
||||
function oneTimeTearDown(){
|
||||
junestTearDown
|
||||
}
|
||||
|
||||
function setUp(){
|
||||
ld_exec() {
|
||||
echo "ld_exec $@"
|
||||
}
|
||||
LD_EXEC=ld_exec
|
||||
}
|
||||
|
||||
function test_ln(){
|
||||
LN=echo assertCommandSuccess ln_cmd -s ln_file new_file
|
||||
assertEquals "-s ln_file new_file" "$(cat $STDOUTF)"
|
||||
|
||||
LN=false assertCommandSuccess ln_cmd -s ln_file new_file
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false -s ln_file new_file" "$(cat $STDOUTF)"
|
||||
|
||||
LN=false LD_EXEC=false assertCommandFail ln_cmd
|
||||
}
|
||||
|
||||
function test_getent(){
|
||||
GETENT=echo assertCommandSuccess getent_cmd passwd
|
||||
assertEquals "passwd" "$(cat $STDOUTF)"
|
||||
|
||||
GETENT=false assertCommandSuccess getent_cmd passwd
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false passwd" "$(cat $STDOUTF)"
|
||||
|
||||
GETENT=false LD_EXEC=false assertCommandFail getent_cmd
|
||||
}
|
||||
|
||||
function test_cp(){
|
||||
CP=echo assertCommandSuccess cp_cmd passwd
|
||||
assertEquals "passwd" "$(cat $STDOUTF)"
|
||||
|
||||
CP=false assertCommandSuccess cp_cmd passwd
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false passwd" "$(cat $STDOUTF)"
|
||||
|
||||
CP=false LD_EXEC=false assertCommandFail cp_cmd
|
||||
}
|
||||
|
||||
function test_download(){
|
||||
WGET=/bin/true
|
||||
CURL=/bin/false
|
||||
assertCommandSuccess download_cmd
|
||||
|
||||
WGET=/bin/false
|
||||
CURL=/bin/true
|
||||
assertCommandSuccess download_cmd
|
||||
|
||||
WGET=/bin/false CURL=/bin/false assertCommandFail download_cmd
|
||||
}
|
||||
|
||||
function test_rm(){
|
||||
RM=echo assertCommandSuccess rm_cmd rm_file
|
||||
assertEquals "rm_file" "$(cat $STDOUTF)"
|
||||
|
||||
RM=false assertCommandSuccess rm_cmd rm_file
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false rm_file" "$(cat $STDOUTF)"
|
||||
|
||||
RM=false LD_EXEC=false assertCommandFail rm_cmd rm_file
|
||||
}
|
||||
|
||||
function test_chown(){
|
||||
local id=$(id -u)
|
||||
|
||||
CHOWN=echo assertCommandSuccess chown_cmd $id chown_file
|
||||
assertEquals "$id chown_file" "$(cat $STDOUTF)"
|
||||
|
||||
CHOWN=false assertCommandSuccess chown_cmd $id chown_file
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false $id chown_file" "$(cat $STDOUTF)"
|
||||
|
||||
CHOWN=false LD_EXEC=false assertCommandFail chown_cmd $id chown_file
|
||||
}
|
||||
|
||||
function test_mkdir(){
|
||||
MKDIR=echo assertCommandSuccess mkdir_cmd -p new_dir/new_dir
|
||||
assertEquals "-p new_dir/new_dir" "$(cat $STDOUTF)"
|
||||
|
||||
MKDIR=false assertCommandSuccess mkdir_cmd -p new_dir/new_dir
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false -p new_dir/new_dir" "$(cat $STDOUTF)"
|
||||
|
||||
MKDIR=false LD_EXEC=false assertCommandFail mkdir_cmd -p new_dir/new_dir
|
||||
}
|
||||
|
||||
function test_chroot(){
|
||||
CHROOT=echo assertCommandSuccess chroot_cmd root
|
||||
assertEquals "root" "$(cat $STDOUTF)"
|
||||
|
||||
CHROOT=false CLASSIC_CHROOT=echo assertCommandSuccess chroot_cmd root
|
||||
assertEquals "root" "$(cat $STDOUTF)"
|
||||
|
||||
CHROOT=false CLASSIC_CHROOT=false assertCommandSuccess chroot_cmd root
|
||||
assertEquals "ld_exec $JUNEST_HOME/usr/bin/chroot root" "$(cat $STDOUTF)"
|
||||
|
||||
CHROOT=false CLASSIC_CHROOT=false LD_EXEC=false assertCommandFail chroot_cmd root
|
||||
}
|
||||
|
||||
function test_proot_cmd_compat(){
|
||||
PROOT="/bin/true" assertCommandSuccess proot_cmd "" ""
|
||||
|
||||
PROOT="/bin/false" assertCommandFail proot_cmd --helps
|
||||
}
|
||||
|
||||
function test_proot_cmd_seccomp(){
|
||||
envv(){
|
||||
env
|
||||
}
|
||||
PROOT=envv
|
||||
assertCommandSuccess proot_cmd cmd
|
||||
assertEquals "" "$(cat $STDOUTF | grep "^PROOT_NO_SECCOMP")"
|
||||
|
||||
envv(){
|
||||
env | grep "^PROOT_NO_SECCOMP"
|
||||
}
|
||||
PROOT=envv
|
||||
local output=$(proot_cmd | grep "^PROOT_NO_SECCOMP")
|
||||
assertCommandSuccess proot_cmd cmd
|
||||
# The variable PROOT_NO_SECCOMP will be produced
|
||||
# twice due to the fallback mechanism
|
||||
assertEquals "PROOT_NO_SECCOMP=1
|
||||
PROOT_NO_SECCOMP=1" "$(cat $STDOUTF | grep "^PROOT_NO_SECCOMP")"
|
||||
}
|
||||
|
||||
function test_copy_passwd_and_group(){
|
||||
getent_cmd_mock() {
|
||||
echo $@
|
||||
}
|
||||
GETENT=getent_cmd_mock assertCommandSuccess _copy_passwd_and_group
|
||||
assertEquals "$(echo -e "passwd\npasswd $USER")" "$(cat $JUNEST_HOME/etc/passwd)"
|
||||
assertEquals "group" "$(cat $JUNEST_HOME/etc/group)"
|
||||
}
|
||||
|
||||
function test_copy_passwd_and_group_fallback(){
|
||||
cp_cmd_mock() {
|
||||
echo $@
|
||||
}
|
||||
CP=cp_cmd_mock GETENT=false LD_EXEC=false assertCommandSuccess _copy_passwd_and_group
|
||||
assertEquals "$(echo -e "/etc/passwd $JUNEST_HOME//etc/passwd\n/etc/group $JUNEST_HOME//etc/group")" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
function test_copy_passwd_and_group_failure(){
|
||||
CP=false GETENT=false LD_EXEC=false assertCommandFailOnStatus 1 _copy_passwd_and_group
|
||||
}
|
||||
|
||||
function test_nested_env(){
|
||||
JUNEST_ENV=1 assertCommandFailOnStatus 106 bash -c "source $JUNEST_ROOT/lib/utils/utils.sh; source $JUNEST_ROOT/lib/core/common.sh"
|
||||
}
|
||||
|
||||
function test_nested_env_not_set_variable(){
|
||||
JUNEST_ENV=aaa assertCommandFailOnStatus 107 bash -c "source $JUNEST_ROOT/lib/utils/utils.sh; source $JUNEST_ROOT/lib/core/common.sh"
|
||||
}
|
||||
|
||||
source $JUNEST_ROOT/tests/utils/shunit2
|
||||
|
|
@ -1,349 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
JUNEST_ROOT=$(readlink -f $(dirname $0)/../..)
|
||||
|
||||
source "$JUNEST_ROOT/tests/unit-tests/utils.sh"
|
||||
|
||||
# Disable the exiterr
|
||||
set +e
|
||||
|
||||
function oneTimeSetUp(){
|
||||
SKIP_ROOT_TESTS=${SKIP_ROOT_TESTS:-0}
|
||||
setUpUnitTests
|
||||
}
|
||||
|
||||
function setUp(){
|
||||
ORIGIN_CWD=$(TMPDIR=/tmp mktemp -d -t junest-cwd.XXXXXXXXXX)
|
||||
cd $ORIGIN_CWD
|
||||
JUNEST_HOME=$(TMPDIR=/tmp mktemp -d -t junest-home.XXXXXXXXXX)
|
||||
mkdir -p ${JUNEST_HOME}/etc/junest
|
||||
echo "JUNEST_ARCH=x86_64" > ${JUNEST_HOME}/etc/junest/info
|
||||
mkdir -p ${JUNEST_HOME}/etc/ca-certificates
|
||||
JUNEST_TEMPDIR=$(TMPDIR=/tmp mktemp -d -t junest-temp.XXXXXXXXXX)
|
||||
source "$JUNEST_ROOT/lib/utils.sh"
|
||||
source "$JUNEST_ROOT/lib/core.sh"
|
||||
|
||||
set +e
|
||||
|
||||
trap - QUIT EXIT ABRT KILL TERM INT
|
||||
trap "rm -rf ${JUNEST_HOME}; rm -rf ${JUNEST_TEMPDIR}" EXIT QUIT ABRT KILL TERM INT
|
||||
|
||||
ld_exec() {
|
||||
echo "ld_exec $@"
|
||||
}
|
||||
LD_EXEC=ld_exec
|
||||
}
|
||||
|
||||
|
||||
function tearDown(){
|
||||
# the CA directories are read only and can be deleted only by changing the mod
|
||||
[ -d ${JUNEST_HOME}/etc/ca-certificates ] && chmod -R +w ${JUNEST_HOME}/etc/ca-certificates
|
||||
rm -rf $JUNEST_HOME
|
||||
rm -rf $JUNEST_TEMPDIR
|
||||
rm -rf $ORIGIN_CWD
|
||||
trap - QUIT EXIT ABRT KILL TERM INT
|
||||
}
|
||||
|
||||
|
||||
function test_ln(){
|
||||
LN=echo assertCommandSuccess ln_cmd -s ln_file new_file
|
||||
assertEquals "-s ln_file new_file" "$(cat $STDOUTF)"
|
||||
|
||||
LN=false assertCommandSuccess ln_cmd -s ln_file new_file
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false -s ln_file new_file" "$(cat $STDOUTF)"
|
||||
|
||||
LN=false LD_EXEC=false assertCommandFail ln_cmd
|
||||
}
|
||||
|
||||
function test_getent(){
|
||||
GETENT=echo assertCommandSuccess getent_cmd passwd
|
||||
assertEquals "passwd" "$(cat $STDOUTF)"
|
||||
|
||||
GETENT=false assertCommandSuccess getent_cmd passwd
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false passwd" "$(cat $STDOUTF)"
|
||||
|
||||
GETENT=false LD_EXEC=false assertCommandFail getent_cmd
|
||||
}
|
||||
|
||||
function test_cp(){
|
||||
CP=echo assertCommandSuccess cp_cmd passwd
|
||||
assertEquals "passwd" "$(cat $STDOUTF)"
|
||||
|
||||
CP=false assertCommandSuccess cp_cmd passwd
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false passwd" "$(cat $STDOUTF)"
|
||||
|
||||
CP=false LD_EXEC=false assertCommandFail cp_cmd
|
||||
}
|
||||
|
||||
function test_download(){
|
||||
WGET=/bin/true
|
||||
CURL=/bin/false
|
||||
assertCommandSuccess download_cmd
|
||||
|
||||
WGET=/bin/false
|
||||
CURL=/bin/true
|
||||
assertCommandSuccess download_cmd
|
||||
|
||||
WGET=/bin/false CURL=/bin/false assertCommandFail download_cmd
|
||||
}
|
||||
|
||||
function test_rm(){
|
||||
RM=echo assertCommandSuccess rm_cmd rm_file
|
||||
assertEquals "rm_file" "$(cat $STDOUTF)"
|
||||
|
||||
RM=false assertCommandSuccess rm_cmd rm_file
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false rm_file" "$(cat $STDOUTF)"
|
||||
|
||||
RM=false LD_EXEC=false assertCommandFail rm_cmd rm_file
|
||||
}
|
||||
|
||||
function test_chown(){
|
||||
local id=$(id -u)
|
||||
|
||||
CHOWN=echo assertCommandSuccess chown_cmd $id chown_file
|
||||
assertEquals "$id chown_file" "$(cat $STDOUTF)"
|
||||
|
||||
CHOWN=false assertCommandSuccess chown_cmd $id chown_file
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false $id chown_file" "$(cat $STDOUTF)"
|
||||
|
||||
CHOWN=false LD_EXEC=false assertCommandFail chown_cmd $id chown_file
|
||||
}
|
||||
|
||||
function test_mkdir(){
|
||||
MKDIR=echo assertCommandSuccess mkdir_cmd -p new_dir/new_dir
|
||||
assertEquals "-p new_dir/new_dir" "$(cat $STDOUTF)"
|
||||
|
||||
MKDIR=false assertCommandSuccess mkdir_cmd -p new_dir/new_dir
|
||||
assertEquals "ld_exec ${JUNEST_HOME}/usr/bin/false -p new_dir/new_dir" "$(cat $STDOUTF)"
|
||||
|
||||
MKDIR=false LD_EXEC=false assertCommandFail mkdir_cmd -p new_dir/new_dir
|
||||
}
|
||||
|
||||
function test_chroot(){
|
||||
CHROOT=echo assertCommandSuccess chroot_cmd root
|
||||
assertEquals "root" "$(cat $STDOUTF)"
|
||||
|
||||
CHROOT=false CLASSIC_CHROOT=echo assertCommandSuccess chroot_cmd root
|
||||
assertEquals "root" "$(cat $STDOUTF)"
|
||||
|
||||
CHROOT=false CLASSIC_CHROOT=false assertCommandSuccess chroot_cmd root
|
||||
assertEquals "ld_exec $JUNEST_HOME/usr/bin/chroot root" "$(cat $STDOUTF)"
|
||||
|
||||
CHROOT=false CLASSIC_CHROOT=false LD_EXEC=false assertCommandFail chroot_cmd root
|
||||
}
|
||||
|
||||
function test_proot_cmd_compat(){
|
||||
PROOT="/bin/true" assertCommandSuccess proot_cmd "" ""
|
||||
|
||||
PROOT="/bin/false" assertCommandFail proot_cmd --helps
|
||||
}
|
||||
|
||||
function test_proot_cmd_seccomp(){
|
||||
envv(){
|
||||
env
|
||||
}
|
||||
PROOT=envv
|
||||
assertCommandSuccess proot_cmd cmd
|
||||
assertEquals "" "$(cat $STDOUTF | grep "^PROOT_NO_SECCOMP")"
|
||||
|
||||
envv(){
|
||||
env | grep "^PROOT_NO_SECCOMP"
|
||||
}
|
||||
PROOT=envv
|
||||
local output=$(proot_cmd | grep "^PROOT_NO_SECCOMP")
|
||||
assertCommandSuccess proot_cmd cmd
|
||||
# The variable PROOT_NO_SECCOMP will be produced
|
||||
# twice due to the fallback mechanism
|
||||
assertEquals "PROOT_NO_SECCOMP=1
|
||||
PROOT_NO_SECCOMP=1" "$(cat $STDOUTF | grep "^PROOT_NO_SECCOMP")"
|
||||
}
|
||||
|
||||
function test_is_env_installed(){
|
||||
rm -rf $JUNEST_HOME/*
|
||||
assertCommandFail is_env_installed
|
||||
touch $JUNEST_HOME/just_file
|
||||
assertCommandSuccess is_env_installed
|
||||
}
|
||||
|
||||
function test_setup_env(){
|
||||
rm -rf $JUNEST_HOME/*
|
||||
wget_mock(){
|
||||
# Proof that the setup is happening
|
||||
# inside $JUNEST_TEMPDIR
|
||||
local cwd=${PWD#${JUNEST_TEMPDIR}}
|
||||
local parent_dir=${PWD%${cwd}}
|
||||
assertEquals "$JUNEST_TEMPDIR" "${parent_dir}"
|
||||
touch file
|
||||
tar -czvf ${CMD}-${ARCH}.tar.gz file
|
||||
}
|
||||
WGET=wget_mock
|
||||
setup_env 1> /dev/null
|
||||
assertTrue "[ -e $JUNEST_HOME/file ]"
|
||||
|
||||
assertCommandFailOnStatus 102 setup_env "noarch"
|
||||
}
|
||||
|
||||
|
||||
function test_setup_env_from_file(){
|
||||
rm -rf $JUNEST_HOME/*
|
||||
touch file
|
||||
tar -czvf ${CMD}-${ARCH}.tar.gz file 1> /dev/null
|
||||
assertCommandSuccess setup_env_from_file ${CMD}-${ARCH}.tar.gz
|
||||
assertTrue "[ -e $JUNEST_HOME/file ]"
|
||||
}
|
||||
|
||||
function test_setup_env_from_file_not_existing_file(){
|
||||
assertCommandFailOnStatus 103 setup_env_from_file noexist.tar.gz
|
||||
}
|
||||
|
||||
function test_setup_env_from_file_with_absolute_path(){
|
||||
rm -rf $JUNEST_HOME/*
|
||||
touch file
|
||||
tar -czvf ${CMD}-${ARCH}.tar.gz file 1> /dev/null
|
||||
assertCommandSuccess setup_env_from_file ${ORIGIN_WD}/${CMD}-${ARCH}.tar.gz
|
||||
assertTrue "[ -e $JUNEST_HOME/file ]"
|
||||
}
|
||||
|
||||
function test_run_env_as_root_different_arch(){
|
||||
echo "JUNEST_ARCH=XXX" > ${JUNEST_HOME}/etc/junest/info
|
||||
assertCommandFailOnStatus 104 run_env_as_root pwd
|
||||
}
|
||||
|
||||
function _test_run_env_as_root() {
|
||||
chroot_cmd() {
|
||||
[ "$JUNEST_ENV" != "1" ] && return 1
|
||||
echo $@
|
||||
}
|
||||
|
||||
assertCommandSuccess run_env_as_root $@
|
||||
}
|
||||
|
||||
function test_run_env_as_root_cmd(){
|
||||
_test_run_env_as_root pwd
|
||||
assertEquals "$JUNEST_HOME /bin/sh --login -c pwd" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
function test_run_env_as_classic_root_no_cmd(){
|
||||
_test_run_env_as_root
|
||||
assertEquals "$JUNEST_HOME /bin/sh --login -c /bin/sh --login" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
function test_run_env_as_user(){
|
||||
_run_env_with_qemu() {
|
||||
echo $@
|
||||
}
|
||||
assertCommandSuccess run_env_as_user "-k 3.10" "/usr/bin/mkdir" "-v" "/newdir2"
|
||||
assertEquals "-b $HOME -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10 /usr/bin/mkdir -v /newdir2" "$(cat $STDOUTF)"
|
||||
|
||||
SH=("/usr/bin/echo")
|
||||
assertCommandSuccess run_env_as_user "-k 3.10"
|
||||
assertEquals "-b $HOME -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10" "$(cat $STDOUTF)"
|
||||
|
||||
[[ -e /etc/hosts ]] && assertEquals "$(cat /etc/hosts)" "$(cat ${JUNEST_HOME}/etc/hosts)"
|
||||
[[ -e /etc/host.conf ]] && assertEquals "$(cat /etc/host.conf)" "$(cat ${JUNEST_HOME}/etc/host.conf)"
|
||||
[[ -e /etc/nsswitch.conf ]] && assertEquals "$(cat /etc/nsswitch.conf)" "$(cat ${JUNEST_HOME}/etc/nsswitch.conf)"
|
||||
[[ -e /etc/resolv.conf ]] && assertEquals "$(cat /etc/resolv.conf)" "$(cat ${JUNEST_HOME}/etc/resolv.conf)"
|
||||
|
||||
[[ -e /etc/hosts.equiv ]] && assertEquals "$(cat /etc/hosts.equiv)" "$(cat ${JUNEST_HOME}/etc/hosts.equiv)"
|
||||
[[ -e /etc/netgroup ]] && assertEquals "$(cat /etc/netgroup)" "$(cat ${JUNEST_HOME}/etc/netgroup)"
|
||||
|
||||
[[ -e /etc/passwd ]]
|
||||
assertEquals 0 $?
|
||||
[[ -e /etc/group ]]
|
||||
assertEquals 0 $?
|
||||
|
||||
}
|
||||
|
||||
function test_copy_passwd_and_group(){
|
||||
getent_cmd_mock() {
|
||||
echo $@
|
||||
}
|
||||
GETENT=getent_cmd_mock assertCommandSuccess _copy_passwd_and_group
|
||||
assertEquals "$(echo -e "passwd\npasswd $USER")" "$(cat $JUNEST_HOME/etc/passwd)"
|
||||
assertEquals "group" "$(cat $JUNEST_HOME/etc/group)"
|
||||
}
|
||||
|
||||
function test_copy_passwd_and_group_fallback(){
|
||||
cp_cmd_mock() {
|
||||
echo $@
|
||||
}
|
||||
CP=cp_cmd_mock GETENT=false LD_EXEC=false assertCommandSuccess _copy_passwd_and_group
|
||||
assertEquals "$(echo -e "/etc/passwd $JUNEST_HOME//etc/passwd\n/etc/group $JUNEST_HOME//etc/group")" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
function test_copy_passwd_and_group_failure(){
|
||||
CP=false GETENT=false LD_EXEC=false assertCommandFailOnStatus 1 _copy_passwd_and_group
|
||||
}
|
||||
|
||||
function test_run_env_as_fakeroot(){
|
||||
_run_env_with_qemu() {
|
||||
echo $@
|
||||
}
|
||||
assertCommandSuccess run_env_as_fakeroot "-k 3.10" "/usr/bin/mkdir" "-v" "/newdir2"
|
||||
assertEquals "-0 -b ${HOME} -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10 /usr/bin/mkdir -v /newdir2" "$(cat $STDOUTF)"
|
||||
|
||||
SH=("/usr/bin/echo")
|
||||
assertCommandSuccess run_env_as_fakeroot "-k 3.10"
|
||||
assertEquals "-0 -b ${HOME} -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10" "$(cat $STDOUTF)"
|
||||
|
||||
[[ -e /etc/hosts ]] && assertEquals "$(cat /etc/hosts)" "$(cat ${JUNEST_HOME}/etc/hosts)"
|
||||
[[ -e /etc/host.conf ]] && assertEquals "$(cat /etc/host.conf)" "$(cat ${JUNEST_HOME}/etc/host.conf)"
|
||||
[[ -e /etc/nsswitch.conf ]] && assertEquals "$(cat /etc/nsswitch.conf)" "$(cat ${JUNEST_HOME}/etc/nsswitch.conf)"
|
||||
[[ -e /etc/resolv.conf ]] && assertEquals "$(cat /etc/resolv.conf)" "$(cat ${JUNEST_HOME}/etc/resolv.conf)"
|
||||
}
|
||||
|
||||
function test_run_env_with_quotes(){
|
||||
_run_env_with_qemu() {
|
||||
echo $@
|
||||
}
|
||||
assertCommandSuccess run_env_as_user "-k 3.10" "bash" "-c" "/usr/bin/mkdir -v /newdir2"
|
||||
assertEquals "-b ${HOME} -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10 bash -c /usr/bin/mkdir -v /newdir2" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
function test_run_env_with_proot_args(){
|
||||
proot_cmd() {
|
||||
[ "$JUNEST_ENV" != "1" ] && return 1
|
||||
echo $@
|
||||
}
|
||||
|
||||
assertCommandSuccess _run_env_with_proot --help
|
||||
assertEquals "--help /bin/sh --login" "$(cat $STDOUTF)"
|
||||
|
||||
assertCommandSuccess _run_env_with_proot --help mycommand
|
||||
assertEquals "--help /bin/sh --login -c mycommand" "$(cat $STDOUTF)"
|
||||
|
||||
assertCommandFail _run_env_with_proot
|
||||
}
|
||||
|
||||
function test_delete_env(){
|
||||
echo "N" | delete_env 1> /dev/null
|
||||
assertCommandSuccess is_env_installed
|
||||
echo "Y" | delete_env 1> /dev/null
|
||||
assertCommandFail is_env_installed
|
||||
}
|
||||
|
||||
function test_nested_env(){
|
||||
JUNEST_ENV=1 assertCommandFailOnStatus 106 bash -c "source $JUNEST_ROOT/lib/utils.sh; source $JUNEST_ROOT/lib/core.sh"
|
||||
}
|
||||
|
||||
function test_nested_env_not_set_variable(){
|
||||
JUNEST_ENV=aaa assertCommandFailOnStatus 107 bash -c "source $JUNEST_ROOT/lib/utils.sh; source $JUNEST_ROOT/lib/core.sh"
|
||||
}
|
||||
|
||||
function test_qemu() {
|
||||
echo "JUNEST_ARCH=arm" > ${JUNEST_HOME}/etc/junest/info
|
||||
rm_cmd() {
|
||||
echo $@
|
||||
}
|
||||
ln_cmd() {
|
||||
echo $@
|
||||
}
|
||||
_run_env_with_proot() {
|
||||
echo $@
|
||||
}
|
||||
|
||||
RANDOM=100 ARCH=x86_64 assertCommandSuccess _run_env_with_qemu ""
|
||||
assertEquals "$(echo -e "-s $JUNEST_HOME/opt/qemu/qemu-arm-static-x86_64 /tmp/qemu-arm-static-x86_64-100\n-q /tmp/qemu-arm-static-x86_64-100")" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
source $JUNEST_ROOT/tests/unit-tests/shunit2
|
||||
114
tests/unit-tests/test-proot.sh
Executable file
114
tests/unit-tests/test-proot.sh
Executable file
|
|
@ -0,0 +1,114 @@
|
|||
#!/bin/bash
|
||||
|
||||
JUNEST_ROOT=$(readlink -f $(dirname $0)/../..)
|
||||
|
||||
source "$JUNEST_ROOT/tests/utils/utils.sh"
|
||||
|
||||
source "$JUNEST_ROOT/lib/utils/utils.sh"
|
||||
|
||||
# Disable the exiterr
|
||||
set +e
|
||||
|
||||
function oneTimeSetUp(){
|
||||
setUpUnitTests
|
||||
}
|
||||
|
||||
function setUp(){
|
||||
cwdSetUp
|
||||
junestSetUp
|
||||
|
||||
# Attempt to source the files under test to revert variable
|
||||
# overrides (i.e. SH variable)
|
||||
source "$JUNEST_ROOT/lib/core/common.sh"
|
||||
source "$JUNEST_ROOT/lib/core/proot.sh"
|
||||
set +e
|
||||
}
|
||||
|
||||
function tearDown(){
|
||||
junestTearDown
|
||||
cwdTearDown
|
||||
}
|
||||
|
||||
function test_run_env_as_user(){
|
||||
_run_env_with_qemu() {
|
||||
echo $@
|
||||
}
|
||||
assertCommandSuccess run_env_as_user "-k 3.10" "/usr/bin/mkdir" "-v" "/newdir2"
|
||||
assertEquals "-b $HOME -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10 /usr/bin/mkdir -v /newdir2" "$(cat $STDOUTF)"
|
||||
|
||||
SH=("/usr/bin/echo")
|
||||
assertCommandSuccess run_env_as_user "-k 3.10"
|
||||
assertEquals "-b $HOME -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10" "$(cat $STDOUTF)"
|
||||
|
||||
[[ -e /etc/hosts ]] && assertEquals "$(cat /etc/hosts)" "$(cat ${JUNEST_HOME}/etc/hosts)"
|
||||
[[ -e /etc/host.conf ]] && assertEquals "$(cat /etc/host.conf)" "$(cat ${JUNEST_HOME}/etc/host.conf)"
|
||||
[[ -e /etc/nsswitch.conf ]] && assertEquals "$(cat /etc/nsswitch.conf)" "$(cat ${JUNEST_HOME}/etc/nsswitch.conf)"
|
||||
[[ -e /etc/resolv.conf ]] && assertEquals "$(cat /etc/resolv.conf)" "$(cat ${JUNEST_HOME}/etc/resolv.conf)"
|
||||
|
||||
[[ -e /etc/hosts.equiv ]] && assertEquals "$(cat /etc/hosts.equiv)" "$(cat ${JUNEST_HOME}/etc/hosts.equiv)"
|
||||
[[ -e /etc/netgroup ]] && assertEquals "$(cat /etc/netgroup)" "$(cat ${JUNEST_HOME}/etc/netgroup)"
|
||||
|
||||
[[ -e /etc/passwd ]]
|
||||
assertEquals 0 $?
|
||||
[[ -e /etc/group ]]
|
||||
assertEquals 0 $?
|
||||
|
||||
}
|
||||
|
||||
function test_run_env_as_fakeroot(){
|
||||
_run_env_with_qemu() {
|
||||
echo $@
|
||||
}
|
||||
assertCommandSuccess run_env_as_fakeroot "-k 3.10" "/usr/bin/mkdir" "-v" "/newdir2"
|
||||
assertEquals "-0 -b ${HOME} -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10 /usr/bin/mkdir -v /newdir2" "$(cat $STDOUTF)"
|
||||
|
||||
SH=("/usr/bin/echo")
|
||||
assertCommandSuccess run_env_as_fakeroot "-k 3.10"
|
||||
assertEquals "-0 -b ${HOME} -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10" "$(cat $STDOUTF)"
|
||||
|
||||
[[ -e /etc/hosts ]] && assertEquals "$(cat /etc/hosts)" "$(cat ${JUNEST_HOME}/etc/hosts)"
|
||||
[[ -e /etc/host.conf ]] && assertEquals "$(cat /etc/host.conf)" "$(cat ${JUNEST_HOME}/etc/host.conf)"
|
||||
[[ -e /etc/nsswitch.conf ]] && assertEquals "$(cat /etc/nsswitch.conf)" "$(cat ${JUNEST_HOME}/etc/nsswitch.conf)"
|
||||
[[ -e /etc/resolv.conf ]] && assertEquals "$(cat /etc/resolv.conf)" "$(cat ${JUNEST_HOME}/etc/resolv.conf)"
|
||||
}
|
||||
|
||||
function test_run_env_with_quotes(){
|
||||
_run_env_with_qemu() {
|
||||
echo $@
|
||||
}
|
||||
assertCommandSuccess run_env_as_user "-k 3.10" "bash" "-c" "/usr/bin/mkdir -v /newdir2"
|
||||
assertEquals "-b ${HOME} -b /tmp -b /proc -b /sys -b /dev -r ${JUNEST_HOME} -k 3.10 bash -c /usr/bin/mkdir -v /newdir2" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
function test_run_env_with_proot_args(){
|
||||
proot_cmd() {
|
||||
[ "$JUNEST_ENV" != "1" ] && return 1
|
||||
echo $@
|
||||
}
|
||||
|
||||
assertCommandSuccess _run_env_with_proot --help
|
||||
assertEquals "--help /bin/sh --login" "$(cat $STDOUTF)"
|
||||
|
||||
assertCommandSuccess _run_env_with_proot --help mycommand
|
||||
assertEquals "--help /bin/sh --login -c mycommand" "$(cat $STDOUTF)"
|
||||
|
||||
assertCommandFail _run_env_with_proot
|
||||
}
|
||||
|
||||
function test_qemu() {
|
||||
echo "JUNEST_ARCH=arm" > ${JUNEST_HOME}/etc/junest/info
|
||||
rm_cmd() {
|
||||
echo $@
|
||||
}
|
||||
ln_cmd() {
|
||||
echo $@
|
||||
}
|
||||
_run_env_with_proot() {
|
||||
echo $@
|
||||
}
|
||||
|
||||
RANDOM=100 ARCH=x86_64 assertCommandSuccess _run_env_with_qemu ""
|
||||
assertEquals "$(echo -e "-s $JUNEST_HOME/opt/qemu/qemu-arm-static-x86_64 /tmp/qemu-arm-static-x86_64-100\n-q /tmp/qemu-arm-static-x86_64-100")" "$(cat $STDOUTF)"
|
||||
}
|
||||
|
||||
source $JUNEST_ROOT/tests/utils/shunit2
|
||||
81
tests/unit-tests/test-setup.sh
Executable file
81
tests/unit-tests/test-setup.sh
Executable file
|
|
@ -0,0 +1,81 @@
|
|||
#!/bin/bash
|
||||
|
||||
JUNEST_ROOT=$(readlink -f $(dirname $0)/../..)
|
||||
|
||||
source "$JUNEST_ROOT/tests/utils/utils.sh"
|
||||
|
||||
source "$JUNEST_ROOT/lib/utils/utils.sh"
|
||||
source "$JUNEST_ROOT/lib/core/common.sh"
|
||||
source "$JUNEST_ROOT/lib/core/setup.sh"
|
||||
|
||||
# Disable the exiterr
|
||||
set +e
|
||||
|
||||
function oneTimeSetUp(){
|
||||
setUpUnitTests
|
||||
}
|
||||
|
||||
function setUp(){
|
||||
cwdSetUp
|
||||
junestSetUp
|
||||
}
|
||||
|
||||
function tearDown(){
|
||||
junestTearDown
|
||||
cwdTearDown
|
||||
}
|
||||
|
||||
function test_is_env_installed(){
|
||||
rm -rf $JUNEST_HOME/*
|
||||
assertCommandFail is_env_installed
|
||||
touch $JUNEST_HOME/just_file
|
||||
assertCommandSuccess is_env_installed
|
||||
}
|
||||
|
||||
function test_setup_env(){
|
||||
rm -rf $JUNEST_HOME/*
|
||||
wget_mock(){
|
||||
# Proof that the setup is happening
|
||||
# inside $JUNEST_TEMPDIR
|
||||
local cwd=${PWD#${JUNEST_TEMPDIR}}
|
||||
local parent_dir=${PWD%${cwd}}
|
||||
assertEquals "$JUNEST_TEMPDIR" "${parent_dir}"
|
||||
touch file
|
||||
tar -czvf ${CMD}-${ARCH}.tar.gz file
|
||||
}
|
||||
WGET=wget_mock
|
||||
setup_env 1> /dev/null
|
||||
assertTrue "[ -e $JUNEST_HOME/file ]"
|
||||
|
||||
assertCommandFailOnStatus 102 setup_env "noarch"
|
||||
}
|
||||
|
||||
|
||||
function test_setup_env_from_file(){
|
||||
rm -rf $JUNEST_HOME/*
|
||||
touch file
|
||||
tar -czvf ${CMD}-${ARCH}.tar.gz file 1> /dev/null
|
||||
assertCommandSuccess setup_env_from_file ${CMD}-${ARCH}.tar.gz
|
||||
assertTrue "[ -e $JUNEST_HOME/file ]"
|
||||
}
|
||||
|
||||
function test_setup_env_from_file_not_existing_file(){
|
||||
assertCommandFailOnStatus 103 setup_env_from_file noexist.tar.gz
|
||||
}
|
||||
|
||||
function test_setup_env_from_file_with_absolute_path(){
|
||||
rm -rf $JUNEST_HOME/*
|
||||
touch file
|
||||
tar -czf ${CMD}-${ARCH}.tar.gz file
|
||||
assertCommandSuccess setup_env_from_file ${CMD}-${ARCH}.tar.gz
|
||||
assertTrue "[ -e $JUNEST_HOME/file ]"
|
||||
}
|
||||
|
||||
function test_delete_env(){
|
||||
echo "N" | delete_env 1> /dev/null
|
||||
assertCommandSuccess is_env_installed
|
||||
echo "Y" | delete_env 1> /dev/null
|
||||
assertCommandFail is_env_installed
|
||||
}
|
||||
|
||||
source $JUNEST_ROOT/tests/utils/shunit2
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
#!/bin/bash
|
||||
source "$(dirname $0)/utils.sh"
|
||||
source "$(dirname $0)/../utils/utils.sh"
|
||||
|
||||
unset HOME
|
||||
export HOME=$(TMPDIR=/tmp mktemp -d -t pearl-user-home.XXXXXXX)
|
||||
|
||||
source "$(dirname $0)/../../lib/utils.sh"
|
||||
source "$(dirname $0)/../../lib/utils/utils.sh"
|
||||
|
||||
# Disable the exiterr
|
||||
set +e
|
||||
|
|
@ -94,4 +94,4 @@ function test_contains_element(){
|
|||
assertCommandFailOnStatus 1 contains_element "blabla" "${array[@]}"
|
||||
}
|
||||
|
||||
source $(dirname $0)/shunit2
|
||||
source $(dirname $0)/../utils/shunit2
|
||||
|
|
|
|||
|
|
@ -1,32 +0,0 @@
|
|||
|
||||
function setUpUnitTests(){
|
||||
OUTPUT_DIR="${SHUNIT_TMPDIR}/output"
|
||||
mkdir "${OUTPUT_DIR}"
|
||||
STDOUTF="${OUTPUT_DIR}/stdout"
|
||||
STDERRF="${OUTPUT_DIR}/stderr"
|
||||
}
|
||||
|
||||
function assertCommandSuccess(){
|
||||
$(set -e
|
||||
"$@" > $STDOUTF 2> $STDERRF
|
||||
)
|
||||
assertTrue "The command $1 did not return 0 exit status" $?
|
||||
}
|
||||
|
||||
function assertCommandFail(){
|
||||
$(set -e
|
||||
"$@" > $STDOUTF 2> $STDERRF
|
||||
)
|
||||
assertFalse "The command $1 returned 0 exit status" $?
|
||||
}
|
||||
|
||||
# $1: expected exit status
|
||||
# $2-: The command under test
|
||||
function assertCommandFailOnStatus(){
|
||||
local status=$1
|
||||
shift
|
||||
$(set -e
|
||||
"$@" > $STDOUTF 2> $STDERRF
|
||||
)
|
||||
assertEquals $status $?
|
||||
}
|
||||
57
tests/utils/utils.sh
Normal file
57
tests/utils/utils.sh
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
function cwdSetUp(){
|
||||
ORIGIN_CWD=$(TMPDIR=/tmp mktemp -d -t junest-cwd.XXXXXXXXXX)
|
||||
cd $ORIGIN_CWD
|
||||
}
|
||||
|
||||
function cwdTearDown(){
|
||||
rm -rf $ORIGIN_CWD
|
||||
}
|
||||
|
||||
function junestSetUp(){
|
||||
JUNEST_HOME=$(TMPDIR=/tmp mktemp -d -t junest-home.XXXXXXXXXX)
|
||||
mkdir -p ${JUNEST_HOME}/etc/junest
|
||||
echo "JUNEST_ARCH=x86_64" > ${JUNEST_HOME}/etc/junest/info
|
||||
mkdir -p ${JUNEST_HOME}/etc/ca-certificates
|
||||
trap - QUIT EXIT ABRT KILL TERM INT
|
||||
trap "rm -rf ${JUNEST_HOME}" EXIT QUIT ABRT KILL TERM INT
|
||||
}
|
||||
|
||||
function junestTearDown(){
|
||||
# the CA directories are read only and can be deleted only by changing the mod
|
||||
[ -d ${JUNEST_HOME}/etc/ca-certificates ] && chmod -R +w ${JUNEST_HOME}/etc/ca-certificates
|
||||
rm -rf $JUNEST_HOME
|
||||
trap - QUIT EXIT ABRT KILL TERM INT
|
||||
unset JUNEST_HOME
|
||||
}
|
||||
|
||||
function setUpUnitTests(){
|
||||
OUTPUT_DIR="${SHUNIT_TMPDIR}/output"
|
||||
mkdir "${OUTPUT_DIR}"
|
||||
STDOUTF="${OUTPUT_DIR}/stdout"
|
||||
STDERRF="${OUTPUT_DIR}/stderr"
|
||||
}
|
||||
|
||||
function assertCommandSuccess(){
|
||||
$(set -e
|
||||
"$@" > $STDOUTF 2> $STDERRF
|
||||
)
|
||||
assertTrue "The command $1 did not return 0 exit status" $?
|
||||
}
|
||||
|
||||
function assertCommandFail(){
|
||||
$(set -e
|
||||
"$@" > $STDOUTF 2> $STDERRF
|
||||
)
|
||||
assertFalse "The command $1 returned 0 exit status" $?
|
||||
}
|
||||
|
||||
# $1: expected exit status
|
||||
# $2-: The command under test
|
||||
function assertCommandFailOnStatus(){
|
||||
local status=$1
|
||||
shift
|
||||
$(set -e
|
||||
"$@" > $STDOUTF 2> $STDERRF
|
||||
)
|
||||
assertEquals $status $?
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue