Skip to content

Commit

Permalink
refactor cloud run tests for container reg cleanup (#5053)
Browse files Browse the repository at this point in the history
* refactor hello-broken for cleanup

* refactor helloworld test for better cleanup

* remove debug suffix

* fix logging test, remove nonsense gcloud commands

* delete unused build file

* remove debug statements

* refactor markdown preview tests

* remove debug comments

* refactor system package tests

* shorten suffixes

* refactor image processing test

* add project flags

* refactor pubsub test

* add missing check=true

* run blacken, fix lint
  • Loading branch information
leahecole authored Dec 14, 2020
1 parent 99f20c8 commit 54208af
Show file tree
Hide file tree
Showing 32 changed files with 981 additions and 767 deletions.
129 changes: 91 additions & 38 deletions run/hello-broken/e2e_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,86 +23,139 @@

import pytest

# Unique suffix to create distinct service names
SUFFIX = uuid.uuid4().hex[:10]
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
IMAGE_NAME = f"gcr.io/{PROJECT}/hello-{SUFFIX}"

@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
@pytest.fixture
def container_image():
# Build container image for Cloud Run deployment
subprocess.run(
[
"gcloud",
"builds",
"submit",
"--tag",
IMAGE_NAME,
"--project",
project,
"--substitutions",
f"_SUFFIX={suffix}",
"--config",
"e2e_test_setup.yaml",
PROJECT,
"--quiet",
],
check=True,
)

# Get the URL for the service and the token
service = subprocess.run(
yield IMAGE_NAME

# Delete container image
subprocess.run(
[
"gcloud",
"container",
"images",
"delete",
IMAGE_NAME,
"--quiet",
"--project",
PROJECT,
],
check=True,
)


@pytest.fixture
def deployed_service(container_image):
# Deploy image to Cloud Run
service_name = f"hello-{SUFFIX}"
subprocess.run(
[
"gcloud",
"run",
"deploy",
service_name,
"--image",
container_image,
"--project",
project,
"--platform=managed",
PROJECT,
"--region=us-central1",
"services",
"describe",
f"hello-{suffix}",
"--format=value(status.url)",
"--platform=managed",
"--no-allow-unauthenticated",
],
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
yield service_name

subprocess.run(
[
"gcloud",
"run",
"services",
"delete",
f"hello-{suffix}",
"--project",
project,
"--platform",
"managed",
"--region",
"us-central1",
service_name,
"--platform=managed",
"--region=us-central1",
"--quiet",
"--project",
PROJECT,
],
check=True,
)


def test_end_to_end(services):
service = services[0].decode()
token = services[1].decode()
@pytest.fixture
def service_url_auth_token(deployed_service):
# Get Cloud Run service URL and auth token
service_url = (
subprocess.run(
[
"gcloud",
"run",
"services",
"describe",
deployed_service,
"--platform=managed",
"--region=us-central1",
"--format=value(status.url)",
"--project",
PROJECT,
],
stdout=subprocess.PIPE,
check=True,
)
.stdout.strip()
.decode()
)
auth_token = (
subprocess.run(
["gcloud", "auth", "print-identity-token"],
stdout=subprocess.PIPE,
check=True,
)
.stdout.strip()
.decode()
)

yield service_url, auth_token

# no deletion needed


def test_end_to_end(service_url_auth_token):
service_url, auth_token = service_url_auth_token

# Broken
with pytest.raises(Exception) as e:
req = request.Request(service, headers={"Authorization": f"Bearer {token}"})
req = request.Request(
service_url, headers={"Authorization": f"Bearer {auth_token}"}
)
request.urlopen(req)
assert "HTTP Error 500: Internal Server Error" in str(e.value)

# Improved
req = request.Request(
f"{service}/improved", headers={"Authorization": f"Bearer {token}"}
f"{service_url}/improved", headers={"Authorization": f"Bearer {auth_token}"}
)
response = request.urlopen(req)
assert response.status == 200
Expand Down
39 changes: 0 additions & 39 deletions run/hello-broken/e2e_test_setup.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions run/hello-broken/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def index():
# [END cloudrun_broken_service_problem]

return f"Hello {NAME}"


# [END run_broken_service]
# [END cloudrun_broken_service]

Expand Down
9 changes: 3 additions & 6 deletions run/hello-broken/noxfile_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@

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"],

"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': 'GOOGLE_CLOUD_PROJECT',
"gcloud_project_env": "GOOGLE_CLOUD_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': {},
"envs": {},
}
Loading

0 comments on commit 54208af

Please sign in to comment.