summaryrefslogtreecommitdiffstats
path: root/testsuite
blob: 22b934aadbe6ac0a83517e25f35f78450172edd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash

# Run integration tests (on the installed packages).
#
# (C) 2005-2012 Martin Pitt <mpitt@debian.org>
# (C) 2012-2024 Christoph Berg <myon@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

set -eu

# default config
TESTSDIR="$(dirname $0)/t"
: ${PG_UMASKS="077"} # default umask(s) to try

help ()
{
    echo "postgresql-common testsuite"
    echo "Syntax: $0 [options] [test ...]"
    echo "    -D               drop all existing clusters (USE WITH CARE)"
    echo "    -f number        run tests starting at this number"
    echo "    -F suffix        flavor suffix on package names, e.g. 'ee'"
    echo "    -i               install packages for versions specified by -v"
    echo "    -M               don't mount tmpfses, run in the host system"
    echo "    -s               start a shell in the testbed on failure"
    echo "    -u 'umask ...'   umasks to run testsuite with [default: 077]"
    echo "    -v 'version ...' PostgreSQL versions to test [default: client versions installed]"
    echo "    -V               debug output"
    exit ${1:-0}
}

# option parsing
while getopts "Dhf:F:iMsu:v:V" opt ; do
    case $opt in
        D) DROP_ALL_CLUSTERS=1 ;;
        f) FROM="$OPTARG" ;;
        F) export PG_FLAVOR="$OPTARG" ;;
        i) INSTALL=1 ;;
        h) help ;;
        M) export NO_TMPFS=1 ;;
        s) export FAILURE="shell" ;;
        u) PG_UMASKS="$OPTARG" ;;
        v) export PG_VERSIONS="$OPTARG" ;; # used in t/TestLib.pm
        V) VERBOSE=1 ;;
        *) help 1 ;;
    esac
done

if [ "$(id -u)" != 0 ]; then
    echo "Error: this test suite needs to be run as root" >&2
    exit 1
fi

# install packages for versions specified by -v
# needs network for apt, so run before unshare
if [ "${INSTALL:-}" ] && [ -z "${UNSHARED:-}" ]; then
    . /etc/os-release
    case $ID in
        debian|ubuntu)
            for v in $PG_VERSIONS; do
                case $v in 8.*|9.*|10|11)
                    [ "$(perl -I. -le 'use PgCommon; print $PgCommon::have_python2')" = "1" ] && PYTHON2_PACKAGE=postgresql-plpython-$v${PG_FLAVOR:=}
                    ;;
                esac
                case $v in
                    8.*|9.0) ;;
                    9.[1-6]) # plpython3 only for Python < 3.12
                        py3ver=$(apt-cache policy python3)
                        case $py3ver in
                            *3.[0-9].*|*3.1[01].*) PYTHON3_PACKAGE=postgresql-plpython3-$v${PG_FLAVOR:-} ;;
                        esac
                        ;;
                    *) PYTHON3_PACKAGE=postgresql-plpython3-$v${PG_FLAVOR:-} ;;
                esac
                apt-get install -y \
                    postgresql-contrib-$v${PG_FLAVOR:=} \
                    postgresql-plperl-$v$PG_FLAVOR \
                    ${PYTHON2_PACKAGE:-} \
                    ${PYTHON3_PACKAGE:-} \
                    postgresql-pltcl-$v$PG_FLAVOR \
                    postgresql-server-dev-$v$PG_FLAVOR \
                    postgresql-doc-$v$PG_FLAVOR
            done
            apt-get install -y \
                debhelper \
                libecpg-dev \
                locales-all \
                procps systemd \
                netcat-openbsd \
                hunspell-en-us \
                gcc
        ;;
        redhat|centos)
            for v in $PG_VERSIONS; do
                vv=$(echo $v | tr -d .)
                yum install -y \
                    postgresql$vv${PG_FLAVOR:=}-contrib \
                    postgresql$vv$PG_FLAVOR-plperl \
                    postgresql$vv$PG_FLAVOR-plpython3 \
                    postgresql$vv$PG_FLAVOR-pltcl \
                    postgresql$vv$PG_FLAVOR-devel \
                    postgresql$vv$PG_FLAVOR-doc
            done
            yum install -y \
                nmap-ncat \
                perl-Test-Simple perl-Time-HiRes \
                gcc
        ;;
        *)
            echo "Unknown distribution ID $ID in /etc/os-release"
            exit 1
        ;;
    esac
