From 9c41e2a9fb9cc7cb95d1a144fe091ae411328e8b Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Mon, 15 Mar 2021 15:01:43 -0700
Subject: [PATCH v7 3/4] Extend pg_amcheck test suite

One of the pg_amcheck regression tests creates numerous corruptions
and only then verifies that pg_amcheck reports all expected
corruption messages and no others.  This overlooks that if a bug
exists such that corruption is reported where none exists, it will
be harder to determine what went wrong if pg_amcheck is only run
after corruptions do exit.  Therefore, check that pg_amcheck reports
no corruptions early in the test case.
---
 src/bin/pg_amcheck/t/003_check.pl | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/bin/pg_amcheck/t/003_check.pl b/src/bin/pg_amcheck/t/003_check.pl
index 10a70b4ae3..66dd14e498 100644
--- a/src/bin/pg_amcheck/t/003_check.pl
+++ b/src/bin/pg_amcheck/t/003_check.pl
@@ -3,7 +3,7 @@ use warnings;
 
 use PostgresNode;
 use TestLib;
-use Test::More tests => 60;
+use Test::More tests => 63;
 
 my ($node, $port, %corrupt_page, %remove_relation);
 
@@ -309,11 +309,6 @@ plan_to_remove_relation_file('db2', 's1.t1_btree');
 # Leave 'db3' uncorrupted
 #
 
-# Perform the corruptions we planned above using only a single database restart.
-#
-perform_all_corruptions();
-
-
 # Standard first arguments to TestLib functions
 my @cmd = ('pg_amcheck', '--quiet', '-p', $port);
 
@@ -323,6 +318,22 @@ my $line_pointer_corruption_re = qr/line pointer/;
 my $missing_file_re = qr/could not open file ".*": No such file or directory/;
 my $index_missing_relation_fork_re = qr/index ".*" lacks a main relation fork/;
 
+# We have created test databases with tables populated with data, but have not
+# yet corrupted anything.  As such, we expect no corruption and verify that
+# none is reported
+#
+$node->command_checks_all(
+	[ @cmd, '-d', 'db1', '-d', 'db2', '-d', 'db3' ],
+	0,
+	[ $no_output_re ],
+	[ $no_output_re ],
+	'pg_amcheck prior to corruption');
+
+# Perform the corruptions we planned above using only a single database restart.
+#
+perform_all_corruptions();
+
+
 # Checking databases with amcheck installed and corrupt relations, pg_amcheck
 # command itself should return exit status = 2, because tables and indexes are
 # corrupt, not exit status = 1, which would mean the pg_amcheck command itself
-- 
2.21.1 (Apple Git-122.3)

