Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor fixtures, add static resources #4599

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 7 commits into from
Sep 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 65 additions & 143 deletions memorystore/redis/cloud_run_deployment/e2e_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,83 +24,41 @@

import pytest

# Unique suffix to create distinct service names

@pytest.fixture()
def services():
# Unique suffix to create distinct service names
suffix = uuid.uuid4().hex[:10]
project = os.environ["GOOGLE_CLOUD_PROJECT"]
SUFFIX = uuid.uuid4().hex[:10]
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
VPC_CONNECTOR_NAME = "test-connector"
MEMORYSTORE_REDIS_NAME = "static-test-instance"

# Create a VPC network
network_name = f"test-network-{suffix}"
subprocess.run(
[
"gcloud",
"compute",
"networks",
"create",
network_name,
"--project",
project,
], check=True
)

# Create a Serverless VPC Access connector
connector_name = f"test-connector-{suffix}"
subprocess.run(
[
"gcloud",
"compute",
"networks",
"vpc-access",
"connectors",
"create",
connector_name,
"--network",
network_name,
"--region=us-central1",
"--range=192.168.16.0/28",
"--project",
project,
], check=True
)

# Create a Memorystore Redis instance
instance_name = f"test-instance-{suffix}"
subprocess.run(
[
"gcloud",
"redis",
"instances",
"create",
instance_name,
"--region=us-central1",
"--network",
network_name,
"--project",
project,
], check=True
)

@pytest.fixture
def redis_host():
# Get the Redis instance's IP
redis_host = subprocess.run(
[
"gcloud",
"redis",
"instances",
"describe",
instance_name,
MEMORYSTORE_REDIS_NAME,
"--region=us-central1",
"--format=value(host)",
"--project",
project,
PROJECT,
],
stdout=subprocess.PIPE,
check=True
).stdout.strip().decode()
yield redis_host

# no deletion needs to happen, this is a "get" of a static instance


@pytest.fixture
def container_image():
# Build container image for Cloud Run deployment
image_name = f"gcr.io/{project}/test-visit-count-{suffix}"
image_name = f"gcr.io/{PROJECT}/test-visit-count-{SUFFIX}"
subprocess.run(
[
"cp",
Expand All @@ -116,46 +74,85 @@ def services():
"--tag",
image_name,
"--project",
project,
PROJECT,
], check=True
)
yield image_name

subprocess.run(["rm", "Dockerfile"], check=True)

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


@pytest.fixture
def deployed_service(container_image, redis_host):
# Deploy image to Cloud Run
service_name = f"test-visit-count-{suffix}"
service_name = f"test-visit-count-{SUFFIX}"
subprocess.run(
[
"gcloud",
"run",
"deploy",
service_name,
"--image",
image_name,
container_image,
"--platform=managed",
"--no-allow-unauthenticated",
"--region=us-central1",
"--vpc-connector",
connector_name,
VPC_CONNECTOR_NAME,
"--set-env-vars",
f"REDISHOST={redis_host},REDISPORT=6379",
"--project",
project,
PROJECT,
], check=True
)
yield service_name

# Delete Cloud Run service
subprocess.run(
[
"gcloud",
"run",
"services",
"delete",
service_name,
"--platform=managed",
"--region=us-central1",
"--quiet",
"--project",
PROJECT,
], check=True
)


@pytest.fixture
def service_url_auth_token(deployed_service):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the way you separated out each part of the "services" into their own fixtures!

# Get Cloud Run service URL and auth token
service_url = subprocess.run(
[
"gcloud",
"run",
"services",
"describe",
service_name,
deployed_service,
"--platform=managed",
"--region=us-central1",
"--format=value(status.url)",
"--project",
project,
PROJECT,
],
stdout=subprocess.PIPE,
check=True
Expand All @@ -168,86 +165,11 @@ def services():

yield service_url, auth_token

# Delete Cloud Run service
subprocess.run(
[
"gcloud",
"run",
"services",
"delete",
service_name,
"--platform=managed",
"--region=us-central1",
"--quiet",
"--project",
project,
], check=True
)

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

# Delete Redis instance
subprocess.run(
[
"gcloud",
"redis",
"instances",
"delete",
instance_name,
"--region=us-central1",
"--quiet",
"--async",
"--project",
project,
], check=True
)

# Delete Serverless VPC Access connector
subprocess.run(
[
"gcloud",
"compute",
"networks",
"vpc-access",
"connectors",
"delete",
connector_name,
"--region=us-central1",
"--quiet",
"--project",
project,
], check=True
)

# Delete VPC network
subprocess.run(
[
"gcloud",
"compute",
"networks",
"delete",
network_name,
"--quiet",
"--project",
project,
], check=True
)
# no deletion needed


def test_end_to_end(services):
service_url, auth_token = services
def test_end_to_end(service_url_auth_token):
service_url, auth_token = service_url_auth_token

req = request.Request(
service_url,
Expand Down