Merge pull request #30 from fsquillace/dev

Merge from dev branch
This commit is contained in:
Filippo Squillace 2014-11-30 16:03:58 +00:00
commit 8fbcb1b6fa
9 changed files with 552 additions and 110 deletions

View file

@ -52,10 +52,11 @@ JuJu can only works on GNU/Linux OS with kernel version greater or equal
Advanced usage
--------------
### Build image ###
You can build a new JuJu image from scratch by running the following command:
```
# juju -b
```
In this way the script will create a directory containing all the essentials
files in order to make JuJu working properly (such as pacman, yaourt, arch-chroot and proot).
Remember that the script to build the image must run in an ArchLinux OS with
@ -66,6 +67,17 @@ After creating the image juju-x86\_64.tar.gz you can install it by running:
# juju -i juju-x86_64.tar.gz
### Bind directories ###
To bind and host directory to a guest location, you can use proot arguments:
```
$ juju -p "-b /mnt/mydata:/home/user/mydata"
```
Check out the proot options with:
```
$ juju -p "--help"
```
Dependencies
------------
JuJu comes with a very short list of dependencies in order to be installed in most

186
bin/juju
View file

@ -18,8 +18,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
NAME='juju'
VERSION='1.0'
NAME='JuJu'
CMD='juju'
VERSION='2.5.6'
CODE_NAME='Lion'
DESCRIPTION='The GNU/Linux distribution container for non-root users'
AUTHOR='Filippo Squillace <feel dot squally at gmail.com>'
HOMEPAGE='https://github.com/fsquillace/juju'
COPYRIGHT='2012-2014'
source "$(dirname $0)/../lib/core.sh"
@ -28,22 +34,23 @@ source "$(dirname $0)/../lib/core.sh"
###################################
usage() {
echo -e "JuJu: The portable GNU/Linux distribution"
echo -e "Usage: $NAME [options]"
echo -e "$NAME: $DESCRIPTION"
echo -e "Usage: $CMD [options] [command]"
echo -e "Options:"
echo -e "-i, --setup-from-file <image> Setup the JuJu image in ${JUJU_HOME}"
echo -e "-f, --fakeroot Run JuJu with fakeroot privileges"
echo -e "-r, --root Run JuJu with root privileges"
echo -e "-b, --build-image Build a JuJu image (must run in ArchLinux)"
echo -e "-d, --delete Delete JuJu from ${JUJU_HOME}"
echo -e "-i, --setup-from-file <image> Setup the $NAME image in ${JUJU_HOME}"
echo -e "-f, --fakeroot Run $NAME with fakeroot privileges"
echo -e "-r, --root Run $NAME with root privileges"
echo -e "-p, --proot-args Proot arguments"
echo -e "-b, --build-image Build a $NAME image (must run in ArchLinux)"
echo -e "-d, --delete Delete $NAME from ${JUJU_HOME}"
echo -e "-h, --help Show this help message"
echo -e "-v, --version Show the $NAME version"
}
version() {
echo -e "JuJu ($VERSION): The portable GNU/Linux distribution"
echo -e "Copyright (c) 2012-2014 Filippo Squillace <feel.squally@gmail.com>"
echo -e "Homepage: http://github.com/fsquillace/juju"
echo -e "$NAME $VERSION ($CODE_NAME): $DESCRIPTION"
echo -e "Copyright (c) $COPYRIGHT $AUTHOR"
echo -e "Homepage: $HOMEPAGE"
}
check_cli(){
@ -60,7 +67,7 @@ check_cli(){
if $OPT_BUILD_IMAGE || $OPT_HELP || $OPT_VERSION || $OPT_SETUP_FROM_FILE || \
$OPT_FAKEROOT || $OPT_ROOT
then
die "The JuJu delete option must be used exclusively"
die "The $NAME delete option must be used exclusively"
fi
fi
if $OPT_HELP
@ -68,7 +75,7 @@ check_cli(){
if $OPT_BUILD_IMAGE || $OPT_DELETE || $OPT_VERSION || $OPT_SETUP_FROM_FILE || \
$OPT_FAKEROOT || $OPT_ROOT
then
die "The JuJu help option must be used exclusively"
die "The $NAME help option must be used exclusively"
fi
fi
if $OPT_VERSION
@ -76,91 +83,102 @@ check_cli(){
if $OPT_BUILD_IMAGE || $OPT_DELETE || $OPT_HELP || $OPT_SETUP_FROM_FILE || \
$OPT_FAKEROOT || $OPT_ROOT
then
die "The JuJu version option must be used exclusively"
die "The $NAME version option must be used exclusively"
fi
fi
if $OPT_FAKEROOT && $OPT_ROOT
then
die "You must access to JuJu with either fakeroot or root permissions"
die "You must access to $NAME with either fakeroot or root permissions"
fi
if $OPT_PROOT_ARGS
then
if $OPT_BUILD_IMAGE || $OPT_DELETE || $OPT_HELP || $OPT_SETUP_FROM_FILE || \
$OPT_ROOT || $OPT_VERSION
then
die "Invalid syntax: Proot args are not allowed with the other options"
fi
fi
if [ "$ARGS" != "" ]
then
if $OPT_BUILD_IMAGE || $OPT_DELETE || $OPT_HELP || $OPT_SETUP_FROM_FILE || \
$OPT_VERSION
then
die "No arguments are needed. For the CLI syntax run: $CMD --help"
fi
fi
[ "$ARGS" != "" ] && die "No arguments are needed. For the CLI syntax run: $NAME --help"
return 0
}
###################################
### MAIN PROGRAM ###
###################################
function parse_arguments(){
TEMP=$(getopt -o drp:fbi:hv --long delete,root,proot-args:,fakeroot,build-image,setup-from-file:,help,version -n "$CMD" -- "$@")
eval set -- "$TEMP"
TEMP=`getopt -o drfbi:hv --long delete,root,fakeroot,build-image,setup-from-file:,help,version -n 'juju' -- "$@"`
OPT_SETUP_FROM_FILE=false
IMAGE_FILE=""
OPT_FAKEROOT=false
OPT_ROOT=false
OPT_PROOT_ARGS=false
PROOT_ARGS=""
OPT_BUILD_IMAGE=false
OPT_DELETE=false
OPT_HELP=false
OPT_VERSION=false
while true ; do
case "$1" in
-i|--setup-from-file) OPT_SETUP_FROM_FILE=true ; shift ; IMAGE_FILE=$1 ; shift ;;
-f|--fakeroot) OPT_FAKEROOT=true ; shift ;;
-r|--root) OPT_ROOT=true ; shift ;;
-p|--proot-args) OPT_PROOT_ARGS=true ; shift ; PROOT_ARGS=$1; shift ;;
-b|--build-image) OPT_BUILD_IMAGE=true ; shift ;;
-d|--delete) OPT_DELETE=true ; shift ;;
-h|--help) OPT_HELP=true ; shift ;;
-v|--version) OPT_VERSION=true ; shift ;;
--) shift ; break ;;
*) die "Internal error!" ;;
esac
done
if [ $? != 0 ] ; then error "Error on parsing the command line. Try juju -h." ; exit ; fi
ARGS=()
for arg do
ARGS+=($arg)
done
}
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
function execute_operation(){
$OPT_HELP && usage && return
$OPT_VERSION && version && return
OPT_SETUP_FROM_FILE=false
IMAGE_FILE=""
OPT_FAKEROOT=false
OPT_ROOT=false
OPT_BUILD_IMAGE=false
OPT_DELETE=false
OPT_HELP=false
OPT_VERSION=false
while true ; do
case "$1" in
-i|--setup-from-file) OPT_SETUP_FROM_FILE=true ; shift ; IMAGE_FILE=$1 ; shift ;;
-f|--fakeroot) OPT_FAKEROOT=true ; shift ;;
-r|--root) OPT_ROOT=true ; shift ;;
-b|--build-image) OPT_BUILD_IMAGE=true ; shift ;;
-d|--delete) OPT_DELETE=true ; shift ;;
-h|--help) OPT_HELP=true ; shift ;;
-v|--version) OPT_VERSION=true ; shift ;;
--) shift ; break ;;
*) error "Internal error!" ; exit 1 ;;
esac
done
if $OPT_BUILD_IMAGE; then
build_image_juju
return
elif $OPT_DELETE; then
delete_juju
return
fi
ARGS=()
for arg do
ARGS+=($arg)
done
if ! is_juju_installed
then
if $OPT_SETUP_FROM_FILE; then
setup_from_file_juju $IMAGE_FILE
else
setup_juju
fi
elif $OPT_SETUP_FROM_FILE; then
die "Error: The image cannot be installed since $JUJU_HOME is not empty."
fi
check_cli || exit 1
################ DEFINE ACTION ########################
$OPT_HELP && usage && exit
$OPT_VERSION && version && exit
if $OPT_BUILD_IMAGE; then
build_image_juju
exit
elif $OPT_DELETE; then
delete_juju
exit
fi
if ! is_juju_installed
then
if $OPT_SETUP_FROM_FILE; then
setup_from_file_juju $IMAGE_FILE
else
setup_juju
fi
elif $OPT_SETUP_FROM_FILE; then
die "Error: The image cannot be installed since $JUJU_HOME is not empty."
fi
if $OPT_FAKEROOT; then
run_juju_as_fakeroot
elif $OPT_ROOT; then
run_juju_as_root
else
run_juju_as_user
fi
if $OPT_FAKEROOT; then
run_juju_as_fakeroot "${PROOT_ARGS}" ${ARGS[@]}
elif $OPT_ROOT; then
run_juju_as_root ${ARGS[@]}
else
run_juju_as_user "${PROOT_ARGS}" ${ARGS[@]}
fi
}
parse_arguments $@
check_cli
execute_operation
# vim: set ts=4 sw=4 noet:

