Debian Codegraph @

src/bin/pg_verifybackup/t/004_options.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Verify the behavior of assorted pg_verifybackup options.
5
6 use strict;
7 use warnings FATAL => 'all';
8 use File::Path qw(rmtree);
9 use PostgreSQL::Test::Cluster;
10 use PostgreSQL::Test::Utils;
11 use Test::More;
12
13 # Start up the server and take a backup.
14 my $primary = PostgreSQL::Test::Cluster->new('primary');
15 $primary->init(allows_streaming => 1);
16 $primary->start;
17 my $backup_path = $primary->backup_dir . '/test_options';
18 $primary->command_ok(
19 [
20 'pg_basebackup',
21 '--pgdata' => $backup_path,
22 '--no-sync',
23 '--checkpoint' => 'fast'
24 ],
25 "base backup ok");
26
27 # Verify that pg_verifybackup --quiet succeeds and produces no output.
28 my $stdout;
29 my $stderr;
30 my $result = IPC::Run::run [ 'pg_verifybackup', '--quiet', $backup_path ],
31 '>' => \$stdout,
32 '2>' => \$stderr;
33 ok($result, "--quiet succeeds: exit code 0");
34 is($stdout, '', "--quiet succeeds: no stdout");
35 is($stderr, '', "--quiet succeeds: no stderr");
36
37 # Should still work if we specify --format=plain.
38 $primary->command_ok(
39 [ 'pg_verifybackup', '--format' => 'plain', $backup_path ],
40 "verifies with --format=plain");
41
42 # Should not work if we specify --format=y because that's invalid.
43 $primary->command_fails_like(
44 [ 'pg_verifybackup', '--format' => 'y', $backup_path ],
45 qr(invalid backup format "y", must be "plain" or "tar"),
46 "does not verify with --format=y");
47
48 # Should produce a lengthy list of errors; we test for just one of those.
49 $primary->command_fails_like(
50 [
51 'pg_verifybackup',
52 '--format' => 'tar',
53 '--no-parse-wal',
54 $backup_path
55 ],
56 qr("pg_multixact" is not a regular file),
57 "does not verify with --format=tar --no-parse-wal");
58
59 # Test invalid options
60 command_fails_like(
61 [ 'pg_verifybackup', '--progress', '--quiet', $backup_path ],
62 qr{cannot specify both -P/--progress and -q/--quiet},
63 'cannot use --progress and --quiet at the same time');
64
65 # Corrupt the PG_VERSION file.
66 my $version_pathname = "$backup_path/PG_VERSION";
67 my $version_contents = slurp_file($version_pathname);
68 open(my $fh, '>', $version_pathname) || die "open $version_pathname: $!";
69 print $fh 'q' x length($version_contents);
70 close($fh);
71
72 # Verify that pg_verifybackup -q now fails.
73 command_fails_like(
74 [ 'pg_verifybackup', '--quiet', $backup_path ],
75 qr/checksum mismatch for file \"PG_VERSION\"/,
76 '--quiet checksum mismatch');
77
78 # Since we didn't change the length of the file, verification should succeed
79 # if we ignore checksums. Check that we get the right message, too.
80 command_like(
81 [ 'pg_verifybackup', '--skip-checksums', $backup_path ],
82 qr/backup successfully verified/,
83 '--skip-checksums skips checksumming');
84
85 # Validation should succeed if we ignore the problem file. Also, check
86 # the progress information.
87 command_checks_all(
88 [
89 'pg_verifybackup',
90 '--progress',
91 '--ignore' => 'PG_VERSION',
92 $backup_path
93 ],
94 0,
95 [qr/backup successfully verified/],
96 [qr{(\d+/\d+ kB \(\d+%\) verified)+}],
97 '--ignore ignores problem file');
98
99 # PG_VERSION is already corrupt; let's try also removing all of pg_xact.
100 rmtree($backup_path . "/pg_xact");
101
102 # We're ignoring the problem with PG_VERSION, but not the problem with
103 # pg_xact, so verification should fail here.
104 command_fails_like(
105 [ 'pg_verifybackup', '--ignore' => 'PG_VERSION', $backup_path ],
106 qr/pg_xact.*is present in the manifest but not on disk/,
107 '--ignore does not ignore all problems');
108
109 # If we use --ignore twice, we should be able to ignore all of the problems.
110 command_like(
111 [
112 'pg_verifybackup',
113 '--ignore' => 'PG_VERSION',
114 '--ignore' => 'pg_xact',
115 $backup_path
116 ],
117 qr/backup successfully verified/,
118 'multiple --ignore options work');
119
120 # Verify that when --ignore is not used, both problems are reported.
121 $result = IPC::Run::run [ 'pg_verifybackup', $backup_path ],
122 '>' => \$stdout,
123 '2>' => \$stderr;
124 ok(!$result, "multiple problems: fails");
125 like(
126 $stderr,
127 qr/pg_xact.*is present in the manifest but not on disk/,
128 "multiple problems: missing files reported");
129 like(
130 $stderr,
131 qr/checksum mismatch for file \"PG_VERSION\"/,
132 "multiple problems: checksum mismatch reported");
133
134 # Verify that when --exit-on-error is used, only the problem detected
135 # first is reported.
136 $result =
137 IPC::Run::run [ 'pg_verifybackup', '--exit-on-error', $backup_path ],
138 '>' => \$stdout,
139 '2>' => \$stderr;
140 ok(!$result, "--exit-on-error reports 1 error: fails");
141 like(
142 $stderr,
143 qr/pg_xact.*is present in the manifest but not on disk/,
144 "--exit-on-error reports 1 error: missing files reported");
145 unlike(
146 $stderr,
147 qr/checksum mismatch for file \"PG_VERSION\"/,
148 "--exit-on-error reports 1 error: checksum mismatch not reported");
149
150 # Test valid manifest with nonexistent backup directory.
151 command_fails_like(
152 [
153 'pg_verifybackup',
154 '--manifest-path' => "$backup_path/backup_manifest",
155 "$backup_path/fake"
156 ],
157 qr/could not open directory/,
158 'nonexistent backup directory');
159
160 done_testing();