fi

# shift away args
shift $(($OPTIND - 1))

# stop currently running clusters
if [ -d /run/systemd/system ] ; then
    # stop postgresql@* explicitly (#759725)
    systemctl stop postgresql "postgresql@*" "pg_receivewal@*"
else
    service postgresql stop
fi

# drop all existing clusters if the user requested it
if [ "${DROP_ALL_CLUSTERS:-}" ]; then
    pg_lsclusters -h | while read ver cluster rest; do
        pg_dropcluster $ver $cluster
    done
fi

# set up test directories
dirs="/etc/postgresql /var/lib/postgresql /var/log/postgresql /var/run/postgresql /var/backups/postgresql"

if [ -z "${NO_TMPFS:-}" ]; then
    # clean up after us
    cleanup () {
        set +e
        umount -l $dirs
        sed -i -e '/# by pg-testsuite/d' /etc/postgresql-common/createcluster.conf
        ./pg_updateaptconfig
        systemctl daemon-reload 2> /dev/null || : # poke generator to handle the system's clusters again
        exit
    }
    trap "cleanup" 0 HUP INT QUIT ILL ABRT PIPE TERM

    for d in $dirs; do
        mkdir -p $d
        mount -t tmpfs tmpfs $d
    done

    chown postgres:postgres     /etc/postgresql ; chmod  755     /etc/postgresql
    chown postgres:postgres /var/lib/postgresql ; chmod  755 /var/lib/postgresql
    chown     root:postgres /var/log/postgresql ; chmod 1775 /var/log/postgresql
    chown postgres:postgres /var/run/postgresql ; chmod 2775 /var/run/postgresql /var/backups/postgresql
fi

if [ -d /run/systemd/system ]; then
    systemctl daemon-reload # poke generator to forget the system's clusters
fi

# the RPM packages enable the logging_collector by default, which the testsuite
# doesn't like. We disable it unconditionally here.
if ! grep -q logging_collector /etc/postgresql-common/createcluster.conf; then
    echo "logging_collector = off # by pg-testsuite" >> /etc/postgresql-common/createcluster.conf
fi

# reset core limit for pg_ctl tests
ulimit -S -c 0

# set environment
unset TMPDIR
unset LC_ALL
export LANG=en_US.utf8

# set variables which cause taint check errors
export IFS
export CDPATH=/usr
export ENV=/nonexisting
export BASH_ENV=/nonexisting

if [ $# -eq 0 ]; then
    set -- $TESTSDIR/*.t
fi

# dump environment for debugging
if [ "${VERBOSE:-}" ]; then
    echo "Environment:"
    env | sort
    echo "Mounts:"
    cat /proc/mounts
    echo "Namespaces:"
    ls -l /proc/self/ns
    echo
fi

for U in $PG_UMASKS; do
    echo "====== Running all tests with umask $U ======="
    umask $U
    for T; do
        TBASE=${T##*/}
        [ "${FROM:-}" ] && [ "${TBASE%%_*}" -lt "${FROM:-}" ] && continue
        echo "### PostgreSQL test $TBASE ###"
        perl -I. $T || {
            EXIT=$?
            FAILED_TESTS="${FAILED_TESTS:-} $T"
            if [ "${FAILURE:-}" ]; then
                echo "*** $TBASE failed with status $EXIT, dropping you into a shell in the testbed ***"
                ${SHELL:-/bin/sh}
            fi
        }
        echo "### End test $TBASE ###"
        echo
    done
done

if [ "${FAILED_TESTS:-}" ]; then
    echo "Failed tests: $FAILED_TESTS"
    echo
fi

exit ${EXIT:-0}