You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
4.5 KiB

  1. #!/usr/bin/env bash-shlib
  2. # -*- mode: shell-script -*-
  3. include shunit
  4. depends sed grep git mkdir readlink
  5. export -f matches
  6. export grep
  7. tmp=/tmp
  8. tprog="../bin/compose-core"
  9. tprog=$(readlink -f $tprog)
  10. export PATH=".:$PATH"
  11. short_tprog=$(basename "$tprog")
  12. ##
  13. ## Convenience function
  14. ##
  15. init_test() {
  16. test_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  17. cd "$test_tmpdir"
  18. export CACHEDIR="$test_tmpdir/.cache"
  19. export VARDIR="$test_tmpdir/.var"
  20. export COMPOSE_DISABLE_DOCKER_COMPOSE_STORE="1"
  21. mkdir -p "$CACHEDIR"
  22. }
  23. tear_test() {
  24. rm -rf "$test_tmpdir"
  25. }
  26. ##
  27. ## Tests
  28. ##
  29. function test_compose_run_args {
  30. init_test
  31. export CHARM_STORE=$test_tmpdir
  32. mkdir -p $test_tmpdir/{www,mysql}
  33. cat <<EOF2 > $test_tmpdir/www/metadata.yml
  34. EOF2
  35. cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
  36. EOF2
  37. cat <<EOF2 > $test_tmpdir/compose.yml
  38. web_site:
  39. charm: www
  40. EOF2
  41. export DISABLE_SYSTEM_CONFIG_FILE=true
  42. assert_list <<EOF
  43. ### Testing args passing to docker-compose
  44. ## -- simple action
  45. cd "$test_tmpdir"
  46. out=\$("$tprog" --dry-compose-run run web_site 2>&1 >/dev/null )
  47. expected="docker-compose run web_site"
  48. [ "\$out" == "\$expected" ] || {
  49. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  50. exit 1
  51. }
  52. ## -- simple single dash arg
  53. cd "$test_tmpdir"
  54. out=\$("$tprog" --dry-compose-run run -T web_site 2>&1 >/dev/null )
  55. expected="docker-compose run -T web_site"
  56. [ "\$out" == "\$expected" ] || {
  57. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  58. exit 1
  59. }
  60. ## -- desaggregation of combined single char args
  61. cd "$test_tmpdir"
  62. out=\$("$tprog" --dry-compose-run logs -ft --tail 20 web_site 2>&1 >/dev/null)
  63. expected="docker-compose logs -f -t --tail 20 web_site"
  64. [ "\$out" == "\$expected" ] || {
  65. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  66. exit 1
  67. }
  68. ## -- desaggregation of combined single char option and valued option char
  69. cd "$test_tmpdir"
  70. out=\$("$tprog" --dry-compose-run run -Tv x:y web_site 2>&1 >/dev/null)
  71. expected="docker-compose run -T -v x:y web_site"
  72. [ "\$out" == "\$expected" ] || {
  73. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  74. exit 1
  75. }
  76. ## -- simple unexpected single dash arg
  77. cd "$test_tmpdir"
  78. out=\$("$tprog" --dry-compose-run run -Z web_site 2>&1)
  79. expected_reg="Unknown option '-Z'"
  80. [[ "\$out" =~ \$expected_reg ]] || {
  81. echo -e "Can't find '\$expected_reg' in out:\n\$out"
  82. exit 1
  83. }
  84. ## -- simple unexpected single dash arg after expected one
  85. cd "$test_tmpdir"
  86. out=\$("$tprog" --dry-compose-run run -T -Z web_site 2>&1)
  87. expected_reg="Unknown option '-Z'"
  88. [[ "\$out" =~ \$expected_reg ]] || {
  89. echo -e "Can't find '\$expected_reg' in out:\n\$out"
  90. exit 1
  91. }
  92. ## -- simple unexpected single dash arg after expected aggregated one
  93. cd "$test_tmpdir"
  94. out=\$("$tprog" --dry-compose-run run -TZ web_site 2>&1)
  95. expected_reg="Unknown option '-Z'"
  96. [[ "\$out" =~ \$expected_reg ]] || {
  97. echo -e "Can't find '\$expected_reg' in out:\n\$out"
  98. exit 1
  99. }
  100. ## -- multiple services
  101. cd "$test_tmpdir"
  102. out=\$("$tprog" --dry-compose-run logs --tail 15 web_site mysql 2>&1 >/dev/null)
  103. expected="docker-compose logs --tail 15 web_site mysql"
  104. [ "\$out" == "\$expected" ] || {
  105. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  106. exit 1
  107. }
  108. ## -- single services
  109. cd "$test_tmpdir"
  110. out=\$("$tprog" --dry-compose-run run web_site mysql 2>&1 >/dev/null)
  111. expected="docker-compose run web_site mysql"
  112. [ "\$out" == "\$expected" ] || {
  113. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  114. exit 1
  115. }
  116. EOF
  117. tear_test
  118. }
  119. function test_filter_opts {
  120. src=$(cat <<'EOF'
  121. -d, --detach
  122. --name NAME
  123. --entrypoint CMD
  124. -e KEY=VAL
  125. -l, --label KEY=VAL
  126. -u, --user=""
  127. --no-deps
  128. --rm
  129. -p, --publish=[]
  130. --service-ports
  131. --use-aliases
  132. -v, --volume=[]
  133. -T
  134. -w, --workdir=""
  135. EOF
  136. )
  137. export src
  138. assert_list <<EOF
  139. ### Testing filtering opts
  140. ## -- multi_opts_filter should find opts with args
  141. . "$tprog"
  142. out=\$(echo "\$src" | multi_opts_filter | tr " " "\n") || exit 12
  143. expected="--name
  144. --entrypoint
  145. -e
  146. -l
  147. --label
  148. -u
  149. --user
  150. -p
  151. --publish
  152. -v
  153. --volume
  154. -w
  155. --workdir"
  156. [ "\$out" == "\$expected" ] || {
  157. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  158. exit 1
  159. }
  160. ## -- single_opts_filter should find opts with args
  161. . "$tprog"
  162. out=\$(echo "\$src" | single_opts_filter | tr " " "\n") || exit 12
  163. expected="-d
  164. --detach
  165. --no-deps
  166. --rm
  167. --service-ports
  168. --use-aliases
  169. -T"
  170. [ "\$out" == "\$expected" ] || {
  171. echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
  172. exit 1
  173. }
  174. EOF
  175. }
  176. continue_on_error="0" testbench $*