1####################################################################################################################################
2# Tests for archive-push command to make sure aync queue limits are implemented correctly
3####################################################################################################################################
4package pgBackRestTest::Module::Mock::MockArchiveStopTest;
5use parent 'pgBackRestTest::Env::HostEnvTest';
6
7####################################################################################################################################
8# Perl includes
9####################################################################################################################################
10use strict;
11use warnings FATAL => qw(all);
12use Carp qw(confess);
13
14use File::Basename qw(dirname);
15
16use pgBackRestDoc::Common::Exception;
17use pgBackRestDoc::Common::Ini;
18use pgBackRestDoc::Common::Log;
19
20use pgBackRestTest::Env::ArchiveInfo;
21use pgBackRestTest::Env::BackupInfo;
22use pgBackRestTest::Env::HostEnvTest;
23use pgBackRestTest::Env::Host::HostBackupTest;
24use pgBackRestTest::Env::Manifest;
25use pgBackRestTest::Common::DbVersion;
26use pgBackRestTest::Common::ExecuteTest;
27use pgBackRestTest::Common::RunTest;
28use pgBackRestTest::Common::StorageRepo;
29use pgBackRestTest::Common::VmTest;
30use pgBackRestTest::Common::Wait;
31
32####################################################################################################################################
33# run
34####################################################################################################################################
35sub run
36{
37    my $self = shift;
38
39    # Generate test WAL file
40    my $strWalTestFile = $self->testPath() . '/test-wal-' . PG_VERSION_94;
41    my $strWalHash = $self->walGenerateContentChecksum(PG_VERSION_94);
42    storageTest()->put($strWalTestFile, $self->walGenerateContent(PG_VERSION_94));
43
44    foreach my $rhRun
45    (
46        {vm => VM2, remote => false, storage =>    S3, encrypt => false, compress =>  NONE, error => 0},
47        {vm => VM2, remote =>  true, storage => POSIX, encrypt =>  true, compress =>  NONE, error => 0},
48        {vm => VM3, remote => false, storage =>   GCS, encrypt =>  true, compress =>   BZ2, error => 0},
49        {vm => VM3, remote =>  true, storage => AZURE, encrypt => false, compress =>   LZ4, error => 1},
50        {vm => VM4, remote => false, storage =>    S3, encrypt =>  true, compress =>  NONE, error => 0},
51        {vm => VM4, remote =>  true, storage =>   GCS, encrypt => false, compress =>   ZST, error => 0},
52    )
53    {
54        # Only run tests for this vm
55        next if ($rhRun->{vm} ne vmTest($self->vm()));
56
57        # Increment the run, log, and decide whether this unit test should be run
58        my $bRemote = $rhRun->{remote};
59        my $strStorage = $rhRun->{storage};
60        my $bEncrypt = $rhRun->{encrypt};
61        my $strCompressType = $rhRun->{compress};
62        my $iError = $rhRun->{error};
63
64        # Increment the run, log, and decide whether this unit test should be run
65        if (!$self->begin(
66                "rmt ${bRemote}, cmp ${strCompressType}, error " . ($iError ? 'connect' : 'version') .
67                    ", storage ${strStorage}, enc ${bEncrypt}")) {next}
68
69        # Create hosts, file object, and config
70        my ($oHostDbPrimary, $oHostDbStandby, $oHostBackup) = $self->setup(
71            true, $self->expect(), {bHostBackup => $bRemote, strCompressType => $strCompressType, bArchiveAsync => true,
72            strStorage => $strStorage, bRepoEncrypt => $bEncrypt});
73
74        # Create compression extension
75        my $strCompressExt = $strCompressType ne NONE ? ".${strCompressType}" : '';
76
77        # Create the wal path
78        my $strWalPath = $oHostDbPrimary->dbBasePath() . '/pg_xlog';
79        storageTest()->pathCreate($strWalPath, {bCreateParent => true});
80
81        # Create the test path for pg_control and generate pg_control for stanza-create
82        storageTest()->pathCreate($oHostDbPrimary->dbBasePath() . '/' . DB_PATH_GLOBAL, {bCreateParent => true});
83        $self->controlGenerate($oHostDbPrimary->dbBasePath(), PG_VERSION_94);
84
85        # Create the archive info file
86        $oHostBackup->stanzaCreate('create required data for stanza', {strOptionalParam => '--no-online'});
87
88        # Push a WAL segment
89        &log(INFO, '    push first WAL');
90        $oHostDbPrimary->archivePush($strWalPath, $strWalTestFile, 1);
91
92        # Break the database version of the archive info file
93        if ($iError == 0)
94        {
95            $oHostBackup->infoMunge(
96                $oHostBackup->repoArchivePath(ARCHIVE_INFO_FILE),
97                {&INFO_ARCHIVE_SECTION_DB => {&INFO_ARCHIVE_KEY_DB_VERSION => '8.0'},
98                 &INFO_ARCHIVE_SECTION_DB_HISTORY => {1 => {&INFO_ARCHIVE_KEY_DB_VERSION => '8.0'}}});
99        }
100
101        # Push two more segments with errors to exceed archive-push-queue-max
102        &log(INFO, '    push second WAL');
103
104        $oHostDbPrimary->archivePush(
105            $strWalPath, $strWalTestFile, 2, ERROR_REPO_INVALID, undef, $iError ? '--repo1-host=bogus' : undef);
106
107        &log(INFO, '    push third WAL');
108
109        $oHostDbPrimary->archivePush(
110            $strWalPath, $strWalTestFile, 3, ERROR_REPO_INVALID, undef, $iError ? '--repo1-host=bogus' : undef);
111
112        # Now this segment will get dropped
113        &log(INFO, '    push fourth WAL');
114
115        $oHostDbPrimary->archivePush($strWalPath, $strWalTestFile, 4, undef, undef, '--repo1-host=bogus');
116
117        # Fix the database version
118        if ($iError == 0)
119        {
120            $oHostBackup->infoRestore($oHostBackup->repoArchivePath(ARCHIVE_INFO_FILE));
121        }
122
123        #---------------------------------------------------------------------------------------------------------------------------
124        $self->testResult(
125            sub {storageRepo()->list($oHostBackup->repoArchivePath(PG_VERSION_94 . '-1/0000000100000001'))},
126            "000000010000000100000001-${strWalHash}${strCompressExt}",
127            'segment 2-4 not pushed', {iWaitSeconds => 5});
128
129        #---------------------------------------------------------------------------------------------------------------------------
130        $oHostDbPrimary->archivePush($strWalPath, $strWalTestFile, 5);
131
132        $self->testResult(
133            sub {storageRepo()->list($oHostBackup->repoArchivePath(PG_VERSION_94 . '-1/0000000100000001'))},
134            "(000000010000000100000001-${strWalHash}${strCompressExt}, " .
135                "000000010000000100000005-${strWalHash}${strCompressExt})",
136            'segment 5 is pushed', {iWaitSeconds => 5});
137    }
138}
139
1401;
141