Пример #1
0
    def test_create_batch_setting_number(self):
        """Test creating a bach of deployment reports also creates fingerprints."""
        instances = DeploymentReportFactory.create_batch(
            size=10, number_of_fingerprints=10)

        assert instances[0].system_fingerprints.count() == 10
        assert DeploymentsReport.objects.all().count() == 10
        assert SystemFingerprint.objects.all().count() == 100
Пример #2
0
 def test_last_discovered_built_from_factory(self,
                                             django_assert_num_queries):
     """Test last_discovered property."""
     report_id = DeploymentReportFactory().id
     # query for deployment report to force only deployment report to be loaded
     deployment_report = DeploymentsReport.objects.get(id=report_id)
     report = ReportEntity.from_report_id(deployment_report.id)
     with django_assert_num_queries(0):
         assert isinstance(report.last_discovered, datetime)
Пример #3
0
 def test_last_discovered_initialized(self, django_assert_num_queries):
     """Test last_discovered property with ReportEntity initialized manually."""
     report_id = DeploymentReportFactory().id
     # query for deployment report to force only deployment report to be loaded
     deployment_report = DeploymentsReport.objects.get(id=report_id)
     report = ReportEntity(
         deployment_report,
         hosts=deployment_report.system_fingerprints.all())
     with django_assert_num_queries(2):
         assert isinstance(report.last_discovered, datetime)
Пример #4
0
 def test_build(self):
     """Check report_id with instances created by build method."""
     deployments_report: DeploymentsReport = DeploymentReportFactory.build()
     assert deployments_report.id is None
     assert deployments_report.report_id == deployments_report.id
     # save instance to db. it should have got an id
     deployments_report.save()
     assert deployments_report.id
     # using build method report_id won't match pk
     assert deployments_report.id != deployments_report.report_id
Пример #5
0
 def test_build_with_defaults(self, patched_randint):
     """Check no fingerprints are created when using build."""
     instance = DeploymentReportFactory.build()
     assert patched_randint.mock_calls == []
     assert instance.id is None
     assert DeploymentsReport.objects.all().count() == 0
     assert SystemFingerprint.objects.all().count() == 0
     # saving deployment won't create fingerprints
     instance.save()
     assert instance.id
     assert SystemFingerprint.objects.all().count() == 0
Пример #6
0
    def test_get_insights_report_200_generate_exists(self):
        """Retrieve insights report."""
        deployment_report = DeploymentReportFactory(
            status=DeploymentsReport.STATUS_COMPLETE,
        )
        url = f"/api/v1/reports/{deployment_report.id}/insights/"
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        response_json = response.json()

        self.assertIn(
            create_filename("metadata", "json", deployment_report.id),
            response_json.keys(),
        )
        for key in response_json:
            self.assertIn(f"report_id_{deployment_report.id}/", key)
Пример #7
0
    def test_get_insights_report_200_exists(self):
        """Retrieve insights report."""
        deployment_report = DeploymentReportFactory(
            number_of_fingerprints=11,
            status=DeploymentsReport.STATUS_COMPLETE,
        )
        url = f"/api/v1/reports/{deployment_report.id}/insights/"
        # mock slice size so we can expect 2 slices on this test
        with override_settings(QPC_INSIGHTS_REPORT_SLICE_SIZE=10):
            response = self.client.get(url)
        self.assertEqual(response.status_code, 200, response.json())
        response_json = response.json()

        self.assertIn(
            create_filename("metadata", "json", deployment_report.id),
            response_json.keys(),
        )
        report_slices = {}
        metadata_filename = f"report_id_{deployment_report.id}/metadata.json"
        for key in response_json:
            self.assertIn(f"report_id_{deployment_report.id}/", key)
            if key != metadata_filename:
                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[metadata_filename]["report_slices"]
        self.assertEqual(len(report_slices_in_metadata), 2)
        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_names = {
            host["bios_uuid"]
            for slice_key in report_slices  # pylint: disable=consider-using-dict-items
            for host in report_slices[slice_key]["hosts"]
        }
        expected_host_names = {
            host.bios_uuid for host in deployment_report.system_fingerprints.all()
        }
        self.assertSetEqual(returned_host_names, expected_host_names)
        # 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_host_names))
Пример #8
0
 def test_from_report_id_fingerprint_not_fount(self):
     """Check if the proper error is raised."""
     deployment_report = DeploymentReportFactory(number_of_fingerprints=0)
     with pytest.raises(SystemFingerprint.DoesNotExist):
         ReportEntity.from_report_id(deployment_report.id)
Пример #9
0
def report_entity():
    """Return a report entity with 100 hosts."""
    deployment_report = DeploymentReportFactory.build(id=42)
    fingerprints = SystemFingerprintFactory.build_batch(100)
    return ReportEntity(deployment_report, fingerprints)
Пример #10
0
def deployment_reports():
    """Return a list of deployment reports with variable number of fingerprints."""
    return DeploymentReportFactory.create_batch(size=5)
Пример #11
0
def report_entity():
    """Return a ReportEntity with 10 hosts."""
    deployment_report = DeploymentReportFactory.create(
        number_of_fingerprints=10)
    return ReportEntity.from_report_id(deployment_report.id)
Пример #12
0
 def test_create_batch(self):
     """Check report_id matches model pk using create_batch method."""
     deployments_reports = DeploymentReportFactory.create_batch(size=2)
     assert all(d.id for d in deployments_reports)
     assert all(d.id == d.report_id for d in deployments_reports)
Пример #13
0
 def test_create_with_defaults(self, patched_randint):
     """Test related fingerprint creation without default values."""
     DeploymentReportFactory.create()
     assert patched_randint.mock_calls == [mock.call(1, 5)]
     assert DeploymentsReport.objects.all().count() == 1
     assert SystemFingerprint.objects.all().count() == 1
Пример #14
0
 def test_build_setting_number(self):
     """Check for failure when setting number of fingerprints with build method."""
     with pytest.raises(ValueError):
         DeploymentReportFactory.build(number_of_fingerprints=1)