Skip to content

Commit d08b210

Browse files
authored
tests(asset): normalize VPCSC configuration in systests (#9614)
Toward #9580.
1 parent 3dab9f3 commit d08b210

File tree

2 files changed

+61
-72
lines changed

2 files changed

+61
-72
lines changed

asset/noxfile.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,11 @@ def system(session):
118118
session.install("-e", "../test_utils/")
119119
session.install("-e", ".")
120120

121-
# Additional setup for VPCSC system tests
122-
env = {
123-
"PROJECT_ID": os.environ.get("PROJECT_ID"),
124-
"GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT": "secure-gcp-test-project-4",
125-
"GOOGLE_CLOUD_TESTS_IN_VPCSC": "true",
126-
}
127-
128121
# Run py.test against the system tests.
129122
if system_test_exists:
130-
session.run("py.test", "--quiet", system_test_path, env=env, *session.posargs)
123+
session.run("py.test", "--quiet", system_test_path, *session.posargs)
131124
if system_test_folder_exists:
132-
session.run(
133-
"py.test", "--quiet", system_test_folder_path, env=env, *session.posargs
134-
)
125+
session.run("py.test", "--quiet", system_test_folder_path, *session.posargs)
135126

136127

137128
@nox.session(python="3.7")

asset/tests/system/test_vpcsc.py

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,67 +22,65 @@
2222
from google.api_core import exceptions
2323
from google.cloud import asset_v1
2424
from google.cloud.asset_v1 import enums
25+
from test_utils.vpcsc_config import vpcsc_config
2526

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={},
7572
)
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={},
8784
)
88-
TestVPCServiceControl._do_test(delayed_inside, delayed_outside)
85+
86+
assert _VPCSC_PROHIBITED_MESSAGE in exc.value.message

0 commit comments

Comments
 (0)