Skip to content

Commit

Permalink
refactor little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
seletskiy committed Apr 25, 2016
1 parent 8bdebcd commit d2f1097
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/lorg
/.last-testcase
32 changes: 10 additions & 22 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@

set -euo pipefail

which hastur >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "missing dependency: hastur"
exit 1
fi
cd tests/

tests_lib=tests/lib/tests.sh
source deps.sh
source sudo.sh
source hastur.sh
source getopt.sh
source containers.sh

if [ ! -f $tests_lib ]; then
echo "missing dependency: tests.sh"
echo "trying fix this via updating git submodules"
:usage:parse-opts opts "${@}"

git submodule init
git submodule update

if [ ! -f $tests_lib ]; then
echo "file '$tests_lib' not found"
exit 1
fi
fi

source $tests_lib
source tests/hastur.sh
source tests/getopt.sh
:deps:check-hastur
:deps:check-tests.sh

:hastur:init

$tests_lib -d tests/testcases -s tests/local-setup.sh ${@:--A}
:deps:tests.sh -d testcases -s local-setup.sh "${opts[@]:--A}"
8 changes: 8 additions & 0 deletions tests/containers.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
_containers_count=3

:containers:set-count() {
_containers_count=$1
}

:containers:count() {
echo "$_containers_count"
}

:containers:spawn() {
tests:eval :hastur -kS ${@:-/bin/true}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
_tests_lib=lib/tests.sh

:deps:check-hastur() {
which hastur >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "missing dependency: hastur"
exit 1
fi
}

:deps:hastur() {
sudo hastur "${@}"
}

:deps:check-tests.sh() {
if [ ! -f $_tests_lib ]; then
echo "missing dependency: tests.sh"
echo "trying fix this via updating git submodules"

git submodule init
git submodule update

if [ ! -f $_tests_lib ]; then
echo "file '$_tests_lib' not found"
exit 1
fi
fi
}

:deps:tests.sh() {
$_tests_lib "${@}"
}
85 changes: 64 additions & 21 deletions tests/getopt.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,68 @@
_long_opts=$(paste -sd, <<OPTS
:usage() {
cat <<HELP
Usage: run_tests.sh [options] [-v] [(-A | -O)]
Options:
--keep-containers Do not remove containers after every testcase.
--keep-images Remove containers, but keep base images to speed up
starting.
--containers Set containers count [default: 3].
-A Run all testcases.
-O Run last failed testcase.
-v Set verbosity level.
HELP
}

:usage:parse-opts() {
local opts_var="$1"
shift

local long_opts=$(paste -sd, <<OPTS
help
keep-containers
keep-images
containers-count:
containers:
OPTS
)

set -- $(getopt --long $_long_opts -o AOv -- "${@}")

while :; do
case "$1" in
--keep-containers)
:hastur:keep-containers
;;
--keep-images)
:hastur:keep-images
;;
--)
shift
break
;;
esac
)

shift
done
set -- $(getopt --long $long_opts -o vhAO:: -- "${@}")

:containers:set-count 3

local opts=()

while :; do
case "$1" in
--keep-containers)
shift
:hastur:keep-containers
;;

--keep-images)
shift
:hastur:keep-images
;;

--containers)
shift
:containers:set-count "$1"
;;

-h|--help)
:usage
exit 1
;;

--)
shift
break
;;

*)
opts+=("$1")
shift
esac
done

eval "$opts_var=(${opts})"
}
12 changes: 8 additions & 4 deletions tests/hastur.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _hastur_root_dir="/var/lib/hastur"
:hastur() {
mkdir -p $_hastur_root_dir

sudo hastur -q -r $_hastur_root_dir "${@}"
:deps:hastur -q -r $_hastur_root_dir "${@}"
}

:hastur:init() {
Expand All @@ -30,6 +30,8 @@ _hastur_root_dir="/var/lib/hastur"
else
printf "fail.\n\n%s\n" "$hastur_out"
fi

trap :hastur:cleanup EXIT
}

:hastur:destroy-containers() {
Expand All @@ -45,13 +47,15 @@ _hastur_root_dir="/var/lib/hastur"
}

:hastur:cleanup() {
printf "Cleaning up hastur containers... "
printf "Cleaning up hastur containers...\n"

tests:debug() {
echo "${@}" >&2
}

:hastur:destroy-containers

:hastur:destroy-root

printf "ok.\n"
}

trap :hastur:cleanup EXIT
10 changes: 6 additions & 4 deletions tests/local-setup.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
tests:involve tests/containers.sh
tests:involve tests/hastur.sh
tests:involve tests/sudo.sh
tests:involve deps.sh
tests:involve sudo.sh
tests:involve hastur.sh
tests:involve getopt.sh
tests:involve containers.sh

for (( i = $_containers_count; i > 0; i-- )) {
for (( i = $(:containers:count); i > 0; i-- )) {
:containers:spawn
}

Expand Down

0 comments on commit d2f1097

Please sign in to comment.