030_errors.t 14.3 KB
Newer Older
1
# Check all kinds of error conditions.
Martin Pitt's avatar
Martin Pitt committed
2 3 4

use strict; 

5 6
require File::Temp;

Martin Pitt's avatar
Martin Pitt committed
7 8
use lib 't';
use TestLib;
Evgeny's avatar
Evgeny committed
9
use Test::More tests => 147;
Martin Pitt's avatar
Martin Pitt committed
10 11
use PgCommon;

12
my $version = $MAJORS[-1];
13

Martin Pitt's avatar
Martin Pitt committed
14
my $socketdir = '/tmp/postgresql-testsuite/';
15 16
my ($pg_uid, $pg_gid) = (getpwnam 'postgres')[2,3];

17 18 19 20 21
# create a pid file with content $1 and return its path
sub create_pidfile {
    my $fname = "/var/lib/postgresql/$version/main/postmaster.pid";
    open F, ">$fname" or die "open: $!";
    print F $_[0];
22
    close F;
23 24 25
    chown $pg_uid, $pg_gid, $fname or die "chown: $!";
    chmod 0700, $fname or die "chmod: $!";
    return $fname;
26
}
Martin Pitt's avatar
Martin Pitt committed
27

28 29 30 31
sub check_nonexisting_cluster_error {
    my $outref;
    my $result = exec_as 0, $_[0], $outref;
    is $result, 1, "'$_[0]' fails";
32
    like $$outref, qr/(invalid version|does not exist)/i, "$_[0] gives error message about nonexisting cluster";
33 34 35
    unlike $$outref, qr/invalid symbolic link/i, "$_[0] does not print 'invalid symbolic link' gibberish";
}

Martin Pitt's avatar
Martin Pitt committed
36
# create cluster
37
ok ((system "pg_createcluster --socketdir '$socketdir' $version main >/dev/null") == 0,
Martin Pitt's avatar
Martin Pitt committed
38
    "pg_createcluster --socketdir");
39
like_program_out 'postgres', 'pg_lsclusters -h', 0, qr/$version\s*main.*5432.*down/, 'cluster was created';
Martin Pitt's avatar
Martin Pitt committed
40

41 42
is ((get_cluster_port $version, 'main'), 5432, 'Port of created cluster is 5432');

43 44 45 46 47 48
# creating cluster with the same name should fail
like_program_out 'root', "pg_createcluster --socketdir '$socketdir' $version main", 1, qr/already exists/,
    "pg_createcluster on existing cluster";
# and the original one still exists
like_program_out 'postgres', 'pg_lsclusters -h', 0, qr/$version\s*main.*5432.*down/, 'original cluster still exists';

49 50 51 52 53 54 55 56
# attempt to create clusters with an invalid port
like_program_out 0, "pg_createcluster $version test -p foo", 1,
    qr/invalid.*number expected/,
    'pg_createcluster -p checks that port option is numeric';
like_program_out 0, "pg_createcluster $version test -p 42", 1,
    qr/must be a positive integer between/,
    'pg_createcluster -p checks valid port range';

Martin Pitt's avatar
Martin Pitt committed
57
# chown cluster to an invalid user to test error
58 59
my $badid = 98;
(system "chown -R $badid /var/lib/postgresql/$version/main") == 0 or die "chown failed: $!";
60
is ((system "pg_ctlcluster $version main start 2>/dev/null"), 256,
Martin Pitt's avatar
Martin Pitt committed
61
    'pg_ctlcluster fails on invalid cluster owner uid');
62
(system "chown -R postgres:$badid /var/lib/postgresql/$version/main") == 0 or die "chown failed: $!";
63
is ((system "pg_ctlcluster $version main start 2>/dev/null"), 256,
Martin Pitt's avatar
Martin Pitt committed
64
    'pg_ctlcluster as root fails on invalid cluster owner gid');
