def test_process_details_report_success(self): """Test processing a details report success.""" fact_collection = { 'name': 'dhcp181-3.gsslab.rdu2.redhat.com', 'metadata': {}, 'sources': [] } deployments_report = DeploymentsReport(report_id=1, id=1) deployments_report.save() details_report = DetailsReport(id=1, deployment_report=deployments_report) with patch('fingerprinter.task.FingerprintTaskRunner._process_sources', return_value=[fact_collection]): status_message, status = \ self.fp_task_runner._process_details_report('', details_report) self.assertIn('success', status_message.lower()) self.assertEqual(status, 'completed')
def test_process_details_report_exception(self): """Test processing a details report with an exception.""" fact_collection = { 'name': 'dhcp181-3.gsslab.rdu2.redhat.com', 'metadata': {}, 'sources': [] } deployments_report = DeploymentsReport(report_id=1, id=1) deployments_report.save() details_report = DetailsReport(id=1, deployment_report=deployments_report) with patch('fingerprinter.task.FingerprintTaskRunner._process_sources', return_value=[fact_collection]): with patch('fingerprinter.task.SystemFingerprintSerializer.save', side_effect=DataError): status_message, status = \ self.fp_task_runner._process_details_report('', details_report) self.assertIn('failed', status_message.lower()) self.assertEqual(status, 'failed')
def test_process_details_report_failed(self): """Test processing a details report no valid fps.""" fact_collection = {} deployments_report = DeploymentsReport(report_id=1) details_report = DetailsReport(deployment_report=deployments_report) with patch('fingerprinter.task.FingerprintTaskRunner._process_sources', return_value=fact_collection): status_message, status = \ self.fp_task_runner._process_details_report('', details_report) self.assertIn('failed', status_message.lower()) self.assertEqual(status, 'failed')
def setUp(self): """Create test case setup.""" management.call_command('flush', '--no-input') self.report_version = create_report_version() self.sys_platform_id = str(uuid.uuid4()) self.connection_uuid = str(uuid.uuid4()) self.fingerprints = \ [{ 'connection_host': '1.2.3.4', 'connection_port': 22, 'connection_uuid': self.connection_uuid, 'cpu_count': 2, 'cpu_core_per_socket': 1, 'cpu_siblings': 1, 'cpu_hyperthreading': False, 'cpu_socket_count': 2, 'cpu_core_count': 2, 'date_anaconda_log': '2017-07-18', 'date_yum_history': '2017-07-18', 'etc_release_name': '', 'etc_release_version': '', 'etc_release_release': '', 'uname_hostname': '1.2.3.4', 'virt_virt': 'virt-guest', 'virt_type': 'vmware', 'virt_num_guests': 1, 'virt_num_running_guests': 1, 'virt_what_type': 'vt', 'system_platform_id': self.sys_platform_id, 'ip_addresses': ['1.2.3.4']}] self.insights_hosts = [{ 'display_name': 'dhcp181-116.gsslab.rdu2.redhat.com', 'fqdn': 'dhcp181-116.gsslab.rdu2.redhat.com', 'bios_uuid': 'F1011E42-F121-ED73-0BB8-9CF9E721FC0A', 'ip_addresses': ['10.10.181.116'], 'mac_addresses': ['00:50:56:9e:7b:19'], 'subscription_manager_id': 'F1011E42-F121-ED73-0BB8-9CF9E721FC0A', 'facts': [{ 'namespace': 'qpc', 'facts': { 'bios_uuid': 'F1011E42-F121-ED73-0BB8-9CF9E721FC0A', 'ip_addresses': ['10.10.181.116'], 'mac_addresses': ['00:50:56:9e:7b:19'], 'subscription_manager_id': 'F1011E42-F121-ED73-0BB8-9CF9E721FC0A', 'name': 'dhcp181-116.gsslab.rdu2.redhat.com', 'os_release': 'Red Hat Enterprise Linux \ Server release 5.9 (Tikanga)', 'os_version': '5.9 (Tikanga)', 'infrastructure_type': 'virtualized', 'cpu_count': 1, 'architecture': 'x86_64', 'is_redhat': True, 'redhat_certs': '69.pem', 'cpu_socket_count': 1, 'cpu_core_count': 1 }, 'rh_product_certs': [], 'rh_products_installed': ['RHEL', 'EAP', 'FUSE'] }] }] self.deployments_report = DeploymentsReport( id=1, report_id=1, report_version=self.report_version, status=DeploymentsReport.STATUS_COMPLETE, cached_insights=None, cached_fingerprints=json.dumps(self.fingerprints))
class InsightsReportTest(TestCase): """Tests against the Insights reports function.""" # pylint: disable= no-self-use, invalid-name def setUp(self): """Create test case setup.""" management.call_command('flush', '--no-input') self.report_version = create_report_version() self.sys_platform_id = str(uuid.uuid4()) self.connection_uuid = str(uuid.uuid4()) self.fingerprints = \ [{ 'connection_host': '1.2.3.4', 'connection_port': 22, 'connection_uuid': self.connection_uuid, 'cpu_count': 2, 'cpu_core_per_socket': 1, 'cpu_siblings': 1, 'cpu_hyperthreading': False, 'cpu_socket_count': 2, 'cpu_core_count': 2, 'date_anaconda_log': '2017-07-18', 'date_yum_history': '2017-07-18', 'etc_release_name': '', 'etc_release_version': '', 'etc_release_release': '', 'uname_hostname': '1.2.3.4', 'virt_virt': 'virt-guest', 'virt_type': 'vmware', 'virt_num_guests': 1, 'virt_num_running_guests': 1, 'virt_what_type': 'vt', 'system_platform_id': self.sys_platform_id, 'ip_addresses': ['1.2.3.4']}] self.insights_hosts = [{ 'display_name': 'dhcp181-116.gsslab.rdu2.redhat.com', 'fqdn': 'dhcp181-116.gsslab.rdu2.redhat.com', 'bios_uuid': 'F1011E42-F121-ED73-0BB8-9CF9E721FC0A', 'ip_addresses': ['10.10.181.116'], 'mac_addresses': ['00:50:56:9e:7b:19'], 'subscription_manager_id': 'F1011E42-F121-ED73-0BB8-9CF9E721FC0A', 'facts': [{ 'namespace': 'qpc', 'facts': { 'bios_uuid': 'F1011E42-F121-ED73-0BB8-9CF9E721FC0A', 'ip_addresses': ['10.10.181.116'], 'mac_addresses': ['00:50:56:9e:7b:19'], 'subscription_manager_id': 'F1011E42-F121-ED73-0BB8-9CF9E721FC0A', 'name': 'dhcp181-116.gsslab.rdu2.redhat.com', 'os_release': 'Red Hat Enterprise Linux \ Server release 5.9 (Tikanga)', 'os_version': '5.9 (Tikanga)', 'infrastructure_type': 'virtualized', 'cpu_count': 1, 'architecture': 'x86_64', 'is_redhat': True, 'redhat_certs': '69.pem', 'cpu_socket_count': 1, 'cpu_core_count': 1 }, 'rh_product_certs': [], 'rh_products_installed': ['RHEL', 'EAP', 'FUSE'] }] }] self.deployments_report = DeploymentsReport( id=1, report_id=1, report_version=self.report_version, status=DeploymentsReport.STATUS_COMPLETE, cached_insights=None, cached_fingerprints=json.dumps(self.fingerprints)) def test_get_insights_report_200_exists(self): """Retrieve insights report.""" url = '/api/v1/reports/1/insights/' expected_hosts = [{'unique_id': i} for i in range(10001)] self.deployments_report.cached_insights = json.dumps(expected_hosts) self.deployments_report.save() with patch('api.insights_report.view.get_object_or_404', return_value=self.deployments_report): response = self.client.get(url) self.assertEqual(response.status_code, 200) response_json = response.json() self.assertIn(create_filename('metadata', 'json', 1), response_json.keys()) report_slices = {} for key in response_json: self.assertIn('report_id_1/', key) if key != 'report_id_1/metadata.json': report_slices[key] = response_json[key] # metadata slice number_hosts matches the actual # number of hosts in a slice report_slices_in_metadata = \ response_json['report_id_1/metadata.json']['report_slices'] total_returned_hosts_num = 0 for key_1, key_2 in zip(report_slices_in_metadata, report_slices): self.assertEqual(report_slices_in_metadata[key_1]['number_hosts'], len(report_slices[key_2]['hosts'])) # used later to check for the total size total_returned_hosts_num += len(report_slices[key_2]['hosts']) # no hosts lost returned_host_ids = { host['unique_id'] for slice_key in report_slices for host in report_slices[slice_key]['hosts'] } expected_host_ids = {host['unique_id'] for host in expected_hosts} self.assertSetEqual(returned_host_ids, expected_host_ids) # sum of all hosts in a slice is equal to # the total number of host (before call) self.assertEqual(total_returned_hosts_num, len(expected_hosts)) def test_get_insights_report_200_generate_exists(self): """Retrieve insights report.""" url = '/api/v1/reports/1/insights/' self.deployments_report.cached_insights = json.dumps( self.insights_hosts) self.deployments_report.save() with patch('api.insights_report.view.get_object_or_404', return_value=self.deployments_report): response = self.client.get(url) self.assertEqual(response.status_code, 200) response_json = response.json() self.assertIn(create_filename('metadata', 'json', 1), response_json.keys()) for key in response_json: self.assertIn('report_id_1/', key) def test_get_insights_report_404_no_canonical(self): """Retrieve insights report.""" url = '/api/v1/reports/1/insights/' no_canonical = [{ 'connection_host': '1.2.3.4', 'connection_port': 22, 'connection_uuid': self.connection_uuid, 'cpu_count': 2, 'cpu_core_per_socket': 1, 'cpu_siblings': 1, 'cpu_hyperthreading': False, 'cpu_socket_count': 2, 'cpu_core_count': 2, 'date_anaconda_log': '2017-07-18', 'date_yum_history': '2017-07-18', 'etc_release_name': '', 'etc_release_version': '', 'etc_release_release': '', 'uname_hostname': '1.2.3.4', 'virt_virt': 'virt-guest', 'virt_type': 'vmware', 'virt_num_guests': 1, 'virt_num_running_guests': 1, 'virt_what_type': 'vt', 'system_platform_id': self.sys_platform_id }] self.deployments_report.cached_insights = None self.deployments_report.cached_fingerprints = json.dumps(no_canonical) self.deployments_report.save() with patch('api.insights_report.view.get_object_or_404', return_value=self.deployments_report): response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_get_insights_report_404_missing(self): """Check that legacy report without insights returns 404.""" url = '/api/v1/reports/1/insights/' fingerprints = [{ 'connection_host': '1.2.3.4', 'connection_port': 22, 'connection_uuid': self.connection_uuid, 'cpu_count': 2, 'cpu_core_per_socket': 1, 'cpu_siblings': 1, 'cpu_hyperthreading': False, 'cpu_socket_count': 2, 'cpu_core_count': 2, 'date_anaconda_log': '2017-07-18', 'date_yum_history': '2017-07-18', 'etc_release_name': '', 'etc_release_version': '', 'etc_release_release': '', 'uname_hostname': '1.2.3.4', 'virt_virt': 'virt-guest', 'virt_type': 'vmware', 'virt_num_guests': 1, 'virt_num_running_guests': 1, 'virt_what_type': 'vt', 'mac_addresses': ['1.2.3.4'] }, { 'connection_host': '1.2.3.4', 'connection_port': 22, 'connection_uuid': self.connection_uuid, 'cpu_count': 2, 'cpu_core_per_socket': 1, 'cpu_siblings': 1, 'cpu_hyperthreading': False, 'cpu_socket_count': 2, 'cpu_core_count': 2, 'date_anaconda_log': '2017-07-18', 'date_yum_history': '2017-07-18', 'etc_release_name': '', 'etc_release_version': '', 'etc_release_release': '', 'uname_hostname': '1.2.3.4', 'virt_virt': 'virt-guest', 'virt_type': 'vmware', 'virt_num_guests': 1, 'virt_num_running_guests': 1, 'virt_what_type': 'vt', 'mac_addresses': ['1.2.3.4'], 'system_platform_id': self.sys_platform_id }] self.deployments_report.cached_insights = None self.deployments_report.cached_fingerprints = json.dumps(fingerprints) self.deployments_report.save() with patch('api.insights_report.view.get_object_or_404', return_value=self.deployments_report): response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_get_insights_report_bad_id(self): """Fail to get a report for bad id.""" url = '/api/v1/reports/string/insights/' # Query API response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_get_insights_nonexistent(self): """Fail to get a report for report id that doesn't exist.""" url = '/api/v1/reports/2/insights/' # Query API response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def setUp(self): """Create test case setup.""" self.deployment_report = DeploymentsReport( report_version=create_report_version()) self.deployment_report.save()
class FingerprintModelTest(TestCase): """Tests against the Fingerprint model.""" def setUp(self): """Create test case setup.""" self.deployment_report = DeploymentsReport( report_version=create_report_version()) self.deployment_report.save() ################################################################ # Test Model Create ################################################################ def test_empty_fingerprint(self): """Create an empty fingerprint.""" fingerprint_dict = {'deployment_report': self.deployment_report.id, 'metadata': {}, 'sources': []} serializer = SystemFingerprintSerializer(data=fingerprint_dict) is_valid = serializer.is_valid() if not is_valid: print(serializer.errors) self.assertTrue(is_valid) serializer.save() # pylint: disable=invalid-name def test_product_with_version_fingerprint(self): """Create a fingerprint with products.""" product_dict = {'name': 'product1', 'presence': 'unknown', 'version': ['1', '2'], 'metadata': {}} fingerprint_dict = {'deployment_report': self.deployment_report.id, 'metadata': {}, 'products': [product_dict], 'sources': []} serializer = SystemFingerprintSerializer(data=fingerprint_dict) is_valid = serializer.is_valid() if not is_valid: print(serializer.errors) self.assertTrue(is_valid) serializer.save() def test_product_fingerprint(self): """Create a fingerprint with products.""" product_dict = {'name': 'product1', 'presence': 'unknown', 'metadata': {}} fingerprint_dict = {'deployment_report': self.deployment_report.id, 'metadata': {}, 'products': [product_dict], 'sources': []} serializer = SystemFingerprintSerializer(data=fingerprint_dict) is_valid = serializer.is_valid() if not is_valid: print(serializer.errors) self.assertTrue(is_valid) serializer.save() def test_entitlement_fingerprint(self): """Create a fingerprint with entitlements.""" entitlement_dict = {'name': 'RHEL Server', 'entitlement_id': '69', 'metadata': {}} fingerprint_dict = {'deployment_report': self.deployment_report.id, 'metadata': {}, 'entitlements': [entitlement_dict], 'sources': []} serializer = SystemFingerprintSerializer(data=fingerprint_dict) is_valid = serializer.is_valid() if not is_valid: print(serializer.errors) self.assertTrue(is_valid) serializer.save()
def setUp(self): """Create test case setup.""" self.deployment_report = DeploymentsReport() self.deployment_report.save()