Compare commits

...

14 Commits

  1. 0
      .package
  2. 765
      bin/compose
  3. 29
      bin/test
  4. 258
      test/args
  5. 254
      test/base

0
.package

765
bin/compose
File diff suppressed because it is too large
View File

29
bin/test

@ -0,0 +1,29 @@
#!/usr/bin/env bash-shlib
# -*- mode: shell-script -*-
include common
exname="$(basename $0)"
if ! [ -e "../.package" ]; then
echo "Please execute this in test directory, with '../bin/$exname'." >&2
echo "Or, if pkgcmd is installed, use 't'..."
exit 1
fi
base=$(realpath "$PWD/..")
export base
cd "$base/test"
for tfile in *; do
[ -e "$tfile" ] || continue
[ -d "$tfile" ] && continue
(
/usr/bin/env bash-shlib "$tfile" "$@"
) && continue
exit "$?"
done

258
test/args

@ -0,0 +1,258 @@
#!/usr/bin/env bash-shlib
# -*- mode: shell-script -*-
include shunit
depends sed grep git mkdir readlink
export -f matches
export grep
tmp=/tmp
tprog="../bin/compose"
tprog=$(readlink -f $tprog)
export PATH=".:$PATH"
short_tprog=$(basename "$tprog")
##
## Convenience function
##
init_test() {
test_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
cd "$test_tmpdir"
export CACHEDIR="$test_tmpdir/.cache"
export VARDIR="$test_tmpdir/.var"
mkdir -p "$CACHEDIR"
}
tear_test() {
rm -rf "$test_tmpdir"
}
##
## Tests
##
function test_compose_run_args {
init_test
export CHARM_STORE=$test_tmpdir
mkdir -p $test_tmpdir/{www,mysql}
cat <<EOF2 > $test_tmpdir/www/metadata.yml
EOF2
cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
EOF2
cat <<EOF2 > $test_tmpdir/compose.yml
web_site:
charm: www
EOF2
export DISABLE_SYSTEM_CONFIG_FILE=true
assert_list <<EOF
### Testing args passing to docker-compose
## -- simple action
cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run run web_site 2>&1 >/dev/null )
expected="docker-compose run web_site"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- simple single dash arg
cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run run -T web_site 2>&1 >/dev/null )
expected="docker-compose run -T web_site"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- desaggregation of combined single char args
cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run logs -ft --tail 20 web_site 2>&1 >/dev/null)
expected="docker-compose logs -f -t --tail 20 web_site"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- desaggregation of combined single char option and valued option char
cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run run -Tv x:y web_site 2>&1 >/dev/null)
expected="docker-compose run -T -v x:y web_site"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- simple unexpected single dash arg
cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run run -Z web_site 2>&1)
expected_reg="Unknown option '-Z'"
[[ "\$out" =~ \$expected_reg ]] || {
echo -e "Can't find '\$expected_reg' in out:\n\$out"
exit 1
}
## -- simple unexpected single dash arg after expected one
cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run run -T -Z web_site 2>&1)
expected_reg="Unknown option '-Z'"
[[ "\$out" =~ \$expected_reg ]] || {
echo -e "Can't find '\$expected_reg' in out:\n\$out"
exit 1
}
## -- simple unexpected single dash arg after expected aggregated one
cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run run -TZ web_site 2>&1)
expected_reg="Unknown option '-Z'"
[[ "\$out" =~ \$expected_reg ]] || {
echo -e "Can't find '\$expected_reg' in out:\n\$out"
exit 1
}
## -- multiple services
cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run logs --tail 15 web_site mysql 2>&1 >/dev/null)
expected="docker-compose logs --tail 15 web_site mysql"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- single services
cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run run web_site mysql 2>&1 >/dev/null)
expected="docker-compose run web_site mysql"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
EOF
tear_test
}
function test_filter_opts {
src=$(cat <<'EOF'
-d, --detach
--name NAME
--entrypoint CMD
-e KEY=VAL
-l, --label KEY=VAL
-u, --user=""
--no-deps
--rm
-p, --publish=[]
--service-ports
--use-aliases
-v, --volume=[]
-T
-w, --workdir=""
EOF
)
export src
assert_list <<EOF
### Testing filtering opts
## -- multi_opts_filter should find opts with args
. "$tprog"
out=\$(echo "\$src" | multi_opts_filter | tr " " "\n") || exit 12
expected="--name
--entrypoint
-e
-l
--label
-u
--user
-p
--publish
-v
--volume
-w
--workdir"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- single_opts_filter should find opts with args
. "$tprog"
out=\$(echo "\$src" | single_opts_filter | tr " " "\n") || exit 12
expected="-d
--detach
--no-deps
--rm
--service-ports
--use-aliases
-T"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
EOF
}
continue_on_error="0" testbench $*

254
test/test → test/base

