|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2019 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# flake8: noqa |
| 18 | + |
| 19 | +import os |
| 20 | +import pytest |
| 21 | + |
| 22 | +from google.api_core import exceptions |
| 23 | +from google.cloud import asset_v1 |
| 24 | +from google.cloud.asset_v1 import enums |
| 25 | + |
| 26 | +PROJECT_INSIDE = os.environ.get("PROJECT_ID", None) |
| 27 | +PROJECT_OUTSIDE = os.environ.get( |
| 28 | + "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT", None |
| 29 | +) |
| 30 | +IS_INSIDE_VPCSC = os.environ.get("GOOGLE_CLOUD_TESTS_IN_VPCSC", "false") |
| 31 | + |
| 32 | + |
| 33 | +class TestVPCServiceControl(object): |
| 34 | + @staticmethod |
| 35 | + def _is_rejected(call): |
| 36 | + try: |
| 37 | + responses = call() |
| 38 | + except exceptions.PermissionDenied as e: |
| 39 | + return e.message == "Request is prohibited by organization's policy" |
| 40 | + except: |
| 41 | + pass |
| 42 | + return False |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def _do_test(delayed_inside, delayed_outside): |
| 46 | + if IS_INSIDE_VPCSC.lower() == "true": |
| 47 | + assert TestVPCServiceControl._is_rejected(delayed_outside) |
| 48 | + assert not (TestVPCServiceControl._is_rejected(delayed_inside)) |
| 49 | + else: |
| 50 | + assert not (TestVPCServiceControl._is_rejected(delayed_outside)) |
| 51 | + assert TestVPCServiceControl._is_rejected(delayed_inside) |
| 52 | + |
| 53 | + @pytest.mark.skipif( |
| 54 | + PROJECT_INSIDE is None, reason="Missing environment variable: PROJECT_ID" |
| 55 | + ) |
| 56 | + @pytest.mark.skipif( |
| 57 | + PROJECT_OUTSIDE is None, |
| 58 | + reason="Missing environment variable: GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT", |
| 59 | + ) |
| 60 | + def test_export_assets(self): |
| 61 | + client = asset_v1.AssetServiceClient() |
| 62 | + output_config = {} |
| 63 | + parent_inside = "projects/" + PROJECT_INSIDE |
| 64 | + delayed_inside = lambda: client.export_assets(parent_inside, output_config) |
| 65 | + parent_outside = "projects/" + PROJECT_OUTSIDE |
| 66 | + delayed_outside = lambda: client.export_assets(parent_outside, output_config) |
| 67 | + TestVPCServiceControl._do_test(delayed_inside, delayed_outside) |
| 68 | + |
| 69 | + @pytest.mark.skipif( |
| 70 | + PROJECT_INSIDE is None, reason="Missing environment variable: PROJECT_ID" |
| 71 | + ) |
| 72 | + @pytest.mark.skipif( |
| 73 | + PROJECT_OUTSIDE is None, |
| 74 | + reason="Missing environment variable: GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT", |
| 75 | + ) |
| 76 | + def test_batch_get_assets_history(self): |
| 77 | + client = asset_v1.AssetServiceClient() |
| 78 | + content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED |
| 79 | + read_time_window = {} |
| 80 | + parent_inside = "projects/" + PROJECT_INSIDE |
| 81 | + delayed_inside = lambda: client.batch_get_assets_history( |
| 82 | + parent_inside, content_type, read_time_window |
| 83 | + ) |
| 84 | + parent_outside = "projects/" + PROJECT_OUTSIDE |
| 85 | + delayed_outside = lambda: client.batch_get_assets_history( |
| 86 | + parent_outside, content_type, read_time_window |
| 87 | + ) |
| 88 | + TestVPCServiceControl._do_test(delayed_inside, delayed_outside) |
0 commit comments