Skip to content

Commit 7bdc256

Browse files
authored
tests(trace): normalize VPCSC configuration in systests (#9618)
Toward #9580.
1 parent 34ebdd1 commit 7bdc256

File tree

3 files changed

+73
-110
lines changed

3 files changed

+73
-110
lines changed

packages/google-cloud-trace/noxfile.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,11 @@ def system(session):
121121
session.install("-e", "../test_utils/")
122122
session.install("-e", ".")
123123

124-
# Additional setup for VPCSC system tests
125-
env = {
126-
"PROJECT_ID": os.environ.get(
127-
"PROJECT_ID"
128-
),
129-
"GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT": "secure-gcp-test-project-4",
130-
}
131-
132124
# Run py.test against the system tests.
133125
if system_test_exists:
134-
session.run("py.test", "--quiet", system_test_path, env=env, *session.posargs)
126+
session.run("py.test", "--quiet", system_test_path, *session.posargs)
135127
if system_test_folder_exists:
136-
session.run("py.test", "--quiet", system_test_folder_path, env=env, *session.posargs)
128+
session.run("py.test", "--quiet", system_test_folder_path, *session.posargs)
137129

138130

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

packages/google-cloud-trace/tests/system/gapic/v1/test_system_trace_service_v1_vpcsc.py

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -21,68 +21,52 @@
2121

2222
from google.api_core import exceptions
2323
from google.cloud import trace_v1
24+
from test_utils.vpcsc_config import vpcsc_config
2425

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

packages/google-cloud-trace/tests/system/gapic/v2/test_system_trace_service_v2_vpcsc.py

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,27 @@
2121

2222
from google.api_core import exceptions
2323
from google.cloud import trace_v2
24+
from test_utils.vpcsc_config import vpcsc_config
2425

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 TestVPCServiceControlV2(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-
pass
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_batch_write_spans(self):
50-
client = trace_v2.TraceServiceClient()
51-
52-
proejct_inside = client.project_path(PROJECT_INSIDE)
53-
proejct_outside = client.project_path(PROJECT_OUTSIDE)
54-
spans = []
55-
56-
write_inside = lambda: client.batch_write_spans(proejct_inside, spans)
57-
write_outside = lambda: client.batch_write_spans(proejct_outside, spans)
58-
59-
assert not TestVPCServiceControlV2._is_rejected(write_inside)
60-
assert TestVPCServiceControlV2._is_rejected(write_outside)
26+
_VPCSC_PROHIBITED_MESSAGE = "Request is prohibited by organization's policy."
27+
28+
29+
@pytest.fixture
30+
def client():
31+
return trace_v2.TraceServiceClient()
32+
33+
34+
@vpcsc_config.skip_unless_inside_vpcsc
35+
def test_batch_write_spans_w_inside(client):
36+
project_inside = client.project_path(vpcsc_config.project_inside)
37+
client.batch_write_spans(project_inside, []) # no raise
38+
39+
40+
@vpcsc_config.skip_unless_inside_vpcsc
41+
def test_batch_write_spans_w_outside(client):
42+
project_outside = client.project_path(vpcsc_config.project_outside)
43+
44+
with pytest.raises(exceptions.PermissionDenied) as exc:
45+
client.batch_write_spans(project_outside, [])
46+
47+
assert _VPCSC_PROHIBITED_MESSAGE in exc.value.message

0 commit comments

Comments
 (0)