Update copyright for 2022
[pgsql.git] / src / bin / pg_resetwal / t / 002_corrupted.pl
blob52678428b54ffffa95ef4b292bdfe7b4804fc5c4
2 # Copyright (c) 2021-2022, PostgreSQL Global Development Group
4 # Tests for handling a corrupted pg_control
6 use strict;
7 use warnings;
9 use PostgreSQL::Test::Cluster;
10 use PostgreSQL::Test::Utils;
11 use Test::More tests => 6;
13 my $node = PostgreSQL::Test::Cluster->new('main');
14 $node->init;
16 my $pg_control = $node->data_dir . '/global/pg_control';
17 my $size = (stat($pg_control))[7];
19 # Read out the head of the file to get PG_CONTROL_VERSION in
20 # particular.
21 my $data;
22 open my $fh, '<', $pg_control or BAIL_OUT($!);
23 binmode $fh;
24 read $fh, $data, 16;
25 close $fh;
27 # Fill pg_control with zeros
28 open $fh, '>', $pg_control or BAIL_OUT($!);
29 binmode $fh;
30 print $fh pack("x[$size]");
31 close $fh;
33 command_checks_all(
34 [ 'pg_resetwal', '-n', $node->data_dir ],
36 [qr/pg_control version number/],
38 qr/pg_resetwal: warning: pg_control exists but is broken or wrong version; ignoring it/
40 'processes corrupted pg_control all zeroes');
42 # Put in the previously saved header data. This uses a different code
43 # path internally, allowing us to process a zero WAL segment size.
44 open $fh, '>', $pg_control or BAIL_OUT($!);
45 binmode $fh;
46 print $fh $data, pack("x[" . ($size - 16) . "]");
47 close $fh;
49 command_checks_all(
50 [ 'pg_resetwal', '-n', $node->data_dir ],
52 [qr/pg_control version number/],
54 qr/\Qpg_resetwal: warning: pg_control specifies invalid WAL segment size (0 bytes); proceed with caution\E/
56 'processes zero WAL segment size');