Skip to content

Commit e864d81

Browse files
mkudejimbusunkim96
authored andcommitted
test(videointelligence): enrich VPCSC tests (#9193)
1 parent bf878ec commit e864d81

1 file changed

Lines changed: 53 additions & 77 deletions

File tree

  • packages/google-cloud-videointelligence/tests

packages/google-cloud-videointelligence/tests/system.py

Lines changed: 53 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,123 +11,99 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
1514
"""System tests for VideoIntelligence API."""
1615

1716
import json
1817
import os
1918
import requests
20-
import time
2119
import unittest
2220

21+
from google.auth.transport import requests as goog_auth_requests
2322
from google.cloud import videointelligence
24-
from google.cloud.videointelligence_v1 import enums
23+
from google.oauth2 import service_account
24+
25+
CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform"
26+
CREDENTIALS_FILE = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
27+
OUTSIDE_BUCKET = os.environ.get("GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_BUCKET")
28+
INSIDE_BUCKET = os.environ.get("GOOGLE_CLOUD_TESTS_VPCSC_INSIDE_PERIMETER_BUCKET")
29+
IS_INSIDE_VPCSC = os.environ.get("GOOGLE_CLOUD_TESTS_IN_VPCSC")
2530

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")
31+
32+
def get_access_token():
33+
"""Returns an access token.
34+
35+
Generates access tokens using the provided service account key file.
36+
"""
37+
creds = service_account.Credentials.from_service_account_file(
38+
CREDENTIALS_FILE, scopes=[CLOUD_PLATFORM_SCOPE]
39+
)
40+
with requests.Session() as session:
41+
creds.refresh(goog_auth_requests.Request(session=session))
42+
return creds.token
3243

3344

3445
class VideoIntelligenceSystemTestBase(unittest.TestCase):
3546
client = None
3647

37-
def setUp(self):
38-
self.input_uri = "gs://cloud-samples-data/video/cat.mp4"
39-
4048

4149
def setUpModule():
4250
VideoIntelligenceSystemTestBase.client = (
4351
videointelligence.VideoIntelligenceServiceClient()
4452
)
4553

4654

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-
7555
@unittest.skipUnless(
76-
OUTSIDE_PROJECT_API_KEY,
77-
"GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT_API_KEY not set in environment.",
56+
CREDENTIALS_FILE, "GOOGLE_APPLICATION_CREDENTIALS not set in environment."
7857
)
7958
class TestVideoIntelligenceClientVpcSc(VideoIntelligenceSystemTestBase):
80-
# Tests to verify VideoIntelligence service requests blocked when trying to access resources outside of a secure perimeter.
59+
# Tests to verify VideoIntelligence service requests blocked when trying to
60+
# access resources outside of a secure perimeter.
8161
def setUp(self):
8262
VideoIntelligenceSystemTestBase.setUp(self)
8363
# 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-
}
64+
self.url = "https://videointelligence.googleapis.com/v1/videos:annotate"
65+
self.body = {"features": ["LABEL_DETECTION"], "location_id": "us-west1"}
9266

93-
@unittest.skipUnless(PROJECT_NUMBER, "PROJECT_NUMBER not set in environment.")
9467
@unittest.skipUnless(
95-
OUTSIDE_IP, "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_IP not set in environment."
68+
OUTSIDE_BUCKET,
69+
"GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_BUCKET not set in environment.",
9670
)
97-
def test_outside_ip_address_blocked(self):
71+
@unittest.skipUnless(
72+
IS_INSIDE_VPCSC, "GOOGLE_CLOUD_TESTS_IN_VPCSC not set in environment."
73+
)
74+
def test_outside_perimeter_blocked(self):
9875
headers = {
76+
"Authorization": "Bearer " + get_access_token(),
9977
"Content-Type": "application/json",
100-
"X-User-IP": OUTSIDE_IP,
101-
"X-Google-GFE-Cloud-Client-Network-Project-Number": PROJECT_NUMBER,
10278
}
79+
self.body["input_uri"] = "gs://{bucket}/cat.mp4".format(bucket=OUTSIDE_BUCKET)
10380
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)
81+
resp = json.loads(r.text)
82+
print(resp)
10683
# 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-
)
84+
self.assertEqual(resp["error"]["code"], 403)
85+
self.assertEqual(resp["error"]["status"], "PERMISSION_DENIED")
11986

120-
@unittest.skipUnless(PROJECT_NUMBER, "PROJECT_NUMBER not set in environment.")
12187
@unittest.skipUnless(
122-
INSIDE_IP, "GOOGLE_CLOUD_TESTS_VPCSC_INSIDE_IP not set in environment."
88+
INSIDE_BUCKET,
89+
"GOOGLE_CLOUD_TESTS_VPCSC_INSIDE_PERIMETER_BUCKET not set in environment.",
90+
)
91+
@unittest.skipUnless(
92+
IS_INSIDE_VPCSC, "GOOGLE_CLOUD_TESTS_IN_VPCSC not set in environment."
12393
)
124-
def test_inside_ip_address_allowed(self):
94+
def test_inside_perimeter_allowed(self):
12595
headers = {
96+
"Authorization": "Bearer " + get_access_token(),
12697
"Content-Type": "application/json",
127-
"X-User-IP": INSIDE_IP,
128-
"X-Google-GFE-Cloud-Client-Network-Project-Number": PROJECT_NUMBER,
12998
}
99+
self.body["input_uri"] = "gs://{bucket}/cat.mp4".format(bucket=INSIDE_BUCKET)
130100
r = requests.post(url=self.url, data=json.dumps(self.body), headers=headers)
131101
operation = json.loads(r.text)
132-
# Assert it returns non-empty operation name.
133-
self.assertNotEqual(operation["name"], "")
102+
print(operation)
103+
104+
get_op_url = "https://videointelligence.googleapis.com/v1/" + operation["name"]
105+
get_op = requests.get(url=get_op_url, headers=headers)
106+
get_op_resp = json.loads(get_op.text)
107+
print(get_op_resp)
108+
# Assert that we do not get an error.
109+
self.assertEqual(get_op_resp["name"], operation["name"])

0 commit comments

Comments
 (0)