def test_hello_world_checksum_sha256(self): """ Test calculating the sha256 of a string """ chksum = CheckSum('nofile', 'sha256') shasum = chksum.hashfile(self.fake_file) self.assertEqual(self.hello_world_sha256sum, shasum)
def test_compare_file_not_match(self, mock_get_hashes): """ compute checksum for a file and it should not match """ mock_get_hashes.return_value = self.hello_world_sha256sum chksum = CheckSum('onefile') self.assertFalse(chksum.compare('badchecksum'))
def test_hello_world_checksum_md5(self): """ Test calculating the md5 of a string """ chksum = CheckSum('nofile', 'md5') mdsum = chksum.hashfile(self.fake_file) self.assertEqual(self.hello_world_md5sum, mdsum)
def test_compare_dir_not_match(self, mock_get_hashes): """ compute checksum for a directory and it should not match """ mock_get_hashes.return_value = self.increment_hash_multi chksum = CheckSum('onedir') self.assertFalse(chksum.compare('badchecksum'))
def test_get_hash_multi(self, mock_isfile): """ Calculate the hash of files in a directory """ mock_isfile.return_value = False chksum = CheckSum('onedir') chksum.get_hash(u"./emptydir") self.assertEqual(self.increment_hash_emptydir, chksum._increment_hash)
def test_compute_dir(self, mock_hashes): """ Test hashing a directory """ mock_hashes.return_value = self.increment_hash_multi chksum = CheckSum('onedir') chksum.count = 2 result = chksum.compute() self.assertEquals(self.dir_compute, result)
def test_compute_file(self, mock_get_checksum): """ Test compute the checksum of a file """ mock_get_checksum.return_value = self.hello_world_sha256sum chksum = CheckSum('onefile') chksum.count = 1 result = chksum.compute() self.assertEquals(self.file_compute, result)
def execute(self): conf = self.conf LOG.info('Executing FS restore...') restore_timestamp = None restore_abs_path = conf.restore_abs_path if conf.restore_from_date: restore_timestamp = utils.date_to_timestamp(conf.restore_from_date) if conf.backup_media == 'fs': self.engine.restore( hostname_backup_name=self.conf.hostname_backup_name, restore_path=restore_abs_path, overwrite=conf.overwrite, recent_to_date=conf.restore_from_date) try: if conf.consistency_checksum: backup_checksum = conf.consistency_checksum restore_checksum = CheckSum(restore_abs_path, ignorelinks=True) if restore_checksum.compare(backup_checksum): LOG.info('Consistency check success.') else: raise ConsistencyCheckException( "Backup Consistency Check failed: backup checksum " "({0}) and restore checksum ({1}) did not match.". format(backup_checksum, restore_checksum.checksum)) except OSError as e: raise ConsistencyCheckException( "Backup Consistency Check failed: could not checksum file" " {0} ({1})".format(e.filename, e.strerror)) return {} res = restore.RestoreOs(conf.client_manager, conf.container) if conf.backup_media == 'nova': LOG.info("Restoring nova backup. Instance ID: {0}, timestamp: {1}". format(conf.nova_inst_id, restore_timestamp)) nova_network = None if conf.nova_restore_network: nova_network = conf.nova_restore_network res.restore_nova(conf.nova_inst_id, restore_timestamp, nova_network) elif conf.backup_media == 'cinder': LOG.info("Restoring cinder backup from glance. Volume ID: {0}, " "timestamp: {1}".format(conf.cinder_vol_id, restore_timestamp)) res.restore_cinder_by_glance(conf.cinder_vol_id, restore_timestamp) elif conf.backup_media == 'cindernative': LOG.info("Restoring cinder native backup. Volume ID {0}, Backup ID" " {1}, timestamp: {2}".format(conf.cindernative_vol_id, conf.cindernative_backup_id, restore_timestamp)) res.restore_cinder(conf.cindernative_vol_id, conf.cindernative_backup_id, restore_timestamp) else: raise Exception("unknown backup type: %s" % conf.backup_media) return {}
def test_get_hash_files(self, mock_isfile, mock_open): """ Test calculating the hash of a file """ mock_isfile.return_value = True mock_open.return_value = self.fake_file chksum = CheckSum('onefile') chksum.get_hash('onefile') self.assertEqual(self.increment_hash_one, chksum._increment_hash) chksum.get_hash('otherfile') self.assertEqual(self.increment_hash_multi, chksum._increment_hash)
def execute_method(self): conf = self.conf logging.info('[*] Executing FS restore...') restore_timestamp = None restore_abs_path = conf.restore_abs_path if conf.restore_from_date: restore_timestamp = utils.date_to_timestamp(conf.restore_from_date) if conf.backup_media == 'fs': backup = self.storage.find_one(conf.hostname_backup_name, restore_timestamp) self.engine.restore(backup, restore_abs_path, conf.overwrite) try: if conf.consistency_checksum: backup_checksum = conf.consistency_checksum restore_checksum = CheckSum(restore_abs_path, ignorelinks=True) if restore_checksum.compare(backup_checksum): logging.info('[*] Consistency check success.') else: raise ConsistencyCheckException( "Backup Consistency Check failed: backup checksum " "({0}) and restore checksum ({1}) did not match.". format(backup_checksum, restore_checksum.checksum)) except OSError as e: raise ConsistencyCheckException( "Backup Consistency Check failed: could not checksum file" " {0} ({1})".format(e.filename, e.strerror)) return {} res = restore.RestoreOs(conf.client_manager, conf.container) if conf.backup_media == 'nova': res.restore_nova(conf.nova_inst_id, restore_timestamp) elif conf.backup_media == 'cinder': res.restore_cinder_by_glance(conf.cinder_vol_id, restore_timestamp) elif conf.backup_media == 'cindernative': res.restore_cinder(conf.cindernative_vol_id, restore_timestamp) else: raise Exception("unknown backup type: %s" % conf.backup_media) return {}
def test_unknown_hasher_type(self): """ Test un-known hash algorithm """ with self.assertRaises(ValueError): CheckSum('nope', 'bulshit')
def backup(self, app_mode): """ :type app_mode: freezer.mode.mode.Mode :return: """ backup_media = self.conf.backup_media time_stamp = utils.DateTime.now().timestamp self.conf.time_stamp = time_stamp if backup_media == 'fs': LOG.info('Path to backup: {0}'.format(self.conf.path_to_backup)) app_mode.prepare() snapshot_taken = snapshot.snapshot_create(self.conf) if snapshot_taken: app_mode.release() try: filepath = '.' chdir_path = os.path.expanduser( os.path.normpath(self.conf.path_to_backup.strip())) if not os.path.exists(chdir_path): msg = 'Path to backup does not exist {0}'.format( chdir_path) LOG.critical(msg) raise IOError(msg) if not os.path.isdir(chdir_path): filepath = os.path.basename(chdir_path) chdir_path = os.path.dirname(chdir_path) os.chdir(chdir_path) # Checksum for Backup Consistency if self.conf.consistency_check: ignorelinks = (self.conf.dereference_symlink == 'none' or self.conf.dereference_symlink == 'hard') consistency_checksum = CheckSum( filepath, ignorelinks=ignorelinks).compute() LOG.info('Computed checksum for consistency {0}'.format( consistency_checksum)) self.conf.consistency_checksum = consistency_checksum return self.engine.backup( backup_path=filepath, hostname_backup_name=self.conf.hostname_backup_name, no_incremental=self.conf.no_incremental, max_level=self.conf.max_level, always_level=self.conf.always_level, restart_always_level=self.conf.restart_always_level) finally: # whether an error occurred or not, remove the snapshot anyway app_mode.release() if snapshot_taken: snapshot.snapshot_remove(self.conf, self.conf.shadow, self.conf.windows_volume) backup_os = backup.BackupOs(self.conf.client_manager, self.conf.container, self.storage) if backup_media == 'nova': LOG.info('Executing nova backup. Instance ID: {0}'.format( self.conf.nova_inst_id)) backup_os.backup_nova(self.conf.nova_inst_id) elif backup_media == 'cindernative': LOG.info('Executing cinder native backup. Volume ID: {0}, ' 'incremental: {1}'.format(self.conf.cindernative_vol_id, self.conf.incremental)) backup_os.backup_cinder(self.conf.cindernative_vol_id, incremental=self.conf.incremental) elif backup_media == 'cinder': LOG.info('Executing cinder snapshot. Volume ID: {0}'.format( self.conf.cinder_vol_id)) backup_os.backup_cinder_by_glance(self.conf.cinder_vol_id) else: raise Exception('unknown parameter backup_media %s' % backup_media) return None