From 2b9f1839f117dcd04fb575c5bada5155ac441d0d Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Sun, 13 Feb 2022 20:02:08 +0100 Subject: [PATCH 1/2] Add create-bin-wrappers command --- README.md | 2 +- bin/junest | 24 ++++++++++++++++++++++++ lib/core/wrappers.sh | 23 +++++++++++++++++++++-- tests/unit-tests/test-wrappers.sh | 19 ++++++++++++++++--- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 564f013..458c1ac 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ Run JuNest installed programs directly from host OS --------------------------------------- Installed programs can be accessible directly from host without -calling `junest` command. +entering directly into a JuNest session (no need to call `junest` command). For instance, supposing the host OS is an Ubuntu distro you can directly run `pacman` by simply updating the `PATH` variable: diff --git a/bin/junest b/bin/junest index 7040197..22540fb 100755 --- a/bin/junest +++ b/bin/junest @@ -68,6 +68,9 @@ 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 " create-bin-wrappers Create bin wrappers in $JUNEST_HOME/usr/bin_wrappers" + echo -e " -f, --force Replace the wrapper files even if they already exist" + echo } version() { @@ -78,6 +81,7 @@ function parse_arguments(){ # Actions ACT_SETUP=false ACT_BUILD=false + ACT_CREATE_WRAPPERS=false ACT_NAMESPACE=false ACT_PROOT=false ACT_GROOT=false @@ -88,6 +92,7 @@ function parse_arguments(){ case "$1" in s|setup) ACT_SETUP=true ; shift ;; b|build) ACT_BUILD=true ; shift ;; + create-bin-wrappers) ACT_CREATE_WRAPPERS=true ; shift ;; n|ns) ACT_NAMESPACE=true ; shift ;; p|proot) ACT_PROOT=true ; shift ;; g|groot) ACT_GROOT=true ; shift ;; @@ -103,6 +108,9 @@ function parse_arguments(){ elif $ACT_BUILD then _parse_build_opts "$@" + elif $ACT_CREATE_WRAPPERS + then + _parse_create_wrappers_opts "$@" elif $ACT_NAMESPACE then _parse_ns_opts "$@" @@ -204,6 +212,17 @@ function _parse_build_opts() { done } +function _parse_create_wrappers_opts() { + OPT_FORCE=false + while [[ -n "$1" ]] + do + case "$1" in + -f|--force) OPT_FORCE=true ; shift ;; + *) die "Invalid option $1" ;; + esac + done +} + function _parse_setup_opts() { OPT_FROM_FILE=false IMAGE_FILE="" @@ -256,6 +275,11 @@ function execute_operation() { die "Error: The image is still not installed in $JUNEST_HOME. Run this first: $CMD setup" fi + if $ACT_CREATE_WRAPPERS; then + create_wrappers $OPT_FORCE + exit + fi + local run_env if $ACT_NAMESPACE; then if $OPT_FAKEROOT; then diff --git a/lib/core/wrappers.sh b/lib/core/wrappers.sh index 45206bb..da5517f 100644 --- a/lib/core/wrappers.sh +++ b/lib/core/wrappers.sh @@ -1,13 +1,32 @@ +#!/usr/bin/env bash +# +# Dependencies: +# None +# +# vim: ft=sh - +####################################### +# Create bin wrappers +# +# Globals: +# JUNEST_HOME (RO) : The JuNest home directory. +# Arguments: +# force ($1?) : Create bin wrappers even if the bin file exists. +# Defaults to false. +# Returns: +# None +# Output: +# None +####################################### function create_wrappers() { + local force=${1:-false} mkdir -p "${JUNEST_HOME}/usr/bin_wrappers" cd "${JUNEST_HOME}/usr/bin" || return 1 for file in * do [[ -x $file ]] || continue - if [[ -e ${JUNEST_HOME}/usr/bin_wrappers/$file ]] + if [[ -e ${JUNEST_HOME}/usr/bin_wrappers/$file ]] && ! $force then continue fi diff --git a/tests/unit-tests/test-wrappers.sh b/tests/unit-tests/test-wrappers.sh index a78c81f..9daf0f9 100755 --- a/tests/unit-tests/test-wrappers.sh +++ b/tests/unit-tests/test-wrappers.sh @@ -45,13 +45,26 @@ 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 + echo "original" > $JUNEST_HOME/usr/bin_wrappers/myfile chmod +x $JUNEST_HOME/usr/bin_wrappers/myfile - assertCommandSuccess create_wrappers + assertCommandSuccess create_wrappers false 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)" + assertEquals "original" "$(cat $JUNEST_HOME/usr/bin_wrappers/myfile)" +} + +function test_create_wrappers_forced_already_exist(){ + echo "new" > $JUNEST_HOME/usr/bin/myfile + chmod +x $JUNEST_HOME/usr/bin/myfile + mkdir -p $JUNEST_HOME/usr/bin_wrappers + echo "original" > $JUNEST_HOME/usr/bin_wrappers/myfile + chmod +x $JUNEST_HOME/usr/bin_wrappers/myfile + assertCommandSuccess create_wrappers true + 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 ]" + assertNotEquals "original" "$(cat $JUNEST_HOME/usr/bin_wrappers/myfile)" } function test_create_wrappers_executable_no_longer_exist(){ From e794a6150cdb3d6298685dcd3c8cb12ffe06aa24 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Wed, 16 Feb 2022 22:55:40 +0100 Subject: [PATCH 2/2] Add more unit tests --- bin/junest | 2 +- lib/checks/check_all.sh | 1 + tests/unit-tests/test-junest.sh | 143 +++++++++++++++++--------------- 3 files changed, 79 insertions(+), 67 deletions(-) diff --git a/bin/junest b/bin/junest index 22540fb..c09f339 100755 --- a/bin/junest +++ b/bin/junest @@ -69,7 +69,7 @@ usage() { echo -e " -n, --disable-check Disable the $NAME image check" echo echo -e " create-bin-wrappers Create bin wrappers in $JUNEST_HOME/usr/bin_wrappers" - echo -e " -f, --force Replace the wrapper files even if they already exist" + echo -e " -f, --force Create the wrapper files even if they already exist" echo } diff --git a/lib/checks/check_all.sh b/lib/checks/check_all.sh index c975542..210ebfb 100755 --- a/lib/checks/check_all.sh +++ b/lib/checks/check_all.sh @@ -22,4 +22,5 @@ $JUNEST_SCRIPT ns --backend-command "$JUNEST_HOME/usr/bin/bwrap" -- exit sudo -E $JUNEST_SCRIPT groot -- "$CHECK_SCRIPT" --run-root-tests --skip-aur-tests # Test the wrappers work +$JUNEST_SCRIPT create-bin-wrappers --force $JUNEST_HOME/usr/bin_wrappers/pacman --help diff --git a/tests/unit-tests/test-junest.sh b/tests/unit-tests/test-junest.sh index 0ba7cf5..123b4bd 100755 --- a/tests/unit-tests/test-junest.sh +++ b/tests/unit-tests/test-junest.sh @@ -12,75 +12,74 @@ function oneTimeSetUp(){ } function setUp(){ + ## Mock functions ## + function usage(){ + echo "usage" + } + function version(){ + echo "version" + } + function build_image_env(){ + local disable_check=$1 + echo "build_image_env($disable_check)" + } + function delete_env(){ + echo "delete_env" + } + function setup_env_from_file(){ + echo "setup_env_from_file($1)" + } + function setup_env(){ + echo "setup_env($1)" + } + function run_env_as_proot_fakeroot(){ + local backend_command="$1" + local backend_args="$2" + local no_copy_files="$3" + shift 3 + echo "run_env_as_proot_fakeroot($backend_command,$backend_args,$no_copy_files,$@)" + } + function run_env_as_groot(){ + local backend_command="$1" + local backend_args="$2" + local no_copy_files="$3" + shift 3 + echo "run_env_as_groot($backend_command,$backend_args,$no_copy_files,$@)" + } + function run_env_as_chroot(){ + local backend_command="$1" + local backend_args="$2" + local no_copy_files="$3" + shift 3 + echo "run_env_as_chroot($backend_command,$backend_args,$no_copy_files,$@)" + } + function run_env_as_proot_user(){ + local backend_command="$1" + local backend_args="$2" + local no_copy_files="$3" + shift 3 + echo "run_env_as_proot_user($backend_command,$backend_args,$no_copy_files,$@)" + } + function run_env_as_bwrap_fakeroot(){ + local backend_command="$1" + local backend_args="$2" + local no_copy_files="$3" + shift 3 + echo "run_env_as_bwrap_fakeroot($backend_command,$backend_args,$no_copy_files,$@)" + } + function run_env_as_bwrap_user(){ + local backend_command="$1" + local backend_args="$2" + local no_copy_files="$3" + shift 3 + echo "run_env_as_bwrap_user($backend_command,$backend_args,$no_copy_files,$@)" + } function is_env_installed(){ return 0 } -} - -## Mock functions ## -function usage(){ - echo "usage" -} -function version(){ - echo "version" -} -function build_image_env(){ - local disable_check=$1 - echo "build_image_env($disable_check)" -} -function delete_env(){ - echo "delete_env" -} -function create_wrappers(){ - : -} -function setup_env_from_file(){ - echo "setup_env_from_file($1)" -} -function setup_env(){ - echo "setup_env($1)" -} -function run_env_as_proot_fakeroot(){ - local backend_command="$1" - local backend_args="$2" - local no_copy_files="$3" - shift 3 - echo "run_env_as_proot_fakeroot($backend_command,$backend_args,$no_copy_files,$@)" -} -function run_env_as_groot(){ - local backend_command="$1" - local backend_args="$2" - local no_copy_files="$3" - shift 3 - echo "run_env_as_groot($backend_command,$backend_args,$no_copy_files,$@)" -} -function run_env_as_chroot(){ - local backend_command="$1" - local backend_args="$2" - local no_copy_files="$3" - shift 3 - echo "run_env_as_chroot($backend_command,$backend_args,$no_copy_files,$@)" -} -function run_env_as_proot_user(){ - local backend_command="$1" - local backend_args="$2" - local no_copy_files="$3" - shift 3 - echo "run_env_as_proot_user($backend_command,$backend_args,$no_copy_files,$@)" -} -function run_env_as_bwrap_fakeroot(){ - local backend_command="$1" - local backend_args="$2" - local no_copy_files="$3" - shift 3 - echo "run_env_as_bwrap_fakeroot($backend_command,$backend_args,$no_copy_files,$@)" -} -function run_env_as_bwrap_user(){ - local backend_command="$1" - local backend_args="$2" - local no_copy_files="$3" - shift 3 - echo "run_env_as_bwrap_user($backend_command,$backend_args,$no_copy_files,$@)" + function create_wrappers(){ + : + } } function test_help(){ @@ -106,6 +105,18 @@ function test_build_image_env(){ assertEquals "build_image_env(true)" "$(cat $STDOUTF)" } +function test_create_wrappers(){ + function create_wrappers(){ + local force=$1 + echo "create_wrappers($force)" + } + assertCommandSuccess main create-bin-wrappers + assertEquals "create_wrappers(false)" "$(cat $STDOUTF)" + + assertCommandSuccess main create-bin-wrappers --force + assertEquals "create_wrappers(true)" "$(cat $STDOUTF)" +} + function test_delete_env(){ assertCommandSuccess main s -d assertEquals "delete_env" "$(cat $STDOUTF)"