Skip to content

Commit b4a9475

Browse files
committed
fix: bring tests into line with spec.
See: https://docs.google.com/document/d/1qCRRNqzKn7YnssbB1g-RTkJH11S3vmJ97JjsLWo6alA In particular: - Ensure 'inside_vpcxc' returns True IFF env var is set, regardless of value. - Add 'bucket_outside' test and corresponding skip decorator. - Rename 'skip_*' decorators to clarify semantics. - Expand reason messages in skip decorators to clarify semantics, explain how to enable the skipped test.
1 parent cf157b2 commit b4a9475

1 file changed

Lines changed: 45 additions & 19 deletions

File tree

test_utils/test_utils/vpcsc_config.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,25 @@
2222
INSIDE_VPCSC_ENVVAR = "GOOGLE_CLOUD_TESTS_IN_VPCSC"
2323
PROJECT_INSIDE_ENVVAR = "PROJECT_ID"
2424
PROJECT_OUTSIDE_ENVVAR = "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT"
25+
BUCKET_OUTSIDE_ENVVVAR = "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_BUCKET"
2526

2627

2728
class VPCSCTestConfig(object):
2829
"""System test utility for VPCSC detection.
29-
30+
3031
See: https://cloud.google.com/vpc-service-controls/docs/
3132
"""
3233

3334
@property
3435
def inside_vpcsc(self):
35-
"""Is the test environment inside VPCSC.
36+
"""Test whether the test environment is configured to run inside VPCSC.
3637
3738
Returns:
38-
bool: true if the environment is inside VPCSC, else false.
39+
bool:
40+
true if the environment is configured to run inside VPCSC,
41+
else false.
3942
"""
40-
value = os.environ.get(INSIDE_VPCSC_ENVVAR, "true").lower()
41-
return value == "true"
43+
return INSIDE_VPCSC_ENVVAR in os.environ
4244

4345
@property
4446
def project_inside(self):
@@ -58,35 +60,59 @@ def project_outside(self):
5860
"""
5961
return os.environ.get(PROJECT_OUTSIDE_ENVVAR, None)
6062

61-
def skip_if_running_inside_vpcsc(self, testcase):
63+
@property
64+
def bucket_outside(self):
65+
"""GCS bucket for testing inside access.
66+
67+
Returns:
68+
str: bucket ID used for testing inside access; None if undefined.
69+
"""
70+
return os.environ.get(BUCKET_OUTSIDE_ENVVAR, None)
71+
72+
def skip_if_inside_vpcsc(self, testcase):
6273
"""Test decorator: skip if running inside VPCSC."""
63-
reason = "Running inside VPCSC"
74+
reason = (
75+
"Running inside VPCSC. "
76+
"Set the {} environment variable to enable this test."
77+
).format(INSIDE_VPCSC_ENVVAR)
6478
skip = pytest.mark.skipif(self.inside_vpcsc, reason=reason)
6579
return skip(testcase)
6680

67-
def skip_if_running_outside_vpcsc(self, testcase):
81+
def skip_unless_inside_vpcsc(self, testcase):
6882
"""Test decorator: skip if running outside VPCSC."""
69-
reason = "Running outside VPCSC"
83+
reason = (
84+
"Running outside VPCSC. "
85+
"Unset the {} environment variable to enable this test."
86+
).format(INSIDE_VPCSC_ENVVAR)
7087
skip = pytest.mark.skipif(not self.inside_vpcsc, reason=reason)
7188
return skip(testcase)
7289

73-
def skip_if_inside_vpcsc(self, testcase):
90+
def skip_unless_inside_project(self, testcase):
7491
"""Test decorator: skip if inside project env var not set."""
75-
reason = "Running inside VPCSC"
76-
skip = pytest.mark.skipif(self.inside_vpcsc, reason=reason)
77-
return skip(testcase)
78-
79-
def skip_if_no_inside_project(self, testcase):
80-
"""Test decorator: skip if inside project env var not set."""
81-
reason = "Missing envvar: {}".format(PROJECT_INSIDE_ENVVAR)
92+
reason = (
93+
"Project ID for running inside VPCSC not set. "
94+
"Set the {} environment variable to enable this test."
95+
).format(PROJECT_INSIDE_ENVVAR)
8296
skip = pytest.mark.skipif(self.project_inside is None, reason=reason)
8397
return skip(testcase)
8498

85-
def skip_if_no_outside_project(self, testcase):
99+
def skip_unless_outside_project(self, testcase):
86100
"""Test decorator: skip if outside project env var not set."""
87-
reason = "Missing envvar: {}".format(PROJECT_OUTSIDE_ENVVAR)
101+
reason = (
102+
"Project ID for running outside VPCSC not set. "
103+
"Set the {} environment variable to enable this test."
104+
).format(PROJECT_OUTSIDE_ENVVAR)
88105
skip = pytest.mark.skipif(self.project_outside is None, reason=reason)
89106
return skip(testcase)
90107

108+
def skip_unless_outside_bucket(self, testcase):
109+
"""Test decorator: skip if outside bucket env var not set."""
110+
reason = (
111+
"Bucket ID for running outside VPCSC not set. "
112+
"Set the {} environment variable to enable this test."
113+
).format(BUCKET_OUTSIDE_ENVVAR)
114+
skip = pytest.mark.skipif(self.bucket_outside is None, reason=reason)
115+
return skip(testcase)
116+
91117

92118
vpcsc_config = VPCSCTestConfig()

0 commit comments

Comments
 (0)