#!/bin/bash #!- Library include . /etc/shlib #!- 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 ## function init_test() { test_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX) cd "$test_tmpdir" } function tear_test() { rm -rf "$test_tmpdir" } ## ## Tests ## ## # Checking arguments function test_calling_sourcing { assert_list < $test_tmpdir/testcharm/metadata.yml EOF2 . "$tprog" _setup_state_dir test -z "\$(get_docker_compose_mixin_from_metadata testcharm)" ## -- volumes export CHARM_STORE=$test_tmpdir export CONFIGSTORE=/tmp/CONFIG export DATASTORE=/tmp/DATA mkdir -p $test_tmpdir/testcharm cat < $test_tmpdir/testcharm/metadata.yml data-resources: - /a config-resources: - /b host-resources: - /tmp:/tmp EOF2 . "$tprog" _setup_state_dir test "\$(get_docker_compose_mixin_from_metadata testcharm)" == "volumes: - /tmp/DATA/testcharm/a:/a:rw - /tmp/CONFIG/testcharm/b:/b:rw - /tmp:/tmp:rw" ## -- docker-compose export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/testcharm cat < $test_tmpdir/testcharm/metadata.yml docker-compose: volumes: - /any:/vol entrypoint: any EOF2 . "$tprog" _setup_state_dir test "\$(get_docker_compose_mixin_from_metadata testcharm)" == "entrypoint: any volumes: - /any:/vol" ## -- image export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/testcharm cat < $test_tmpdir/testcharm/metadata.yml docker-image: toto EOF2 . "$tprog" _setup_state_dir test "\$(get_docker_compose_mixin_from_metadata testcharm)" == "image: toto" ## -- build export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/testcharm/build cat < $test_tmpdir/testcharm/metadata.yml EOF2 . "$tprog" _setup_state_dir out="\$(get_docker_compose_mixin_from_metadata testcharm)" || { echo "Failed" exit 1 } echo "\$out" test "\$out" == "build: testcharm/build" ## -- subordinate with image export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/testcharm cat < $test_tmpdir/testcharm/metadata.yml subordinate: true docker-image: toto EOF2 . "$tprog" _setup_state_dir ! get_docker_compose_mixin_from_metadata testcharm ## -- subordinate with build subdir export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/testcharm/build cat < $test_tmpdir/testcharm/metadata.yml subordinate: true EOF2 . "$tprog" _setup_state_dir ! get_docker_compose_mixin_from_metadata testcharm EOF tear_test } function test_merge_yaml { init_test assert_list < $test_tmpdir/compose.yml toto: charm: www blabla: xxx EOF2 . "$tprog" _setup_state_dir COMPOSE_YML_FILE=$test_tmpdir/compose.yml test "\$(get_compose_service_def toto)" == "blabla: xxx charm: www" ## -- Addition of default charm export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/www cat < $test_tmpdir/compose.yml www: blabla: xxx EOF2 . "$tprog" _setup_state_dir COMPOSE_YML_FILE=$test_tmpdir/compose.yml test "\$(get_compose_service_def www)" == "blabla: xxx charm: www" EOF } ## ## ## function test_get_master_charm_for_service() { init_test assert_list < $test_tmpdir/www/metadata.yml EOF2 . "$tprog" _setup_state_dir test "\$(_get_master_charm_for_service www)" == "www" ## -- subordinate export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/{www,mysql} cat < $test_tmpdir/www/metadata.yml subordinate: true requires: a-label-for-relation: interface: a-name-relation scope: container EOF2 cat < $test_tmpdir/compose.yml www: charm: www relations: a-name-relation: mysql: label: value EOF2 . "$tprog" _setup_state_dir COMPOSE_YML_FILE=$test_tmpdir/compose.yml test "\$(_get_master_charm_for_service www)" == "mysql" EOF } ## ## ## function test_get_docker_compose_service_mixin() { init_test assert_list < $test_tmpdir/www/metadata.yml data-resources: - /tmp/a config-resources: - /tmp/b EOF2 . "$tprog" _setup_state_dir out=\$(_get_docker_compose_service_mixin www) test "\$out" == "www: volumes: - /www/tmp/a:/tmp/a:rw - /www/tmp/b:/tmp/b:rw" || { echo -e "** _get_docker_compose_service_mixin www:\n\$out"; exit 1 } ## -- Simple (compose, but no subordinate) export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/{www,mysql} cat < $test_tmpdir/www/metadata.yml data-resources: - /tmp/a config-resources: - /tmp/b EOF2 cat < $test_tmpdir/compose.yml www: charm: www relations: a-name-relation: mysql: label: value EOF2 . "$tprog" _setup_state_dir COMPOSE_YML_FILE=$test_tmpdir/compose.yml test "\$(_get_docker_compose_service_mixin www)" == "www: links: - mysql volumes: - /www/tmp/a:/tmp/a:rw - /www/tmp/b:/tmp/b:rw" ## -- compose, subordinate export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/{www,mysql} cat < $test_tmpdir/www/metadata.yml subordinate: true data-resources: - /tmp/a config-resources: - /tmp/b requires: a-name-relation: interface: a-name-relation scope: container EOF2 cat < $test_tmpdir/compose.yml www: charm: www relations: a-name-relation: mysql: label: value EOF2 . "$tprog" _setup_state_dir COMPOSE_YML_FILE=$test_tmpdir/compose.yml test "\$(_get_docker_compose_service_mixin www)" == "mysql: volumes: - /www/tmp/a:/tmp/a:rw - /www/tmp/b:/tmp/b:rw" EOF } function test_get_docker_compose { init_test assert_list < $test_tmpdir/www/metadata.yml data-resources: - /tmp/a config-resources: - /tmp/b EOF2 . "$tprog" _setup_state_dir out=\$(get_docker_compose www) echo "OUT:" echo "\$out" test "\$out" == "www: volumes: - /www/tmp/a:/tmp/a:rw - /www/tmp/b:/tmp/b:rw" ## -- simple with docker-compose export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/{www,mysql} cat < $test_tmpdir/www/metadata.yml data-resources: - /tmp/a config-resources: - /tmp/b EOF2 cat < $test_tmpdir/mysql/metadata.yml data-resources: - /tmp/c config-resources: - /tmp/d EOF2 cat < $test_tmpdir/compose.yml web_site: charm: www relations: db-connection: mysql: user: toto dbname: tata EOF2 . "$tprog" COMPOSE_YML_FILE=$test_tmpdir/compose.yml _setup_state_dir out=\$(get_docker_compose www) test "\$out" == "www: volumes: - /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" exit 1 } out=\$(get_docker_compose web_site) test "\$out" == "\ mysql: volumes: - /mysql/tmp/c:/tmp/c:rw - /mysql/tmp/d:/tmp/d:rw 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" exit 1 } ## -- subordinate export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/{www,mysql} cat < $test_tmpdir/www/metadata.yml subordinate: true data-resources: - /tmp/a config-resources: - /tmp/b requires: my-db-connection: interface: db-connection scope: container EOF2 cat < $test_tmpdir/mysql/metadata.yml data-resources: - /tmp/c config-resources: - /tmp/d EOF2 cat < $test_tmpdir/compose.yml web_site: charm: www relations: db-connection: mysql: user: toto dbname: tata EOF2 . "$tprog" COMPOSE_YML_FILE=$test_tmpdir/compose.yml _setup_state_dir # should fail because of missing relations ! 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 - /mysql/tmp/c:/tmp/c:rw - /mysql/tmp/d:/tmp/d:rw" ## -- subordinate with complex features export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/{www,mysql} cat < $test_tmpdir/www/metadata.yml subordinate: true data-resources: - /tmp/a config-resources: - /tmp/b requires: my-db-connection: interface: db-connection scope: container docker-compose: volumes: - /special-volume-from-www:/special-volume-from-www EOF2 cat < $test_tmpdir/mysql/metadata.yml data-resources: - /tmp/c config-resources: - /tmp/d docker-compose: entrypoint: custom-entrypoint volumes: - /special-volume-from-mysql:/special-volume-from-mysql EOF2 cat < $test_tmpdir/compose.yml web_site: charm: www relations: db-connection: mysql: user: toto dbname: tata EOF2 . "$tprog" COMPOSE_YML_FILE=$test_tmpdir/compose.yml _setup_state_dir # should fail because of missing relations #! get_docker_compose www || exit 1 # volumes gets mixed test "\$(get_docker_compose web_site)" == "mysql: entrypoint: custom-entrypoint volumes: - /www/tmp/a:/tmp/a:rw - /www/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" EOF tear_test } ## XXXvlab: only broken due to Wrap being used for relations # function test_run_service_relations { # init_test # assert_list < $test_tmpdir/www/metadata.yml # data-resources: # - /tmp/a # config-resources: # - /tmp/b # EOF2 # . "$tprog" # _setup_state_dir # echo "Docker Compose:" # get_docker_compose www # echo "Run Service relations:" # _run_service_relation() { # echo "\$FUNCNAME: received $*" # } # out=\$(run_service_relations www) # test -z "\$out" # ## -- simple with docker-compose # export CHARM_STORE=$test_tmpdir # mkdir -p $test_tmpdir/{www,mysql} # cat < $test_tmpdir/www/metadata.yml # data-resources: # - /tmp/a # config-resources: # - /tmp/b # EOF2 # cat < $test_tmpdir/mysql/metadata.yml # data-resources: # - /tmp/c # config-resources: # - /tmp/d # EOF2 # cat < $test_tmpdir/compose.yml # web_site: # charm: www # relations: # db-connection: # mysql: # user: toto # dbname: tata # EOF2 # . "$tprog" # COMPOSE_YML_FILE=$test_tmpdir/compose.yml # _setup_state_dir # _setup_state_dir # echo "Docker Compose:" # get_docker_compose web_site # echo "Run Service relations:" # _run_service_relation() { # echo "\$FUNCNAME \$2 <-- \$1 --> \$3" # } # export -f _run_service_relation # out=\$(run_service_relations www) # test -z "\$out" || exit 1 # out=\$(run_service_relations web_site) # echo "OUT: \$out" # test "\$out" == "_run_service_relation web_site <-- db-connection --> mysql" # ## -- subordinate # export CHARM_STORE=$test_tmpdir # mkdir -p $test_tmpdir/{www,mysql} # cat < $test_tmpdir/www/metadata.yml # subordinate: true # data-resources: # - /tmp/a # config-resources: # - /tmp/b # requires: # my-db-connection: # interface: db-connection # scope: container # EOF2 # cat < $test_tmpdir/mysql/metadata.yml # data-resources: # - /tmp/c # config-resources: # - /tmp/d # EOF2 # cat < $test_tmpdir/compose.yml # web_site: # charm: www # relations: # db-connection: # mysql: # user: toto # dbname: tata # EOF2 # . "$tprog" # COMPOSE_YML_FILE=$test_tmpdir/compose.yml # _setup_state_dir # echo "Docker Compose:" # get_docker_compose web_site # echo "Run Service relations:" # _run_service_relation() { # echo "\$FUNCNAME \$2 <-- \$1 --> \$3" # } # export -f _run_service_relation # out=\$(run_service_relations www) # test -z "\$out" || exit 1 # out=\$(run_service_relations web_site) # echo "\$out" # test "\$out" == "_run_service_relation web_site <-- db-connection --> mysql" # EOF # tear_test # } function test_get_docker_compose_2() { init_test assert_list < $test_tmpdir/www/metadata.yml provides: web-proxy: reverse-tech-dep: true EOF2 touch $test_tmpdir/mysql/metadata.yml cat < $test_tmpdir/compose.yml web_site: charm: mysql relations: web-proxy: www: user: toto EOF2 . "$tprog" 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" || { 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_docker_compose_links "web_site") test "\$out" == "www: links: - mysql" || { echo -e "** _get_docker_compose_links:\n\$out"; exit 1 } out=\$(get_docker_compose web_site) test "\$out" == "www: links: - mysql" || { echo -e "** get_docker_compose:\n\$out"; exit 1 } EOF tear_test } function test_compose_config { init_test export CHARM_STORE=$test_tmpdir mkdir -p $test_tmpdir/{www,mysql} cat < $test_tmpdir/www/metadata.yml subordinate: true data-resources: - /tmp/a config-resources: - /tmp/b requires: my-db-connection: interface: db-connection scope: container docker-compose: volumes: - /special-volume-from-www:/special-volume-from-www EOF2 cat < $test_tmpdir/mysql/metadata.yml docker-image: docker.0k.io/mysql data-resources: - /tmp/c config-resources: - /tmp/d docker-compose: entrypoint: custom-entrypoint volumes: - /special-volume-from-mysql:/special-volume-from-mysql EOF2 cat < $test_tmpdir/compose.yml web_site: charm: www relations: db-connection: mysql: user: toto dbname: tata EOF2 assert_list <