Skip to content

Commit aba3216

Browse files
mkudejimbusunkim96
authored andcommitted
Add VPC SC integration tests. (#8607)
1 parent 4031007 commit aba3216

File tree

2 files changed

+143
-2
lines changed

2 files changed

+143
-2
lines changed

videointelligence/noxfile.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,19 @@ def system(session):
118118
session.install("-e", "../test_utils/")
119119
session.install("-e", ".")
120120

121+
# Additional set up for VPC SC.
122+
env = {
123+
"PROJECT_NUMBER": "570941833855",
124+
"GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_IP": "10.1.1.1",
125+
"GOOGLE_CLOUD_TESTS_VPCSC_INSIDE_IP": "55.55.0.0",
126+
}
121127
# Run py.test against the system tests.
122128
if system_test_exists:
123-
session.run("py.test", "--quiet", system_test_path, *session.posargs)
129+
session.run("py.test", "--quiet", system_test_path, env=env, *session.posargs)
124130
if system_test_folder_exists:
125-
session.run("py.test", "--quiet", system_test_folder_path, *session.posargs)
131+
session.run(
132+
"py.test", "--quiet", system_test_folder_path, env=env * session.posargs
133+
)
126134

127135

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

videointelligence/tests/system.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Copyright 2017, Google LLC All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""System tests for VideoIntelligence API."""
16+
17+
import json
18+
import os
19+
import requests
20+
import time
21+
import unittest
22+
23+
from google.cloud import videointelligence
24+
from google.cloud.videointelligence_v1 import enums
25+
26+
PROJECT_NUMBER = os.environ.get("PROJECT_NUMBER")
27+
OUTSIDE_PROJECT_API_KEY = os.environ.get(
28+
"GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT_API_KEY"
29+
)
30+
OUTSIDE_IP = os.environ.get("GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_IP")
31+
INSIDE_IP = os.environ.get("GOOGLE_CLOUD_TESTS_VPCSC_INSIDE_IP")
32+
33+
34+
class VideoIntelligenceSystemTestBase(unittest.TestCase):
35+
client = None
36+
37+
def setUp(self):
38+
self.input_uri = "gs://cloud-samples-data/video/cat.mp4"
39+
40+
41+
def setUpModule():
42+
VideoIntelligenceSystemTestBase.client = (
43+
videointelligence.VideoIntelligenceServiceClient()
44+
)
45+
46+
47+
class TestVideoIntelligenceClient(VideoIntelligenceSystemTestBase):
48+
def test_annotate_video(self):
49+
features_element = enums.Feature.LABEL_DETECTION
50+
features = [features_element]
51+
response = self.client.annotate_video(
52+
input_uri=self.input_uri, features=features
53+
)
54+
55+
# Wait for the operation to complete.
56+
# Long timeout value warranted due to https://github.com/grpc/grpc/issues/19173
57+
lro_timeout_seconds = 180
58+
start_time = time.time()
59+
cnt = 0
60+
while not response.done() and (time.time() - start_time) < lro_timeout_seconds:
61+
time.sleep(1)
62+
cnt += 1
63+
if not response.done():
64+
self.fail(
65+
"wait for operation timed out after {lro_timeout_seconds} seconds".format(
66+
lro_timeout_seconds=lro_timeout_seconds
67+
)
68+
)
69+
70+
result = response.result()
71+
annotations = result.annotation_results[0]
72+
assert len(annotations.segment_label_annotations) > 0
73+
74+
75+
@unittest.skipUnless(
76+
OUTSIDE_PROJECT_API_KEY,
77+
"GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT_API_KEY not set in environment.",
78+
)
79+
class TestVideoIntelligenceClientVpcSc(VideoIntelligenceSystemTestBase):
80+
# Tests to verify VideoIntelligence service requests blocked when trying to access resources outside of a secure perimeter.
81+
def setUp(self):
82+
VideoIntelligenceSystemTestBase.setUp(self)
83+
# api-endpoint
84+
self.url = "https://videointelligence.googleapis.com/v1/videos:annotate?key={}".format(
85+
OUTSIDE_PROJECT_API_KEY
86+
)
87+
self.body = {
88+
"input_uri": self.input_uri,
89+
"features": ["LABEL_DETECTION"],
90+
"location_id": "us-west1",
91+
}
92+
93+
@unittest.skipUnless(PROJECT_NUMBER, "PROJECT_NUMBER not set in environment.")
94+
@unittest.skipUnless(
95+
OUTSIDE_IP, "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_IP not set in environment."
96+
)
97+
def test_outside_ip_address_blocked(self):
98+
headers = {
99+
"Content-Type": "application/json",
100+
"X-User-IP": OUTSIDE_IP,
101+
"X-Google-GFE-Cloud-Client-Network-Project-Number": PROJECT_NUMBER,
102+
}
103+
r = requests.post(url=self.url, data=json.dumps(self.body), headers=headers)
104+
outside_project_operation = json.loads(r.text)
105+
print(outside_project_operation)
106+
# Assert it returns permission denied from VPC SC
107+
self.assertEqual(outside_project_operation["error"]["code"], 403)
108+
self.assertEqual(
109+
outside_project_operation["error"]["status"], "PERMISSION_DENIED"
110+
)
111+
self.assertEqual(
112+
outside_project_operation["error"]["details"][0]["violations"][0]["type"],
113+
"VPC_SERVICE_CONTROLS",
114+
)
115+
self.assertEqual(
116+
outside_project_operation["error"]["message"],
117+
"Request is prohibited by organization's policy",
118+
)
119+
120+
@unittest.skipUnless(PROJECT_NUMBER, "PROJECT_NUMBER not set in environment.")
121+
@unittest.skipUnless(
122+
INSIDE_IP, "GOOGLE_CLOUD_TESTS_VPCSC_INSIDE_IP not set in environment."
123+
)
124+
def test_inside_ip_address_allowed(self):
125+
headers = {
126+
"Content-Type": "application/json",
127+
"X-User-IP": INSIDE_IP,
128+
"X-Google-GFE-Cloud-Client-Network-Project-Number": PROJECT_NUMBER,
129+
}
130+
r = requests.post(url=self.url, data=json.dumps(self.body), headers=headers)
131+
operation = json.loads(r.text)
132+
# Assert it returns non-empty operation name.
133+
self.assertNotEqual(operation["name"], "")

0 commit comments

Comments
 (0)