class TestBinaryService(unittest.TestCase): def setUp(self): self.config = get_config_for_testing() self.mongo_server = MongoMgr(config=self.config) self._init_test_data() self.binary_service = BinaryService(config=self.config) def _init_test_data(self): self.backend_db_interface = BackEndDbInterface(config=self.config) self.backend_db_interface.add_firmware(TEST_FW) self.backend_db_interface.shutdown() def tearDown(self): self.mongo_server.shutdown() gc.collect() def test_get_binary_and_file_name(self): binary, file_name = self.binary_service.get_binary_and_file_name( TEST_FW.uid) self.assertEqual(file_name, TEST_FW.file_name, 'file_name not correct') self.assertEqual(binary, TEST_FW.binary, 'invalid result not correct') def test_get_binary_and_file_name_invalid_uid(self): binary, file_name = self.binary_service.get_binary_and_file_name( 'invalid_uid') self.assertIsNone(binary, 'should be none') self.assertIsNone(file_name, 'should be none') def test_get_repacked_binary_and_file_name(self): tar, file_name = self.binary_service.get_repacked_binary_and_file_name( TEST_FW.uid) self.assertEqual(file_name, '{}.tar.gz'.format(TEST_FW.file_name), 'file_name not correct') file_type = magic.from_buffer(tar, mime=False) assert 'gzip compressed data' in file_type, 'Result is not an tar.gz file' def test_get_repacked_binary_and_file_name_invalid_uid(self): binary, file_name = self.binary_service.get_repacked_binary_and_file_name( 'invalid_uid') self.assertIsNone(binary, 'should be none') self.assertIsNone(file_name, 'should be none')
class InterComBackEndRawDownloadTask(InterComListenerAndResponder): CONNECTION_TYPE = 'raw_download_task' OUTGOING_CONNECTION_TYPE = 'raw_download_task_resp' def __init__(self, config=None): super().__init__(config) self.binary_service = BinaryService(config=self.config) def get_response(self, task): return self.binary_service.get_binary_and_file_name(task)
class TestBinaryService: def setup(self): self.tmp_dir = TemporaryDirectory() self.config = get_config_for_testing(temp_dir=self.tmp_dir) self.mongo_server = MongoMgr(config=self.config) self._init_test_data() self.binary_service = BinaryService(config=self.config) def _init_test_data(self): self.backend_db_interface = BackEndDbInterface(config=self.config) self.backend_db_interface.add_firmware(TEST_FW) store_binary_on_file_system(self.tmp_dir.name, TEST_FW) self.backend_db_interface.shutdown() def teardown(self): self.tmp_dir.cleanup() self.mongo_server.shutdown() gc.collect() def test_get_binary_and_file_name(self): binary, file_name = self.binary_service.get_binary_and_file_name(TEST_FW.uid) assert file_name == TEST_FW.file_name, 'file_name not correct' assert binary == TEST_FW.binary, 'invalid result not correct' def test_get_binary_and_file_name_invalid_uid(self): binary, file_name = self.binary_service.get_binary_and_file_name('invalid_uid') assert binary is None, 'should be none' assert file_name is None, 'should be none' def test_get_repacked_binary_and_file_name(self): tar, file_name = self.binary_service.get_repacked_binary_and_file_name(TEST_FW.uid) assert file_name == '{}.tar.gz'.format(TEST_FW.file_name), 'file_name not correct' file_type = magic.from_buffer(tar, mime=False) assert 'gzip compressed data' in file_type, 'Result is not an tar.gz file' def test_get_repacked_binary_and_file_name_invalid_uid(self): binary, file_name = self.binary_service.get_repacked_binary_and_file_name('invalid_uid') assert binary is None, 'should be none' assert file_name is None, 'should be none'
def compare(self, uid_list): logging.info(f'Compare in progress: {uid_list}') bs = BinaryService(config=self.config) fo_list = [] for uid in uid_list: fo = self.db_interface.get_complete_object_including_all_summaries( uid) fo.binary = bs.get_binary_and_file_name(fo.uid)[0] fo_list.append(fo) return self.compare_objects(fo_list)
def compare(self, uid_list): logging.info('Compare in progress: {}'.format(uid_list)) bs = BinaryService(config=self.config) fo_list = [] for uid in uid_list: try: fo = self.db_interface.get_complete_object_including_all_summaries(uid) fo.binary = bs.get_binary_and_file_name(fo.uid)[0] fo_list.append(fo) except Exception as exception: return exception return self.compare_objects(fo_list)
def get_response(self, task): binary_service = BinaryService(config=self.config) result = binary_service.get_binary_and_file_name(task) return result