From c8642d909fdd57c36dd71e0b0bb4071523324794 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Wed, 20 Jan 2016 14:13:11 -0300
Subject: [PATCH] PostgresNode: Add names to nodes

This makes the log files easier to follow when investigating a test
failure.

Author: Michael Paquier
Review: Noah Misch
---
 src/bin/pg_basebackup/t/010_pg_basebackup.pl  |  2 +-
 .../pg_controldata/t/001_pg_controldata.pl    |  2 +-
 src/bin/pg_ctl/t/002_status.pl                |  2 +-
 src/bin/pg_rewind/RewindTest.pm               |  4 +-
 src/bin/scripts/t/010_clusterdb.pl            |  2 +-
 src/bin/scripts/t/011_clusterdb_all.pl        |  2 +-
 src/bin/scripts/t/020_createdb.pl             |  2 +-
 src/bin/scripts/t/030_createlang.pl           |  2 +-
 src/bin/scripts/t/040_createuser.pl           |  2 +-
 src/bin/scripts/t/050_dropdb.pl               |  2 +-
 src/bin/scripts/t/060_droplang.pl             |  2 +-
 src/bin/scripts/t/070_dropuser.pl             |  2 +-
 src/bin/scripts/t/080_pg_isready.pl           |  2 +-
 src/bin/scripts/t/090_reindexdb.pl            |  2 +-
 src/bin/scripts/t/091_reindexdb_all.pl        |  2 +-
 src/bin/scripts/t/100_vacuumdb.pl             |  2 +-
 src/bin/scripts/t/101_vacuumdb_all.pl         |  2 +-
 src/bin/scripts/t/102_vacuumdb_stages.pl      |  2 +-
 src/test/perl/PostgresNode.pm                 | 45 ++++++++++++-------
 src/test/ssl/t/001_ssltests.pl                |  2 +-
 20 files changed, 48 insertions(+), 37 deletions(-)

diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl
index 5991cf77f4a..a2750773fa7 100644
--- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl
+++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl
@@ -12,7 +12,7 @@ program_options_handling_ok('pg_basebackup');
 
 my $tempdir = TestLib::tempdir;
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 
 # Initialize node without replication settings
 $node->init(hba_permit_replication => 0);
