a2aa380de18267114ae0137fcd680be8ae61143a
[debian/amanda] / installcheck / ampgsql.pl
1 # Copyright (c) 2009, 2010 Zmanda, Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10 # for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 #
16 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 74;
20
21 use lib "@amperldir@";
22 use strict;
23 use warnings;
24 use Amanda::Constants;
25 use Amanda::Paths;
26 use Amanda::Debug qw( debug );
27 use Amanda::Util;
28 use File::Path;
29 use Installcheck;
30 use Installcheck::Application;
31 use Installcheck::Config;
32 use Installcheck::Run;
33 use IPC::Open3;
34
35 Amanda::Debug::dbopen("installcheck");
36 Installcheck::log_test_output();
37 my $debug = !exists $ENV{'HARNESS_ACTIVE'};
38
39 sub skip_all {
40     my $reason = shift @_;
41     SKIP: {
42         skip($reason, Test::More->builder->expected_tests);
43     }
44     exit 0;
45 }
46
47 skip_all("GNU tar is not available")
48     unless ($Amanda::Constants::GNUTAR and -x $Amanda::Constants::GNUTAR);
49
50 my $postgres_prefix = $ENV{'INSTALLCHECK_POSTGRES_PREFIX'};
51 skip_all("Set INSTALLCHECK_POSTGRES_PREFIX to run tests") unless $postgres_prefix;
52
53 sub get_pg_version {
54     my $verout = Installcheck::Run::run_get("$postgres_prefix/bin/psql", "-X", "--version");
55     my @lines = split(/\n/, $verout);
56     my ($maj, $min, $pat) = ($lines[0] =~ / ([0-9]+)\.([0-9]+)\.([0-9]+)$/);
57     return $maj * 10000 + $min * 100 + $pat;
58 }
59 my $pg_version = get_pg_version();
60
61 my $SIGINT = 2;
62 my $DB_NAME = "installcheck";
63 my $root_dir = "$Installcheck::TMP/ampgsql";
64 my $data_dir = "$root_dir/data";
65 my $config_file = "$data_dir/postgresql.conf";
66 my $recovery_conf_file = "$data_dir/recovery.conf";
67 my $recovery_done_file = "$data_dir/recovery.done";
68 my $socket_dir = "$root_dir/sockets";
69 my $archive_dir = "$root_dir/archive";
70 my $tmp_dir = "$root_dir/tmp";
71 my $log_dir = "$Installcheck::TMP/ampgsql";
72 my $state_dir = "$root_dir/state";
73
74 sub dbg {
75     my ($msg) = @_;
76     if ($debug) {
77         diag($msg);
78     } else {
79         debug($msg);
80     }
81 }
82
83 # run a command with output sent to the debug log
84 sub run_and_log {
85     my ($in_str, $prog, @args) = @_;
86     local *IN;
87
88     debug("running $prog " . join(" ", @args));
89     debug(".. with input '$in_str'") if $in_str;
90
91     my $dbfd = Amanda::Debug::dbfd();
92     my $pid = open3(\*IN, ">&$dbfd", ">&$dbfd", $prog, @args);
93     print IN $in_str;
94     close(IN);
95     waitpid($pid, 0);
96     my $status = $? >> 8;
97     debug("..exit status $status");
98
99     return $status;
100 }
101
102 # run a sub and report on the result; mostly used for setup/teardown
103 sub try_eval {
104     my ($desc, $code, @args) = @_;
105     my $err_str;
106     $err_str = "$@" unless eval {$code->(@args); 1;};
107     ok(!$err_str, $desc) or diag($err_str);
108 }
109
110 sub write_config_file {
111     my ($filename, $cfg) = @_;
112
113     unlink $filename if -f $filename;
114     Amanda::Util::burp($filename, $cfg);
115 }
116
117 # run $code while the postmaster is started, shutting down the postmaster afterward,
118 # logging the result to our debug file
119 sub do_postmaster {
120     my $code = shift @_;
121     my $pidfile = "$data_dir/postmaster.pid";
122     local *IN;
123
124     die "postmaster already running"
125         if -f $pidfile;
126
127     dbg("starting postmaster..");
128
129     my $dbfd = Amanda::Debug::dbfd();
130     my $pid = open3(\*IN, ">&$dbfd", ">&$dbfd",
131             "$postgres_prefix/bin/postmaster", "-D", $data_dir);
132     close(IN);
133
134     # busy-wait for the pidfile to be created, for up to 120s
135     my $ticks = 0;
136     while (!-f $pidfile) {
137         die "postmaster did not start"
138             if ($ticks++ > 120 or !kill 0, $pid);
139         dbg("waiting for postmaster to write its pid");
140         sleep(1);
141     }
142
143     # and finish out those 120 seconds waiting for the db to actually
144     # be ready to roll, using psql -l just like pg_ctl does
145     while ($ticks++ < 120) {
146         local *IN;
147
148         my $psqlpid = open3(\*IN, ">&$dbfd", ">&$dbfd",
149             "$postgres_prefix/bin/psql", "-X", "-h", $socket_dir, "-l");
150         close IN;
151         waitpid($psqlpid, 0);
152         last if (($? >> 8) == 0);
153         sleep(1);
154     }
155
156     if ($ticks == 120) {
157         die("postmaster never started");
158     }
159
160     # use eval to be careful to shut down postgres
161     eval { $code->() if $code };
162     my $err = $@;
163
164     # kill the postmaster and wait for it to die
165     kill $SIGINT, $pid;
166     waitpid($pid, 0);
167     my $status = $? >> 8;
168     dbg("postmaster stopped");
169
170     die "postmaster pid file still exists"
171         if -f $pidfile;
172
173     die $err if $err;
174 }
175
176 # count the WAL files.  Note that this may count some extra stuff, too, but
177 # since it's only used to see the number of WALs *increase*, that's OK.
178 sub count_wals {
179     my @files = glob("$archive_dir/*");
180     if (@files) {
181         debug("WAL files in archive_dir: " . join(" ", @files));
182     } else {
183         debug("No WAL files in archive_dir");
184     }
185     return scalar @files;
186 }
187
188 sub ls_backup_data {
189     my ($level, $backup) = @_;
190
191     my $tmpdir = "$Installcheck::TMP/backup_data";
192     -d $tmpdir && rmtree $tmpdir;
193     mkpath $tmpdir;
194
195     if ($level > 0) {
196         debug("contents of level-$level backup:");
197         Amanda::Util::burp("$tmpdir/backup.tar", $backup->{'data'});
198         run_and_log("", $Amanda::Constants::GNUTAR, "-tvf", "$tmpdir/backup.tar");
199     } else {
200         debug("contents of level-0 backup:");
201         Amanda::Util::burp("$tmpdir/backup.tar", $backup->{'data'});
202         run_and_log("", $Amanda::Constants::GNUTAR, "-C", $tmpdir, "-xvf", "$tmpdir/backup.tar");
203         debug(".. archive_dir.tar contains:");
204         run_and_log("", $Amanda::Constants::GNUTAR, "-tvf", "$tmpdir/archive_dir.tar");
205         debug(".. data_dir.tar looks like:\n" . `ls -l "$tmpdir/data_dir.tar"`);
206     }
207
208     rmtree $tmpdir;
209 }
210
211 # set up all of our dirs
212 try_eval("emptied root_dir", \&rmtree, $root_dir);
213 try_eval("created archive_dir", \&mkpath, $archive_dir);
214 try_eval("created data_dir", \&mkpath, $data_dir);
215 try_eval("created socket_dir", \&mkpath, $socket_dir);
216 try_eval("created log_dir", \&mkpath, $log_dir);
217 try_eval("created state_dir", \&mkpath, $state_dir);
218
219 # create an amanda config for the application
220 my $conf = Installcheck::Config->new();
221 $conf->add_client_param('property', "\"PG-DATADIR\" \"$data_dir\"");
222 $conf->add_client_param('property', "\"PG-ARCHIVEDIR\" \"$archive_dir\"");
223 $conf->add_client_param('property', "\"PG-CLEANUPWAL\" \"yes\"");
224 $conf->add_client_param('property', "\"PG-HOST\" \"$socket_dir\"");
225 $conf->add_client_param('property', "\"PSQL-PATH\" \"$postgres_prefix/bin/psql\"");
226 $conf->write();
227
228 # set up the database
229 dbg("creating initial database");
230 run_and_log("", "$postgres_prefix/bin/initdb", "-D", "$data_dir")
231     and die("error running initdb");
232
233 # enable archive mode for 8.3 and higher
234 my $archive_mode = '';
235 if ($pg_version >= 80300) {
236     $archive_mode = 'archive_mode = on';
237 }
238
239 # write the postgres config file
240 write_config_file $config_file, <<EOF;
241 listen_addresses = ''
242 unix_socket_directory = '$socket_dir'
243 archive_command = 'test ! -f $archive_dir/%f && cp %p $archive_dir/%f'
244 $archive_mode
245 log_destination = 'stderr'
246 # checkpoint every 30 seconds (this is the minimum)
247 checkpoint_timeout = 30
248 # and don't warn me about that
249 checkpoint_warning = 0
250 # and keep 50 segments
251 checkpoint_segments = 50
252 # and bundle commits up to one minute
253 commit_delay = 60
254 EOF
255
256 my $app = new Installcheck::Application("ampgsql");
257 $app->add_property('statedir', $state_dir);
258 $app->add_property('tmpdir', $tmp_dir);
259
260 # take three backups: a level 0, level 1, and level 2.  The level 2 has
261 # no database changes, so it contains no WAL files.
262 my ($backup, $backup_incr, $backup_incr_empty);
263 sub setup_db_and_backup {
264     my $i;
265
266     run_and_log("", "$postgres_prefix/bin/createdb", "-h", $socket_dir, $DB_NAME);
267     pass("created db");
268
269     run_and_log(<<EOF, "$postgres_prefix/bin/psql", "-X", "-h", $socket_dir, "-d", $DB_NAME);
270 CREATE TABLE foo (bar INTEGER, baz INTEGER, longstr CHAR(10240));
271 INSERT INTO foo (bar, baz) VALUES (1, 2);
272 EOF
273     pass("created test data (table and a row)");
274
275     $backup = $app->backup('device' => $data_dir, 'level' => 0, 'config' => 'TESTCONF');
276     ls_backup_data(0, $backup);
277     is($backup->{'exit_status'}, 0, "backup error status ok");
278     ok(!@{$backup->{'errors'}}, "..no errors")
279         or diag(@{$backup->{'errors'}});
280     ok(grep(/^\/PostgreSQL-Database-0$/, @{$backup->{'index'}}), "..contains an index entry")
281         or diag(@{$backup->{'index'}});
282     ok(length($backup->{'data'}) > 0,
283         "..got at least one byte");
284
285     # add a database that should be big enough to fill a WAL, then wait for postgres
286     # to archive it.
287     my $n_wals = count_wals();
288     run_and_log(<<EOF, "$postgres_prefix/bin/psql", "-X", "-h", $socket_dir, "-d", $DB_NAME);
289 INSERT INTO foo (bar, baz) VALUES (1, 2);
290 CREATE TABLE wal_test AS SELECT * FROM GENERATE_SERIES(1, 500000);
291 EOF
292     sleep(1);
293     for ($i = 0; $i < 10; $i++) {
294         last if (count_wals() > $n_wals);
295         dbg("still $n_wals WAL files in archive directory; sleeping");
296         sleep(1);
297     }
298     die "postgres did not archive any WALs" if $i == 10;
299     $n_wals = count_wals();
300
301     $backup_incr = $app->backup('device' => $data_dir, 'level' => 1, 'config' => 'TESTCONF');
302     ls_backup_data(1, $backup_incr);
303     is($backup_incr->{'exit_status'}, 0, "incr backup error status ok");
304     ok(!@{$backup_incr->{'errors'}}, "..no errors")
305         or diag(@{$backup_incr->{'errors'}});
306     ok(grep(/^\/PostgreSQL-Database-1$/, @{$backup_incr->{'index'}}), "..contains an index entry")
307         or diag(@{$backup_incr->{'index'}});
308     ok(length($backup_incr->{'data'}) > 0,
309         "..got at least one byte");
310
311     die "more WALs appeared during backup (timing error)"
312         if count_wals() > $n_wals;
313     ok(count_wals() == $n_wals,
314         "ampgsql did not clean up the latest bunch of WAL files (as expected)");
315
316     # (no more transactions here -> no more WAL files)
317
318     $backup_incr_empty = $app->backup('device' => $data_dir, 'level' => 2, 'config' => 'TESTCONF');
319     ls_backup_data(2, $backup_incr_empty);
320     is($backup_incr_empty->{'exit_status'}, 0, "incr backup with no changes: error status ok");
321     ok(!@{$backup_incr_empty->{'errors'}}, "..no errors")
322         or diag(@{$backup_incr_empty->{'errors'}});
323     ok(grep(/^\/PostgreSQL-Database-2$/, @{$backup_incr_empty->{'index'}}),
324         "..contains an index entry")
325         or diag(@{$backup_incr_empty->{'index'}});
326     ok(length($backup_incr_empty->{'data'}) > 0,
327         "..got at least one byte");
328
329     ok(count_wals() == $n_wals,
330         "ampgsql still did not clean up the latest bunch of WAL files");
331 }
332
333 do_postmaster(\&setup_db_and_backup);
334 pass("finished setting up db");
335
336 sub try_selfcheck {
337     my $sc;
338
339     $sc = $app->selfcheck('device' => $data_dir, 'config' => 'TESTCONF');
340     is($sc->{'exit_status'}, 0, "selfcheck error status ok");
341     ok(!@{$sc->{'errors'}}, "no errors reported");
342     ok(@{$sc->{'oks'}}, "got one or more OK messages");
343
344     $app->set_property('statedir', "$state_dir/foo");
345     $sc = $app->selfcheck('device' => $data_dir, 'config' => 'TESTCONF');
346     is($sc->{'exit_status'}, 0, "selfcheck error status ok");
347     ok(grep(/STATEDIR/, @{$sc->{'errors'}}), "got STATEDIR error");
348
349     my $test_state_dir_par = "$root_dir/parent-to-strip";
350     my $test_state_dir = "$test_state_dir_par/state";
351     $app->set_property('statedir', $test_state_dir);
352     try_eval("created state_dir", \&mkpath, $test_state_dir);
353     my @par_stat = stat($test_state_dir_par);
354     my $old_perms = $par_stat[2] & 0777;
355     ok(chmod(0, $test_state_dir_par), "stripped permissions from parent of statedir");
356     $sc = $app->selfcheck('device' => $data_dir, 'config' => 'TESTCONF');
357     is($sc->{'exit_status'}, 0, "selfcheck error status ok");
358     ok(grep(/STATEDIR/, @{$sc->{'errors'}}), "got STATEDIR error");
359     ok(grep(/$test_state_dir_par\/ /, @{$sc->{'errors'}}), "got perms error for parent of statedir");
360     # repair
361     ok(chmod($old_perms, $test_state_dir_par), "restored permissions on parent of statedir");
362     $app->set_property('statedir', $state_dir); 
363 }
364
365 do_postmaster(\&try_selfcheck);
366
367 ## full restore
368
369 sub try_restore {
370     my ($expected_foo_count, @backups) = @_;
371
372     dbg("*** try_restore from level $#backups");
373
374     try_eval("emptied data_dir", \&rmtree, $data_dir);
375     try_eval("emptied archive_dir", \&rmtree, $archive_dir);
376     try_eval("recreated data_dir", \&mkpath, $data_dir);
377
378     my $orig_cur_dir = POSIX::getcwd();
379     ok($orig_cur_dir, "got current directory");
380
381     ok(chdir($root_dir), "changed working directory (for restore)");
382
383     for my $level (0 .. $#backups) {
384         my $backup = $backups[$level];
385
386         my $restore = $app->restore('objects' => ['./'], level => $level,
387                             'data' => $backup->{'data'});
388         is($restore->{'exit_status'}, 0, "..level $level restore error status ok");
389         if ($level == 0) {
390             ok(-f "$data_dir/PG_VERSION", "..data dir has a PG_VERSION file");
391             ok(-d $archive_dir, "..archive dir exists");
392
393             my $pidfile = "$data_dir/postmaster.pid";
394             ok(! -f $pidfile, "..pidfile is not restored")
395                 or unlink $pidfile;
396         }
397     }
398
399     ok(chdir($orig_cur_dir), "changed working directory (back to original)");
400     is(system('chmod', '-R', 'go-rwx', $archive_dir, $data_dir) >> 8, 0, 'chmod restored files');
401
402     write_config_file $recovery_conf_file, <<EOF;
403 restore_command = 'echo restore_cmd invoked for %f >&2; cp $archive_dir/%f %p'
404 EOF
405
406     my $get_data = sub {
407         like(Installcheck::Run::run_get("$postgres_prefix/bin/psql", "-X",
408                         "-q", "-A", "-t",
409                         "-h", $socket_dir, "-d", $DB_NAME,
410                         "-c", "SELECT count(*) FROM foo;"),
411             qr/^$expected_foo_count/,
412             "..got $expected_foo_count rows from recovered database");
413     };
414
415     do_postmaster($get_data);
416     unlink($recovery_conf_file);
417     unlink($recovery_done_file);
418 }
419
420 # try a level-0, level-1, and level-2 restore
421 try_restore(1, $backup);
422 try_restore(2, $backup, $backup_incr);
423 try_restore(2, $backup, $backup_incr, $backup_incr_empty);
424
425 try_eval("emptied root_dir", \&rmtree, $root_dir);