def test_host_not_subscribed(self): """Test the host subscriptons method for not subscribed error.""" sub_url = 'https://{sat_host}:{port}/' \ 'api/v2/hosts/{host_id}/subscriptions' with requests_mock.Mocker() as mocker: url = construct_url(url=sub_url, sat_host='1.2.3.4', host_id=1) err_msg = { 'displayMessage': 'Host has not been registered ' 'with subscription-manager', 'errors': ['Host has not been registered' ' with subscription-manager'] } # noqa mocker.get(url, status_code=400, json=err_msg) result = request_host_details( self.scan_task, { 'job_id': self.scan_job.id, 'task_sequence_number': self.scan_task.id, 'scan_type': self.scan_task.scan_type, 'source_type': self.scan_task.source.source_type, 'source_name': self.scan_task.source.name }, 1, 'sys', url, url, {}) process_results(self.api, [result], 1) inspect_results = self.scan_task.inspection_result.systems.all() sys_1_result = inspect_results.filter(name='sys_1').first() self.assertEqual(sys_1_result.name, 'sys_1') self.assertEqual(sys_1_result.status, 'failed')
def test_request_host_details_err(self): """Test request_host_details for error mark a failed system.""" host_field_url = 'https://{sat_host}:{port}/api/v2/hosts/{host_id}' with requests_mock.Mocker() as mocker: url = construct_url(url=host_field_url, sat_host='1.2.3.4', host_id=1) mocker.get(url, status_code=500) result = request_host_details( self.scan_task, { 'job_id': self.scan_job.id, 'task_sequence_number': self.scan_task.id, 'scan_type': self.scan_task.scan_type, 'source_type': self.scan_task.source.source_type, 'source_name': self.scan_task.source.name }, 1, 'sys', url, url, {}) expected = { 'unique_name': 'sys_1', 'system_inspection_result': 'failed', 'host_fields_response': {}, 'host_subscriptions_response': {} } self.assertEqual(result, expected) inspect_result = self.scan_task.inspection_result self.assertEqual(len(inspect_result.systems.all()), 0)
def test_processing_fields_with_err(self): """Test the post_processing with error.""" host_field_url = 'https://{sat_host}:{port}/api/v2/hosts/{host_id}' with requests_mock.Mocker() as mocker: url = construct_url(url=host_field_url, sat_host='1.2.3.4', host_id=1) mocker.get(url, status_code=500) result = request_host_details( self.scan_task, { 'job_id': self.scan_job.id, 'task_sequence_number': self.scan_task.id, 'scan_type': self.scan_task.scan_type, 'source_type': self.scan_task.source.source_type, 'source_name': self.scan_task.source.name }, 1, 'sys', url, url, {}) expected = { 'unique_name': 'sys_1', 'system_inspection_result': 'failed', 'host_fields_response': {}, 'host_subscriptions_response': {} } self.assertEqual(result, expected) process_results(self.api, [result], 1) inspect_results = self.scan_task.inspection_result.systems.all() sys_1_result = inspect_results.filter(name='sys_1').first() self.assertEqual(sys_1_result.name, 'sys_1') self.assertEqual(sys_1_result.status, 'failed')
def test_processing_subs_err_nojson(self): """Test the flow of post processing with bad code and not json.""" sub_url = 'https://{sat_host}:{port}/' \ 'api/v2/hosts/{host_id}/subscriptions' with requests_mock.Mocker() as mocker: url = construct_url(url=sub_url, sat_host='1.2.3.4', host_id=1) mocker.get(url, status_code=404, text='error message') result = request_host_details( self.scan_task, { 'job_id': self.scan_job.id, 'task_sequence_number': self.scan_task.id, 'scan_type': self.scan_task.scan_type, 'source_type': self.scan_task.source.source_type, 'source_name': self.scan_task.source.name }, 1, 'sys', url, url, {}) process_results(self.api, [result], 1) inspect_results = self.scan_task.inspection_result.systems.all() sys_1_result = inspect_results.filter(name='sys_1').first() self.assertEqual(sys_1_result.name, 'sys_1') self.assertEqual(sys_1_result.status, 'failed')
def test_get_https_with_err(self): """Test the host subscriptons method with bad status code.""" sub_url = 'https://{sat_host}:{port}/' \ 'api/v2/hosts/{host_id}/subscriptions' with requests_mock.Mocker() as mocker: url = construct_url(url=sub_url, sat_host='1.2.3.4', host_id=1) mocker.get(url, status_code=500) result = request_host_details( self.scan_task, { 'job_id': self.scan_job.id, 'task_sequence_number': self.scan_task.id, 'scan_type': self.scan_task.scan_type, 'source_type': self.scan_task.source.source_type, 'source_name': self.scan_task.source.name }, 1, 'sys', url, url, {}) expected = { 'unique_name': 'sys_1', 'system_inspection_result': 'failed', 'host_fields_response': {}, 'host_subscriptions_response': {} } self.assertEqual(result, expected)