Debian Codegraph @

contrib/basebackup_to_shell/t/001_basic.pl

raw
1 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
2
3 use strict;
4 use warnings FATAL => 'all';
5
6 use PostgreSQL::Test::Cluster;
7 use PostgreSQL::Test::Utils;
8 use Test::More;
9
10 # For testing purposes, we just want basebackup_to_shell to write standard
11 # input to a file. However, Windows doesn't have "cat" or any equivalent, so
12 # we use "gzip" for this purpose.
13 my $gzip = $ENV{'GZIP_PROGRAM'};
14 if (!defined $gzip || $gzip eq '')
15 {
16 plan skip_all => 'gzip not available';
17 }
18
19 # to ensure path can be embedded in postgresql.conf
20 $gzip =~ s{\\}{/}g if ($PostgreSQL::Test::Utils::windows_os);
21
22 my $node = PostgreSQL::Test::Cluster->new('primary');
23
24 # Make sure pg_hba.conf is set up to allow connections from backupuser.
25 # This is only needed on Windows machines that don't use UNIX sockets.
26 $node->init(
27 allows_streaming => 1,
28 auth_extra => [ '--create-role' => 'backupuser' ]);
29
30 $node->append_conf('postgresql.conf',
31 "shared_preload_libraries = 'basebackup_to_shell'");
32 $node->start;
33 $node->safe_psql('postgres', 'CREATE USER backupuser REPLICATION');
34 $node->safe_psql('postgres', 'CREATE ROLE trustworthy');
35
36 # For nearly all pg_basebackup invocations some options should be specified,
37 # to keep test times reasonable. Using @pg_basebackup_defs as the first
38 # element of the array passed to IPC::Run interpolate the array (as it is
39 # not a reference to an array)...
40 my @pg_basebackup_defs =
41 ('pg_basebackup', '--no-sync', '--checkpoint' => 'fast');
42
43 # This particular test module generally wants to run with -Xfetch, because
44 # -Xstream is not supported with a backup target, and with -U backupuser.
45 my @pg_basebackup_cmd = (
46 @pg_basebackup_defs,
47 '--username' => 'backupuser',
48 '--wal-method' => 'fetch');
49
50 # Can't use this module without setting basebackup_to_shell.command.
51 $node->command_fails_like(
52 [ @pg_basebackup_cmd, '--target' => 'shell' ],
53 qr/shell command for backup is not configured/,
54 'fails if basebackup_to_shell.command is not set');
55
56 # Configure basebackup_to_shell.command and reload the configuration file.
57 my $backup_path = PostgreSQL::Test::Utils::tempdir;
58 my $escaped_backup_path = $backup_path;
59 $escaped_backup_path =~ s{\\}{\\\\}g
60 if ($PostgreSQL::Test::Utils::windows_os);
61 my $shell_command =
62 $PostgreSQL::Test::Utils::windows_os
63 ? qq{"$gzip" --fast > "$escaped_backup_path\\\\%f.gz"}
64 : qq{"$gzip" --fast > "$escaped_backup_path/%f.gz"};
65 $node->append_conf('postgresql.conf',
66 "basebackup_to_shell.command='$shell_command'");
67 $node->reload();
68
69 # Should work now.
70 $node->command_ok(
71 [ @pg_basebackup_cmd, '--target' => 'shell' ],
72 'backup with no detail: pg_basebackup');
73 verify_backup('', $backup_path, "backup with no detail");
74
75 # Should fail with a detail.
76 $node->command_fails_like(
77 [ @pg_basebackup_cmd, '--target' => 'shell:foo' ],
78 qr/a target detail is not permitted because the configured command does not include %d/,
79 'fails if detail provided without %d');
80
81 # Reconfigure to restrict access and require a detail.
82 $shell_command =
83 $PostgreSQL::Test::Utils::windows_os
84 ? qq{"$gzip" --fast > "$escaped_backup_path\\\\%d.%f.gz"}
85 : qq{"$gzip" --fast > "$escaped_backup_path/%d.%f.gz"};
86 $node->append_conf('postgresql.conf',
87 "basebackup_to_shell.command='$shell_command'");
88 $node->append_conf('postgresql.conf',
89 "basebackup_to_shell.required_role='trustworthy'");
90 $node->reload();
91
92 # Should fail due to lack of permission.
93 $node->command_fails_like(
94 [ @pg_basebackup_cmd, '--target' => 'shell' ],
95 qr/permission denied to use basebackup_to_shell/,
96 'fails if required_role not granted');
97
98 # Should fail due to lack of a detail.
99 $node->safe_psql('postgres', 'GRANT trustworthy TO backupuser');
100 $node->command_fails_like(
101 [ @pg_basebackup_cmd, '--target' => 'shell' ],
102 qr/a target detail is required because the configured command includes %d/,
103 'fails if %d is present and detail not given');
104
105 # Should work.
106 $node->command_ok([ @pg_basebackup_cmd, '--target' => 'shell:bar' ],
107 'backup with detail: pg_basebackup');
108 verify_backup('bar.', $backup_path, "backup with detail");
109
110 done_testing();
111
112 sub verify_backup
113 {
114 my ($prefix, $backup_dir, $test_name) = @_;
115
116 ok( -f "$backup_dir/${prefix}backup_manifest.gz",
117 "$test_name: backup_manifest.gz was created");
118 ok( -f "$backup_dir/${prefix}base.tar.gz",
119 "$test_name: base.tar.gz was created");
120
121 SKIP:
122 {
123 my $tar = $ENV{TAR};
124 skip "no tar program available", 1 if (!defined $tar || $tar eq '');
125
126 # Decompress.
127 system_or_bail($gzip, '-d',
128 $backup_dir . '/' . $prefix . 'backup_manifest.gz');
129 system_or_bail($gzip, '-d',
130 $backup_dir . '/' . $prefix . 'base.tar.gz');
131
132 # Untar.
133 my $extract_path = PostgreSQL::Test::Utils::tempdir;
134 system_or_bail(
135 $tar,
136 'xf' => $backup_dir . '/' . $prefix . 'base.tar',
137 '-C' => $extract_path);
138
139 # Verify.
140 $node->command_ok(
141 [
142 'pg_verifybackup',
143 '--no-parse-wal',
144 '--manifest-path' => "${backup_dir}/${prefix}backup_manifest",
145 '--exit-on-error',
146 $extract_path
147 ],
148 "$test_name: backup verifies ok");
149 }
150 }