def test_rpe_from_stackdriver_data(filename, expected_resource_type, expected_operation_type, expected_resource_name):
    log_message = get_test_data(filename)

    assets = StackdriverParser._extract_asset_info(log_message)
    asset_info = assets[0]

    GoogleAPIResource.from_resource_data(client_kwargs=test_google_args, **asset_info)
Пример #2
0
def test_bad_asset_type():

    with pytest.raises(ResourceException) as excinfo:
        GoogleAPIResource.from_cai_data(
            '//cloudfakeservice.googleapis.com/widgets/test-resource',
            'cloudfakeservice.googleapis.com/Widget',
            client_kwargs=client_kwargs,
        )

    assert 'Unrecognized asset type' in str(excinfo.value)
Пример #3
0
def test_bad_resource_type():

    with pytest.raises(ResourceException) as excinfo:
        GoogleAPIResource.from_cai_data(
            "//cloudfakeservice.googleapis.com/widgets/test-resource",
            "cloudfakeservice.googleapis.com/Widget",
            client_kwargs=client_kwargs,
        )

    assert "Unrecognized resource type" in str(excinfo.value)
Пример #4
0
def test_bad_content_type():

    with pytest.raises(ResourceException) as excinfo:
        GoogleAPIResource.from_cai_data(
            '//cloudresourcemanager.googleapis.com/projects/test-resource',
            'cloudresourcemanager.googleapis.com/Project',
            'fake_content_type',
            client_kwargs=client_kwargs,
        )

    assert 'Unrecognized content type' in str(excinfo.value)
Пример #5
0
def test_gcp_to_dict():
    r = GoogleAPIResource.from_resource_data(
        resource_type='storage.googleapis.com/Bucket',
        client_kwargs=client_kwargs,
        name=test_resource_name,
        project_id=test_project,
    )

    data = r.to_dict()
    # with no creds, we should still get this key but it should be none
    assert data['project_id'] == test_project
Пример #6
0
    def extract(cls, message):

        message_data = json.loads(message.data)

        name = message_data.get("name")
        asset_type = message_data.get("asset_type")
        project_id = message_data.get("project_id")
        metadata = message_data.get("metadata") or {}
        metadata.update((get_pubsub_message_metadata(message)).dict())

        resource = GoogleAPIResource.from_cai_data(name,
                                                   asset_type,
                                                   project_id=project_id)

        metadata = MicromanagerMetadata(**metadata)

        return ExtractedResources(resources=[resource], metadata=metadata)
Пример #7
0
    def parse_message(cls, message):

        message_data = json.loads(message.data)
        publish_timestamp = int(message.publish_time.timestamp())

        m = EnforcementMessage(**message_data)

        resource = GoogleAPIResource.from_cai_data(m.name,
                                                   m.asset_type,
                                                   project_id=m.project_id)

        return ParsedMessage(
            resources=[resource],
            metadata=m.metadata,
            control_data=m.control_data,
            timestamp=publish_timestamp,
        )
Пример #8
0
    def get_resources(cls, log_message):
        asset_info = cls._extract_asset_info(log_message)

        return [GoogleAPIResource.from_resource_data(**i) for i in asset_info]
Пример #9
0
def test_gcp_full_resource_name(case):
    r = GoogleAPIResource.from_resource_data(resource_type=case.resource_type, client_kwargs=client_kwargs, **case.resource_data)
    assert r.full_resource_name() == case.name
Пример #10
0
def test_gcp_resource_bad_type():
    with pytest.raises(ResourceException) as excinfo:
        GoogleAPIResource.from_resource_data(resource_type='fake.type')

    assert 'Unrecognized resource type' in str(excinfo.value)
Пример #11
0
def test_gcp_from_resource_no_type():
    with pytest.raises(TypeError) as excinfo:
        GoogleAPIResource.from_resource_data(project_id=test_project)

    assert 'required keyword-only argument' in str(excinfo.value)
    assert 'resource_type' in str(excinfo.value)
Пример #12
0
def test_gcp_from_resource(case):
    r = GoogleAPIResource.from_resource_data(resource_type=case.resource_type, client_kwargs=client_kwargs, **case.resource_data)
    assert r.__class__ == case.cls
    assert isinstance(r._get_request_args(), dict)
Пример #13
0
 def add_resource():
     r = resource_data.copy()
     res = GoogleAPIResource.from_resource_data(**r)
     resources.append(res)
Пример #14
0
def test_todict_eats_exceptions(name, asset_type, http):
    res = GoogleAPIResource.from_cai_data(name, asset_type, http=http)

    res.to_dict()
Пример #15
0
def test_inferred_data_lookup_success(name, asset_type, http, inferred_key):
    res = GoogleAPIResource.from_cai_data(name, asset_type, http=http)

    assert inferred_key not in res._resource_data
    res.to_dict()
    assert inferred_key in res._resource_data