def test_extract_and_create_slices_two_reps(self): """Testing the extract method with valid buffer content.""" source_uuid = str(uuid.uuid4()) metadata_json = { 'source': source_uuid, 'report_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319', 'report_slices': { str(self.uuid): {} } } report_json = {'report_slice_id': str(self.uuid)} report_files = { 'metadata.json': metadata_json, '%s.json' % str(self.uuid): report_json } self.processor.report_or_slice = self.report_record self.processor.account_number = '0001' buffer_content = test_handler.create_tar_buffer(report_files) result = self.processor._extract_and_create_slices(buffer_content) expected_result = { 'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319', 'source': source_uuid, } self.assertEqual(result, expected_result)
def test_download_report_success(self): """Test to verify extracting contents is successful.""" metadata_json = { 'report_id': 1, 'report_type': 'insights', 'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319', 'report_slices': { '2345322': {} } } report_json = { 'report_slice_id': '2345322', 'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319' } report_files = { 'metadata.json': metadata_json, '2345322.json': report_json } self.processor.upload_message = { 'url': self.payload_url, 'rh_account': '00001' } buffer_content = test_handler.create_tar_buffer(report_files) with requests_mock.mock() as mock_req: mock_req.get(self.payload_url, content=buffer_content) content = self.processor._download_report() self.assertEqual(buffer_content, content)
def test_transition_to_downloaded(self): """Test that the transition to download works successfully.""" metadata_json = { 'report_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319', 'source': str(uuid.uuid4()), 'report_slices': { str(self.uuid): {} } } report_json = { 'report_slice_id': str(self.uuid), 'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319' } report_files = { '%s.json' % str(self.uuid): report_json, 'metadata.json': metadata_json } self.processor.upload_message = { 'url': self.payload_url, 'rh_account': '00001' } self.processor.report_or_slice = self.report_record self.processor.account_number = '0001' buffer_content = test_handler.create_tar_buffer(report_files) with requests_mock.mock() as mock_req: mock_req.get(self.payload_url, content=buffer_content) self.processor.transition_to_downloaded() self.assertEqual(self.report_record.state, Report.DOWNLOADED)
def test_extract_and_create_slices_failure_no_metadata(self): """Testing the extract method failure no json file.""" report_json = { 'report_slice_id': '2345322', 'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319' } report_files = {'2345322.json': report_json} buffer_content = test_handler.create_tar_buffer(report_files) with self.assertRaises(report_processor.FailExtractException): self.processor._extract_and_create_slices(buffer_content)
def test_extract_and_create_slices_failure(self): """Testing the extract method failure no matching report_slice.""" metadata_json = { 'report_id': 1, 'report_type': 'insights', 'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319', 'report_slices': { str(self.uuid): {} } } report_files = {'metadata.json': metadata_json} buffer_content = test_handler.create_tar_buffer(report_files) with self.assertRaises(report_processor.FailExtractException): self.processor._extract_and_create_slices(buffer_content)
def test_extract_and_create_slices_metadata_fail(self): """Testing the extract method with invalid metadata buffer content.""" metadata_json = 'myfakeencodedstring' slice_uuid = str(self.uuid) report_json = {'report_slice_id': slice_uuid} report_files = { 'metadata.json': metadata_json, '%s.json' % slice_uuid: report_json } self.processor.report_or_slice = self.report_record self.processor.account_number = '0001' buffer_content = test_handler.create_tar_buffer(report_files, meta_encoding='utf-16') with self.assertRaises(report_processor.FailExtractException): self.processor._extract_and_create_slices(buffer_content)
def test__extract_and_create_slices_failure_no_json(self): """Testing the extract method failure invalid json.""" metadata_json = { 'report_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319', 'source': str(uuid.uuid4()), 'report_slices': { '2345322': {} } } report_json = None report_files = { '2345322.json': report_json, 'metadata.json': metadata_json } buffer_content = test_handler.create_tar_buffer(report_files) with self.assertRaises(report_processor.FailExtractException): self.processor._extract_and_create_slices(buffer_content)
def test_download_contents_raises_error(self): """Test to verify downloading contents fails when error is raised.""" report_json = { 'report_id': 1, 'report_type': 'insights', 'status': 'completed', 'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319' } self.processor.upload_message = { 'url': self.payload_url, 'rh_account': '00001' } report_files = {'report.json': report_json} buffer_content = test_handler.create_tar_buffer(report_files) with requests_mock.mock() as mock_req: mock_req.get(self.payload_url, content=buffer_content) with patch('requests.get', side_effect=requests.exceptions.HTTPError): with self.assertRaises( report_processor.RetryDownloadException): content = self.processor._download_report() self.assertEqual(content, buffer_content)
def test_extract_and_create_slices_slice_fail(self): """Testing the extract method with bad slice.""" metadata_json = { 'report_id': 1, 'source': str(uuid.uuid4()), 'source_metadata': { 'foo': 'bar' }, 'report_slices': { str(self.uuid): {} } } report_json = 'myfakeencodedstring' report_files = { 'metadata.json': metadata_json, '%s.json' % str(self.uuid): report_json } self.processor.report_or_slice = self.report_record self.processor.account_number = '0001' buffer_content = test_handler.create_tar_buffer(report_files, encoding='utf-16', meta_encoding='utf-8') with self.assertRaises(report_processor.FailExtractException): self.processor._extract_and_create_slices(buffer_content)
def test_extract_and_create_slices_mismatch(self): """Testing the extract method with mismatched metadata content.""" metadata_json = { 'report_id': 1, 'source': str(uuid.uuid4()), 'source_metadata': { 'foo': 'bar' }, 'report_slices': { str(self.uuid): { 'number_hosts': 5 } } } report_json = {'report_slice_id': '1234556'} report_files = { 'metadata.json': metadata_json, '%s.json' % str(self.uuid): report_json } self.processor.report_or_slice = self.report_record self.processor.account_number = '0001' buffer_content = test_handler.create_tar_buffer(report_files) with self.assertRaises(report_processor.FailExtractException): self.processor._extract_and_create_slices(buffer_content)