-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Videointelligence: Add VPC SC integration tests. #8607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1bd5c96
Adds VPC SC integration tests for Video Intelligence API
9a49e54
Addressing comments
133b008
Adds VPC SC integration tests for Video Intelligence API
34e9c2b
Addressing comments
625ffa0
Removed changes in generated code.
bafb9eb
Merge branch 'master' of https://github.com/mkudejim/google-cloud-python
d6707d3
Reverting generated code changes.
f88ce4f
Set VPS SC environment variables.
e9271da
Remove Api key from code (sensitive data) will have to be set through
4b03329
Merge branch 'master' of https://github.com/googleapis/google-cloud-p…
98a1553
Merge branch 'master' of https://github.com/googleapis/google-cloud-p…
c14396a
Fix lint error.
f885db7
Remove unused imports.
bf54274
Merge branch 'master' of https://github.com/googleapis/google-cloud-p…
e23764d
Increase lro timeout to 180s.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| # Copyright 2017, Google LLC All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """System tests for VideoIntelligence API.""" | ||
|
|
||
| import json | ||
| import os | ||
| import requests | ||
| import time | ||
| import unittest | ||
|
|
||
| from google.cloud import videointelligence | ||
| from google.cloud.videointelligence_v1 import enums | ||
|
|
||
| PROJECT_NUMBER = os.environ.get("PROJECT_NUMBER") | ||
| OUTSIDE_PROJECT_API_KEY = os.environ.get( | ||
| "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT_API_KEY" | ||
| ) | ||
| OUTSIDE_IP = os.environ.get("GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_IP") | ||
| INSIDE_IP = os.environ.get("GOOGLE_CLOUD_TESTS_VPCSC_INSIDE_IP") | ||
|
|
||
|
|
||
| class VideoIntelligenceSystemTestBase(unittest.TestCase): | ||
| client = None | ||
|
|
||
| def setUp(self): | ||
| self.input_uri = "gs://cloud-samples-data/video/cat.mp4" | ||
|
|
||
|
|
||
| def setUpModule(): | ||
| VideoIntelligenceSystemTestBase.client = ( | ||
| videointelligence.VideoIntelligenceServiceClient() | ||
| ) | ||
|
|
||
|
|
||
| class TestVideoIntelligenceClient(VideoIntelligenceSystemTestBase): | ||
| def test_annotate_video(self): | ||
| features_element = enums.Feature.LABEL_DETECTION | ||
| features = [features_element] | ||
| response = self.client.annotate_video( | ||
| input_uri=self.input_uri, features=features | ||
| ) | ||
|
|
||
| # Wait for the operation to complete. | ||
| # Long timeout value warranted due to https://github.com/grpc/grpc/issues/19173 | ||
| lro_timeout_seconds = 180 | ||
| start_time = time.time() | ||
| cnt = 0 | ||
| while not response.done() and (time.time() - start_time) < lro_timeout_seconds: | ||
| time.sleep(1) | ||
| cnt += 1 | ||
| if not response.done(): | ||
| self.fail( | ||
| "wait for operation timed out after {lro_timeout_seconds} seconds".format( | ||
| lro_timeout_seconds=lro_timeout_seconds | ||
| ) | ||
| ) | ||
|
|
||
| result = response.result() | ||
| annotations = result.annotation_results[0] | ||
| assert len(annotations.segment_label_annotations) > 0 | ||
|
|
||
|
|
||
| @unittest.skipUnless( | ||
| OUTSIDE_PROJECT_API_KEY, | ||
| "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT_API_KEY not set in environment.", | ||
| ) | ||
| class TestVideoIntelligenceClientVpcSc(VideoIntelligenceSystemTestBase): | ||
| # Tests to verify VideoIntelligence service requests blocked when trying to access resources outside of a secure perimeter. | ||
| def setUp(self): | ||
| VideoIntelligenceSystemTestBase.setUp(self) | ||
| # api-endpoint | ||
| self.url = "https://videointelligence.googleapis.com/v1/videos:annotate?key={}".format( | ||
| OUTSIDE_PROJECT_API_KEY | ||
| ) | ||
| self.body = { | ||
| "input_uri": self.input_uri, | ||
| "features": ["LABEL_DETECTION"], | ||
| "location_id": "us-west1", | ||
| } | ||
|
|
||
| @unittest.skipUnless(PROJECT_NUMBER, "PROJECT_NUMBER not set in environment.") | ||
| @unittest.skipUnless( | ||
| OUTSIDE_IP, "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_IP not set in environment." | ||
| ) | ||
| def test_outside_ip_address_blocked(self): | ||
| headers = { | ||
| "Content-Type": "application/json", | ||
| "X-User-IP": OUTSIDE_IP, | ||
| "X-Google-GFE-Cloud-Client-Network-Project-Number": PROJECT_NUMBER, | ||
| } | ||
| r = requests.post(url=self.url, data=json.dumps(self.body), headers=headers) | ||
| outside_project_operation = json.loads(r.text) | ||
| print(outside_project_operation) | ||
| # Assert it returns permission denied from VPC SC | ||
| self.assertEqual(outside_project_operation["error"]["code"], 403) | ||
| self.assertEqual( | ||
| outside_project_operation["error"]["status"], "PERMISSION_DENIED" | ||
| ) | ||
| self.assertEqual( | ||
| outside_project_operation["error"]["details"][0]["violations"][0]["type"], | ||
| "VPC_SERVICE_CONTROLS", | ||
| ) | ||
| self.assertEqual( | ||
| outside_project_operation["error"]["message"], | ||
| "Request is prohibited by organization's policy", | ||
| ) | ||
|
|
||
| @unittest.skipUnless(PROJECT_NUMBER, "PROJECT_NUMBER not set in environment.") | ||
| @unittest.skipUnless( | ||
| INSIDE_IP, "GOOGLE_CLOUD_TESTS_VPCSC_INSIDE_IP not set in environment." | ||
| ) | ||
| def test_inside_ip_address_allowed(self): | ||
| headers = { | ||
| "Content-Type": "application/json", | ||
| "X-User-IP": INSIDE_IP, | ||
| "X-Google-GFE-Cloud-Client-Network-Project-Number": PROJECT_NUMBER, | ||
| } | ||
| r = requests.post(url=self.url, data=json.dumps(self.body), headers=headers) | ||
| operation = json.loads(r.text) | ||
| # Assert it returns non-empty operation name. | ||
| self.assertNotEqual(operation["name"], "") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.