65
is ((exec_as 'postgres', "pg_ctlcluster $version main start"), 1,
Martin Pitt's avatar
Martin Pitt committed
66
    'pg_ctlcluster as postgres fails on invalid cluster owner gid');
67
(system "chown -R postgres:postgres /var/lib/postgresql/$version/main") == 0 or die "chown failed: $!";
68
is ((system "pg_ctlcluster $version main start"), 0,
Martin Pitt's avatar
Martin Pitt committed
69 70 71
    'pg_ctlcluster succeeds on valid cluster owner uid/gid');

# check socket
72 73 74
my @contents = ('.s.PGSQL.5432', '.s.PGSQL.5432.lock', "$version-main.pid", "$version-main.pg_stat_tmp");
pop @contents if ($version < 8.4); # remove pg_stat_tmp
ok_dir '/var/run/postgresql', [grep {/main/} @contents], 'No sockets in /var/run/postgresql';
Martin Pitt's avatar
Martin Pitt committed
75 76
ok_dir $socketdir, ['.s.PGSQL.5432', '.s.PGSQL.5432.lock'], "Socket is in $socketdir";

77
# stop cluster, check sockets
78
ok ((system "pg_ctlcluster $version main stop") == 0,
79
    'cluster stops with custom unix_socket_dir');
80 81 82 83 84 85 86 87 88
ok_dir $socketdir, [], "No sockets in $socketdir after stopping cluster";

# remove default socket dir and check that the socket defaults to
# /var/run/postgresql
open F, "+</etc/postgresql/$version/main/postgresql.conf" or
    die "could not open postgresql.conf for r/w: $!";
my @lines = <F>;
seek F, 0, 0 or die "seek: $!";
truncate F, 0;
89
@lines = grep !/^unix_socket_dir/, @lines; # <= 9.2: "_directory", >= 9.3: "_directories"
90 91 92
print F @lines;
close F;

93
ok ((system "pg_ctlcluster $version main start") == 0,
94
    'cluster starts after removing unix_socket_dir');
95 96 97
if ($PgCommon::rpm) {
    ok ((grep { $_ eq '.s.PGSQL.5432' } @{TestLib::dircontent('/tmp')}) == 1, 'Socket is in /tmp');
} else {
98
    ok_dir '/var/run/postgresql', [@contents],
99 100
        'Socket is in default dir /var/run/postgresql';
}
101 102
ok_dir $socketdir, [], "No sockets in $socketdir";

103 104 105
# server should not stop with corrupt file
rename "/var/lib/postgresql/$version/main/postmaster.pid",
    "/var/lib/postgresql/$version/main/postmaster.pid.orig" or die "rename: $!";
106
create_pidfile 'foo';
107 108 109 110
is_program_out 'postgres', "pg_ctlcluster $version main stop", 1, 
    "Error: pid file is invalid, please manually kill the stale server process.\n",
    'pg_ctlcluster fails with corrupted PID file';
like_program_out 'postgres', 'pg_lsclusters -h', 0, qr/online/, 'cluster is still online';
111 112 113 114 115

# restore PID file
(system "cp /var/lib/postgresql/$version/main/postmaster.pid.orig /var/lib/postgresql/$version/main/postmaster.pid") == 0 or die "cp: $!";
is ((exec_as 'postgres', "pg_ctlcluster $version main stop"), 0, 
    'pg_ctlcluster succeeds with restored PID file');
116
like_program_out 'postgres', 'pg_lsclusters -h', 0, qr/down/, 'cluster is down';
117 118

# stop stopped server
119 120
is_program_out 'postgres', "pg_ctlcluster $version main stop", 2,
    "Cluster is not running.\n", 'pg_ctlcluster stop fails on stopped cluster';
121 122 123 124

# simulate crashed server
rename "/var/lib/postgresql/$version/main/postmaster.pid.orig",
    "/var/lib/postgresql/$version/main/postmaster.pid" or die "rename: $!";
