|
21 | 21 |
|
22 | 22 | from google.api_core import exceptions |
23 | 23 | from google.cloud import trace_v1 |
| 24 | +from test_utils.vpcsc_config import vpcsc_config |
24 | 25 |
|
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) |
| 26 | +_VPCSC_PROHIBITED_MESSAGE = "Request is prohibited by organization's policy." |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def client(): |
| 31 | + return trace_v1.TraceServiceClient() |
| 32 | + |
| 33 | + |
| 34 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 35 | +def test_list_traces_w_inside(client): |
| 36 | + list(client.list_traces(vpcsc_config.project_inside)) # no perms issue |
| 37 | + |
| 38 | + |
| 39 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 40 | +def test_list_traces_w_outside(client): |
| 41 | + with pytest.raises(exceptions.PermissionDenied) as exc: |
| 42 | + list(client.list_traces(vpcsc_config.project_outside)) |
| 43 | + |
| 44 | + assert _VPCSC_PROHIBITED_MESSAGE in exc.value.message |
| 45 | + |
| 46 | + |
| 47 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 48 | +def test_get_trace_w_inside(client): |
| 49 | + with pytest.raises(exceptions.InvalidArgument): |
| 50 | + client.get_trace(vpcsc_config.project_inside, "") # no perms issue |
| 51 | + |
| 52 | + |
| 53 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 54 | +def test_get_trace_w_outside(client): |
| 55 | + with pytest.raises(exceptions.PermissionDenied) as exc: |
| 56 | + client.get_trace(vpcsc_config.project_outside, "") |
| 57 | + |
| 58 | + assert _VPCSC_PROHIBITED_MESSAGE in exc.value.message |
| 59 | + |
| 60 | + |
| 61 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 62 | +def test_patch_traces_w_inside(client): |
| 63 | + with pytest.raises(exceptions.InvalidArgument): |
| 64 | + client.patch_traces(vpcsc_config.project_inside, {}) # no perms issue |
| 65 | + |
| 66 | + |
| 67 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 68 | +def test_patch_traces_w_ouside(client): |
| 69 | + with pytest.raises(exceptions.PermissionDenied) as exc: |
| 70 | + client.patch_traces(vpcsc_config.project_outside, {}) |
| 71 | + |
| 72 | + assert _VPCSC_PROHIBITED_MESSAGE in exc.value.message |
0 commit comments