Skip to content

Commit 53ba107

Browse files
committed
Refactor and fix TAP tests of pg_rewind
* Don't pass arguments to prove, since that's not supported on perl 5.8 which is the minimum version supported by the TAP tests. Refactor the test files themselves to run the tests twice, in both local and remote mode. * Use eq rather than == for string comparison. This thinko caused the remote versions of the tests to never run. * Add "use strict" and "use warnings", and fix warnings that that produced. * Increase the delay after standby promotion, to make the tests more robust. * In remote mode, the connection string to the promoted standby was incorrect, leading to connection errors. Patch by Michael Paquier, to address Peter Eisentraut's report.
1 parent b2a5545 commit 53ba107

File tree

5 files changed

+159
-129
lines changed

5 files changed

+159
-129
lines changed

src/bin/pg_rewind/Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,5 @@ clean distclean maintainer-clean:
4747
rm -f pg_rewind$(X) $(OBJS) xlogreader.c
4848
rm -rf tmp_check regress_log
4949

50-
check: all
51-
$(prove_check) :: local
52-
$(prove_check) :: remote
50+
check:
51+
$(prove_check)

src/bin/pg_rewind/RewindTest.pm

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ package RewindTest;
2929
# master and standby servers. The data directories are also available
3030
# in paths $test_master_datadir and $test_standby_datadir
3131

32+
use strict;
33+
use warnings;
34+
3235
use TestLib;
3336
use Test::More;
3437

