2011-10-20 22:44:37 +08:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# this script prepare database and redis instance to run accpetance test
|
2012-07-04 01:31:30 +08:00
|
|
|
#
|
|
|
|
# NOTE: assumes existance of a "template_postgis"
|
|
|
|
# NOTE2: use PG* environment variables to control who and where
|
2012-07-09 21:50:23 +08:00
|
|
|
#
|
|
|
|
# NOTE3: a side effect of the db preparation is the persistent creation
|
|
|
|
# of two database roles which will be valid for the whole cluster
|
|
|
|
# TODO: fix that
|
|
|
|
#
|
2012-07-04 01:31:30 +08:00
|
|
|
|
2013-12-05 18:07:12 +08:00
|
|
|
PREPARE_REDIS=yes
|
|
|
|
PREPARE_PGSQL=yes
|
2016-05-31 22:57:28 +08:00
|
|
|
DOWNLOAD_SQL_FILES=yes
|
2017-11-13 21:36:14 +08:00
|
|
|
PG_PARALLEL=$(pg_config --version | (awk '{$2*=1000; if ($2 >= 9600) print 1; else print 0;}' 2> /dev/null || echo 0))
|
2013-12-05 18:07:12 +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-05-31 22:57:28 +08:00
|
|
|
elif test "$1" = "--no-sql-download"; then
|
|
|
|
DOWNLOAD_SQL_FILES=no
|
|
|
|
shift; continue
|
2013-12-05 18:07:12 +08:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2012-07-04 01:31:30 +08:00
|
|
|
die() {
|
|
|
|
msg=$1
|
|
|
|
echo "${msg}" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
2011-10-20 22:44:37 +08:00
|
|
|
|
2013-11-11 07:50:03 +08:00
|
|
|
# This is where postgresql connection parameters are read from
|
|
|
|
TESTENV=../../config/environments/test.js
|
|
|
|
if [ \! -r ${TESTENV} ]; then
|
|
|
|
echo "Cannot read ${TESTENV}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
TESTUSERID=1
|
|
|
|
|
|
|
|
TESTUSER=`node -e "console.log(require('${TESTENV}').postgres_auth_user || '')"`
|
|
|
|
if test -z "$TESTUSER"; then
|
|
|
|
echo "Missing postgres_auth_user from ${TESTENV}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
TESTUSER=`echo ${TESTUSER} | sed "s/<%= user_id %>/${TESTUSERID}/"`
|
|
|
|
|
|
|
|
TESTPASS=`node -e "console.log(require('${TESTENV}').postgres_auth_pass || 'test')"`
|
|
|
|
# TODO: should postgres_auth_pass be optional ?
|
|
|
|
if test -z "$TESTPASS"; then
|
|
|
|
echo "Missing postgres_auth_pass from ${TESTENV}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
TESTPASS=`echo ${TESTPASS} | sed "s/<%= user_id %>/${TESTUSERID}/"`
|
|
|
|
|
|
|
|
TEST_DB="${TESTUSER}_db"
|
|
|
|
|
2013-12-05 18:07:12 +08:00
|
|
|
# NOTE: will be set by caller trough environment
|
2013-09-12 16:17:02 +08:00
|
|
|
if test -z "$REDIS_PORT"; then REDIS_PORT=6333; fi
|
2011-10-20 22:44:37 +08:00
|
|
|
|
2013-11-11 07:50:03 +08:00
|
|
|
PUBLICUSER=`node -e "console.log(require('${TESTENV}').postgres.user || 'xxx')"`
|
|
|
|
PUBLICPASS=`node -e "console.log(require('${TESTENV}').postgres.password || 'xxx')"`
|
|
|
|
echo "PUBLICUSER: ${PUBLICUSER}"
|
|
|
|
echo "PUBLICPASS: ${PUBLICPASS}"
|
2013-11-13 00:29:57 +08:00
|
|
|
echo "TESTUSER: ${TESTUSER}"
|
|
|
|
echo "TESTPASS: ${TESTPASS}"
|
2013-11-11 07:50:03 +08:00
|
|
|
|
2013-12-05 18:07:12 +08:00
|
|
|
if test x"$PREPARE_PGSQL" = xyes; then
|
|
|
|
|
|
|
|
echo "preparing postgres..."
|
|
|
|
dropdb "${TEST_DB}"
|
|
|
|
createdb -Ttemplate_postgis -EUTF8 "${TEST_DB}" || die "Could not create test database"
|
|
|
|
|
2018-04-11 18:03:06 +08:00
|
|
|
LOCAL_SQL_SCRIPTS='analysis_catalog windshaft.test gadm4 countries_null_values ported/populated_places_simple_reduced cdb_analysis_check cdb_invalidate_varnish'
|
2017-12-13 23:34:36 +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 CDB_QuantileBins CDB_JenksBins CDB_HeadsTailsBins CDB_EqualIntervalBins CDB_Hexagon CDB_XYZ CDB_EstimateRowCount CDB_RectangleGrid'
|
2016-05-31 22:46:57 +08:00
|
|
|
|
|
|
|
CURL_ARGS=""
|
2016-05-31 22:42:42 +08:00
|
|
|
for i in ${REMOTE_SQL_SCRIPTS}
|
2016-02-15 19:47:29 +08:00
|
|
|
do
|
2016-05-31 22:46:57 +08:00
|
|
|
CURL_ARGS="${CURL_ARGS}\"https://github.com/CartoDB/cartodb-postgresql/raw/master/scripts-available/$i.sql\" -o sql/$i.sql "
|
2016-02-15 19:47:29 +08:00
|
|
|
done
|
2016-05-31 22:57:28 +08:00
|
|
|
if test x"$DOWNLOAD_SQL_FILES" = xyes; then
|
|
|
|
echo "Downloading and updating: ${REMOTE_SQL_SCRIPTS}"
|
|
|
|
echo ${CURL_ARGS} | xargs curl -L -s
|
|
|
|
fi
|
2016-05-31 22:42:42 +08:00
|
|
|
|
2016-06-01 00:45:43 +08:00
|
|
|
psql -c "CREATE EXTENSION IF NOT EXISTS plpythonu;" ${TEST_DB}
|
2016-05-31 22:42:42 +08:00
|
|
|
ALL_SQL_SCRIPTS="${REMOTE_SQL_SCRIPTS} ${LOCAL_SQL_SCRIPTS}"
|
|
|
|
for i in ${ALL_SQL_SCRIPTS}
|
|
|
|
do
|
2017-11-13 21:36:14 +08:00
|
|
|
# Strip PARALLEL labels for PostgreSQL releases before 9.6
|
|
|
|
if [ $PG_PARALLEL -eq 0 ]; then
|
2017-11-14 23:01:57 +08:00
|
|
|
TMPFILE=$(mktemp /tmp/$(basename $0).XXXXXXXX)
|
2017-11-13 21:36:14 +08:00
|
|
|
sed -e 's/PARALLEL \= [A-Z]*,/''/g' \
|
2017-11-14 23:01:57 +08:00
|
|
|
-e 's/PARALLEL [A-Z]*/''/g' sql/$i.sql > $TMPFILE
|
|
|
|
mv $TMPFILE sql/$i.sql
|
2017-12-13 23:34:36 +08:00
|
|
|
fi
|
2016-05-31 22:42:42 +08:00
|
|
|
cat 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}/" |
|
|
|
|
PGOPTIONS='--client-min-messages=WARNING' psql -q -v ON_ERROR_STOP=1 ${TEST_DB} > /dev/null || exit 1
|
|
|
|
done
|
2013-12-05 18:07:12 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
if test x"$PREPARE_REDIS" = xyes; then
|
|
|
|
|
|
|
|
echo "preparing redis..."
|
2013-12-10 01:28:49 +08:00
|
|
|
|
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET rails:users:localhost id ${TESTUSERID} \
|
2014-08-29 22:48:28 +08:00
|
|
|
database_name "${TEST_DB}" \
|
2014-07-04 17:47:44 +08:00
|
|
|
database_host localhost \
|
2013-12-10 01:28:49 +08:00
|
|
|
map_key 1234
|
|
|
|
SADD rails:users:localhost:map_key 1235
|
|
|
|
EOF
|
2013-12-05 18:07:12 +08:00
|
|
|
|
2016-01-29 02:44:25 +08:00
|
|
|
# A user configured as with cartodb-2.5.0+
|
2013-12-10 01:28:49 +08:00
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET rails:users:cartodb250user id ${TESTUSERID} \
|
|
|
|
database_name "${TEST_DB}" \
|
|
|
|
database_host "localhost" \
|
|
|
|
database_password "${TESTPASS}" \
|
|
|
|
map_key 4321
|
|
|
|
EOF
|
|
|
|
|
2018-02-15 00:31:05 +08:00
|
|
|
|
|
|
|
# Remove this block when Auth fallback is not used anymore
|
|
|
|
# AUTH_FALLBACK
|
|
|
|
# A user to test auth fallback to no api keys mode
|
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET rails:users:user_previous_to_project_auth id ${TESTUSERID} \
|
|
|
|
database_name "${TEST_DB}" \
|
|
|
|
database_host "localhost" \
|
|
|
|
database_password "${TESTPASS}" \
|
|
|
|
database_publicuser "${PUBLICUSER}"\
|
|
|
|
map_key 4444
|
|
|
|
EOF
|
|
|
|
|
2013-12-10 01:28:49 +08:00
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 0
|
|
|
|
HSET rails:${TEST_DB}:my_table infowindow "this, that, the other"
|
|
|
|
HSET rails:${TEST_DB}:test_table_private_1 privacy "0"
|
|
|
|
EOF
|
2013-12-05 18:07:12 +08:00
|
|
|
|
|
|
|
fi
|
2012-07-10 00:56:09 +08:00
|
|
|
|
2018-02-08 21:49:42 +08:00
|
|
|
# API keys ==============================
|
|
|
|
|
|
|
|
# User localhost -----------------------
|
|
|
|
|
2018-02-07 18:10:50 +08:00
|
|
|
# API Key Master
|
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET api_keys:localhost:1234 \
|
|
|
|
user "localhost" \
|
|
|
|
type "master" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "${TESTUSER}" \
|
|
|
|
database_password "${TESTPASS}"
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# API Key Default public
|
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET api_keys:localhost:default_public \
|
|
|
|
user "localhost" \
|
|
|
|
type "default" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "test_windshaft_publicuser" \
|
|
|
|
database_password "public"
|
|
|
|
EOF
|
|
|
|
|
2018-02-08 19:34:24 +08:00
|
|
|
# API Key Regular
|
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET api_keys:localhost:regular1 \
|
|
|
|
user "localhost" \
|
|
|
|
type "regular" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "test_windshaft_regular1" \
|
|
|
|
database_password "regular1"
|
|
|
|
EOF
|
|
|
|
|
2018-02-08 20:07:25 +08:00
|
|
|
# API Key Regular 2 no Maps API access, only to check grants permissions to the API
|
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET api_keys:localhost:regular2 \
|
|
|
|
user "localhost" \
|
|
|
|
type "regular" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "false" \
|
|
|
|
database_role "test_windshaft_publicuser" \
|
|
|
|
database_password "public"
|
|
|
|
EOF
|
|
|
|
|
2018-02-08 21:49:42 +08:00
|
|
|
# User cartodb250user -----------------------
|
|
|
|
|
|
|
|
# API Key Master
|
2018-02-08 18:13:21 +08:00
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET api_keys:cartodb250user:4321 \
|
|
|
|
user "localhost" \
|
|
|
|
type "master" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "${TESTUSER}" \
|
|
|
|
database_password "${TESTPASS}"
|
|
|
|
EOF
|
|
|
|
|
2018-02-08 21:49:42 +08:00
|
|
|
# API Key Default
|
2018-02-08 18:13:21 +08:00
|
|
|
cat <<EOF | redis-cli -p ${REDIS_PORT} -n 5
|
|
|
|
HMSET api_keys:cartodb250user:default_public \
|
|
|
|
user "localhost" \
|
|
|
|
type "default" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "test_windshaft_publicuser" \
|
|
|
|
database_password "public"
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
2012-12-22 00:32:00 +08:00
|
|
|
echo "Finished preparing data. Ready to run tests"
|