diff --git a/src/bin/pg_controldata/t/001_pg_controldata.pl b/src/bin/pg_controldata/t/001_pg_controldata.pl
index 073815aff68..40405516cad 100644
--- a/src/bin/pg_controldata/t/001_pg_controldata.pl
+++ b/src/bin/pg_controldata/t/001_pg_controldata.pl
@@ -11,7 +11,7 @@ command_fails(['pg_controldata'], 'pg_controldata without arguments fails');
 command_fails([ 'pg_controldata', 'nonexistent' ],
 	'pg_controldata with nonexistent directory fails');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 
 command_like([ 'pg_controldata', $node->data_dir ],
diff --git a/src/bin/pg_ctl/t/002_status.pl b/src/bin/pg_ctl/t/002_status.pl
index f1c131bd26f..98e171e394f 100644
--- a/src/bin/pg_ctl/t/002_status.pl
+++ b/src/bin/pg_ctl/t/002_status.pl
@@ -11,7 +11,7 @@ my $tempdir_short = TestLib::tempdir_short;
 command_exit_is([ 'pg_ctl', 'status', '-D', "$tempdir/nonexistent" ],
 	4, 'pg_ctl status with nonexistent directory');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 
 command_exit_is([ 'pg_ctl', 'status', '-D', $node->data_dir ],
diff --git a/src/bin/pg_rewind/RewindTest.pm b/src/bin/pg_rewind/RewindTest.pm
index 41d294ed3f6..68834cd6a44 100644
--- a/src/bin/pg_rewind/RewindTest.pm
+++ b/src/bin/pg_rewind/RewindTest.pm
@@ -116,7 +116,7 @@ sub setup_cluster
 {
 
 	# Initialize master, data checksums are mandatory
-	$node_master = get_new_node();
+	$node_master = get_new_node('master');
 	$node_master->init;
 
 	# Custom parameters for master's postgresql.conf
@@ -144,7 +144,7 @@ sub start_master
 
 sub create_standby
 {
-	$node_standby = get_new_node();
+	$node_standby = get_new_node('standby');
 	$node_master->backup('my_backup');
 	$node_standby->init_from_backup($node_master, 'my_backup');
 	my $connstr_master = $node_master->connstr('postgres');
diff --git a/src/bin/scripts/t/010_clusterdb.pl b/src/bin/scripts/t/010_clusterdb.pl
index 5131b35d82b..11d678a8676 100644
--- a/src/bin/scripts/t/010_clusterdb.pl
+++ b/src/bin/scripts/t/010_clusterdb.pl
@@ -9,7 +9,7 @@ program_help_ok('clusterdb');
 program_version_ok('clusterdb');
 program_options_handling_ok('clusterdb');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/011_clusterdb_all.pl b/src/bin/scripts/t/011_clusterdb_all.pl
index 15cd30cf4a1..efd541bc4d6 100644
--- a/src/bin/scripts/t/011_clusterdb_all.pl
+++ b/src/bin/scripts/t/011_clusterdb_all.pl
@@ -5,7 +5,7 @@ use PostgresNode;
 use TestLib;
 use Test::More tests => 2;
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/020_createdb.pl b/src/bin/scripts/t/020_createdb.pl
index e0cf8600585..c0f6067a923 100644
--- a/src/bin/scripts/t/020_createdb.pl
+++ b/src/bin/scripts/t/020_createdb.pl
@@ -9,7 +9,7 @@ program_help_ok('createdb');
 program_version_ok('createdb');
 program_options_handling_ok('createdb');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/030_createlang.pl b/src/bin/scripts/t/030_createlang.pl
index 4097f03da99..38e351670d0 100644
--- a/src/bin/scripts/t/030_createlang.pl
+++ b/src/bin/scripts/t/030_createlang.pl
@@ -9,7 +9,7 @@ program_help_ok('createlang');
 program_version_ok('createlang');
 program_options_handling_ok('createlang');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/040_createuser.pl b/src/bin/scripts/t/040_createuser.pl
index fcada6338c6..5b3309e9c12 100644
--- a/src/bin/scripts/t/040_createuser.pl
+++ b/src/bin/scripts/t/040_createuser.pl
@@ -9,7 +9,7 @@ program_help_ok('createuser');
 program_version_ok('createuser');
 program_options_handling_ok('createuser');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/050_dropdb.pl b/src/bin/scripts/t/050_dropdb.pl
index 2adc80a03f7..fb4f6564811 100644
--- a/src/bin/scripts/t/050_dropdb.pl
+++ b/src/bin/scripts/t/050_dropdb.pl
@@ -9,7 +9,7 @@ program_help_ok('dropdb');
 program_version_ok('dropdb');
 program_options_handling_ok('dropdb');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/060_droplang.pl b/src/bin/scripts/t/060_droplang.pl
index 722804747ed..904cea2d4bd 100644
--- a/src/bin/scripts/t/060_droplang.pl
+++ b/src/bin/scripts/t/060_droplang.pl
@@ -9,7 +9,7 @@ program_help_ok('droplang');
 program_version_ok('droplang');
 program_options_handling_ok('droplang');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/070_dropuser.pl b/src/bin/scripts/t/070_dropuser.pl
index 0849f77ed67..22079f6742c 100644
--- a/src/bin/scripts/t/070_dropuser.pl
+++ b/src/bin/scripts/t/070_dropuser.pl
@@ -9,7 +9,7 @@ program_help_ok('dropuser');
 program_version_ok('dropuser');
 program_options_handling_ok('dropuser');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/080_pg_isready.pl b/src/bin/scripts/t/080_pg_isready.pl
index 8f3f25cc364..d9830b5b3ad 100644
--- a/src/bin/scripts/t/080_pg_isready.pl
+++ b/src/bin/scripts/t/080_pg_isready.pl
@@ -11,7 +11,7 @@ program_options_handling_ok('pg_isready');
 
 command_fails(['pg_isready'], 'fails with no server running');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/090_reindexdb.pl b/src/bin/scripts/t/090_reindexdb.pl
index fd4eac347e1..7f57af8e391 100644
--- a/src/bin/scripts/t/090_reindexdb.pl
+++ b/src/bin/scripts/t/090_reindexdb.pl
@@ -9,7 +9,7 @@ program_help_ok('reindexdb');
 program_version_ok('reindexdb');
 program_options_handling_ok('reindexdb');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/091_reindexdb_all.pl b/src/bin/scripts/t/091_reindexdb_all.pl
index d47b18b9892..8e6041460c5 100644
--- a/src/bin/scripts/t/091_reindexdb_all.pl
+++ b/src/bin/scripts/t/091_reindexdb_all.pl
@@ -4,7 +4,7 @@ use warnings;
 use PostgresNode;
 use Test::More tests => 2;
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/100_vacuumdb.pl b/src/bin/scripts/t/100_vacuumdb.pl
index 387d2b41e23..c183ccb6a19 100644
--- a/src/bin/scripts/t/100_vacuumdb.pl
+++ b/src/bin/scripts/t/100_vacuumdb.pl
@@ -9,7 +9,7 @@ program_help_ok('vacuumdb');
 program_version_ok('vacuumdb');
 program_options_handling_ok('vacuumdb');
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/101_vacuumdb_all.pl b/src/bin/scripts/t/101_vacuumdb_all.pl
index 8f1536f44fe..43212587e5a 100644
--- a/src/bin/scripts/t/101_vacuumdb_all.pl
+++ b/src/bin/scripts/t/101_vacuumdb_all.pl
@@ -4,7 +4,7 @@ use warnings;
 use PostgresNode;
 use Test::More tests => 2;
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/bin/scripts/t/102_vacuumdb_stages.pl b/src/bin/scripts/t/102_vacuumdb_stages.pl
index 4cb5b64877d..1300aa79057 100644
--- a/src/bin/scripts/t/102_vacuumdb_stages.pl
+++ b/src/bin/scripts/t/102_vacuumdb_stages.pl
@@ -4,7 +4,7 @@ use warnings;
 use PostgresNode;
 use Test::More tests => 4;
 
-my $node = get_new_node();
+my $node = get_new_node('main');
 $node->init;
 $node->start;
 
diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm
index 0632be25ca6..6152c601bdc 100644
--- a/src/test/perl/PostgresNode.pm
+++ b/src/test/perl/PostgresNode.pm
@@ -43,6 +43,7 @@ INIT
 sub new
 {
 	my $class  = shift;
+	my $name   = shift;
 	my $pghost = shift;
 	my $pgport = shift;
 	my $testname = basename($0);
@@ -51,8 +52,8 @@ sub new
 		_port     => $pgport,
 		_host     => $pghost,
 		_basedir  => TestLib::tempdir,
-		_applname => "node_$pgport",
-		_logfile  => "$TestLib::log_path/${testname}_node_${pgport}.log" };
+		_name     => $name,
+		_logfile  => "$TestLib::log_path/${testname}_${name}.log" };
 
 	bless $self, $class;
 	$self->dump_info;
@@ -78,10 +79,10 @@ sub basedir
 	return $self->{_basedir};
 }
 
-sub applname
+sub name
 {
 	my ($self) = @_;
-	return $self->{_applname};
+	return $self->{_name};
 }
 
 sub logfile
@@ -127,11 +128,11 @@ sub backup_dir
 sub dump_info
 {
 	my ($self) = @_;
+	print "Name: " . $self->name . "\n";
 	print "Data directory: " . $self->data_dir . "\n";
 	print "Backup directory: " . $self->backup_dir . "\n";
 	print "Archive directory: " . $self->archive_dir . "\n";
 	print "Connection string: " . $self->connstr . "\n";
-	print "Application name: " . $self->applname . "\n";
 	print "Log file: " . $self->logfile . "\n";
 }
 
@@ -178,7 +179,7 @@ sub init
 	TestLib::system_or_bail($ENV{PG_REGRESS}, '--config-auth', $pgdata);
 
 	open my $conf, ">>$pgdata/postgresql.conf";
-	print $conf "\n# Added by PostgresNode.pm)\n";
+	print $conf "\n# Added by PostgresNode.pm\n";
 	print $conf "fsync = off\n";
 	print $conf "log_statement = all\n";
 	print $conf "port = $port\n";
@@ -210,8 +211,9 @@ sub backup
 	my ($self, $backup_name) = @_;
 	my $backup_path = $self->backup_dir . '/' . $backup_name;
 	my $port        = $self->port;
+	my $name        = $self->name;
 
-	print "# Taking backup $backup_name from node with port $port\n";
+	print "# Taking backup $backup_name from node \"$name\"\n";
 	TestLib::system_or_bail("pg_basebackup -D $backup_path -p $port -x");
 	print "# Backup finished\n";
 }
@@ -221,11 +223,13 @@ sub init_from_backup
 	my ($self, $root_node, $backup_name) = @_;
 	my $backup_path = $root_node->backup_dir . '/' . $backup_name;
 	my $port        = $self->port;
-	my $root_port   = $root_node->port;
+	my $node_name   = $self->name;
+	my $root_name   = $root_node->name;
 
 	print
-"Initializing node $port from backup \"$backup_name\" of node $root_port\n";
-	die "Backup $backup_path does not exist" unless -d $backup_path;
+"# Initializing node \"$node_name\" from backup \"$backup_name\" of node \"$root_name\"\n";
+	die "Backup \"$backup_name\" does not exist at $backup_path"
+	  unless -d $backup_path;
 
 	mkdir $self->backup_dir;
 	mkdir $self->archive_dir;
@@ -249,7 +253,8 @@ sub start
 	my ($self) = @_;
 	my $port   = $self->port;
 	my $pgdata = $self->data_dir;
-	print("### Starting test server in $pgdata\n");
+	my $name   = $self->name;
+	print("### Starting node \"$name\"\n");
 	my $ret = TestLib::system_log('pg_ctl', '-w', '-D', $self->data_dir, '-l',
 		$self->logfile, 'start');
 
@@ -261,7 +266,6 @@ sub start
 	}
 
 	$self->_update_pid;
