Skip to content

Commit 90627cf

Browse files
committed
Support retaining data dirs on successful TAP tests
This moves the data directories from using temporary directories with randomness in the directory name to a static name, to make it easier to debug. The data directory will be retained if tests fail or the test code dies/exits with failure, and is automatically removed on the next make check. If the environment variable PG_TEST_NOCLEAN is defined, the data directories will be retained regardless of test or exit status. Author: Daniel Gustafsson <daniel@yesql.se>
1 parent 5e8304f commit 90627cf

File tree

7 files changed

+61
-15
lines changed

7 files changed

+61
-15
lines changed

src/Makefile.global.in

+4-2
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,14 @@ endef
362362
ifeq ($(enable_tap_tests),yes)
363363

364364
define prove_installcheck
365-
rm -rf $(CURDIR)/tmp_check/log
365+
rm -rf '$(CURDIR)'/tmp_check
366+
$(MKDIR_P) '$(CURDIR)'/tmp_check
366367
cd $(srcdir) && TESTDIR='$(CURDIR)' PATH="$(bindir):$$PATH" PGPORT='6$(DEF_PGPORT)' top_builddir='$(CURDIR)/$(top_builddir)' PG_REGRESS='$(CURDIR)/$(top_builddir)/src/test/regress/pg_regress' $(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) $(if $(PROVE_TESTS),$(PROVE_TESTS),t/*.pl)
367368
endef
368369

369370
define prove_check
370-
rm -rf $(CURDIR)/tmp_check/log
371+
rm -rf '$(CURDIR)'/tmp_check
372+
$(MKDIR_P) '$(CURDIR)'/tmp_check
371373
cd $(srcdir) && TESTDIR='$(CURDIR)' $(with_temp_install) PGPORT='6$(DEF_PGPORT)' PG_REGRESS='$(CURDIR)/$(top_builddir)/src/test/regress/pg_regress' $(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) $(if $(PROVE_TESTS),$(PROVE_TESTS),t/*.pl)
372374
endef
373375

src/bin/pg_rewind/RewindTest.pm

+5-2
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ sub check_query
114114

115115
sub setup_cluster
116116
{
117+
my $extra_name = shift;
117118

118119
# Initialize master, data checksums are mandatory
119-
$node_master = get_new_node('master');
120+
$node_master = get_new_node('master' . ($extra_name ? "_${extra_name}" : ''));
120121
$node_master->init(allows_streaming => 1);
121122
}
122123

@@ -130,7 +131,9 @@ sub start_master
130131

131132
sub create_standby
132133
{
133-
$node_standby = get_new_node('standby');
134+
my $extra_name = shift;
135+
136+
$node_standby = get_new_node('standby' . ($extra_name ? "_${extra_name}" : ''));
134137
$node_master->backup('my_backup');
135138
$node_standby->init_from_backup($node_master, 'my_backup');
136139
my $connstr_master = $node_master->connstr();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ sub run_test
99
{
1010
my $test_mode = shift;
1111

12-
RewindTest::setup_cluster();
12+
RewindTest::setup_cluster($test_mode);
1313
RewindTest::start_master();
1414

1515
# Create a test table and insert a row in master.
@@ -28,7 +28,7 @@ sub run_test
2828

2929
master_psql("CHECKPOINT");
3030

31-
RewindTest::create_standby();
31+
RewindTest::create_standby($test_mode);
3232

3333
# Insert additional data on master that will be replicated to standby
3434
master_psql("INSERT INTO tbl1 values ('in master, before promotion')");

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ sub run_test
99
{
1010
my $test_mode = shift;
1111

12-
RewindTest::setup_cluster();
12+
RewindTest::setup_cluster($test_mode);
1313
RewindTest::start_master();
1414

1515
# Create a database in master.
1616
master_psql('CREATE DATABASE inmaster');
1717

18-
RewindTest::create_standby();
18+
RewindTest::create_standby($test_mode);
1919

2020
# Create another database, the creation is replicated to the standby
2121
master_psql('CREATE DATABASE beforepromotion');

src/bin/pg_rewind/t/003_extrafiles.pl

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ sub run_test
1414
{
1515
my $test_mode = shift;
1616

17-
RewindTest::setup_cluster();
17+
RewindTest::setup_cluster($test_mode);
1818
RewindTest::start_master();
1919

2020
my $test_master_datadir = $node_master->data_dir;
@@ -27,7 +27,7 @@ sub run_test
2727
append_to_file "$test_master_datadir/tst_both_dir/both_subdir/both_file3",
2828
"in both3";
2929

30-
RewindTest::create_standby();
30+
RewindTest::create_standby($test_mode);
3131

3232
# Create different subdirs and files in master and standby
3333
my $test_standby_datadir = $node_standby->data_dir;

src/bin/pg_rewind/t/004_pg_xlog_symlink.pl

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ sub run_test
2626
my $master_xlogdir = "${TestLib::tmp_check}/xlog_master";
2727

2828
rmtree($master_xlogdir);
29-
RewindTest::setup_cluster();
29+
RewindTest::setup_cluster($test_mode);
3030

3131
my $test_master_datadir = $node_master->data_dir;
3232

@@ -43,7 +43,7 @@ sub run_test
4343

4444
master_psql("CHECKPOINT");
4545

46-
RewindTest::create_standby();
46+
RewindTest::create_standby($test_mode);
4747

4848
# Insert additional data on master that will be replicated to standby
4949
master_psql("INSERT INTO tbl1 values ('in master, before promotion')");

src/test/perl/PostgresNode.pm

+44-3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ use Config;
8686
use Cwd;
8787
use Exporter 'import';
8888
use File::Basename;
89+
use File::Path qw(rmtree);
8990
use File::Spec;
9091
use File::Temp ();
9192
use IPC::Run;
@@ -100,7 +101,7 @@ our @EXPORT = qw(
100101
get_new_node
101102
);
102103

103-
our ($test_localhost, $test_pghost, $last_port_assigned, @all_nodes);
104+
our ($test_localhost, $test_pghost, $last_port_assigned, @all_nodes, $died);
104105

105106
# Windows path to virtual file system root
106107

@@ -149,11 +150,13 @@ sub new
149150
my $self = {
150151
_port => $pgport,
151152
_host => $pghost,
152-
_basedir => TestLib::tempdir("data_" . $name),
153+
_basedir => "$TestLib::tmp_check/t_${testname}_${name}_data",
153154
_name => $name,
154155
_logfile => "$TestLib::log_path/${testname}_${name}.log" };
155156

156157
bless $self, $class;
158+
mkdir $self->{_basedir} or
159+
BAIL_OUT("could not create data directory \"$self->{_basedir}\": $!");
157160
$self->dump_info;
158161

159162
return $self;
@@ -928,9 +931,24 @@ sub get_new_node
928931
return $node;
929932
}
930933

934+
# Retain the errno on die() if set, else assume a generic errno of 1.
935+
# This will instruct the END handler on how to handle artifacts left
936+
# behind from tests.
937+
$SIG{__DIE__} = sub
938+
{
939+
if ($!)
940+
{
941+
$died = $!;
942+
}
943+
else
944+
{
945+
$died = 1;
946+
}
947+
};
948+
931949
# Automatically shut down any still-running nodes when the test script exits.
932950
# Note that this just stops the postmasters (in the same order the nodes were
933-
# created in). Temporary PGDATA directories are deleted, in an unspecified
951+
# created in). Any temporary directories are deleted, in an unspecified
934952
# order, later when the File::Temp objects are destroyed.
935953
END
936954
{
@@ -941,6 +959,13 @@ END
941959
foreach my $node (@all_nodes)
942960
{
943961
$node->teardown_node;
962+
963+
# skip clean if we are requested to retain the basedir
964+
next if defined $ENV{'PG_TEST_NOCLEAN'};
965+
966+
# clean basedir on clean test invocation
967+
$node->clean_node
968+
if TestLib::all_tests_passing() && !defined $died && !$exit_code;
944969
}
945970

946971
$? = $exit_code;
@@ -959,6 +984,22 @@ sub teardown_node
959984
my $self = shift;
960985

961986
$self->stop('immediate');
987+
988+
}
989+
990+
=pod
991+
992+
=item $node->clean_node()
993+
994+
Remove the base directory of the node if the node has been stopped.
995+
996+
=cut
997+
998+
sub clean_node
999+
{
1000+
my $self = shift;
1001+
1002+
rmtree $self->{_basedir} unless defined $self->{_pid};
9621003
}
9631004

9641005
=pod

0 commit comments

Comments
 (0)