View file

@ -70,11 +70,15 @@ then
SH="/bin/sh --login"
elif [ "$JUJU_ENV" == "1" ]
then
PROOT="$LD_LIB"
SH="/bin/sh"
die "Error: Nested JuJu environments are not allowed"
else
die "The variable JUJU_ENV is not properly set"
fi
CHROOT=${JUJU_HOME}/usr/bin/arch-chroot
TRUE=${JUJU_HOME}/usr/bin/true
ID="${JUJU_HOME}/usr/bin/id -u"
################################# MAIN FUNCTIONS ##############################
function is_juju_installed(){
@ -83,7 +87,7 @@ function is_juju_installed(){
}
function cleanup_build_directory(){
function _cleanup_build_directory(){
# $1: maindir (optional) - str: build directory to get rid
local maindir=$1
builtin cd $ORIGIN_WD
@ -92,7 +96,7 @@ function cleanup_build_directory(){
}
function prepare_build_directory(){
function _prepare_build_directory(){
trap - QUIT EXIT ABRT KILL TERM INT
trap "rm -rf ${maindir}; die \"Error occurred when installing JuJu\"" EXIT QUIT ABRT KILL TERM INT
}
@ -110,10 +114,9 @@ function _setup_juju(){
function setup_juju(){
# Setup the JuJu environment
[ "$JUJU_ENV" == "1" ] && die "Error: The operation is not allowed inside JuJu environment"
local maindir=$(TMPDIR=$JUJU_TEMPDIR mktemp -d -t juju.XXXXXXXXXX)
prepare_build_directory
_prepare_build_directory
info "Downloading JuJu..."
builtin cd ${maindir}
@ -123,13 +126,12 @@ function setup_juju(){
info "Installing JuJu..."
_setup_juju ${maindir}/${imagefile}
cleanup_build_directory ${maindir}
_cleanup_build_directory ${maindir}
}
function setup_from_file_juju(){
# Setup from file the JuJu environment
[ "$JUJU_ENV" == "1" ] && die "Error: The operation is not allowed inside JuJu environment"
local imagefile=$1
[ ! -e ${imagefile} ] && die "Error: The JuJu image file ${imagefile} does not exist"
@ -141,37 +143,54 @@ function setup_from_file_juju(){
}
function run_juju_as_root(){
[ "$JUJU_ENV" == "1" ] && die "Error: The operation is not allowed inside JuJu environment"
function _define_chroot_args(){
local comm=${SH}
[ "$1" != "" ] && comm="$@"
echo $comm
}
function _define_proot_args(){
local proot_args="$1"
shift
local comm=$(_define_chroot_args "$@")
echo "$proot_args" "${comm[@]}"
}
function run_juju_as_root(){
mkdir -p ${JUJU_HOME}/${HOME}
${JUJU_HOME}/usr/bin/arch-chroot $JUJU_HOME /usr/bin/bash -c 'mkdir -p /run/lock && /bin/sh'
JUJU_ENV=1 ${CHROOT} $JUJU_HOME /usr/bin/bash -c "mkdir -p /run/lock && $(_define_chroot_args "$@")"
}
function _run_juju_with_proot(){
if ${PROOT} ${JUJU_HOME}/usr/bin/true &> /dev/null
then
JUJU_ENV=1 ${PROOT} $@ ${JUJU_HOME} ${SH}
else
JUJU_ENV=1 PROOT_NO_SECCOMP=1 ${PROOT} $@ ${JUJU_HOME} ${SH}
fi
${PROOT} ${TRUE} &> /dev/null || export PROOT_NO_SECCOMP=1
[ "$(${PROOT} ${ID} 2> /dev/null )" == "0" ] && \
die "You cannot access with root privileges. Use --root option instead."
JUJU_ENV=1 ${PROOT} $@
local ret=$?
export -n PROOT_NO_SECCOMP
return $ret
}
function run_juju_as_fakeroot(){
_run_juju_with_proot "-S"
local comm=$(_define_proot_args "$@")
_run_juju_with_proot "-S" ${JUJU_HOME} ${comm}
}
function run_juju_as_user(){
_run_juju_with_proot "-R"
local comm=$(_define_proot_args "$@")
_run_juju_with_proot "-R" ${JUJU_HOME} ${comm}
}
function delete_juju(){
[ "$JUJU_ENV" == "1" ] && die "Error: The operation is not allowed inside JuJu environment"
! ask "Are you sure to delete JuJu located in ${JUJU_HOME}" "N" && return
if mountpoint -q ${JUJU_HOME}
then
@ -212,7 +231,7 @@ function build_image_juju(){
_check_package git
local maindir=$(TMPDIR=$JUJU_TEMPDIR mktemp -d -t juju.XXXXXXXXXX)
mkdir -p ${maindir}/root
prepare_build_directory
_prepare_build_directory
info "Installing pacman and its dependencies..."
pacstrap -d ${maindir}/root pacman arch-install-scripts binutils libunistring
@ -248,9 +267,15 @@ function build_image_juju(){
echo 'export PATH=$PATH:/opt/juju/bin' > ${maindir}/root/etc/profile.d/juju.sh
chmod +x ${maindir}/root/etc/profile.d/juju.sh
info "Validating JuJu image..."
arch-chroot ${maindir}/root pacman -Qi pacman &> /dev/null
arch-chroot ${maindir}/root yaourt -V &> /dev/null
arch-chroot ${maindir}/root proot --help &> /dev/null
arch-chroot ${maindir}/root arch-chroot --help &> /dev/null
builtin cd ${ORIGIN_WD}
local imagefile="juju-${ARCH}.tar.gz"
info "Compressing image to ${imagefile}..."
tar -zcpf ${imagefile} -C ${maindir}/root .
cleanup_build_directory ${maindir}
_cleanup_build_directory ${maindir}
}

View file

@ -17,7 +17,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
echoerr() { echo "$@" 1>&2; }
function echoerr(){
echo "$@" 1>&2
}
function die(){
# $@: msg (mandatory) - str: Message to print
error $@

5
tests/test_all.sh Executable file
View file

@ -0,0 +1,5 @@
for tst in $(ls $(dirname $0)/test_* | grep -v $(basename $0))
do
$tst
done

162
tests/test_core.sh Executable file
View file

@ -0,0 +1,162 @@
#!/bin/bash
source "$(dirname $0)/utils.sh"
CURRPWD=$PWD
JUJU_MAIN_HOME=/tmp/jujutesthome
[ -e $JUJU_MAIN_HOME ] || JUJU_HOME=$JUJU_MAIN_HOME bash --rcfile "$(dirname $0)/../lib/core.sh" -ic "setup_juju"
JUJU_HOME=""
function install_mini_juju(){
cp -rfa $JUJU_MAIN_HOME/* $JUJU_HOME
}
function set_up(){
cd $CURRPWD
JUJU_HOME=$(TMPDIR=/tmp mktemp -d -t jujuhome.XXXXXXXXXX)
source "$(dirname $0)/../lib/core.sh"
ORIGIN_WD=$(TMPDIR=/tmp mktemp -d -t jujuowd.XXXXXXXXXX)
cd $ORIGIN_WD
JUJU_TEMPDIR=$(TMPDIR=/tmp mktemp -d -t jujutemp.XXXXXXXXXX)
trap - QUIT EXIT ABRT KILL TERM INT
trap "rm -rf ${JUJU_HOME}; rm -rf ${ORIGIN_WD}; rm -rf ${JUJU_TEMPDIR}" EXIT QUIT ABRT KILL TERM INT
}
function tear_down(){
rm -rf $JUJU_HOME
rm -rf $ORIGIN_WD
rm -rf $JUJU_TEMPDIR
trap - QUIT EXIT ABRT KILL TERM INT
}
function test_is_juju_installed(){
is_juju_installed
is_equal $? 1 || return 1
touch $JUJU_HOME/just_file
is_juju_installed
is_equal $? 0 || return 1
}
function test_setup_juju(){
wget_mock(){
# Proof that the setup is happening
# inside $JUJU_TEMPDIR
local cwd=${PWD#${JUJU_TEMPDIR}}
local parent_dir=${PWD%${cwd}}
is_equal $JUJU_TEMPDIR ${parent_dir} || return 1
touch file
tar -czvf juju-${ARCH}.tar.gz file
}
WGET=wget_mock
setup_juju 1> /dev/null
[ -e $JUJU_HOME/file ] || return 1
[ -e $JUJU_HOME/run/lock ] || return 1
}
function test_setup_from_file_juju(){
touch file
tar -czvf juju-${ARCH}.tar.gz file 1> /dev/null
setup_from_file_juju juju-${ARCH}.tar.gz 1> /dev/null
[ -e $JUJU_HOME/file ] || return 1
[ -e $JUJU_HOME/run/lock ] || return 1
export -f setup_from_file_juju
export -f die
bash -ic "setup_from_file_juju noexist.tar.gz" &> /dev/null
is_equal $? 1 || return 1
}
function test_run_juju_as_root(){
install_mini_juju
CHROOT="sudo $CHROOT"
SH="type -t type"
local output=$(run_juju_as_root)
is_equal $output "builtin" || return 1
local output=$(run_juju_as_root pwd)
is_equal $output "/" || return 1
run_juju_as_root "[ -e /run/lock ]"
is_equal $? 0 || return 1
[ -e $JUJU_HOME/${HOME} ] || return 1
}
function test_run_juju_as_user(){
install_mini_juju
local output=$(run_juju_as_user "" "mkdir -v /newdir2" | awk -F: '{print $1}')
is_equal "$output" "/usr/bin/mkdir" || return 1
[ -e $JUJU_HOME/newdir2 ]
is_equal $? 0 || return 1
SH="mkdir -v /newdir"
local output=$(run_juju_as_user "" | awk -F: '{print $1}')
is_equal "$output" "/usr/bin/mkdir" || return 1
[ -e $JUJU_HOME/newdir ]
is_equal $? 0 || return 1
}
function test_run_juju_as_user_proot_args(){
install_mini_juju
run_juju_as_user "--help" "" 1> /dev/null
is_equal $? 0 || return 1
run_juju_as_user "--helps" "" &> /dev/null
is_equal $? 1 || return 1
mkdir $JUJU_TEMPDIR/newdir
touch $JUJU_TEMPDIR/newdir/newfile
run_juju_as_user "-b $JUJU_TEMPDIR/newdir:/newdir" "ls -l /newdir/newfile" 1> /dev/null
is_equal $? 0 || return 1
export -f _run_juju_with_proot
export PROOT
export TRUE
ID="/usr/bin/echo 0" bash -ic "_run_juju_with_proot" &> /dev/null
is_equal $? 1 || return 1
export -n _run_juju_with_proot
unset _run_juju_with_proot
export -n PROOT
export -n TRUE
}
function test_run_juju_as_user_seccomp(){
install_mini_juju
PROOT=""
local output=$(_run_juju_with_proot "" "env" | grep "PROOT_NO_SECCOMP")
is_equal $output "" || return 1
TRUE="/usr/bin/false"
local output=$(_run_juju_with_proot "" "env" | grep "PROOT_NO_SECCOMP")
is_equal $output "PROOT_NO_SECCOMP=1" || return 1
}
function test_run_juju_as_fakeroot(){
install_mini_juju
local output=$(run_juju_as_fakeroot "" "id" | awk '{print $1}')
is_equal "$output" "uid=0(root)" || return 1
}
function test_delete_juju(){
install_mini_juju
echo "N" | delete_juju 1> /dev/null
is_juju_installed
is_equal $? 0 || return 1
echo "Y" | delete_juju 1> /dev/null
is_juju_installed
is_equal $? 1 || return 1
}
function test_nested_juju(){
install_mini_juju
JUJU_ENV=1 bash -ic "source $CURRPWD/$(dirname $0)/../lib/core.sh" &> /dev/null
is_equal $? 1 || return 1
}
for func in $(declare -F | grep test_ | awk '{print $3}' | xargs)
do
set_up
$func && echo -e "${func}...\033[1;32mOK\033[0m" || echo -e "${func}...\033[1;31mFAIL\033[0m"
tear_down
done

141
tests/test_juju.sh Executable file
View file

@ -0,0 +1,141 @@
#!/bin/bash
source "$(dirname $0)/utils.sh"
source $(dirname $0)/../bin/juju -h &> /dev/null
## Mock functions ##
function usage(){
echo "usage"
}
function version(){
echo "version"
}
function build_image_juju(){
echo "build_image_juju"
}
function delete_juju(){
echo "delete_juju"
}
function is_juju_installed(){
return 0
}
function setup_from_file_juju(){
echo "setup_from_file_juju $@"
}
function setup_juju(){
echo "setup_juju"
}
function run_juju_as_fakeroot(){
echo "run_juju_as_fakeroot $@"
}
function run_juju_as_root(){
echo "run_juju_as_root $@"
}
function run_juju_as_user(){
echo "run_juju_as_user $@"
}
function wrap_juju(){
parse_arguments $@
check_cli
execute_operation
}
function set_up(){
echo > /dev/null
}
function tear_down(){
echo > /dev/null
}
function test_help(){
local output=$(wrap_juju -h)
is_equal $output "usage" || return 1
local output=$(wrap_juju --help)
is_equal $output "usage" || return 1
}
function test_version(){
local output=$(wrap_juju -v)
is_equal $output "version" || return 1
local output=$(wrap_juju --version)
is_equal $output "version" || return 1
}
function test_build_image_juju(){
local output=$(wrap_juju -b)
is_equal $output "build_image_juju" || return 1
local output=$(wrap_juju --build-image)
is_equal $output "build_image_juju" || return 1
}
function test_delete_juju(){
local output=$(wrap_juju -d)
is_equal $output "delete_juju" || return 1
local output=$(wrap_juju --delete)
is_equal $output "delete_juju" || return 1
}
function test_run_juju_as_fakeroot(){
local output=$(wrap_juju -f)
is_equal $output "run_juju_as_fakeroot" || return 1
local output=$(wrap_juju --fakeroot)
is_equal $output "run_juju_as_fakeroot" || return 1
local output=$(wrap_juju -f -p "-b arg")
is_equal "${output[@]}" "run_juju_as_fakeroot -b arg" || return 1
local output=$(wrap_juju -f -p "-b arg" -- command)
is_equal "${output[@]}" "run_juju_as_fakeroot -b arg command" || return 1
local output=$(wrap_juju -f command)
is_equal "${output[@]}" "run_juju_as_fakeroot command" || return 1
}
function test_run_juju_as_user(){
local output=$(wrap_juju)
is_equal $output "run_juju_as_user" || return 1
local output=$(wrap_juju -p "-b arg")
is_equal "${output[@]}" "run_juju_as_user -b arg" || return 1
local output=$(wrap_juju -p "-b arg" -- command)
is_equal "${output[@]}" "run_juju_as_user -b arg command" || return 1
local output=$(wrap_juju command)
is_equal "${output[@]}" "run_juju_as_user command" || return 1
}
function test_run_juju_as_root(){
local output=$(wrap_juju -r)
is_equal $output "run_juju_as_root" || return 1
local output=$(wrap_juju -r command)
is_equal "${output[@]}" "run_juju_as_root command" || return 1
}
function test_check_cli(){
export -f check_cli
export -f parse_arguments
export -f execute_operation
export -f wrap_juju
export -f die
bash -ic "wrap_juju -b -h" &> /dev/null
is_equal $? 1 || return 1
bash -ic "wrap_juju -d -r" &> /dev/null
is_equal $? 1 || return 1
bash -ic "wrap_juju -h -f" &> /dev/null
is_equal $? 1 || return 1
bash -ic "wrap_juju -v -i fsd" &> /dev/null
is_equal $? 1 || return 1
bash -ic "wrap_juju -f -r" &> /dev/null
is_equal $? 1 || return 1
bash -ic "wrap_juju -p args -v" &> /dev/null
is_equal $? 1 || return 1
bash -ic "wrap_juju -d args" &> /dev/null
is_equal $? 1 || return 1
export -n check_cli
export -n parse_arguments
export -n execute_operation
export -n wrap_juju
export -n die
unset die
}
for func in $(declare -F | grep test_ | awk '{print $3}' | xargs)
do
set_up
$func && echo -e "${func}...\033[1;32mOK\033[0m" || echo -e "${func}...\033[1;31mFAIL\033[0m"
tear_down
done

66
tests/test_util.sh Executable file
View file

@ -0,0 +1,66 @@
#!/bin/bash
source "$(dirname $0)/utils.sh"
source "$(dirname $0)/../lib/util.sh"
function test_echoerr(){
local actual=$(echoerr "Test" 2>&1)
is_equal "$actual" "Test" || return 1
return 0
}
function test_error(){
local actual=$(error "Test" 2>&1)
local expected=$(echo -e "\033[1;31mTest\033[0m")
is_equal "$actual" "$expected" || return 1
return 0
}
function test_warn(){
local actual=$(warn "Test" 2>&1)
local expected=$(echo -e "\033[1;33mTest\033[0m")
is_equal "$actual" "$expected" || return 1
return 0
}
function test_info(){
local actual=$(info "Test")
local expected=$(echo -e "\033[1;37mTest\033[0m")
is_equal "$actual" "$expected" || return 1
return 0
}
function test_die(){
local actual=$(die "Test" 2>&1)
local expected=$(echo -e "\033[1;31mTest\033[0m")
is_equal "$actual" "$expected" || return 1
export -f die
bash -ic "die Dying" &> /dev/null
is_equal $? 1 || return 1
export -n die
unset die
return 0
}
function test_ask(){
echo "Y" | ask "Test" &> /dev/null
is_equal $? 0 || return 1
echo "y" | ask "Test" &> /dev/null
is_equal $? 0 || return 1
echo "N" | ask "Test" &> /dev/null
is_equal $? 1 || return 1
echo "n" | ask "Test" &> /dev/null
is_equal $? 1 || return 1
echo -e "\n" | ask "Test" &> /dev/null
is_equal $? 0 || return 1
echo -e "\n" | ask "Test" "N" &> /dev/null
is_equal $? 1 || return 1
echo -e "asdf\n\n" | ask "Test" "N" &> /dev/null
is_equal $? 1 || return 1
return 0
}
for func in $(declare -F | grep test_ | awk '{print $3}' | xargs)
do
$func && echo -e "${func}...\033[1;32mOK\033[0m" || echo -e "${func}...\033[1;31mFAIL\033[0m"
done

11
tests/utils.sh Normal file
View file

@ -0,0 +1,11 @@
function is_equal(){
if [ "$1" == "$2" ]
then
return 0
else
echo "$1!=$2"
return 1
fi
}