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.

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