Skip to content

Commit 35228c0

Browse files
lunajrqbusunkim96
authored andcommitted
Add VPCSC system test (#7999)
1 parent a591c20 commit 35228c0

4 files changed

Lines changed: 159 additions & 3 deletions

File tree

trace/noxfile.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,19 @@ 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+
124132
# Run py.test against the system tests.
125133
if system_test_exists:
126-
session.run("py.test", "--quiet", system_test_path, *session.posargs)
134+
session.run("py.test", "--quiet", system_test_path, env=env, *session.posargs)
127135
if system_test_folder_exists:
128-
session.run("py.test", "--quiet", system_test_folder_path, *session.posargs)
136+
session.run("py.test", "--quiet", system_test_folder_path, env=env, *session.posargs)
129137

130138

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

trace/synth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
# Add templated files
5454
# ----------------------------------------------------------------------------
5555
templated_files = common.py_library(unit_cov_level=97, cov_level=100)
56-
s.move(templated_files)
56+
s.move(templated_files, excludes=["noxfile.py"])
5757

5858
s.shell.run(["nox", "-s", "blacken"], hide_output=False)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
# flake8: noqa
18+
19+
import os
20+
import pytest
21+
22+
from google.api_core import exceptions
23+
from google.cloud import trace_v1
24+
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)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
# flake8: noqa
18+
19+
import os
20+
import pytest
21+
22+
from google.api_core import exceptions
23+
from google.cloud import trace_v2
24+
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)

0 commit comments

Comments
 (0)