Skip to content

Commit 3696308

Browse files
gaogaogiraffetseaver
authored andcommitted
Add VPCSC tests. (#8613)
The tests can be run inside or outside of VPC service perimeter. The input to the script should be the following environment variables. PROJECT_ID: a project that is inside the VPC perimeter. GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT: a project that is outside the VPC perimeter.
1 parent bfd6ee8 commit 3696308

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

packages/google-cloud-asset/noxfile.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,21 @@ 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": "secure-gcp-test-project-4",
124+
"GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT": os.environ.get(
125+
"PROJECT_ID"
126+
),
127+
}
128+
121129
# Run py.test against the system tests.
122130
if system_test_exists:
123-
session.run("py.test", "--quiet", system_test_path, *session.posargs)
131+
session.run("py.test", "--quiet", system_test_path, env=env, *session.posargs)
124132
if system_test_folder_exists:
125-
session.run("py.test", "--quiet", system_test_folder_path, *session.posargs)
133+
session.run(
134+
"py.test", "--quiet", system_test_folder_path, env=env, *session.posargs
135+
)
126136

127137

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

packages/google-cloud-asset/synth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@
8484
# Add templated files
8585
# ----------------------------------------------------------------------------
8686
templated_files = gcp.CommonTemplates().py_library(unit_cov_level=79, cov_level=80)
87-
s.move(templated_files)
87+
s.move(templated_files, excludes=["noxfile.py"])
8888

8989
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 asset_v1
24+
from google.cloud.asset_v1 import enums
25+
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", "false")
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",
75+
)
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
87+
)
88+
TestVPCServiceControl._do_test(delayed_inside, delayed_outside)

0 commit comments

Comments
 (0)