@ -1,8 +1,5 @@
#!/bin/bash
#!- Library include
. /etc/shlib
#!-
#!/usr/bin/env bash-shlib
# -*- mode: shell-script -*-
include shunit
@ -19,18 +16,22 @@ tprog=$(readlink -f $tprog)
export PATH=".:$PATH"
short_tprog=$(basename "$tprog")
##
## Convenience function
##
function init_test() {
init_test() {
test_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
cd "$test_tmpdir"
export CACHEDIR="$test_tmpdir/.cache"
export VARDIR="$test_tmpdir/.var"
mkdir -p "$CACHEDIR"
}
function tear_test() {
tear_test() {
rm -rf "$test_tmpdir"
}
@ -41,7 +42,7 @@ function tear_test() {
##
# Checking arguments
function test_calling_sourcing {
test_calling_sourcing() {
assert_list <<EOF
@ -60,7 +61,7 @@ EOF
}
function test_mixin_functions {
test_mixin_functions() {
init_test
@ -79,7 +80,14 @@ EOF2
. "$tprog"
_setup_state_dir
test -z "\$(get_docker_compose_mixin_from_metadata testcharm)"
out="\$(get_docker_compose_mixin_from_metadata testcharm)"
expected='\
labels:
- compose.charm=testcharm'
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- volumes
@ -99,11 +107,20 @@ EOF2
. "$tprog"
_setup_state_dir
test "\$(get_docker_compose_mixin_from_metadata testcharm)" == "volumes:
out=\$(get_docker_compose_mixin_from_metadata testcharm) || exit 1
expected="\
labels:
- compose.charm=testcharm
volumes:
- /tmp/DATA/testcharm/a:/a:rw
- /tmp/CONFIG/testcharm/b:/b:rw
- /tmp:/tmp:rw"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- docker-compose
export CHARM_STORE=$test_tmpdir
@ -118,11 +135,15 @@ EOF2
. "$tprog"
_setup_state_dir
out="\$(get_docker_compose_mixin_from_metadata testcharm)"
test "\$out" == "volumes:
- /any:/vol
entrypoint: any" || {
echo -e "** get_docker_compose_mixin_from_metadata testcharm:\n\$out"
out="\$(get_docker_compose_mixin_from_metadata testcharm)" || exit 1
expected="\
entrypoint: any
labels:
- compose.charm=testcharm
volumes:
- /any:/vol"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
@ -137,25 +158,39 @@ EOF2
. "$tprog"
_setup_state_dir
test "\$(get_docker_compose_mixin_from_metadata testcharm)" == "image: toto"
out="\$(get_docker_compose_mixin_from_metadata testcharm)" || exit 1
expected="\
image: toto
labels:
- compose.charm=testcharm"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- build
export CHARM_STORE=$test_tmpdir
mkdir -p $test_tmpdir/testcharm/build
cat <<EOF2 > $test_tmpdir/testcharm/metadata.yml
# XXX new content to invalidate cache
EOF2
. "$tprog"
_setup_state_dir
out="\$(get_docker_compose_mixin_from_metadata testcharm)" || {
echo "Failed"
out="\$(get_docker_compose_mixin_from_metadata testcharm)" || exit 1
expected="\
build: $test_tmpdir/testcharm/build
labels:
- compose.charm=testcharm"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
echo "\$out"
test "\$out" == "build: testcharm/build"
## -- subordinate with image
@ -413,8 +448,7 @@ EOF
}
function test_get_compose_service_def {
test_get_compose_service_def() {
init_test
@ -426,11 +460,17 @@ function test_get_compose_service_def {
export CHARM_STORE=$test_tmpdir
mkdir $test_tmpdir/www
touch $test_tmpdir/www/metadata.yml
. "$tprog"
_setup_state_dir
test "\$(get_compose_service_def www)" == "charm: www"
out="\$(get_compose_service_def www)"
test "\$out" == "charm: www" || {
echo OUTPUT:
echo "\$out"
false
}
## -- Simple (no docker-compose, no charm dir)
@ -488,13 +528,13 @@ EOF
##
##
##
function test_get_master_charm_for_service() {
function test_get_master_service_for_service() {
init_test
assert_list <<EOF
### Testing get_master_charm_for_service
### Testing get_master_service_for_service
## -- Simple (no subordinate)
@ -506,12 +546,13 @@ EOF2
. "$tprog"
_setup_state_dir
test "\$(_get_master_charm_for_service www)" == "www"
test "\$(get_master_service_for_service www)" == "www"
## -- subordinate
export CHARM_STORE=$test_tmpdir
mkdir -p $test_tmpdir/{www,mysql}
touch $test_tmpdir/mysql/metadata.yml
cat <<EOF2 > $test_tmpdir/www/metadata.yml
subordinate: true
requires:
@ -534,7 +575,7 @@ EOF2
_setup_state_dir
COMPOSE_YML_FILE=$test_tmpdir/compose.yml
test "\$(_get_master_charm_for_service www)" == "mysql"
test "\$(get_master_service_for_service www)" == "mysql"
EOF
}
@ -567,11 +608,11 @@ EOF2
. "$tprog"
_setup_state_dir
out=\$(_get_docker_compose_service_mixin www)
test "\$out" == "www:
volumes:
out=\$(_get_docker_compose_service_mixin www | shyaml get-value www.volumes)
[[ "\$out" == "\
- /www/tmp/a:/tmp/a:rw
- /www/tmp/b:/tmp/b:rw" || {
- /www/tmp/b:/tmp/b:rw" ]] || {
echo -e "** _get_docker_compose_service_mixin www:\n\$out"; exit 1
}
@ -587,6 +628,8 @@ config-resources:
- /tmp/b
EOF2
touch $test_tmpdir/mysql/metadata.yml
cat <<EOF2 > $test_tmpdir/compose.yml
www:
charm: www
@ -600,12 +643,22 @@ EOF2
_setup_state_dir
COMPOSE_YML_FILE=$test_tmpdir/compose.yml
test "\$(_get_docker_compose_service_mixin www)" == "www:
out="\$(_get_docker_compose_service_mixin www)" || exit 1
[ "\$out" == "www:
labels:
- compose.service=www
- compose.master-service=www
- compose.project=\$(basename "$test_tmpdir")
- compose.charm=www
links:
- mysql
volumes:
- /www/tmp/a:/tmp/a:rw
- /www/tmp/b:/tmp/b:rw"
- /www/tmp/b:/tmp/b:rw" ] || {
echo -e "OUT:\n\$out"
exit 1
}
## -- compose, subordinate
@ -636,10 +689,20 @@ EOF2
_setup_state_dir
COMPOSE_YML_FILE=$test_tmpdir/compose.yml
test "\$(_get_docker_compose_service_mixin www)" == "mysql:
out="\$(_get_docker_compose_service_mixin www)" || exit 1
expected="mysql:
labels:
- compose.service=www
- compose.master-service=mysql
- compose.project=$(basename "$test_tmpdir")
- compose.charm=www
volumes:
- /www/tmp/a:/tmp/a:rw
- /www/tmp/b:/tmp/b:rw"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
EOF
}
@ -669,8 +732,11 @@ _setup_state_dir
out=\$(get_docker_compose www)
echo "OUT:"
echo "\$out"
test "\$out" == "www:
volumes:
out=\$(echo "\$out" | shyaml get-value services.www.volumes)
echo "OUT volumes:"
echo "\$out"
test "\$out" == "\\
- /www/tmp/a:/tmp/a:rw
- /www/tmp/b:/tmp/b:rw"
@ -709,36 +775,44 @@ COMPOSE_YML_FILE=$test_tmpdir/compose.yml
_setup_state_dir
out=\$(get_docker_compose www)
test "\$out" == "www:
volumes:
out=\$(get_docker_compose www | shyaml get-value services.www.volumes)
test "\$out" == "\\
- /www/tmp/a:/tmp/a:rw
- /www/tmp/b:/tmp/b:rw" || {
echo -e "** get_docker_compose www:\n\$out"
exit 1
}
out=\$(_get_docker_compose_links web_site)
test "\$out" == "www:
links:
- mysql" || {
echo -e "** _get_docker_compose_links web_site:\n\$out"
out=\$(get_docker_compose_links web_site | shyaml get-value web_site.links)
test "\$out" == "- mysql" || {
echo -e "** get_docker_compose_links web_site:\n\$out"
exit 1
}
out=\$(get_docker_compose web_site)
test "\$out" == "\
out=\$(get_docker_compose web_site | shyaml get-value services)
expected="\
mysql:
labels:
- compose.service=mysql
- compose.master-service=mysql
- compose.project=$(basename "$test_tmpdir")
- compose.charm=mysql
volumes:
- /mysql/tmp/c:/tmp/c:rw
- /mysql/tmp/d:/tmp/d:rw
www:
web_site:
labels:
- compose.service=web_site
- compose.master-service=web_site
- compose.project=$(basename "$test_tmpdir")
- compose.charm=www
links:
- mysql
volumes:
- /www/tmp/a:/tmp/a:rw
- /www/tmp/b:/tmp/b:rw" || {
echo -e "** get_docker_compose web_site:\n\$out"
- /web_site/tmp/a:/tmp/a:rw
- /web_site/tmp/b:/tmp/b:rw"
test "\$out" == "\$expected" || {
echo -e "** get_docker_compose web_site:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
@ -786,12 +860,16 @@ _setup_state_dir
! get_docker_compose www || exit 1
# volumes gets mixed
test "\$(get_docker_compose web_site)" == "mysql:
volumes:
- /www/tmp/a:/tmp/a:rw
- /www/tmp/b:/tmp/b:rw
out="\$(get_docker_compose web_site | shyaml get-value services.mysql.volumes)"
test "\$out" == "\
- /web_site/tmp/a:/tmp/a:rw
- /web_site/tmp/b:/tmp/b:rw
- /mysql/tmp/c:/tmp/c:rw
- /mysql/tmp/d:/tmp/d:rw"
- /mysql/tmp/d:/tmp/d:rw" || {
echo -e "OUT:\n\$out"
exit 1
}
## -- subordinate with complex features
@ -844,15 +922,30 @@ _setup_state_dir
#! get_docker_compose www || exit 1
# volumes gets mixed
test "\$(get_docker_compose web_site)" == "mysql:
out="\$(get_docker_compose web_site | shyaml get-value services.mysql)"
expected="\
entrypoint: custom-entrypoint
labels:
- compose.service=web_site
- compose.charm=www
- compose.service=mysql
- compose.master-service=mysql
- compose.project=$(basename "$test_tmpdir")
- compose.charm=mysql
volumes:
- /www/tmp/a:/tmp/a:rw
- /www/tmp/b:/tmp/b:rw
- /web_site/tmp/a:/tmp/a:rw
- /web_site/tmp/b:/tmp/b:rw
- /special-volume-from-www:/special-volume-from-www
- /mysql/tmp/c:/tmp/c:rw
- /mysql/tmp/d:/tmp/d:rw
- /special-volume-from-mysql:/special-volume-from-mysql"
test "\$out" == "\$expected" || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
EOF
tear_test
@ -1032,12 +1125,12 @@ EOF2
COMPOSE_YML_FILE=$test_tmpdir/compose.yml
_setup_state_dir
out=\$(_get_docker_compose_links "app")
out=\$(get_docker_compose_links "app")
test "\$out" == "app:
links:
- www
- mysql" || {
echo -e "** _get_docker_compose_links:\n\$out"; exit 1
echo -e "** get_docker_compose_links:\n\$out"; exit 1
}
@ -1048,7 +1141,7 @@ mkdir -p $test_tmpdir/{www,mysql}
cat <<EOF2 > $test_tmpdir/www/metadata.yml
provides:
web-proxy:
reverse-tech-dep: true
tech-dep: reversed
EOF2
touch $test_tmpdir/mysql/metadata.yml
@ -1066,26 +1159,25 @@ COMPOSE_YML_FILE=$test_tmpdir/compose.yml
_setup_state_dir
out=\$(get_charm_relation_def "www" "web-proxy") || exit 1
test "\$out" == "reverse-tech-dep: true" || {
test "\$out" == "tech-dep: reversed" || {
echo -e "** get_charm_relation_def:\n\$out"; exit 1
}
out=\$(get_charm_reverse_tech_dep_relation "www" "web-proxy")
test "\$out" == "True" || {
echo -e "** get_charm_reverse_tech_dep_relation:\n\$out"; exit 1
out=\$(get_charm_tech_dep_orientation_for_relation "www" "web-proxy")
test "\$out" == "reversed" || {
echo -e "** get_charm_tech_dep_orientation_for_relation:\n\$out"; exit 1
}
out=\$(_get_docker_compose_links "web_site")
test "\$out" == "www:
out=\$(get_docker_compose_links "web_site")
expected="www:
links:
- mysql" || {
echo -e "** _get_docker_compose_links:\n\$out"; exit 1
- web_site"
test "\$out" == "\$expected" || {
echo -e "** get_docker_compose_links:\n\$out\nExpected:\n\$expected"; exit 1
}
out=\$(get_docker_compose web_site)
test "\$out" == "www:
links:
- mysql" || {
out=\$(get_docker_compose web_site | shyaml get-value services.www.links)
test "\$out" == "- web_site" || {
echo -e "** get_docker_compose:\n\$out"; exit 1
}
@ -1144,23 +1236,23 @@ EOF2
assert_list <<EOF
### Testing get_compose_config
### Testing get_compose_config - syntax validation from docker-compose
## -- no service provided (syntax validation from docker-compose)
## -- no service provided
cd "$test_tmpdir"
export DISABLE_SYSTEM_CONFIG_FILE=true
! "$tprog" config
"$tprog" config
## -- simple service provided (syntax validation from docker-compose)
## -- simple service provided
cd "$test_tmpdir"
export DISABLE_SYSTEM_CONFIG_FILE=true
"$tprog" config mysql
## -- complex service provided (syntax validation from docker-compose)
## -- complex service provided
cd "$test_tmpdir"
@ -1170,9 +1262,9 @@ export DISABLE_SYSTEM_CONFIG_FILE=true
EOF
tear_test
}
continue_on_error="0" testbench $*
Loading…
Cancel
Save