test.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh -ue
  2. REDIS_SERVER=${REDIS_SERVER:-redis-server}
  3. REDIS_PORT=${REDIS_PORT:-56379}
  4. REDIS_SSL_PORT=${REDIS_SSL_PORT:-56443}
  5. TEST_SSL=${TEST_SSL:-0}
  6. SKIPS_AS_FAILS=${SKIPS_AS_FAILS-:0}
  7. SSL_TEST_ARGS=
  8. SKIPS_ARG=
  9. tmpdir=$(mktemp -d)
  10. PID_FILE=${tmpdir}/hiredis-test-redis.pid
  11. SOCK_FILE=${tmpdir}/hiredis-test-redis.sock
  12. if [ "$TEST_SSL" = "1" ]; then
  13. SSL_CA_CERT=${tmpdir}/ca.crt
  14. SSL_CA_KEY=${tmpdir}/ca.key
  15. SSL_CERT=${tmpdir}/redis.crt
  16. SSL_KEY=${tmpdir}/redis.key
  17. openssl genrsa -out ${tmpdir}/ca.key 4096
  18. openssl req \
  19. -x509 -new -nodes -sha256 \
  20. -key ${SSL_CA_KEY} \
  21. -days 3650 \
  22. -subj '/CN=Hiredis Test CA' \
  23. -out ${SSL_CA_CERT}
  24. openssl genrsa -out ${SSL_KEY} 2048
  25. openssl req \
  26. -new -sha256 \
  27. -key ${SSL_KEY} \
  28. -subj '/CN=Hiredis Test Cert' | \
  29. openssl x509 \
  30. -req -sha256 \
  31. -CA ${SSL_CA_CERT} \
  32. -CAkey ${SSL_CA_KEY} \
  33. -CAserial ${tmpdir}/ca.txt \
  34. -CAcreateserial \
  35. -days 365 \
  36. -out ${SSL_CERT}
  37. SSL_TEST_ARGS="--ssl-host 127.0.0.1 --ssl-port ${REDIS_SSL_PORT} --ssl-ca-cert ${SSL_CA_CERT} --ssl-cert ${SSL_CERT} --ssl-key ${SSL_KEY}"
  38. fi
  39. cleanup() {
  40. set +e
  41. kill $(cat ${PID_FILE})
  42. rm -rf ${tmpdir}
  43. }
  44. trap cleanup INT TERM EXIT
  45. cat > ${tmpdir}/redis.conf <<EOF
  46. daemonize yes
  47. pidfile ${PID_FILE}
  48. port ${REDIS_PORT}
  49. bind 127.0.0.1
  50. unixsocket ${SOCK_FILE}
  51. EOF
  52. if [ "$TEST_SSL" = "1" ]; then
  53. cat >> ${tmpdir}/redis.conf <<EOF
  54. tls-port ${REDIS_SSL_PORT}
  55. tls-ca-cert-file ${SSL_CA_CERT}
  56. tls-cert-file ${SSL_CERT}
  57. tls-key-file ${SSL_KEY}
  58. EOF
  59. fi
  60. cat ${tmpdir}/redis.conf
  61. ${REDIS_SERVER} ${tmpdir}/redis.conf
  62. # Wait until we detect the unix socket
  63. while [ ! -S "${SOCK_FILE}" ]; do sleep 1; done
  64. # Treat skips as failures if directed
  65. [ "$SKIPS_AS_FAILS" = 1 ] && SKIPS_ARG="--skips-as-fails"
  66. ${TEST_PREFIX:-} ./hiredis-test -h 127.0.0.1 -p ${REDIS_PORT} -s ${SOCK_FILE} ${SSL_TEST_ARGS} ${SKIPS_ARG}