@@ -58,8 +61,8 @@ our @EXPORT = qw(
5861

5962
# Adjust these paths for your environment
6063
my $testroot = "./tmp_check";
61-
$test_master_datadir="$testroot/data_master";
62-
$test_standby_datadir="$testroot/data_standby";
64+
our $test_master_datadir="$testroot/data_master";
65+
our $test_standby_datadir="$testroot/data_standby";
6366

6467
mkdir $testroot;
6568

@@ -73,8 +76,8 @@ my $port_standby=$port_master + 1;
7376
my $log_path;
7477
my $tempdir_short;
7578

76-
$connstr_master="port=$port_master";
77-
$connstr_standby="port=$port_standby";
79+
my $connstr_master="port=$port_master";
80+
my $connstr_standby="port=$port_standby";
7881

7982
$ENV{PGDATABASE} = "postgres";
8083

@@ -127,7 +130,8 @@ sub append_to_file
127130

128131
sub init_rewind_test
129132
{
130-
($testname, $test_mode) = @_;
133+
my $testname = shift;
134+
my $test_mode = shift;
131135

132136
$log_path="regress_log/pg_rewind_log_${testname}_${test_mode}";
133137

@@ -195,11 +199,13 @@ sub promote_standby
195199
# Now promote slave and insert some new data on master, this will put
196200
# the master out-of-sync with the standby.
197201
system_or_bail("pg_ctl -w -D $test_standby_datadir promote >>$log_path 2>&1");
198-
sleep 1;
202+
sleep 2;
199203
}
200204

201205
sub run_pg_rewind
202206
{
207+
my $test_mode = shift;
208+
203209
# Stop the master and be ready to perform the rewind
204210
system_or_bail("pg_ctl -w -D $test_master_datadir stop -m fast >>$log_path 2>&1");
205211

@@ -212,7 +218,7 @@ sub run_pg_rewind
212218
# overwritten during the rewind.
213219
copy("$test_master_datadir/postgresql.conf", "$testroot/master-postgresql.conf.tmp");
214220
# Now run pg_rewind
215-
if ($test_mode == "local")
221+
if ($test_mode eq "local")
216222
{
217223
# Do rewind using a local pgdata as source
218224
# Stop the master and be ready to perform the rewind
@@ -225,12 +231,12 @@ sub run_pg_rewind
225231
'>>', $log_path, '2>&1');
226232
ok ($result, 'pg_rewind local');
227233
}
228-
elsif ($test_mode == "remote")
234+
elsif ($test_mode eq "remote")
229235
{
230236
# Do rewind using a remote connection as source
231237
my $result =
232238
run(['./pg_rewind',
233-
"--source-server=\"port=$port_standby dbname=postgres\"",
239+
"--source-server", "port=$port_standby dbname=postgres",
234240
"--target-pgdata=$test_master_datadir"],
235241
'>>', $log_path, '2>&1');
236242
ok ($result, 'pg_rewind remote');

src/bin/pg_rewind/t/001_basic.pl

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,87 @@
11
use strict;
22
use warnings;
33
use TestLib;
4-
use Test::More tests => 4;
4+
use Test::More tests => 8;
55

66
use RewindTest;
77

8-
my $testmode = shift;
8+
sub run_test
9+
{
10+
my $test_mode = shift;
911

10-
RewindTest::init_rewind_test('basic', $testmode);
11-
RewindTest::setup_cluster();
12+
RewindTest::init_rewind_test('basic', $test_mode);
13+
RewindTest::setup_cluster();
1214

13-
# Create a test table and insert a row in master.
14-
master_psql("CREATE TABLE tbl1 (d text)");
15-
master_psql("INSERT INTO tbl1 VALUES ('in master')");
15+
# Create a test table and insert a row in master.
16+
master_psql("CREATE TABLE tbl1 (d text)");
17+
master_psql("INSERT INTO tbl1 VALUES ('in master')");
1618

17-
# This test table will be used to test truncation, i.e. the table
18-
# is extended in the old master after promotion
19-
master_psql("CREATE TABLE trunc_tbl (d text)");
20-
master_psql("INSERT INTO trunc_tbl VALUES ('in master')");
19+
# This test table will be used to test truncation, i.e. the table
20+
# is extended in the old master after promotion
21+
master_psql("CREATE TABLE trunc_tbl (d text)");
22+
master_psql("INSERT INTO trunc_tbl VALUES ('in master')");
2123

22-
# This test table will be used to test the "copy-tail" case, i.e. the
23-
# table is truncated in the old master after promotion
24-
master_psql("CREATE TABLE tail_tbl (id integer, d text)");
25-
master_psql("INSERT INTO tail_tbl VALUES (0, 'in master')");
24+
# This test table will be used to test the "copy-tail" case, i.e. the
25+
# table is truncated in the old master after promotion
26+
master_psql("CREATE TABLE tail_tbl (id integer, d text)");
27+
master_psql("INSERT INTO tail_tbl VALUES (0, 'in master')");
2628

29+
master_psql("CHECKPOINT");
2730

28-
master_psql("CHECKPOINT");
31+
RewindTest::create_standby();
2932

30-
RewindTest::create_standby();
33+
# Insert additional data on master that will be replicated to standby
34+
master_psql("INSERT INTO tbl1 values ('in master, before promotion')");
35+
master_psql("INSERT INTO trunc_tbl values ('in master, before promotion')");
36+
master_psql("INSERT INTO tail_tbl SELECT g, 'in master, before promotion: ' || g FROM generate_series(1, 10000) g");
3137

32-
# Insert additional data on master that will be replicated to standby
33-
master_psql("INSERT INTO tbl1 values ('in master, before promotion')");
34-
master_psql("INSERT INTO trunc_tbl values ('in master, before promotion')");
35-
master_psql("INSERT INTO tail_tbl SELECT g, 'in master, before promotion: ' || g FROM generate_series(1, 10000) g");
38+
master_psql('CHECKPOINT');
3639

37-
master_psql('CHECKPOINT');
40+
RewindTest::promote_standby();
3841

39-
RewindTest::promote_standby();
42+
# Insert a row in the old master. This causes the master and standby
43+
# to have "diverged", it's no longer possible to just apply the
44+
# standy's logs over master directory - you need to rewind.
45+
master_psql("INSERT INTO tbl1 VALUES ('in master, after promotion')");
4046

41-
# Insert a row in the old master. This causes the master and standby
42-
# to have "diverged", it's no longer possible to just apply the
43-
# standy's logs over master directory - you need to rewind.
44-
master_psql("INSERT INTO tbl1 VALUES ('in master, after promotion')");
47+
# Also insert a new row in the standby, which won't be present in the
48+
# old master.
49+
standby_psql("INSERT INTO tbl1 VALUES ('in standby, after promotion')");
4550

46-
# Also insert a new row in the standby, which won't be present in the
47-
# old master.
48-
standby_psql("INSERT INTO tbl1 VALUES ('in standby, after promotion')");
51+
# Insert enough rows to trunc_tbl to extend the file. pg_rewind should
52+
# truncate it back to the old size.
53+
master_psql("INSERT INTO trunc_tbl SELECT 'in master, after promotion: ' || g FROM generate_series(1, 10000) g");
4954

50-
# Insert enough rows to trunc_tbl to extend the file. pg_rewind should
51-
# truncate it back to the old size.
52-
master_psql("INSERT INTO trunc_tbl SELECT 'in master, after promotion: ' || g FROM generate_series(1, 10000) g");
55+
# Truncate tail_tbl. pg_rewind should copy back the truncated part
56+
# (We cannot use an actual TRUNCATE command here, as that creates a
57+
# whole new relfilenode)
58+
master_psql("DELETE FROM tail_tbl WHERE id > 10");
59+
master_psql("VACUUM tail_tbl");
5360

54-
# Truncate tail_tbl. pg_rewind should copy back the truncated part
55-
# (We cannot use an actual TRUNCATE command here, as that creates a
56-
# whole new relfilenode)
57-
master_psql("DELETE FROM tail_tbl WHERE id > 10");
58-
master_psql("VACUUM tail_tbl");
61+
RewindTest::run_pg_rewind($test_mode);
5962

60-
RewindTest::run_pg_rewind();
61-
62-
check_query('SELECT * FROM tbl1',
63-
qq(in master
63+
check_query('SELECT * FROM tbl1',
64+
qq(in master
6465
in master, before promotion
6566
in standby, after promotion
6667
),
67-
'table content');
68+
'table content');
6869

69-
check_query('SELECT * FROM trunc_tbl',
70-
qq(in master
70+
check_query('SELECT * FROM trunc_tbl',
71+
qq(in master
7172
in master, before promotion
7273
),
73-
'truncation');
74+
'truncation');
7475

75-
check_query('SELECT count(*) FROM tail_tbl',
76-
qq(10001
76+
check_query('SELECT count(*) FROM tail_tbl',
77+
qq(10001
7778
),
78-
'tail-copy');
79+
'tail-copy');
80+
81+
}
82+
83+
# Run the test in both modes
84+
run_test('local');
85+
run_test('remote');
7986

8087
exit(0);

src/bin/pg_rewind/t/002_databases.pl

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
11
use strict;
22
use warnings;
33
use TestLib;
4-
use Test::More tests => 2;
4+
use Test::More tests => 4;
55

66
use RewindTest;
77

8-
my $testmode = shift;
8+
sub run_test
9+
{
10+
my $test_mode = shift;
911

10-
RewindTest::init_rewind_test('databases', $testmode);
11-
RewindTest::setup_cluster();
12+
RewindTest::init_rewind_test('databases', $test_mode);
13+
RewindTest::setup_cluster();
1214

13-
# Create a database in master.
14-
master_psql('CREATE DATABASE inmaster');
15+
# Create a database in master.
16+
master_psql('CREATE DATABASE inmaster');
1517

16-
RewindTest::create_standby();
18+
RewindTest::create_standby();
1719

18-
# Create another database, the creation is replicated to the standby
19-
master_psql('CREATE DATABASE beforepromotion');
20+
# Create another database, the creation is replicated to the standby
21+
master_psql('CREATE DATABASE beforepromotion');
2022

21-
RewindTest::promote_standby();
23+
RewindTest::promote_standby();
2224

23-
# Create databases in the old master and the new promoted standby.
24-
master_psql('CREATE DATABASE master_afterpromotion');
25-
standby_psql('CREATE DATABASE standby_afterpromotion');
26-
# The clusters are now diverged.
25+
# Create databases in the old master and the new promoted standby.
26+
master_psql('CREATE DATABASE master_afterpromotion');
27+
standby_psql('CREATE DATABASE standby_afterpromotion');
28+
# The clusters are now diverged.
2729

28-
RewindTest::run_pg_rewind();
30+
RewindTest::run_pg_rewind($test_mode);
2931

30-
# Check that the correct databases are present after pg_rewind.
31-
check_query('SELECT datname FROM pg_database',
32-
qq(template1
32+
# Check that the correct databases are present after pg_rewind.
33+
check_query('SELECT datname FROM pg_database',
34+
qq(template1
3335
template0
3436
postgres
3537
inmaster
3638
beforepromotion
3739
standby_afterpromotion
3840
),
39-
'database names');
41+
'database names');
42+
43+
}
44+
45+
# Run the test in both modes.
46+
run_test('local');
47+
run_test('remote');
4048

4149
exit(0);

0 commit comments

Comments
 (0)