Skip to content

Commit 41ae858

Browse files
authored
1 parent 5f716db commit 41ae858

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

test_utils/test_utils/vpcsc_config.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
19+
import pytest
20+
21+
22+
INSIDE_VPCSC_ENVVAR = "GOOGLE_CLOUD_TESTS_IN_VPCSC"
23+
PROJECT_INSIDE_ENVVAR = "PROJECT_ID"
24+
PROJECT_OUTSIDE_ENVVAR = "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT"
25+
BUCKET_OUTSIDE_ENVVVAR = "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_BUCKET"
26+
27+
28+
class VPCSCTestConfig(object):
29+
"""System test utility for VPCSC detection.
30+
31+
See: https://cloud.google.com/vpc-service-controls/docs/
32+
"""
33+
34+
@property
35+
def inside_vpcsc(self):
36+
"""Test whether the test environment is configured to run inside VPCSC.
37+
38+
Returns:
39+
bool:
40+
true if the environment is configured to run inside VPCSC,
41+
else false.
42+
"""
43+
return INSIDE_VPCSC_ENVVAR in os.environ
44+
45+
@property
46+
def project_inside(self):
47+
"""Project ID for testing outside access.
48+
49+
Returns:
50+
str: project ID used for testing outside access; None if undefined.
51+
"""
52+
return os.environ.get(PROJECT_INSIDE_ENVVAR, None)
53+
54+
@property
55+
def project_outside(self):
56+
"""Project ID for testing inside access.
57+
58+
Returns:
59+
str: project ID used for testing inside access; None if undefined.
60+
"""
61+
return os.environ.get(PROJECT_OUTSIDE_ENVVAR, None)
62+
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):
73+
"""Test decorator: skip if running inside VPCSC."""
74+
reason = (
75+
"Running inside VPCSC. "
76+
"Set the {} environment variable to enable this test."
77+
).format(INSIDE_VPCSC_ENVVAR)
78+
skip = pytest.mark.skipif(self.inside_vpcsc, reason=reason)
79+
return skip(testcase)
80+
81+
def skip_unless_inside_vpcsc(self, testcase):
82+
"""Test decorator: skip if running outside VPCSC."""
83+
reason = (
84+
"Running outside VPCSC. "
85+
"Unset the {} environment variable to enable this test."
86+
).format(INSIDE_VPCSC_ENVVAR)
87+
skip = pytest.mark.skipif(not self.inside_vpcsc, reason=reason)
88+
return skip(testcase)
89+
90+
def skip_unless_inside_project(self, testcase):
91+
"""Test decorator: skip if inside project env var not set."""
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)
96+
skip = pytest.mark.skipif(self.project_inside is None, reason=reason)
97+
return skip(testcase)
98+
99+
def skip_unless_outside_project(self, testcase):
100+
"""Test decorator: skip if outside project env var not set."""
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)
105+
skip = pytest.mark.skipif(self.project_outside is None, reason=reason)
106+
return skip(testcase)
107+
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+
117+
118+
vpcsc_config = VPCSCTestConfig()

0 commit comments

Comments
 (0)