125 126 127
is_program_out 'postgres', "pg_ctlcluster $version main start", 0, 
    "Removed stale pid file.\n", 'pg_ctlcluster succeeds with already existing PID file';
like_program_out 'postgres', 'pg_lsclusters -h', 0, qr/online/, 'cluster is online';
128 129
is ((exec_as 'postgres', "pg_ctlcluster $version main stop"), 0, 
    'pg_ctlcluster stop succeeds');
130 131
like_program_out 'postgres', 'pg_lsclusters -h', 0, qr/down/, 'cluster is down';
ok (! -e "/var/lib/postgresql/$version/main/postmaster.pid", 'no pid file left');
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
# trying to stop a stopped server cleans up corrupt and stale pid files
my $pf = create_pidfile 'foo';
is_program_out 'postgres', "pg_ctlcluster $version main stop", 2,
    "Removed stale pid file.\nCluster is not running.\n", 
    'pg_ctlcluster stop succeeds with corrupted PID file';
ok (! -e $pf, 'pid file was cleaned up');

create_pidfile 'foo';
is_program_out 'postgres', "pg_ctlcluster --force $version main stop", 2,
    "Removed stale pid file.\nCluster is not running.\n", 
    'pg_ctlcluster --force stop succeeds with corrupted PID file';
ok (! -e $pf, 'pid file was cleaned up');

create_pidfile '99998';
is_program_out 'postgres', "pg_ctlcluster $version main stop", 2,
    "Removed stale pid file.\nCluster is not running.\n", 
    'pg_ctlcluster stop succeeds with stale PID file';
ok (! -e $pf, 'pid file was cleaned up');

create_pidfile '99998';
is_program_out 'postgres', "pg_ctlcluster --force $version main stop", 2,
    "Removed stale pid file.\nCluster is not running.\n", 
    'pg_ctlcluster --force stop succeeds with stale PID file';
ok (! -e $pf, 'pid file was cleaned up');

158 159 160 161 162 163
create_pidfile '';
is_program_out 'postgres', "pg_ctlcluster --force $version main stop", 2,
    "Removed stale pid file.\nCluster is not running.\n", 
    'pg_ctlcluster stop succeeds with empty PID file';
ok (! -e $pf, 'pid file was cleaned up');

164
# corrupt PID file while server is down
165
create_pidfile 'foo';
166 167 168
is_program_out 'postgres', "pg_ctlcluster $version main start", 0,
    "Removed stale pid file.\n", 'pg_ctlcluster succeeds with corrupted PID file';
like_program_out 'postgres', 'pg_lsclusters -h', 0, qr/online/, 'cluster is online';
169 170

# start running server
171
is_program_out 'postgres', "pg_ctlcluster $version main start", 2,
172 173
    "Cluster is already running.\n", 'pg_ctlcluster start fails on running cluster';
is ((exec_as 'postgres', "pg_ctlcluster $version main stop"), 0, 'pg_ctlcluster stop');
174 175 176 177 178 179 180 181 182 183 184

# backup pg_hba.conf
rename "/etc/postgresql/$version/main/pg_hba.conf",
    "/etc/postgresql/$version/main/pg_hba.conf.orig" or die "rename: $!";

# test check for invalid pg_hba.conf
open F, ">/etc/postgresql/$version/main/pg_hba.conf" or die "could not create pg_hba.conf: $!";
print F "foo\n";
close F;
chmod 0644, "/etc/postgresql/$version/main/pg_hba.conf" or die "chmod: $!";

185
if ($version < '8.4') {
186 187 188 189 190 191
    like_program_out 'postgres', "pg_ctlcluster $version main start", 0, 
	qr/WARNING.*connection to the database failed.*pg_hba.conf/is,
	'pg_ctlcluster start warns about invalid pg_hba.conf';
    is_program_out 'postgres', "pg_ctlcluster $version main stop", 0, '', 'stopping cluster';
} else {
    like_program_out 'postgres', "pg_ctlcluster $version main start", 1, 
192
	qr/FATAL.*pg_hba.conf/is,
193 194 195 196
	'pg_ctlcluster start fails on invalid pg_hba.conf';
    is_program_out 'postgres', "pg_ctlcluster $version main stop", 2, 
	"Cluster is not running.\n", 'stopping cluster';
}
197

