def setUp(self) -> None: try: from pytest_cov.embed import cleanup_on_sigterm except ImportError: pass else: cleanup_on_sigterm() shutil.rmtree('/tmp/backup_output', ignore_errors=True) os.mkdir('/tmp/backup_output') with open('/tmp/example', 'w') as example_file: example_file.write("asd") BackupFile.create_from_path('/tmp/example', "/tmp/backup_output/out2") SidecarProcess.logger = logging.getLogger("dummy") self.sidecar_process = SidecarProcess(TestSidecar.PORT, 3) self.p = Process(target=self.sidecar_process) self.p.start()
def __call__(self, *args, **kwargs): MockNodeHandler.BARRIER.wait() bf = BackupFile.create_from_path(MockNodeHandler.PATH_TO_BACKUP, self.write_file_path) if bf.get_hash() == self.previous_checksum: open(self.write_file_path + ".SAME", "w").close() else: open(self.write_file_path + ".CORRECT", "w").close()
def test_backup_same_checksum(self): expected_file = BackupFile.create_from_path('/tmp/example', "/tmp/backup_output/out2") node_handler_process = NodeHandlerProcess('localhost', TestSidecar.PORT, '/tmp/example', '/tmp/backup_output/out', expected_file.get_hash()) sleep(5) node_handler_process() self.assertTrue(os.path.exists('/tmp/backup_output/out.SAME'))
def test_simple_backup(self): node_handler_process = NodeHandlerProcess('localhost', TestSidecar.PORT, '/tmp/example', '/tmp/backup_output/out', 'dummy_checksum') sleep(5) node_handler_process() expected_file = BackupFile.create_from_path('/tmp/example', "/tmp/backup_output/out2") backup_file = BackupFile("/tmp/backup_output/out") self.assertEqual(expected_file.get_hash(), backup_file.get_hash())
def __handle_client_connection(client_sock, backup_no: int): """ Read message from a specific client socket and closes the socket If a problem arises in the communication with the client, the client socket will also be closed """ socket_transferer = BlockingSocketTransferer(client_sock) try: msg = socket_transferer.receive_plain_text() msg = json.loads(msg) path, previous_checksum = msg['path'], msg['checksum'] SidecarProcess.logger.debug("Previous checksum for path %s is '%s'" % (path, previous_checksum)) except (OSError, TimeoutError) as e: SidecarProcess.logger.exception("Error while reading socket %s: %s" % (client_sock, e)) socket_transferer.abort() return try: backup_file = BackupFile.create_from_path(path, TMP_BACKUP_PATH % backup_no) except Exception: SidecarProcess.logger.exception("Error while making backup file") socket_transferer.abort() return file_checksum = backup_file.get_hash() if file_checksum == previous_checksum: SidecarProcess.logger.info("Previous checksum equals to actual data, skipping backup") socket_transferer.send_plain_text("SAME") socket_transferer.abort() return else: socket_transferer.send_plain_text("DIFF") try: socket_transferer.send_file(TMP_BACKUP_PATH % backup_no) SidecarProcess.logger.debug("Backup file sent") socket_transferer.send_plain_text(file_checksum) except Exception as e: SidecarProcess.logger.exception("Error while writing socket %s: %s" % (client_sock, e)) socket_transferer.abort() return finally: socket_transferer.close() return
def test_generate_different_same_hash(self): backup_file = BackupFile.create_from_path('/tmp/test_path', '/tmp/file.tgz') backup_file2 = BackupFile.create_from_path('/tmp/test_path', '/tmp/file2.tgz') self.assertEqual(backup_file2.get_hash(), backup_file.get_hash())
def test_load_and_create_same_hash(self): backup_file = BackupFile.create_from_path('/tmp/test_path', '/tmp/file.tgz') backup_file2 = BackupFile('/tmp/file.tgz') self.assertEqual(backup_file2.get_hash(), backup_file.get_hash())