run.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/bin/bash
  2. set -eo pipefail
  3. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  4. image="$1"
  5. serverImage="$("$dir/../image-name.sh" librarytest/mysql-initdb "$image")"
  6. "$dir/../docker-build.sh" "$dir" "$serverImage" <<EOD
  7. FROM $image
  8. COPY dir/initdb.sql /docker-entrypoint-initdb.d/
  9. EOD
  10. export MYSQL_ROOT_PASSWORD='this is an example test password'
  11. export MYSQL_USER='0123456789012345' # "ERROR: 1470 String 'my cool mysql user' is too long for user name (should be no longer than 16)"
  12. export MYSQL_PASSWORD='my cool mysql password'
  13. export MYSQL_DATABASE='my cool mysql database'
  14. cname="mysql-container-$RANDOM-$RANDOM"
  15. cid="$(
  16. docker run -d \
  17. -e MYSQL_ROOT_PASSWORD \
  18. -e MYSQL_USER \
  19. -e MYSQL_PASSWORD \
  20. -e MYSQL_DATABASE \
  21. --name "$cname" \
  22. "$serverImage"
  23. )"
  24. trap "docker rm -vf $cid > /dev/null" EXIT
  25. mysql() {
  26. docker run --rm -i \
  27. --link "$cname":mysql \
  28. --entrypoint sh \
  29. -e MYSQL_PWD="$MYSQL_PASSWORD" \
  30. "$image" \
  31. -euc 'if command -v mariadb > /dev/null; then exec mariadb "$@"; else exec mysql "$@"; fi' -- \
  32. -hmysql \
  33. -u"$MYSQL_USER" \
  34. --silent \
  35. "$@" \
  36. "$MYSQL_DATABASE"
  37. }
  38. . "$dir/../../retry.sh" --tries 30 "echo 'SELECT 1' | mysql"
  39. [ "$(echo 'SELECT COUNT(*) FROM test' | mysql)" = 1 ]
  40. [ "$(echo 'SELECT c FROM test' | mysql)" = 'goodbye!' ]