2011-10-05 23:49:54 +08:00
|
|
|
#!/bin/sh
|
2012-07-13 00:08:21 +08:00
|
|
|
|
|
|
|
# this script prepare database and redis instance to run acceptance test
|
|
|
|
#
|
|
|
|
# NOTE: assumes existance of a "template_postgis" loaded with
|
|
|
|
# compatible version of postgis (legacy.sql included)
|
|
|
|
|
2013-12-18 19:33:03 +08:00
|
|
|
PREPARE_REDIS=yes
|
|
|
|
PREPARE_PGSQL=yes
|
2016-10-14 18:28:43 +08:00
|
|
|
OFFLINE=no
|
2013-12-18 19:33:03 +08:00
|
|
|
|
|
|
|
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
|
2016-10-14 18:28:43 +08:00
|
|
|
elif test "$1" = "--offline"; then
|
|
|
|
OFFLINE=yes
|
|
|
|
shift; continue
|
2013-12-18 19:33:03 +08:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
die() {
|
|
|
|
msg=$1
|
|
|
|
echo "${msg}" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2012-07-13 00:08:21 +08:00
|
|
|
# This is where postgresql connection parameters are read from
|
|
|
|
TESTENV=../config/environments/test.js
|
|
|
|
|
|
|
|
# Extract postgres configuration
|
2013-11-18 20:31:11 +08:00
|
|
|
PGHOST=`node -e "console.log(require('${TESTENV}').db_host || '')"`
|
2012-07-13 00:08:21 +08:00
|
|
|
echo "PGHOST: [$PGHOST]"
|
2013-11-18 20:31:11 +08:00
|
|
|
PGPORT=`node -e "console.log(require('${TESTENV}').db_port || '')"`
|
2012-07-13 00:08:21 +08:00
|
|
|
echo "PGPORT: [$PGPORT]"
|
2013-11-18 20:31:11 +08:00
|
|
|
|
|
|
|
PUBLICUSER=`node -e "console.log(require('${TESTENV}').db_pubuser || 'xxx')"`
|
|
|
|
PUBLICPASS=`node -e "console.log(require('${TESTENV}').db_pubuser_pass || 'xxx')"`
|
|
|
|
echo "PUBLICUSER: [${PUBLICUSER}]"
|
|
|
|
echo "PUBLICPASS: [${PUBLICPASS}]"
|
2011-10-05 23:49:54 +08:00
|
|
|
|
2012-06-02 03:46:38 +08:00
|
|
|
|
2013-11-18 19:21:30 +08:00
|
|
|
TESTUSERID=1
|
2013-11-18 20:31:11 +08:00
|
|
|
|
|
|
|
TESTUSER=`node -e "console.log(require('${TESTENV}').db_user || '')"`
|
|
|
|
if test -z "$TESTUSER"; then
|
|
|
|
echo "Missing db_user from ${TESTENV}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
TESTUSER=`echo ${TESTUSER} | sed "s/<%= user_id %>/${TESTUSERID}/"`
|
|
|
|
echo "TESTUSER: [${TESTUSER}]"
|
|
|
|
|
|
|
|
TESTPASS=`node -e "console.log(require('${TESTENV}').db_user_pass || '')"`
|
|
|
|
TESTPASS=`echo ${TESTPASS} | sed "s/<%= user_id %>/${TESTUSERID}/"`
|
|
|
|
echo "TESTPASS: [${TESTPASS}]"
|
|
|
|
|
|
|
|
TEST_DB=`node -e "console.log(require('${TESTENV}').db_base_name || '')"`
|
|
|
|
if test -z "$TEST_DB"; then
|
|
|
|
echo "Missing db_base_name from ${TESTENV}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
TEST_DB=`echo ${TEST_DB} | sed "s/<%= user_id %>/${TESTUSERID}/"`
|
|
|
|
|
2012-07-13 00:08:21 +08:00
|
|
|
export PGHOST PGPORT
|
|
|
|
|
2013-12-18 19:33:03 +08:00
|
|
|
if test x"$PREPARE_PGSQL" = xyes; then
|
|
|
|
|
|
|
|
echo "preparing postgres..."
|
2016-06-01 16:25:16 +08:00
|
|
|
echo "PostgreSQL server version: `psql -A -t -c 'select version()'`"
|
2018-07-20 00:38:17 +08:00
|
|
|
echo "PAUSE; RESUME;" | psql -p 6432 pgbouncer # make sure there are no connections pgbouncer -> test_db
|
2014-09-24 23:23:56 +08:00
|
|
|
dropdb ${TEST_DB} # 2> /dev/null # error expected if doesn't exist, but not otherwise
|
|
|
|
createdb -Ttemplate_postgis -EUTF8 ${TEST_DB} || die "Could not create test database"
|
2016-06-01 16:52:54 +08:00
|
|
|
psql -c 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";' ${TEST_DB}
|
|
|
|
psql -c "CREATE EXTENSION IF NOT EXISTS plpythonu;" ${TEST_DB}
|
|
|
|
|
2018-07-23 21:29:53 +08:00
|
|
|
LOCAL_SQL_SCRIPTS='test populated_places_simple_reduced py_sleep quota_mock'
|
2016-06-22 22:38:30 +08:00
|
|
|
REMOTE_SQL_SCRIPTS='CDB_QueryStatements CDB_QueryTables CDB_CartodbfyTable CDB_TableMetadata CDB_ForeignTable CDB_UserTables CDB_ColumnNames CDB_ZoomFromScale CDB_OverviewsSupport CDB_Overviews'
|
2016-06-01 16:52:54 +08:00
|
|
|
|
2016-10-14 18:28:43 +08:00
|
|
|
if test x"$OFFLINE" = xno; then
|
|
|
|
CURL_ARGS=""
|
|
|
|
for i in ${REMOTE_SQL_SCRIPTS}
|
|
|
|
do
|
|
|
|
CURL_ARGS="${CURL_ARGS}\"https://github.com/CartoDB/cartodb-postgresql/raw/master/scripts-available/$i.sql\" -o support/sql/$i.sql "
|
|
|
|
done
|
|
|
|
echo "Downloading and updating: ${REMOTE_SQL_SCRIPTS}"
|
|
|
|
echo ${CURL_ARGS} | xargs curl -L -s
|
|
|
|
fi
|
2016-06-01 16:52:54 +08:00
|
|
|
|
2017-11-14 18:59:48 +08:00
|
|
|
PG_PARALLEL=$(pg_config --version | (awk '{$2*=1000; if ($2 >= 9600) print 1; else print 0;}' 2> /dev/null || echo 0))
|
|
|
|
|
2016-06-01 16:52:54 +08:00
|
|
|
psql -c "CREATE EXTENSION IF NOT EXISTS plpythonu;" ${TEST_DB}
|
|
|
|
ALL_SQL_SCRIPTS="${REMOTE_SQL_SCRIPTS} ${LOCAL_SQL_SCRIPTS}"
|
|
|
|
for i in ${ALL_SQL_SCRIPTS}
|
2016-02-22 19:21:44 +08:00
|
|
|
do
|
2017-11-14 18:59:48 +08:00
|
|
|
# Strip PARALLEL labels for PostgreSQL releases before 9.6
|
|
|
|
if [ $PG_PARALLEL -eq 0 ]; then
|
|
|
|
TMPFILE=$(mktemp /tmp/$(basename $0).XXXXXXXX)
|
|
|
|
sed -e 's/PARALLEL \= [A-Z]*,/''/g' \
|
|
|
|
-e 's/PARALLEL [A-Z]*/''/g' support/sql/${i}.sql > $TMPFILE
|
|
|
|
mv $TMPFILE support/sql/${i}.sql
|
|
|
|
fi
|
2016-06-01 16:52:54 +08:00
|
|
|
cat support/sql/${i}.sql |
|
|
|
|
sed -e 's/cartodb\./public./g' -e "s/''cartodb''/''public''/g" |
|
|
|
|
sed "s/:PUBLICUSER/${PUBLICUSER}/" |
|
|
|
|
sed "s/:PUBLICPASS/${PUBLICPASS}/" |
|
|
|
|
sed "s/:TESTUSER/${TESTUSER}/" |
|
|
|
|
sed "s/:TESTPASS/${TESTPASS}/" |
|
2018-07-18 21:46:59 +08:00
|
|
|
psql -q -v ON_ERROR_STOP=1 ${TEST_DB} > /dev/null || exit 1
|
2016-02-22 19:21:44 +08:00
|
|
|
done
|
2013-12-18 19:33:03 +08:00
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test x"$PREPARE_REDIS" = xyes; then
|
|
|
|
|
2016-11-30 06:02:20 +08:00
|
|
|
REDIS_HOST=`node -e "console.log(require('${TESTENV}').redis_host || '127.0.0.1')"`
|
2013-12-18 19:33:03 +08:00
|
|
|
REDIS_PORT=`node -e "console.log(require('${TESTENV}').redis_port || '6336')"`
|
|
|
|
|
|
|
|
echo "preparing redis..."
|
|
|
|
|
2014-07-04 22:47:59 +08:00
|
|
|
# delete previous publicuser
|
2016-11-30 06:02:20 +08:00
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
2014-07-04 23:01:58 +08:00
|
|
|
HDEL rails:users:vizzuality database_host
|
2014-07-04 22:47:59 +08:00
|
|
|
HDEL rails:users:vizzuality database_publicuser
|
|
|
|
EOF
|
|
|
|
|
2016-11-30 06:02:20 +08:00
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
2013-12-18 19:33:03 +08:00
|
|
|
HMSET rails:users:vizzuality \
|
|
|
|
id 1 \
|
|
|
|
database_name ${TEST_DB} \
|
2016-11-30 06:02:20 +08:00
|
|
|
database_host ${PGHOST} \
|
2015-12-07 16:40:51 +08:00
|
|
|
map_key 1234
|
2013-12-18 19:33:03 +08:00
|
|
|
SADD rails:users:vizzuality:map_key 1235
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# A user configured as with cartodb-2.5.0+
|
2016-11-30 06:02:20 +08:00
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
2013-12-18 19:33:03 +08:00
|
|
|
HMSET rails:users:cartodb250user \
|
|
|
|
id ${TESTUSERID} \
|
|
|
|
database_name ${TEST_DB} \
|
2016-11-30 06:02:20 +08:00
|
|
|
database_host ${PGHOST} \
|
2013-12-18 19:33:03 +08:00
|
|
|
database_password ${TESTPASS} \
|
|
|
|
map_key 1234
|
2017-11-25 00:57:54 +08:00
|
|
|
SADD rails:users:cartodb250user:map_key 1234
|
2013-12-18 19:33:03 +08:00
|
|
|
EOF
|
|
|
|
|
2016-11-30 06:02:20 +08:00
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 3
|
2013-12-18 19:33:03 +08:00
|
|
|
HMSET rails:oauth_access_tokens:l0lPbtP68ao8NfStCiA3V3neqfM03JKhToxhUQTR \
|
|
|
|
consumer_key fZeNGv5iYayvItgDYHUbot1Ukb5rVyX6QAg8GaY2 \
|
|
|
|
consumer_secret IBLCvPEefxbIiGZhGlakYV4eM8AbVSwsHxwEYpzx \
|
|
|
|
access_token_token l0lPbtP68ao8NfStCiA3V3neqfM03JKhToxhUQTR \
|
|
|
|
access_token_secret 22zBIek567fMDEebzfnSdGe8peMFVFqAreOENaDK \
|
|
|
|
user_id 1 \
|
|
|
|
time sometime
|
|
|
|
EOF
|
|
|
|
|
2018-02-14 23:22:36 +08:00
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET rails:users:cartofante \
|
|
|
|
id 2 \
|
|
|
|
database_name ${TEST_DB} \
|
|
|
|
database_host ${PGHOST} \
|
|
|
|
database_password test_cartodb_user_2_pass \
|
|
|
|
map_key 4321
|
|
|
|
SADD rails:users:fallback_1:map_key 4321
|
|
|
|
EOF
|
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
# delete previous jobs
|
2016-11-30 06:02:20 +08:00
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
2015-12-29 22:46:04 +08:00
|
|
|
EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 batch:jobs:*
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# delete job queue
|
2016-11-30 06:02:20 +08:00
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
2015-12-29 22:46:04 +08:00
|
|
|
DEL batch:queues:localhost
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# delete user index
|
2016-11-30 06:02:20 +08:00
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
2015-12-29 22:46:04 +08:00
|
|
|
DEL batch:users:vizzuality
|
2015-12-10 03:17:45 +08:00
|
|
|
EOF
|
2011-10-05 23:49:54 +08:00
|
|
|
|
2017-11-25 00:57:54 +08:00
|
|
|
# User: vizzuality
|
|
|
|
|
|
|
|
# API Key Default public
|
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
2018-02-12 23:41:00 +08:00
|
|
|
HMSET api_keys:vizzuality:default_public \
|
2017-11-25 00:57:54 +08:00
|
|
|
user "vizzuality" \
|
|
|
|
type "default" \
|
|
|
|
grants_sql "true" \
|
2018-05-28 21:54:48 +08:00
|
|
|
database_role "testpublicuser" \
|
2018-02-12 23:41:00 +08:00
|
|
|
database_password "public"
|
2017-11-25 00:57:54 +08:00
|
|
|
EOF
|
|
|
|
|
|
|
|
# API Key Master
|
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET api_keys:vizzuality:1234 \
|
|
|
|
user "vizzuality" \
|
|
|
|
type "master" \
|
|
|
|
grants_sql "true" \
|
2018-02-12 23:41:00 +08:00
|
|
|
database_role "${TESTUSER}" \
|
|
|
|
database_password "${TESTPASS}"
|
2017-11-25 00:57:54 +08:00
|
|
|
EOF
|
|
|
|
|
2018-02-14 00:53:27 +08:00
|
|
|
# API Key Regular1
|
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET api_keys:vizzuality:regular1 \
|
|
|
|
user "vizzuality" \
|
|
|
|
type "regular" \
|
|
|
|
grants_sql "true" \
|
|
|
|
database_role "regular_1" \
|
|
|
|
database_password "regular1"
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# API Key Regular1
|
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET api_keys:vizzuality:regular2 \
|
|
|
|
user "vizzuality" \
|
|
|
|
type "regular" \
|
|
|
|
grants_sql "true" \
|
|
|
|
database_role "regular_2" \
|
|
|
|
database_password "regular2"
|
|
|
|
EOF
|
|
|
|
|
2017-11-25 00:57:54 +08:00
|
|
|
# User: cartodb250user
|
|
|
|
|
2017-11-24 00:39:28 +08:00
|
|
|
# API Key Default public
|
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
2018-02-12 23:41:00 +08:00
|
|
|
HMSET api_keys:cartodb250user:default_public \
|
2017-11-25 00:57:54 +08:00
|
|
|
user "cartodb250user" \
|
2017-11-24 00:39:28 +08:00
|
|
|
type "default" \
|
2017-11-25 00:57:54 +08:00
|
|
|
grants_sql "true" \
|
2018-05-28 21:54:48 +08:00
|
|
|
database_role "testpublicuser" \
|
2018-02-12 23:41:00 +08:00
|
|
|
database_password "public"
|
2017-11-24 00:39:28 +08:00
|
|
|
EOF
|
|
|
|
|
|
|
|
# API Key Master
|
|
|
|
cat <<EOF | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5
|
2017-11-25 00:57:54 +08:00
|
|
|
HMSET api_keys:cartodb250user:1234 \
|
|
|
|
user "cartodb250user" \
|
2017-11-24 00:39:28 +08:00
|
|
|
type "master" \
|
2017-11-25 00:57:54 +08:00
|
|
|
grants_sql "true" \
|
2018-02-12 23:41:00 +08:00
|
|
|
database_role "${TESTUSER}" \
|
|
|
|
database_password "${TESTPASS}"
|
2017-11-24 00:39:28 +08:00
|
|
|
EOF
|
|
|
|
|
2015-12-10 03:17:45 +08:00
|
|
|
fi
|
2012-06-02 04:00:45 +08:00
|
|
|
|
2011-10-05 23:49:54 +08:00
|
|
|
echo "ok, you can run test now"
|