-
 }
 
 sub stop
@@ -269,8 +273,9 @@ sub stop
 	my ($self, $mode) = @_;
 	my $port   = $self->port;
 	my $pgdata = $self->data_dir;
+	my $name   = $self->name;
 	$mode = 'fast' if (!defined($mode));
-	print "### Stopping node in $pgdata with port $port using mode $mode\n";
+	print "### Stopping node \"$name\" using mode $mode\n";
 	TestLib::system_log('pg_ctl', '-D', $pgdata, '-m', $mode, 'stop');
 	$self->{_pid} = undef;
 	$self->_update_pid;
@@ -282,6 +287,8 @@ sub restart
 	my $port    = $self->port;
 	my $pgdata  = $self->data_dir;
 	my $logfile = $self->logfile;
+	my $name    = $self->name;
+	print "### Restarting node \"$name\"\n";
 	TestLib::system_log('pg_ctl', '-D', $pgdata, '-w', '-l', $logfile,
 		'restart');
 	$self->_update_pid;
@@ -290,6 +297,7 @@ sub restart
 sub _update_pid
 {
 	my $self = shift;
+	my $name = $self->name;
 
 	# If we can open the PID file, read its first line and that's the PID we
 	# want.  If the file cannot be opened, presumably the server is not
@@ -297,7 +305,7 @@ sub _update_pid
 	if (open my $pidfile, $self->data_dir . "/postmaster.pid")
 	{
 		chomp($self->{_pid} = <$pidfile>);
-		print "# Postmaster PID is $self->{_pid}\n";
+		print "# Postmaster PID for node \"$name\" is $self->{_pid}\n";
 		close $pidfile;
 		return;
 	}
@@ -316,6 +324,7 @@ sub _update_pid
 # for another node even when this one is not active.
 sub get_new_node
 {
+	my $name  = shift;
 	my $found = 0;
 	my $port  = $last_port_assigned;
 
@@ -340,7 +349,7 @@ sub get_new_node
 	print "# Found free port $port\n";
 
 	# Lock port number found by creating a new node
-	my $node = new PostgresNode($test_pghost, $port);
+	my $node = new PostgresNode($name, $test_pghost, $port);
 
 	# Add node to list of nodes
 	push(@all_nodes, $node);
@@ -354,8 +363,9 @@ sub get_new_node
 sub DESTROY
 {
 	my $self = shift;
+	my $name = $self->name;
 	return if not defined $self->{_pid};
-	print "# signalling QUIT to $self->{_pid}\n";
+	print "### Signalling QUIT to $self->{_pid} for node \"$name\"\n";
 	TestLib::system_log('pg_ctl', 'kill', 'QUIT', $self->{_pid});
 }
 
@@ -371,7 +381,8 @@ sub psql
 	my ($self, $dbname, $sql) = @_;
 
 	my ($stdout, $stderr);
-	print("# Running SQL command: $sql\n");
+	my $name = $self->name;
+	print("### Running SQL command on node \"$name\": $sql\n");
 
 	IPC::Run::run [ 'psql', '-XAtq', '-d', $self->connstr($dbname), '-f',
 		'-' ], '<', \$sql, '>', \$stdout, '2>', \$stderr
diff --git a/src/test/ssl/t/001_ssltests.pl b/src/test/ssl/t/001_ssltests.pl
index cd38e455973..80e8ea1fe79 100644
--- a/src/test/ssl/t/001_ssltests.pl
+++ b/src/test/ssl/t/001_ssltests.pl
@@ -74,7 +74,7 @@ chmod 0600, "ssl/client.key";
 #### Part 0. Set up the server.
 
 diag "setting up data directory...";
-my $node = get_new_node();
+my $node = get_new_node('master');
 $node->init;
 
 # PGHOST is enforced here to set up the node, subsequent connections
-- 
GitLab