Skip to content

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 11 commits into from
Jun 3, 2020
4 changes: 2 additions & 2 deletions run/markdown-preview/noxfile_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.

# We only run the e2e test in py37 session.
'ignored_versions': ["2.7", "3.6", "3.8"],
# 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
Expand Down
102 changes: 102 additions & 0 deletions run/system-package/e2e_test.py
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
40 changes: 40 additions & 0 deletions run/system-package/e2e_test_setup.yaml
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}', '.']
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

@tmatsuo tmatsuo Jun 3, 2020

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

'--tag=gcr.io/$PROJECT_ID/sys-package:${_SUFFIX}'

@averikitsch WDYT?

Copy link
Contributor Author

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?

Copy link
Contributor

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}.

Copy link
Contributor Author

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


- # 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}'

39 changes: 39 additions & 0 deletions run/system-package/noxfile_config.py
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': {},
}