def test_archive_batch(self, archive_wal_mock, get_next_batch_mock, fsync_mock, caplog): """ Test archive using batch limit """ # See all logs caplog.set_level(0) # Setup the test fxlogdb_mock = MagicMock() backup_manager = MagicMock() archiver = FileWalArchiver(backup_manager) archiver.config.name = "test_server" wal_info = WalFileInfo(name="test_wal_file") wal_info.orig_filename = "test_wal_file" wal_info2 = WalFileInfo(name="test_wal_file2") wal_info2.orig_filename = "test_wal_file2" # Test queue with batch limit 1 with a list of 2 files batch = WalArchiverQueue([wal_info, wal_info2], batch_size=1) assert batch.size == 2 assert batch.run_size == 1 get_next_batch_mock.return_value = batch archiver.archive(fxlogdb_mock) # check the log for messages assert ("Found %s xlog segments from %s for %s." " Archive a batch of %s segments in this run." % (batch.size, archiver.name, archiver.config.name, batch.run_size)) in caplog.text assert ("Batch size reached (%s) - " "Exit %s process for %s" % (batch.batch_size, archiver.name, archiver.config.name)) in caplog.text
def test_archive( self, datetime_mock, move_mock, archive_wal_mock, get_next_batch_mock, unlink_mock, capsys, caplog, ): """ Test FileWalArchiver.archive method """ # See all logs caplog.set_level(0) fxlogdb_mock = MagicMock() backup_manager = MagicMock() archiver = FileWalArchiver(backup_manager) archiver.config.name = "test_server" archiver.config.errors_directory = "/server/errors" wal_info = WalFileInfo(name="test_wal_file") wal_info.orig_filename = "test_wal_file" batch = WalArchiverQueue([wal_info]) assert batch.size == 1 assert batch.run_size == 1 get_next_batch_mock.return_value = batch archive_wal_mock.side_effect = DuplicateWalFile datetime_mock.utcnow.return_value.strftime.return_value = "test_time" archiver.archive(fxlogdb_mock) out, err = capsys.readouterr() assert ( "\tError: %s is already present in server %s. " "File moved to errors directory." % (wal_info.name, archiver.config.name) ) in out assert ( "\tError: %s is already present in server %s. " "File moved to errors directory." % (wal_info.name, archiver.config.name) ) in caplog.text archive_wal_mock.side_effect = MatchingDuplicateWalFile archiver.archive(fxlogdb_mock) unlink_mock.assert_called_with(wal_info.orig_filename) # Test batch errors caplog_reset(caplog) batch.errors = ["testfile_1", "testfile_2"] archive_wal_mock.side_effect = DuplicateWalFile archiver.archive(fxlogdb_mock) out, err = capsys.readouterr() assert ( "Some unknown objects have been found while " "processing xlog segments for %s. " "Objects moved to errors directory:" % archiver.config.name ) in out assert ( "Archiver is about to move %s unexpected file(s) to errors " "directory for %s from %s" % (len(batch.errors), archiver.config.name, archiver.name) ) in caplog.text assert ( "Moving unexpected file for %s from %s: %s" % (archiver.config.name, archiver.name, "testfile_1") ) in caplog.text assert ( "Moving unexpected file for %s from %s: %s" % (archiver.config.name, archiver.name, "testfile_2") ) in caplog.text move_mock.assert_any_call( "testfile_1", os.path.join( archiver.config.errors_directory, "%s.%s.unknown" % ("testfile_1", "test_time"), ), ) move_mock.assert_any_call( "testfile_2", os.path.join( archiver.config.errors_directory, "%s.%s.unknown" % ("testfile_2", "test_time"), ), )
def test_archive(self, datetime_mock, move_mock, archive_wal_mock, get_next_batch_mock, unlink_mock, capsys, caplog): """ Test FileWalArchiver.archive method """ fxlogdb_mock = MagicMock() backup_manager = MagicMock() archiver = FileWalArchiver(backup_manager) archiver.config.name = "test_server" wal_info = WalFileInfo(name="test_wal_file") wal_info.orig_filename = "test_wal_file" batch = WalArchiverQueue([wal_info]) assert batch.size == 1 assert batch.run_size == 1 get_next_batch_mock.return_value = batch archive_wal_mock.side_effect = DuplicateWalFile archiver.archive(fxlogdb_mock) out, err = capsys.readouterr() assert ("\tError: %s is already present in server %s. " "File moved to errors directory." % (wal_info.name, archiver.config.name)) in out assert ("\tError: %s is already present in server %s. " "File moved to errors directory." % (wal_info.name, archiver.config.name)) in caplog.text archive_wal_mock.side_effect = MatchingDuplicateWalFile archiver.archive(fxlogdb_mock) unlink_mock.assert_called_with(wal_info.orig_filename) # Test batch errors caplog_reset(caplog) datetime_mock.utcnow.strftime.return_value = 'test_time' batch.errors = ['testfile_1', 'testfile_2'] archive_wal_mock.side_effect = DuplicateWalFile archiver.archive(fxlogdb_mock) out, err = capsys.readouterr() assert ("Some unknown objects have been found while " "processing xlog segments for %s. " "Objects moved to errors directory:" % archiver.config.name) in out assert ("Archiver is about to move %s unexpected file(s) to errors " "directory for %s from %s" % (len(batch.errors), archiver.config.name, archiver.name)) in caplog.text assert ("Moving unexpected file for %s from %s: %s" % (archiver.config.name, archiver.name, 'testfile_1')) in caplog.text assert ("Moving unexpected file for %s from %s: %s" % (archiver.config.name, archiver.name, 'testfile_2')) in caplog.text move_mock.assert_any_call( 'testfile_1', os.path.join(archiver.config.errors_directory, "%s.%s.unknown" % ('testfile_1', 'test_time'))) move_mock.assert_any_call( 'testfile_2', os.path.join(archiver.config.errors_directory, "%s.%s.unknown" % ('testfile_2', 'test_time')))