|
| 1 | +# Copyright 2020 Google, LLC. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# This tests the Pub/Sub to Cloud Run integration |
| 16 | + |
| 17 | +import datetime |
| 18 | +import os |
| 19 | +import subprocess |
| 20 | +import time |
| 21 | +import uuid |
| 22 | + |
| 23 | +from google.cloud import logging_v2 |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | + |
| 28 | +SUFFIX = uuid.uuid4().hex[0:6] |
| 29 | +PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] |
| 30 | +CLOUD_RUN_SERVICE = f"pubsub-test-{SUFFIX}" |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture() |
| 34 | +def cloud_run_service(): |
| 35 | + # Build and Deploy Cloud Run Services |
| 36 | + subprocess.run( |
| 37 | + ["gcloud", "builds", "submit", "--project", PROJECT, |
| 38 | + f"--substitutions=_SERVICE={CLOUD_RUN_SERVICE}", |
| 39 | + "--config=e2e_test_setup.yaml", "--quiet"], |
| 40 | + check=True |
| 41 | + ) |
| 42 | + |
| 43 | + yield |
| 44 | + |
| 45 | + # Delete Cloud Run service and image container |
| 46 | + subprocess.run( |
| 47 | + ["gcloud", "run", "services", "delete", CLOUD_RUN_SERVICE, |
| 48 | + "--project", PROJECT, "--platform", "managed", "--region", |
| 49 | + "us-central1", "--quiet"], |
| 50 | + check=True |
| 51 | + ) |
| 52 | + |
| 53 | + subprocess.run( |
| 54 | + ["gcloud", "container", "images", "delete", |
| 55 | + f"gcr.io/{PROJECT}/{CLOUD_RUN_SERVICE}", "--quiet"], |
| 56 | + check=True |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def service_url(cloud_run_service): |
| 62 | + # Get the URL for the cloud run service |
| 63 | + service_url = subprocess.run( |
| 64 | + [ |
| 65 | + "gcloud", |
| 66 | + "run", |
| 67 | + "--project", |
| 68 | + PROJECT, |
| 69 | + "--platform=managed", |
| 70 | + "--region=us-central1", |
| 71 | + "services", |
| 72 | + "describe", |
| 73 | + CLOUD_RUN_SERVICE, |
| 74 | + "--format=value(status.url)", |
| 75 | + ], |
| 76 | + stdout=subprocess.PIPE, |
| 77 | + check=True |
| 78 | + ).stdout.strip() |
| 79 | + |
| 80 | + yield service_url.decode() |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture() |
| 84 | +def pubsub_topic(service_url): |
| 85 | + # Create pub/sub topic |
| 86 | + topic = f"e2e_{SUFFIX}" |
| 87 | + subprocess.run( |
| 88 | + ["gcloud", "pubsub", "topics", "create", topic, |
| 89 | + "--project", PROJECT, "--quiet"], check=True |
| 90 | + ) |
| 91 | + |
| 92 | + # Create pubsub push subscription to Cloud Run Service |
| 93 | + # Attach service account with Cloud Run Invoker role |
| 94 | + # See tutorial for details on setting up service-account: |
| 95 | + # https://cloud.google.com/run/docs/tutorials/pubsub |
| 96 | + subprocess.run( |
| 97 | + ["gcloud", "pubsub", "subscriptions", "create", f"{topic}_sub", |
| 98 | + "--topic", topic, "--push-endpoint", service_url, "--project", |
| 99 | + PROJECT, "--push-auth-service-account", |
| 100 | + f"cloud-run-invoker@{PROJECT}.iam.gserviceaccount.com", |
| 101 | + "--quiet"], check=True |
| 102 | + ) |
| 103 | + |
| 104 | + yield topic |
| 105 | + |
| 106 | + # Delete topic |
| 107 | + subprocess.run( |
| 108 | + ["gcloud", "pubsub", "topics", "delete", topic, |
| 109 | + "--project", PROJECT, "--quiet"], check=True |
| 110 | + ) |
| 111 | + |
| 112 | + # Delete subscription |
| 113 | + subprocess.run( |
| 114 | + ["gcloud", "pubsub", "subscriptions", "delete", f"{topic}_sub", |
| 115 | + "--project", PROJECT, "--quiet"], check=True |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def test_end_to_end(pubsub_topic): |
| 120 | + # Post the message "Runner" to the topic |
| 121 | + subprocess.run( |
| 122 | + ["gcloud", "pubsub", "topics", "publish", f"{pubsub_topic}", |
| 123 | + "--project", PROJECT, "--message", "Runner", "--quiet"], check=True |
| 124 | + ) |
| 125 | + |
| 126 | + # Check the logs for "Hello Runner" |
| 127 | + time.sleep(20) # Slight delay writing to stackdriver |
| 128 | + client = logging_v2.LoggingServiceV2Client() |
| 129 | + resource_names = [f"projects/{PROJECT}"] |
| 130 | + |
| 131 | + # We add timestamp for making the query faster. |
| 132 | + now = datetime.datetime.now(datetime.timezone.utc) |
| 133 | + filter_date = now - datetime.timedelta(minutes=1) |
| 134 | + filters = ( |
| 135 | + f"timestamp>=\"{filter_date.isoformat('T')}\" " |
| 136 | + "resource.type=cloud_run_revision " |
| 137 | + f"AND resource.labels.service_name={CLOUD_RUN_SERVICE} " |
| 138 | + ) |
| 139 | + |
| 140 | + # Retry a maximum number of 10 times to find results in stackdriver |
| 141 | + found = False |
| 142 | + for x in range(10): |
| 143 | + iterator = client.list_log_entries(resource_names, filter_=filters) |
| 144 | + for entry in iterator: |
| 145 | + if entry.text_payload == "Hello Runner!": |
| 146 | + found = True |
| 147 | + break |
| 148 | + # When message found, exit loop |
| 149 | + if found is True: |
| 150 | + break |
| 151 | + time.sleep(5) # Slight delay before retry |
| 152 | + |
| 153 | + assert found |
0 commit comments