From d325bf8a50923cc90df3cfbab77c496617d11c2c Mon Sep 17 00:00:00 2001
From: Amul Sul <amul.sul@enterprisedb.com>
Date: Wed, 6 Oct 2021 09:16:45 -0400
Subject: [PATCH] TAP-test_recovery_end_command_v4

---
 src/test/recovery/t/020_archive_status.pl | 52 ++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/src/test/recovery/t/020_archive_status.pl b/src/test/recovery/t/020_archive_status.pl
index cea65735a39..9ec5a2fff93 100644
--- a/src/test/recovery/t/020_archive_status.pl
+++ b/src/test/recovery/t/020_archive_status.pl
@@ -8,7 +8,7 @@ use strict;
 use warnings;
 use PostgresNode;
 use TestLib;
-use Test::More tests => 16;
+use Test::More tests => 19;
 use Config;
 
 my $primary = PostgresNode->new('primary');
@@ -234,3 +234,53 @@ ok( -f "$standby2_data/$segment_path_1_done"
 	  && -f "$standby2_data/$segment_path_2_done",
 	".done files created after archive success with archive_mode=always on standby"
 );
+
+# Test archive_cleanup_command and recovery_end_command execution
+my $standby3 = PostgresNode->new('standby3');
+$standby3->init_from_backup($primary, 'backup', has_restoring => 1);
+my $standby3_data = $standby3->data_dir;
+my $archive_cleanup_command_file = "$standby3_data/archive_cleanup_command.done";
+my $recovery_end_command_file = "$standby3_data/recovery_end_command.done";
+$standby3->append_conf('postgresql.conf',
+	"archive_cleanup_command = 'echo archive_cleanuped > $archive_cleanup_command_file'");
+$standby3->append_conf('postgresql.conf',
+	"recovery_end_command = 'echo recovery_ended > $recovery_end_command_file'");
+
+$standby3->start;
+$primary_lsn = $primary->lsn('write');
+$standby3->poll_query_until('postgres',
+	qq{ SELECT pg_wal_lsn_diff(pg_last_wal_replay_lsn(), '$primary_lsn') >= 0 }
+) or die "Timed out while waiting for xlog replay on standby3";
+
+# archive_cleanup_command executed with every restart point and that can be
+# trigger using checkpoint
+$standby3->safe_psql('postgres', q{CHECKPOINT});
+ok(-f "$archive_cleanup_command_file",
+	'archive_cleanup_command executed on checkpoint');
+
+# recovery_end_command_file executed only on recovery end which can happen on
+# promotion.
+$standby3->promote;
+ok(-f "$recovery_end_command_file",
+	'recovery_end_command executed after promotion');
+
+# Test effect of failing archive_cleanup_command and recovery_end_command execution
+my $standby4 = PostgresNode->new('standby4');
+$standby4->init_from_backup($primary, 'backup', has_restoring => 1);
+$standby4->append_conf('postgresql.conf',
+	"archive_cleanup_command = 'echo archive_cleanuped > /non_existing_dir/file'");
+$standby4->append_conf('postgresql.conf',
+	"recovery_end_command = 'echo recovery_ended > /non_existing_dir/file'");
+
+$standby4->start;
+$primary_lsn = $primary->lsn('write');
+$standby4->poll_query_until('postgres',
+	qq{ SELECT pg_wal_lsn_diff(pg_last_wal_replay_lsn(), '$primary_lsn') >= 0 }
+) or die "Timed out while waiting for xlog replay on standby4";
+
+# Failing to execute archive_cleanup_command and/or recovery_end_command does
+# not affect promotion.
+$standby4->safe_psql('postgres', q{CHECKPOINT});
+$standby4->promote;
+is($standby4->safe_psql( 'postgres', q{SELECT pg_is_in_recovery()}), 'f',
+	"standby promoted successfully despite incorrect archive_cleanup_command and recovery_end_command");
-- 
2.18.0