Evgeny's avatar
Evgeny committed
198 199 200 201 202 203 204 205 206 207 208
# test check for pg_hba.conf with removed passwordless local superuser access
open F, ">/etc/postgresql/$version/main/pg_hba.conf" or die "could not create pg_hba.conf: $!";
print F "local all all md5\n";
close F;
chmod 0644, "/etc/postgresql/$version/main/pg_hba.conf" or die "chmod: $!";

like_program_out 'postgres', "pg_ctlcluster $version main start", 0,
    qr/WARNING.*connection to the database failed.*postgres/is,
    'pg_ctlcluster start warns about absence of passwordless superuser connection';
is_program_out 'postgres', "pg_ctlcluster $version main stop", 0, '', 'stopping cluster';

209 210 211 212 213
# restore pg_hba.conf
unlink "/etc/postgresql/$version/main/pg_hba.conf";
rename "/etc/postgresql/$version/main/pg_hba.conf.orig",
    "/etc/postgresql/$version/main/pg_hba.conf" or die "rename: $!";

214 215 216 217 218 219 220 221 222
# leftover files must not create confusion
open F, '>/etc/postgresql/postgresql.conf';
print F "data_directory = '/nonexisting'\n";
close F;
my @c = get_version_clusters $version;
is_deeply (\@c, ['main'], 
   'leftover /etc/postgresql/postgresql.conf is not regarded as a cluster');
unlink '/etc/postgresql/postgresql.conf';

223
# fails by default due to access restrictions
224 225 226
# remove cluster and directory; this should work as user "postgres"
is_program_out 'postgres', "pg_dropcluster $version main", 0, '',
    , "pg_dropcluster works as user postgres";
227 228 229 230

# graceful handling of absent data dir (might not be mounted)
ok ((system "pg_createcluster $version main >/dev/null") == 0,
    "pg_createcluster succeeds");
231
rename "/var/lib/postgresql/$version", "/var/lib/postgresql/$version.orig" or die "rename: $!";
232 233 234 235 236
my $outref;
is ((exec_as 0, "pg_ctlcluster $version main start", $outref, 1), 1,
    'pg_ctlcluster fails on nonexisting /var/lib/postgresql');
like $$outref, qr/^Error:.*\/var\/lib\/postgresql.*not accessible.*$/, 'proper error message for nonexisting /var/lib/postgresql';

237
rename "/var/lib/postgresql/$version.orig", "/var/lib/postgresql/$version" or die "rename: $!";
238 239 240 241
is_program_out 'postgres', "pg_ctlcluster $version main start", 0, '',
    'pg_ctlcluster start succeeds again with reappeared /var/lib/postgresql';
is_program_out 'postgres', "pg_ctlcluster $version main stop", 0, '', 'stopping cluster';

242 243 244 245 246 247
# pg_ctlcluster checks colliding ports
ok ((system "pg_createcluster $version other >/dev/null") == 0,
    "pg_createcluster other");
set_cluster_port $version, 'other', '5432';
is ((exec_as 'postgres', "pg_ctlcluster $version main start"), 0,
    'pg_ctlcluster: main cluster on conflicting port starts');
248 249

# clusters can run side by side on different socket directories
250
set_cluster_socketdir $version, 'other', $socketdir;
251 252 253 254 255 256 257
PgCommon::set_conf_value $version, 'other', 'postgresql.conf',
    'listen_addresses', ''; # otherwise they will conflict on TCP socket
is ((exec_as 'postgres', "pg_ctlcluster $version other start"), 0,
    'pg_ctlcluster: other cluster starts on conflicting port, but different socket dirs');
is ((exec_as 'postgres', "pg_ctlcluster $version other stop"), 0);

