From dac89b30cb3bb662f7579e4cea7df6769f2a2449 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Thu, 5 Dec 2013 11:07:12 +0100 Subject: [PATCH] Allow requesting run_test.sh to prepare redis but not postgresql Adds --nocreate-pg, --nocreate-redis, --nodrop-pg, --nodrop-redis NOTE that dropping pg is still unimplemented --- run_tests.sh | 56 +++++++++++++++++++++++++------ test/support/prepare_db.sh | 68 +++++++++++++++++++++++++------------- 2 files changed, 91 insertions(+), 33 deletions(-) diff --git a/run_tests.sh b/run_tests.sh index a13760ef..888060d5 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,7 +1,9 @@ #!/bin/sh -OPT_CREATE=yes # create the test environment -OPT_DROP=yes # drop the test environment +OPT_CREATE_REDIS=yes # create the redis test environment +OPT_CREATE_PGSQL=yes # create the PostgreSQL test environment +OPT_DROP_REDIS=yes # drop the redis test environment +OPT_DROP_PGSQL=yes # drop the PostgreSQL test environment cd $(dirname $0) BASEDIR=$(pwd) @@ -11,7 +13,7 @@ REDIS_PORT=`node -e "console.log(require('${BASEDIR}/config/environments/test.js export REDIS_PORT cleanup() { - if test x"$OPT_DROP" = xyes; then + if test x"$OPT_DROP_REDIS" = xyes; then if test x"$PID_REDIS" = x; then PID_REDIS=$(cat ${BASEDIR}/redis.pid) if test x"$PID_REDIS" = x; then @@ -19,9 +21,13 @@ cleanup() { return; fi fi - echo "Cleaning up" + echo "Killing test redis pid ${PID_REDIS}" kill ${PID_REDIS} fi + if test x"$OPT_DROP_PGSQL" = xyes; then + # TODO: drop postgresql ? + echo "Dropping PostgreSQL test database isn't implemented yet" + fi } cleanup_and_exit() { @@ -39,12 +45,32 @@ die() { trap 'cleanup_and_exit' 1 2 3 5 9 13 while [ -n "$1" ]; do + # This is kept for backward compatibility if test "$1" = "--nodrop"; then - OPT_DROP=no + OPT_DROP_REDIS=no + OPT_DROP_PGSQL=no shift continue + elif test "$1" = "--nodrop-pg"; then + OPT_DROP_PGSQL=no + shift + continue + elif test "$1" = "--nodrop-redis"; then + OPT_DROP_REDIS=no + shift + continue + elif test "$1" = "--nocreate-pg"; then + OPT_CREATE_PGSQL=no + shift + continue + elif test "$1" = "--nocreate-redis"; then + OPT_CREATE_REDIS=no + shift + continue + # This is kept for backward compatibility elif test "$1" = "--nocreate"; then - OPT_CREATE=no + OPT_CREATE_REDIS=no + OPT_CREATE_PGSQL=no shift continue else @@ -62,16 +88,26 @@ fi TESTS=$@ -if test x"$OPT_CREATE" = xyes; then +if test x"$OPT_CREATE_REDIS" = xyes; then echo "Starting redis on port ${REDIS_PORT}" echo "port ${REDIS_PORT}" | redis-server - > ${BASEDIR}/test.log & PID_REDIS=$! echo ${PID_REDIS} > ${BASEDIR}/redis.pid - - echo "Preparing the environment" - cd ${BASEDIR}/test/support; sh prepare_db.sh || die "database preparation failure"; cd - fi +PREPARE_DB_OPTS= +if test x"$OPT_CREATE_PGSQL" != xyes; then + PREPARE_DB_OPTS="$PREPARE_DB_OPTS --skip-pg" +fi +if test x"$OPT_CREATE_REDIS" != xyes; then + PREPARE_DB_OPTS="$PREPARE_DB_OPTS --skip-redis" +fi + +echo "Preparing the environment" +cd ${BASEDIR}/test/support +sh prepare_db.sh ${PREPARE_DB_OPTS} || die "database preparation failure" +cd - + PATH=node_modules/.bin/:$PATH echo "Running tests" diff --git a/test/support/prepare_db.sh b/test/support/prepare_db.sh index 40be5d92..fcc5b2fa 100755 --- a/test/support/prepare_db.sh +++ b/test/support/prepare_db.sh @@ -10,6 +10,19 @@ # TODO: fix that # +PREPARE_REDIS=yes +PREPARE_PGSQL=yes + +while [ -n "$1" ]; do + if test "$1" = "--skip-pg"; then + PREPARE_PGSQL=no + shift; continue + elif test "$1" = "--skip-redis"; then + PREPARE_REDIS=no + shift; continue + fi +done + die() { msg=$1 echo "${msg}" >&2 @@ -42,12 +55,9 @@ TESTPASS=`echo ${TESTPASS} | sed "s/<%= user_id %>/${TESTUSERID}/"` TEST_DB="${TESTUSER}_db" +# NOTE: will be set by caller trough environment if test -z "$REDIS_PORT"; then REDIS_PORT=6333; fi -echo "preparing postgres..." -dropdb "${TEST_DB}" -createdb -Ttemplate_postgis -EUTF8 "${TEST_DB}" || die "Could not create test database" - PUBLICUSER=`node -e "console.log(require('${TESTENV}').postgres.user || 'xxx')"` PUBLICPASS=`node -e "console.log(require('${TESTENV}').postgres.password || 'xxx')"` echo "PUBLICUSER: ${PUBLICUSER}" @@ -55,28 +65,40 @@ echo "PUBLICPASS: ${PUBLICPASS}" echo "TESTUSER: ${TESTUSER}" echo "TESTPASS: ${TESTPASS}" -cat sql/windshaft.test.sql sql/gadm4.sql | - sed "s/:PUBLICUSER/${PUBLICUSER}/" | - sed "s/:PUBLICPASS/${PUBLICPASS}/" | - sed "s/:TESTUSER/${TESTUSER}/" | - sed "s/:TESTPASS/${TESTPASS}/" | - psql ${TEST_DB} +if test x"$PREPARE_PGSQL" = xyes; then -echo "preparing redis..." -echo "HSET rails:users:localhost id ${TESTUSERID}" | redis-cli -p ${REDIS_PORT} -n 5 -echo 'HSET rails:users:localhost database_name "'"${TEST_DB}"'"' | redis-cli -p ${REDIS_PORT} -n 5 -echo "HSET rails:users:localhost map_key 1234" | redis-cli -p ${REDIS_PORT} -n 5 -echo "SADD rails:users:localhost:map_key 1235" | redis-cli -p ${REDIS_PORT} -n 5 + echo "preparing postgres..." + dropdb "${TEST_DB}" + createdb -Ttemplate_postgis -EUTF8 "${TEST_DB}" || die "Could not create test database" -# A user configured as with cartodb-2.5.0+ -echo "HSET rails:users:cartodb250user id ${TESTUSERID}" | redis-cli -p ${REDIS_PORT} -n 5 -echo 'HSET rails:users:cartodb250user database_name "'${TEST_DB}'"' | redis-cli -p ${REDIS_PORT} -n 5 -echo 'HSET rails:users:cartodb250user database_host "localhost"' | redis-cli -p ${REDIS_PORT} -n 5 -echo 'HSET rails:users:cartodb250user database_password "'${TESTPASS}'"' | redis-cli -p ${REDIS_PORT} -n 5 -echo "HSET rails:users:cartodb250user map_key 4321" | redis-cli -p ${REDIS_PORT} -n 5 + cat sql/windshaft.test.sql sql/gadm4.sql | + sed "s/:PUBLICUSER/${PUBLICUSER}/" | + sed "s/:PUBLICPASS/${PUBLICPASS}/" | + sed "s/:TESTUSER/${TESTUSER}/" | + sed "s/:TESTPASS/${TESTPASS}/" | + psql ${TEST_DB} -echo 'HSET rails:'"${TEST_DB}"':my_table infowindow "this, that, the other"' | redis-cli -p ${REDIS_PORT} -n 0 -echo 'HSET rails:'"${TEST_DB}"':test_table_private_1 privacy "0"' | redis-cli -p ${REDIS_PORT} -n 0 +fi + +if test x"$PREPARE_REDIS" = xyes; then + + echo "preparing redis..." + echo "HSET rails:users:localhost id ${TESTUSERID}" | redis-cli -p ${REDIS_PORT} -n 5 + echo 'HSET rails:users:localhost database_name "'"${TEST_DB}"'"' | redis-cli -p ${REDIS_PORT} -n 5 + echo "HSET rails:users:localhost map_key 1234" | redis-cli -p ${REDIS_PORT} -n 5 + echo "SADD rails:users:localhost:map_key 1235" | redis-cli -p ${REDIS_PORT} -n 5 + + # A user configured as with cartodb-2.5.0+ + echo "HSET rails:users:cartodb250user id ${TESTUSERID}" | redis-cli -p ${REDIS_PORT} -n 5 + echo 'HSET rails:users:cartodb250user database_name "'${TEST_DB}'"' | redis-cli -p ${REDIS_PORT} -n 5 + echo 'HSET rails:users:cartodb250user database_host "localhost"' | redis-cli -p ${REDIS_PORT} -n 5 + echo 'HSET rails:users:cartodb250user database_password "'${TESTPASS}'"' | redis-cli -p ${REDIS_PORT} -n 5 + echo "HSET rails:users:cartodb250user map_key 4321" | redis-cli -p ${REDIS_PORT} -n 5 + + echo 'HSET rails:'"${TEST_DB}"':my_table infowindow "this, that, the other"' | redis-cli -p ${REDIS_PORT} -n 0 + echo 'HSET rails:'"${TEST_DB}"':test_table_private_1 privacy "0"' | redis-cli -p ${REDIS_PORT} -n 0 + +fi echo "Finished preparing data. Ready to run tests"