run.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/bash
  2. set -eo pipefail
  3. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  4. image="$1"
  5. cliFlags=( -h redis )
  6. testDir="$(readlink -f "$(dirname "$BASH_SOURCE")")"
  7. testName="$(basename "$testDir")"
  8. if [[ "$testName" == *tls* ]]; then
  9. redisCliHelp="$(docker run --rm --entrypoint redis-cli "$image" --help 2>&1 || :)"
  10. if ! grep -q -- '--tls' <<<"$redisCliHelp"; then
  11. echo >&2 "skipping; not built with TLS support (possibly version < 6.0 or 32bit variant)"
  12. exit 0
  13. fi
  14. tlsImage="$("$testDir/../image-name.sh" librarytest/redis-tls "$image")"
  15. "$testDir/../docker-build.sh" "$testDir" "$tlsImage" <<-EOD
  16. FROM alpine:3.11 AS certs
  17. RUN apk add --no-cache openssl
  18. RUN set -eux; \
  19. mkdir /certs; \
  20. openssl genrsa -out /certs/ca-private.key 8192; \
  21. openssl req -new -x509 \
  22. -key /certs/ca-private.key \
  23. -out /certs/ca.crt \
  24. -days $(( 365 * 30 )) \
  25. -subj '/CN=lolca'; \
  26. openssl genrsa -out /certs/private.key 4096; \
  27. openssl req -new -key /certs/private.key \
  28. -out /certs/cert.csr -subj '/CN=redis'; \
  29. openssl x509 -req -in /certs/cert.csr \
  30. -CA /certs/ca.crt -CAkey /certs/ca-private.key -CAcreateserial \
  31. -out /certs/cert.crt -days $(( 365 * 30 )); \
  32. openssl verify -CAfile /certs/ca.crt /certs/cert.crt
  33. FROM $image
  34. COPY --from=certs --chown=redis:redis /certs /certs
  35. CMD [ \
  36. "--tls-port", "6379", "--port", "0", \
  37. "--tls-cert-file", "/certs/cert.crt", \
  38. "--tls-key-file", "/certs/private.key", \
  39. "--tls-ca-cert-file", "/certs/ca.crt" \
  40. ]
  41. EOD
  42. image="$tlsImage"
  43. cliFlags+=(
  44. --tls
  45. --cert /certs/cert.crt
  46. --key /certs/private.key
  47. --cacert /certs/ca.crt
  48. )
  49. fi
  50. cname="redis-container-$RANDOM-$RANDOM"
  51. cid="$(docker run -d --name "$cname" "$image")"
  52. trap "docker rm -vf $cid > /dev/null" EXIT
  53. redis-cli() {
  54. docker run --rm -i \
  55. --link "$cname":redis \
  56. --entrypoint redis-cli \
  57. "$image" \
  58. "${cliFlags[@]}" \
  59. "$@"
  60. }
  61. # http://redis.io/topics/quickstart#check-if-redis-is-working
  62. . "$dir/../../retry.sh" --tries 20 '[ "$(redis-cli ping)" = "PONG" ]'
  63. [ "$(redis-cli set mykey somevalue)" = 'OK' ]
  64. [ "$(redis-cli get mykey)" = 'somevalue' ]