# ... but will give an error when running on the same port
258
set_cluster_socketdir $version, 'other', ($PgCommon::rpm and $version < 9.4) ? '/tmp' : '/var/run/postgresql';
259
like_program_out 'postgres', "pg_ctlcluster $version other start", 1,
260 261
    qr/Port conflict:.*port 5432/,
    'pg_ctlcluster other cluster fails on conflicting port and same socket dir';
262 263 264 265 266 267 268
is_program_out 'postgres', "pg_ctlcluster $version main stop", 0, '', 
    'stopping main cluster';
is ((exec_as 'postgres', "pg_ctlcluster $version other start"), 0,
    'pg_ctlcluster: other cluster on conflicting port starts after main is down');
ok ((system "pg_dropcluster $version other --stop") == 0, 
    'pg_dropcluster other');

269
# clean up
270
ok ((system "pg_dropcluster $version main") == 0, 
Martin Pitt's avatar
Martin Pitt committed
271 272 273 274
    'pg_dropcluster');
ok_dir $socketdir, [], 'No sockets any more';
rmdir $socketdir or die "rmdir: $!";

275
# ensure sane error messages for nonexisting clusters
276
check_nonexisting_cluster_error 'pg_lsclusters 4.5 foo';
277 278 279 280 281 282 283 284 285
check_nonexisting_cluster_error 'psql --cluster 4.5/foo';
check_nonexisting_cluster_error "psql --cluster $MAJORS[0]/foo";
check_nonexisting_cluster_error "pg_dropcluster 4.5 foo";
check_nonexisting_cluster_error "pg_dropcluster $MAJORS[0] foo";
check_nonexisting_cluster_error "pg_upgradecluster 4.5 foo";
check_nonexisting_cluster_error "pg_upgradecluster $MAJORS[0] foo";
check_nonexisting_cluster_error "pg_ctlcluster 4.5 foo stop";
check_nonexisting_cluster_error "pg_ctlcluster $MAJORS[0] foo stop";

286 287
check_clean;

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
# check that pg_dropcluster copes with partially existing cluster
# configurations (which can happen if the disk becomes full)

mkdir '/etc/postgresql/';
mkdir "/etc/postgresql/$MAJORS[-1]";
mkdir "/etc/postgresql/$MAJORS[-1]/broken" or die "mkdir: $!";
symlink "/var/lib/postgresql/$MAJORS[-1]/broken", "/etc/postgresql/$MAJORS[-1]/broken/pgdata" or die "symlink: $!";

unlike_program_out 0, "pg_dropcluster $MAJORS[-1] broken", 0, qr/error/i, 
    'pg_dropcluster cleans up broken cluster configuration (only /etc with pgdata)';

check_clean;

mkdir '/etc/postgresql/';
mkdir '/var/lib/postgresql/';
mkdir "/etc/postgresql/$MAJORS[-1]" and 
mkdir "/etc/postgresql/$MAJORS[-1]/broken";
mkdir "/var/lib/postgresql/$MAJORS[-1]";
mkdir "/var/lib/postgresql/$MAJORS[-1]/broken";
mkdir "/var/lib/postgresql/$MAJORS[-1]/broken/base" or die "mkdir: $!";
open F, ">/etc/postgresql/$MAJORS[-1]/broken/postgresql.conf" or die "open: $!";
309
print F "data_directory = '/var/lib/postgresql/$MAJORS[-1]/broken'\n";
310 311 312 313 314 315 316 317 318
close F;
open F, ">/var/lib/postgresql/$MAJORS[-1]/broken/PG_VERSION" or die "open: $!";
close F;

unlike_program_out 0, "pg_dropcluster $MAJORS[-1] broken", 0, qr/error/i, 
    'pg_dropcluster cleans up broken cluster configuration (/etc with pgdata and postgresql.conf and partial /var)';

check_clean;

Martin Pitt's avatar
Martin Pitt committed
319
# vim: filetype=perl