-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Adding e2e test for system-package sample #3949
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
11 commits
Select commit
Hold shift + click to select a range
66f91a1
Adding e2e test for system-package sample
dinagraves b99a200
Linting
dinagraves bce8cc1
Using py3.8 instead of 3.7
dinagraves de801c9
Merge branch 'master' into e2e-tests
f8e2d02
Moving noxfile up a directory
dinagraves d4e5274
Merge branch 'e2e-tests' of github.com:GoogleCloudPlatform/python-doc…
dinagraves f71ca3b
Moving the noxfile_config back to the sample directories
dinagraves 55847be
Moving noxfile
dinagraves 7a34ccf
Merge branch 'master' into e2e-tests
dinagraves 8f303c8
Usingn COMMIT_SHA
dinagraves 1ada2ee
Reverting back to SUFFIX
dinagraves 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,102 @@ | ||
# Copyright 2020 Google, LLC. | ||
# | ||
# 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. | ||
|
||
# This sample creates a secure two-service application running on Cloud Run. | ||
# This test builds and deploys the two secure services | ||
# to test that they interact properly together. | ||
|
||
import os | ||
import subprocess | ||
from urllib import request | ||
import uuid | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture() | ||
def services(): | ||
# Unique suffix to create distinct service names | ||
suffix = uuid.uuid4().hex | ||
project = os.environ['GOOGLE_CLOUD_PROJECT'] | ||
|
||
# Build and Deploy Cloud Run Services | ||
subprocess.run( | ||
[ | ||
"gcloud", | ||
"builds", | ||
"submit", | ||
"--project", | ||
project, | ||
"--substitutions", | ||
f"_SUFFIX={suffix}", | ||
"--config", | ||
"e2e_test_setup.yaml", | ||
"--quiet", | ||
], check=True | ||
) | ||
|
||
# Get the URL for the service and the token | ||
service = subprocess.run( | ||
[ | ||
"gcloud", | ||
"run", | ||
"--project", | ||
project, | ||
"--platform=managed", | ||
"--region=us-central1", | ||
"services", | ||
"describe", | ||
f"sys-package-{suffix}", | ||
"--format=value(status.url)", | ||
], | ||
stdout=subprocess.PIPE, | ||
check=True | ||
).stdout.strip() | ||
|
||
token = subprocess.run( | ||
["gcloud", "auth", "print-identity-token"], stdout=subprocess.PIPE, | ||
check=True | ||
).stdout.strip() | ||
|
||
yield service, token | ||
|
||
subprocess.run( | ||
["gcloud", "run", "services", "delete", f"sys-package-{suffix}", | ||
"--project", project, "--platform", "managed", "--region", | ||
"us-central1", "--quiet"], | ||
check=True | ||
) | ||
|
||
|
||
def test_end_to_end(services): | ||
service = services[0].decode() | ||
token = services[1].decode() | ||
data = ( | ||
"diagram.png?dot=digraph G { A -> {B, C, D} -> {F} }".replace(" ", "%20")) | ||
print(service) | ||
print(f"{service}{data}") | ||
|
||
req = request.Request( | ||
f"{service}/{data}", | ||
headers={ | ||
"Authorization": f"Bearer {token}", | ||
}, | ||
) | ||
|
||
response = request.urlopen(req) | ||
assert response.status == 200 | ||
|
||
body = response.read() | ||
# Response is a png | ||
assert b"PNG" in body |
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,40 @@ | ||
# Copyright 2020 Google, LLC. | ||
# | ||
# 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. | ||
|
||
steps: | ||
- # Build the renderer image | ||
name: gcr.io/cloud-builders/docker:latest | ||
args: ['build', '--tag=gcr.io/$PROJECT_ID/sys-package-${_SUFFIX}', '.'] | ||
|
||
- # Push the container image to Container Registry | ||
name: gcr.io/cloud-builders/docker | ||
args: ['push', 'gcr.io/$PROJECT_ID/sys-package-${_SUFFIX}'] | ||
|
||
- # Deploy to Cloud Run | ||
name: gcr.io/cloud-builders/gcloud | ||
args: | ||
- run | ||
- deploy | ||
- sys-package-${_SUFFIX} | ||
- --image | ||
- gcr.io/$PROJECT_ID/sys-package-${_SUFFIX} | ||
- --region | ||
- us-central1 | ||
- --platform | ||
- managed | ||
- --no-allow-unauthenticated | ||
|
||
images: | ||
- 'gcr.io/$PROJECT_ID/sys-package-${_SUFFIX}' | ||
|
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,39 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# 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. | ||
|
||
# Default TEST_CONFIG_OVERRIDE for python repos. | ||
|
||
# You can copy this file into your directory, then it will be inported from | ||
# the noxfile.py. | ||
|
||
# The source of truth: | ||
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py | ||
|
||
TEST_CONFIG_OVERRIDE = { | ||
# You can opt out from the test for specific Python versions. | ||
|
||
# We only run the cloud run tests in py38 session. | ||
'ignored_versions': ["2.7", "3.6", "3.7"], | ||
|
||
# An envvar key for determining the project id to use. Change it | ||
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a | ||
# build specific Cloud project. You can also use your own string | ||
# to use your own Cloud project. | ||
'gcloud_project_env': 'GCLOUD_PROJECT', | ||
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', | ||
|
||
# A dictionary you want to inject into your test. Don't put any | ||
# secrets here. These values will override predefined values. | ||
'envs': {}, | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using _SUFFIX here, can we use $COMMIT_SHA as the image tag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm - I have to get the URL in the test and to do that I need the name of the service. Not sure how I would get the COMMIT SHA into pytest to get the URL?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can just use the
${_SUFFIX}
as the tag, instead of a part of the image name?I mean
@averikitsch WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's just part of the image name and not the service, wouldn't I create problems by deploying the image to the same service name if other tests are also running? Wouldn't I overwrite it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to keep the service name as
sys-package-${_SUFFIX}
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like COMMIT_SHA only works on builds that are triggered by a commit. Since I'm explicitly calling the build in the test, it doesn't have the substitution available and is null.
Results in error:
Step #0: invalid argument "gcr.io/python-docs-samples-tests/sys-package:" for "-t, --tag" flag: invalid reference format