|
| 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 trace_v1 |
| 24 | + |
| 25 | +PROJECT_INSIDE = os.environ.get("PROJECT_ID", None) |
| 26 | +PROJECT_OUTSIDE = os.environ.get( |
| 27 | + "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT", None |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +class TestVPCServiceControlV1(object): |
| 32 | + @staticmethod |
| 33 | + def _is_rejected(call): |
| 34 | + try: |
| 35 | + responses = call() |
| 36 | + except exceptions.PermissionDenied as e: |
| 37 | + return e.message == "Request is prohibited by organization's policy" |
| 38 | + except: |
| 39 | + return False |
| 40 | + return False |
| 41 | + |
| 42 | + @pytest.mark.skipif( |
| 43 | + PROJECT_INSIDE is None, reason="Missing environment variable: PROJECT_ID" |
| 44 | + ) |
| 45 | + @pytest.mark.skipif( |
| 46 | + PROJECT_OUTSIDE is None, |
| 47 | + reason="Missing environment variable: GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT", |
| 48 | + ) |
| 49 | + def test_list_traces(self): |
| 50 | + client = trace_v1.TraceServiceClient() |
| 51 | + |
| 52 | + list_inside = lambda: list(client.list_traces(PROJECT_INSIDE)) |
| 53 | + list_outside = lambda: list(client.list_traces(PROJECT_OUTSIDE)) |
| 54 | + |
| 55 | + assert not TestVPCServiceControlV1._is_rejected(list_inside) |
| 56 | + assert TestVPCServiceControlV1._is_rejected(list_outside) |
| 57 | + |
| 58 | + @pytest.mark.skipif( |
| 59 | + PROJECT_INSIDE is None, reason="Missing environment variable: PROJECT_ID" |
| 60 | + ) |
| 61 | + @pytest.mark.skipif( |
| 62 | + PROJECT_OUTSIDE is None, |
| 63 | + reason="Missing environment variable: GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT", |
| 64 | + ) |
| 65 | + def test_get_trace(self): |
| 66 | + client = trace_v1.TraceServiceClient() |
| 67 | + |
| 68 | + get_inside = lambda: client.get_trace(PROJECT_INSIDE, "") |
| 69 | + get_outside = lambda: client.get_trace(PROJECT_OUTSIDE, "") |
| 70 | + |
| 71 | + assert not TestVPCServiceControlV1._is_rejected(get_inside) |
| 72 | + assert TestVPCServiceControlV1._is_rejected(get_outside) |
| 73 | + |
| 74 | + @pytest.mark.skipif( |
| 75 | + PROJECT_INSIDE is None, reason="Missing environment variable: PROJECT_ID" |
| 76 | + ) |
| 77 | + @pytest.mark.skipif( |
| 78 | + PROJECT_OUTSIDE is None, |
| 79 | + reason="Missing environment variable: GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT", |
| 80 | + ) |
| 81 | + def test_patch_traces(self): |
| 82 | + client = trace_v1.TraceServiceClient() |
| 83 | + |
| 84 | + patch_inside = lambda: client.patch_traces(PROJECT_INSIDE, {}) |
| 85 | + patch_outside = lambda: client.patch_traces(PROJECT_OUTSIDE, {}) |
| 86 | + |
| 87 | + assert not TestVPCServiceControlV1._is_rejected(patch_inside) |
| 88 | + assert TestVPCServiceControlV1._is_rejected(patch_outside) |
0 commit comments