def test_download_propagates_exceptions(self): """ Test that download raises all exceptions from Swift. """ self.service.download.side_effect = SwiftError(None) with self.assertRaises(SwiftError): openstack_utils.download_swift_account(DOWNLOAD_FOLDER)
def test_download(self, auth, file_responses, expected_failed_files): """ Test for download_swift_account function. :param dict auth: User authorization :param list[dict] file_responses: List of dicts, each dictionary corresponds to a single response for downloaded file :param dict expected_failed_files: Expected response from the download call """ self.service.download.return_value = file_responses actual_failed_files = openstack_utils.download_swift_account(DOWNLOAD_FOLDER, **auth) self.assertEqual(dict(actual_failed_files), expected_failed_files)
def do_backup_swift(): """ Perform full swift backup sequence. """ logger.warning( 'SWIFT storage is not maintained and will be dropped in a future version.' ) with filter_logger('swiftclient.service', filter_swift), filter_logger('swiftclient', filter_swift): logger.info("Starting backup of swift containers") error_report = "" download_results = [] try: download_results = openstack_utils.download_swift_account( settings.BACKUP_SWIFT_TARGET) except SwiftError: error_report += "Miscellaneous error while downloading swift containers\n" logger.exception("Misc error while downloading swift containers") if download_results: error_report += "Following containers failed to download:\n" for container in download_results: error_report += "#. {name}; Failed files: {count}.\n".format( name=container.name, count=container.number_of_failures) error_report += "Extra information: \n{extra_info}".format( extra_info=container.extra_information) # In case of downloading errors run tarsnap nevertheless, so we at least backup something. tarsnap_successful = make_tarsnap_backup( keyfile=settings.BACKUP_SWIFT_TARSNAP_KEY_LOCATION, cachedir=settings.BACKUP_SWIFT_TARSNAP_CACHE_LOCATION, directory=settings.BACKUP_SWIFT_TARGET, archive_name="{}-{}".format( settings.BACKUP_SWIFT_TARSNAP_KEY_ARCHIVE_NAME, datetime.datetime.now().isoformat())) if not tarsnap_successful: error_report += "Error while running tarsnap\n" if error_report: error_report += "Please check the server logs, they might contain more details." mail_admins("Error when backing up swift containers", error_report) logger.error("Error when backing up swift containers\b %s", error_report) else: logger.info("Swift backup finished successfully") if settings.BACKUP_SWIFT_SNITCH: ping_heartbeat_url(settings.BACKUP_SWIFT_SNITCH)