|
22 | 22 | from google.api_core import exceptions |
23 | 23 | from google.cloud import asset_v1 |
24 | 24 | from google.cloud.asset_v1 import enums |
| 25 | +from test_utils.vpcsc_config import vpcsc_config |
25 | 26 |
|
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", "true") |
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", |
| 27 | +_VPCSC_PROHIBITED_MESSAGE = "Request is prohibited by organization's policy" |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def client(): |
| 32 | + return asset_v1.AssetServiceClient() |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def output_config(): |
| 37 | + bucket_uri = "gs:{}/g-c-p-export-test".format(vpcsc_config.bucket_outside) |
| 38 | + output_config = {"gcsDestination": {"uri": bucket_uri}} |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def parent_inside(): |
| 43 | + return "projects/" + vpcsc_config.project_inside |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +def parent_outside(): |
| 48 | + return "projects/" + vpcsc_config.project_outside |
| 49 | + |
| 50 | + |
| 51 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 52 | +def test_export_assets_inside(client, output_config, parent_inside): |
| 53 | + with pytest.raises(exceptions.InvalidArgument): |
| 54 | + client.export_assets(parent_inside, output_config) |
| 55 | + |
| 56 | + |
| 57 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 58 | +def test_export_assets_outside(client, output_config, parent_outside): |
| 59 | + with pytest.raises(exceptions.PermissionDenied) as exc: |
| 60 | + client.export_assets(parent_outside, output_config) |
| 61 | + |
| 62 | + assert _VPCSC_PROHIBITED_MESSAGE in exc.value.message |
| 63 | + |
| 64 | + |
| 65 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 66 | +def test_batch_get_assets_history_inside(client, parent_inside): |
| 67 | + read_time_window = {} |
| 68 | + client.batch_get_assets_history( |
| 69 | + parent_inside, |
| 70 | + content_type=enums.ContentType.CONTENT_TYPE_UNSPECIFIED, |
| 71 | + read_time_window={}, |
75 | 72 | ) |
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 |
| 73 | + |
| 74 | + |
| 75 | +@vpcsc_config.skip_unless_inside_vpcsc |
| 76 | +def test_batch_get_assets_history_outside(client, parent_outside): |
| 77 | + content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED |
| 78 | + read_time_window = {} |
| 79 | + with pytest.raises(exceptions.PermissionDenied) as exc: |
| 80 | + client.batch_get_assets_history( |
| 81 | + parent_outside, |
| 82 | + content_type=enums.ContentType.CONTENT_TYPE_UNSPECIFIED, |
| 83 | + read_time_window={}, |
87 | 84 | ) |
88 | | - TestVPCServiceControl._do_test(delayed_inside, delayed_outside) |
| 85 | + |
| 86 | + assert _VPCSC_PROHIBITED_MESSAGE in exc.value.message |
0 commit comments