mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Refactor routine to find single log content pattern in TAP tests
The same routine to check if a specific pattern can be found in the server logs was copied over four different test scripts. This refactors the whole to use a single routine located in PostgreSQL::Test::Cluster, named log_contains, to grab the contents of the server logs and check for a specific pattern. On HEAD, the code previously used assumed that slurp_file() could not handle an undefined offset, setting it to zero, but slurp_file() does do an extra fseek() before retrieving the log contents only if an offset is defined. In two places, the test was retrieving the full log contents with slurp_file() after calling substr() to apply an offset, ignoring that slurp_file() would be able to handle that. Backpatch all the way down to ease the introduction of new tests that could rely on the new routine. Author: Vignesh C Reviewed-by: Andrew Dunstan, Dagfinn Ilmari Mannsåker, Michael Paquier Discussion: https://postgr.es/m/CALDaNm0YSiLpjCmajwLfidQrFOrLNKPQir7s__PeVvh9U3uoTQ@mail.gmail.com Backpatch-through: 11
This commit is contained in:
		@@ -2470,6 +2470,21 @@ sub log_check
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
=pod
 | 
					=pod
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					=item $node->log_contains(pattern, offset)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Find pattern in logfile of node after offset byte.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					=cut
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					sub log_contains
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						my ($self, $pattern, $offset) = @_;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return TestLib::slurp_file($self->logfile, $offset) =~ m/$pattern/;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					=pod
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=item $node->run_log(...)
 | 
					=item $node->run_log(...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Runs a shell command like TestLib::run_log, but with connection parameters set
 | 
					Runs a shell command like TestLib::run_log, but with connection parameters set
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -168,8 +168,7 @@ $node_primary->wait_for_catchup($node_standby, 'replay', $start_lsn);
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
$node_standby->stop;
 | 
					$node_standby->stop;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ok( !find_in_log(
 | 
					ok( !$node_standby->log_contains(
 | 
				
			||||||
		$node_standby,
 | 
					 | 
				
			||||||
		"requested WAL segment [0-9A-F]+ has already been removed"),
 | 
							"requested WAL segment [0-9A-F]+ has already been removed"),
 | 
				
			||||||
	'check that required WAL segments are still available');
 | 
						'check that required WAL segments are still available');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -191,8 +190,7 @@ $node_primary->safe_psql('postgres', "CHECKPOINT;");
 | 
				
			|||||||
my $invalidated = 0;
 | 
					my $invalidated = 0;
 | 
				
			||||||
for (my $i = 0; $i < 10000; $i++)
 | 
					for (my $i = 0; $i < 10000; $i++)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (find_in_log(
 | 
						if ($node_primary->log_contains(
 | 
				
			||||||
			$node_primary,
 | 
					 | 
				
			||||||
			"invalidating slot \"rep1\" because its restart_lsn [0-9A-F/]+ exceeds max_slot_wal_keep_size",
 | 
								"invalidating slot \"rep1\" because its restart_lsn [0-9A-F/]+ exceeds max_slot_wal_keep_size",
 | 
				
			||||||
			$logstart))
 | 
								$logstart))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -215,7 +213,7 @@ is($result, "rep1|f|t|lost|",
 | 
				
			|||||||
my $checkpoint_ended = 0;
 | 
					my $checkpoint_ended = 0;
 | 
				
			||||||
for (my $i = 0; $i < 10000; $i++)
 | 
					for (my $i = 0; $i < 10000; $i++)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (find_in_log($node_primary, "checkpoint complete: ", $logstart))
 | 
						if ($node_primary->log_contains("checkpoint complete: ", $logstart))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		$checkpoint_ended = 1;
 | 
							$checkpoint_ended = 1;
 | 
				
			||||||
		last;
 | 
							last;
 | 
				
			||||||
@@ -245,8 +243,7 @@ $node_standby->start;
 | 
				
			|||||||
my $failed = 0;
 | 
					my $failed = 0;
 | 
				
			||||||
for (my $i = 0; $i < 10000; $i++)
 | 
					for (my $i = 0; $i < 10000; $i++)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (find_in_log(
 | 
						if ($node_standby->log_contains(
 | 
				
			||||||
			$node_standby,
 | 
					 | 
				
			||||||
			"requested WAL segment [0-9A-F]+ has already been removed",
 | 
								"requested WAL segment [0-9A-F]+ has already been removed",
 | 
				
			||||||
			$logstart))
 | 
								$logstart))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -351,8 +348,7 @@ advance_wal($node_primary3, 2);
 | 
				
			|||||||
my $max_attempts = $TestLib::timeout_default;
 | 
					my $max_attempts = $TestLib::timeout_default;
 | 
				
			||||||
while ($max_attempts-- >= 0)
 | 
					while ($max_attempts-- >= 0)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (find_in_log(
 | 
						if ($node_primary3->log_contains(
 | 
				
			||||||
			$node_primary3,
 | 
					 | 
				
			||||||
			"terminating process $senderpid to release replication slot \"rep3\"",
 | 
								"terminating process $senderpid to release replication slot \"rep3\"",
 | 
				
			||||||
			$logstart))
 | 
								$logstart))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -374,8 +370,7 @@ $node_primary3->poll_query_until('postgres',
 | 
				
			|||||||
$max_attempts = $TestLib::timeout_default;
 | 
					$max_attempts = $TestLib::timeout_default;
 | 
				
			||||||
while ($max_attempts-- >= 0)
 | 
					while ($max_attempts-- >= 0)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (find_in_log(
 | 
						if ($node_primary3->log_contains(
 | 
				
			||||||
			$node_primary3,
 | 
					 | 
				
			||||||
			'invalidating slot "rep3" because its restart_lsn', $logstart))
 | 
								'invalidating slot "rep3" because its restart_lsn', $logstart))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		ok(1, "slot invalidation logged");
 | 
							ok(1, "slot invalidation logged");
 | 
				
			||||||
@@ -412,17 +407,3 @@ sub get_log_size
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return (stat $node->logfile)[7];
 | 
						return (stat $node->logfile)[7];
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
# find $pat in logfile of $node after $off-th byte
 | 
					 | 
				
			||||||
sub find_in_log
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	my ($node, $pat, $off) = @_;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	$off = 0 unless defined $off;
 | 
					 | 
				
			||||||
	my $log = TestLib::slurp_file($node->logfile);
 | 
					 | 
				
			||||||
	return 0 if (length($log) <= $off);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	$log = substr($log, $off);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return $log =~ m/$pat/;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -132,21 +132,11 @@ while ($max_attempts-- >= 0)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	last
 | 
						last
 | 
				
			||||||
	  if (
 | 
						  if (
 | 
				
			||||||
		find_in_log(
 | 
							$node_standby->log_contains(
 | 
				
			||||||
			$node_standby, qr!WARNING: ( [A-Z0-9]+:)? creating missing directory: pg_tblspc/!,
 | 
								qr!WARNING: ( [A-Z0-9]+:)? creating missing directory: pg_tblspc/!,
 | 
				
			||||||
			$logstart));
 | 
								$logstart));
 | 
				
			||||||
	usleep(100_000);
 | 
						usleep(100_000);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
ok($max_attempts > 0, "invalid directory creation is detected");
 | 
					ok($max_attempts > 0, "invalid directory creation is detected");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
done_testing();
 | 
					done_testing();
 | 
				
			||||||
 | 
					 | 
				
			||||||
# find $pat in logfile of $node after $off-th byte
 | 
					 | 
				
			||||||
sub find_in_log
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	my ($node, $pat, $off) = @_;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile, $off);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return $log =~ m/$pat/;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user