def test_poll_discovery_occurrence(self):
        # create discovery occurrence
        note_id = 'discovery-note-{}'.format(uuid.uuid4())
        client = containeranalysis_v1.ContainerAnalysisClient()
        grafeas_client = client.get_grafeas_client()
        note = {'discovery': {'analysis_kind': NoteKind.DISCOVERY}}
        grafeas_client.\
            create_note(parent=f"projects/{PROJECT_ID}", note_id=note_id, note=note)
        occurrence = {
            'note_name': f"projects/{PROJECT_ID}/notes/{note_id}",
            'resource_uri': self.image_url,
            'discovery': {
                'analysis_status':
                DiscoveryOccurrence.AnalysisStatus.FINISHED_SUCCESS
            }
        }
        created = grafeas_client.\
            create_occurrence(parent=f"projects/{PROJECT_ID}",
                              occurrence=occurrence)

        disc = samples.poll_discovery_finished(self.image_url, 10, PROJECT_ID)
        status = disc.discovery.analysis_status
        assert disc is not None
        assert status == DiscoveryOccurrence.AnalysisStatus.FINISHED_SUCCESS

        # clean up
        samples.delete_occurrence(basename(created.name), PROJECT_ID)
        samples.delete_note(note_id, PROJECT_ID)
Exemplo n.º 2
0
    def test_find_high_severity_vulnerabilities(self):
        occ_list = samples.find_high_severity_vulnerabilities_for_image(
            self.image_url, PROJECT_ID)
        assert len(occ_list) == 0

        # create new high severity vulnerability
        note_id = 'discovery-note-{}'.format(int(time()))
        client = containeranalysis_v1.ContainerAnalysisClient()
        grafeas_client = client.get_grafeas_client()
        note = {
            'vulnerability': {
                'severity':
                Severity.CRITICAL,
                'details': [{
                    'affected_cpe_uri': 'your-uri-here',
                    'affected_package': 'your-package-here',
                    'min_affected_version': {
                        'kind': Version.VersionKind.MINIMUM
                    },
                    'fixed_version': {
                        'kind': Version.VersionKind.MAXIMUM
                    }
                }]
            }
        }
        grafeas_client.\
            create_note(grafeas_client.project_path(PROJECT_ID), note_id, note)
        occurrence = {
            'note_name': client.note_path(PROJECT_ID, note_id),
            'resource_uri': self.image_url,
            'vulnerability': {
                'package_issue': [{
                    'affected_cpe_uri': 'your-uri-here',
                    'affected_package': 'your-package-here',
                    'min_affected_version': {
                        'kind': Version.VersionKind.MINIMUM
                    },
                    'fixed_version': {
                        'kind': Version.VersionKind.MAXIMUM
                    }
                }]
            }
        }
        created = grafeas_client.\
            create_occurrence(grafeas_client.project_path(PROJECT_ID),
                              occurrence)
        # query again
        tries = 0
        count = 0
        while count != 1 and tries < TRY_LIMIT:
            tries += 1
            occ_list = samples.find_vulnerabilities_for_image(
                self.image_url, PROJECT_ID)
            count = len(occ_list)
            sleep(SLEEP_TIME)
        assert len(occ_list) == 1
        # clean up
        samples.delete_occurrence(basename(created.name), PROJECT_ID)
        samples.delete_note(note_id, PROJECT_ID)
Exemplo n.º 3
0
 def test_delete_note(self):
     samples.delete_note(self.note_id, PROJECT_ID)
     try:
         samples.get_note(self.note_obj, PROJECT_ID)
     except InvalidArgument:
         pass
     else:
         # didn't raise exception we expected
         assert (False)
Exemplo n.º 4
0
    def test_poll_discovery_occurrence(self):
        # try with no discovery occurrence
        try:
            samples.poll_discovery_finished(self.image_url, 5, PROJECT_ID)
        except RuntimeError:
            pass
        else:
            # we expect timeout error
            assert False

        # create discovery occurrence
        note_id = 'discovery-note-{}'.format(uuid.uuid4())
        client = containeranalysis_v1.ContainerAnalysisClient()
        grafeas_client = client.get_grafeas_client()
        note = {
            'discovery': {
                'analysis_kind': NoteKind.DISCOVERY
            }
        }
        grafeas_client.\
            create_note(grafeas_client.project_path(PROJECT_ID), note_id, note)
        occurrence = {
            'note_name': grafeas_client.note_path(PROJECT_ID, note_id),
            'resource_uri': self.image_url,
            'discovery': {
                'analysis_status': DiscoveryOccurrence.AnalysisStatus
                                                      .FINISHED_SUCCESS
            }
        }
        created = grafeas_client.\
            create_occurrence(grafeas_client.project_path(PROJECT_ID),
                              occurrence)

        # poll again
        disc = samples.poll_discovery_finished(self.image_url, 10, PROJECT_ID)
        status = disc.discovery.analysis_status
        assert disc is not None
        assert status == DiscoveryOccurrence.AnalysisStatus.FINISHED_SUCCESS

        # clean up
        samples.delete_occurrence(basename(created.name), PROJECT_ID)
        samples.delete_note(note_id, PROJECT_ID)
Exemplo n.º 5
0
 def teardown_method(self, test_method):
     print('TEAR DOWN {}'.format(test_method.__name__))
     try:
         samples.delete_note(self.note_id, PROJECT_ID)
     except NotFound:
         pass