|
| 1 | +#! /bin/sh |
| 2 | + |
| 3 | +# Do some "integration" testing against running PostgreSQL server. |
| 4 | + |
| 5 | +# This file is to be sourced. |
| 6 | + |
| 7 | +: ${PGTESTS_DATADIR=`pwd`/datadir} |
| 8 | +: ${PGTESTS_ADMIN=`id -u -n`} |
| 9 | +: ${PGTESTS_ADMINDB=$PGTESTS_ADMIN} |
| 10 | +: ${PGTESTS_ADMINPASS=$PGTESTS_ADMIN} |
| 11 | +: ${PGTESTS_PORT=54321} |
| 12 | +: ${PGTESTS_SOCKETDIR=/tmp} |
| 13 | +: ${PGTESTS_USERS=test:test} |
| 14 | +: ${PGTESTS_DATABASES=test:test} |
| 15 | + |
| 16 | +# Stop the old cluster and/or remove it's data. |
| 17 | +: ${PGTESTS_STARTCLEANUP=:} |
| 18 | + |
| 19 | +# Cleanup once we exit the script. |
| 20 | +: ${PGTESTS_CLEANUP=:} |
| 21 | + |
| 22 | +# Cleanup once we exit the script. |
| 23 | +: ${PGTESTS_CLEANUP=:} |
| 24 | + |
| 25 | +export PGPORT=$PGTESTS_PORT |
| 26 | +export PGHOST=$PGTESTS_SOCKETDIR |
| 27 | + |
| 28 | +warning () |
| 29 | +{ |
| 30 | + echo >&2 " ! $*" |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +__trap_cb () |
| 35 | +{ |
| 36 | + IFS=' ' |
| 37 | + for __func in $__TRAP_ACTIONS |
| 38 | + do |
| 39 | + $__func |
| 40 | + done |
| 41 | +} |
| 42 | +trap __trap_cb EXIT |
| 43 | + |
| 44 | + |
| 45 | +__pgtests_initdb () |
| 46 | +{ |
| 47 | + initdb "$PGTESTS_DATADIR" -U "$PGTESTS_ADMIN" \ |
| 48 | + --auth-local=peer --auth-host=md5 \ |
| 49 | + ${PGTESTS_LOCALE+--locale="$PGTESTS_LOCALE"} |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +__pgtests_start () |
| 54 | +{ |
| 55 | + pg_ctl -D "$PGTESTS_DATADIR" -l "$PGTESTS_DATADIR"/start.log start -o "-k $PGTESTS_SOCKETDIR -p $PGTESTS_PORT" -w |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +__pgtests_create_admins_db () |
| 60 | +{ |
| 61 | + createdb -h "$PGTESTS_SOCKETDIR" "$PGTESTS_ADMINDB" --owner "$PGTESTS_ADMIN" -p "$PGTESTS_PORT" |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +__pgtests_passwd() |
| 66 | +{ |
| 67 | + psql -d postgres --set=user="$1" --set=pass="$2" -tA \ |
| 68 | + <<<"ALTER USER :\"user\" WITH ENCRYPTED PASSWORD :'pass';" |
| 69 | +} |
| 70 | +
|
| 71 | +pgtests_start () |
| 72 | +{ |
| 73 | + unset __TRAP_ACTIONS |
| 74 | +
|
| 75 | + if $PGTESTS_STARTCLEANUP; then |
| 76 | + # We don't plan to be serious here. This pgtests_* effort is just to |
| 77 | + # ease _testing_ against running postgresql server without too much |
| 78 | + # writing. |
| 79 | + if test -f "$PGTESTS_DATADIR"/postmaster.pid; then |
| 80 | + # Give it a try. |
| 81 | + warning "Seems like server works, trying to stop." |
| 82 | + pg_ctl stop -D "$PGTESTS_DATADIR" -w |
| 83 | + fi |
| 84 | +
|
| 85 | + # Cleanup testing directory |
| 86 | + if test -e "$PGTESTS_DATADIR"; then |
| 87 | + warning "Removing old data directory." |
| 88 | + rm -r "$PGTESTS_DATADIR" |
| 89 | + fi |
| 90 | + fi |
| 91 | +
|
| 92 | + __pgtests_initdb && __TRAP_ACTIONS="pgtests_cleanup $__TRAP_ACTIONS" |
| 93 | + __pgtests_start && __TRAP_ACTIONS="pgtests_stop $__TRAP_ACTIONS" |
| 94 | + __pgtests_create_admins_db |
| 95 | +
|
| 96 | + __pgtests_passwd "$PGTESTS_ADMIN" "$PGTESTS_ADMINPASS" |
| 97 | +
|
| 98 | +
|
| 99 | + for _pgt_user in $PGTESTS_USERS |
| 100 | + do |
| 101 | + save_IFS=$IFS |
| 102 | + IFS=: |
| 103 | + _user= |
| 104 | + _pass= |
| 105 | + for _part in $_pgt_user |
| 106 | + do |
| 107 | + if test -z "$_user"; then |
| 108 | + _user=$_part |
| 109 | + else |
| 110 | + _pass=$_part |
| 111 | + fi |
| 112 | + done |
| 113 | +
|
| 114 | + createuser "$_user" |
| 115 | + __pgtests_passwd "$_user" "$_pass" |
| 116 | + IFS=$save_IFS |
| 117 | + done |
| 118 | +
|
| 119 | +
|
| 120 | + for _pgt_db in $PGTESTS_DATABASES |
| 121 | + do |
| 122 | + save_IFS=$IFS |
| 123 | + IFS=: |
| 124 | + _db= |
| 125 | + _user= |
| 126 | + for _part in $_pgt_db |
| 127 | + do |
| 128 | + if test -z "$_user"; then |
| 129 | + _user=$_part |
| 130 | + else |
| 131 | + _db=$_part |
| 132 | + fi |
| 133 | + done |
| 134 | +
|
| 135 | + createdb "$_db" --owner "$_part" |
| 136 | +
|
| 137 | + IFS=$save_IFS |
| 138 | + done |
| 139 | +} |
| 140 | +
|
| 141 | +
|
| 142 | +__clean_trap_action () |
| 143 | +{ |
| 144 | + __new_actions= |
| 145 | + for __action in $__TRAP_ACTIONS |
| 146 | + do |
| 147 | + if test "$__action" = "$1"; then |
| 148 | + : |
| 149 | + else |
| 150 | + __new_actions="$__action $__new_actions" |
| 151 | + fi |
| 152 | + done |
| 153 | +
|
| 154 | + __TRAP_ACTIONS=$__new_actions |
| 155 | +} |
| 156 | +
|
| 157 | +
|
| 158 | +pgtests_cleanup () |
| 159 | +{ |
| 160 | + if $PGTESTS_CLEANUP && $PGTESTS_AUTOSTOP; then |
| 161 | + rm -r "$PGTESTS_DATADIR" |
| 162 | + fi |
| 163 | + __clean_trap_action pgtests_cleanup |
| 164 | +} |
| 165 | +
|
| 166 | +
|
| 167 | +pgtests_stop () |
| 168 | +{ |
| 169 | + if $PGTESTS_AUTOSTOP; then |
| 170 | + pg_ctl stop -D "$PGTESTS_DATADIR" -w |
| 171 | + fi |
| 172 | + __clean_trap_action pgtests_stop |
| 173 | +} |
0 commit comments