|
| 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 image processing sample |
| 16 | + |
| 17 | +import os |
| 18 | +import subprocess |
| 19 | +import time |
| 20 | +import uuid |
| 21 | + |
| 22 | +from google.cloud import storage |
| 23 | +from google.cloud.storage import Blob, notification |
| 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"image-proc-{SUFFIX}" |
| 31 | +INPUT_BUCKET = f"image-proc-input-{SUFFIX}" |
| 32 | +OUTPUT_BUCKET = f"image-proc-output-{SUFFIX}" |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture() |
| 36 | +def cloud_run_service(): |
| 37 | + # Build and Deploy Cloud Run Services |
| 38 | + subprocess.run( |
| 39 | + ["gcloud", "builds", "submit", "--project", PROJECT, |
| 40 | + f"--substitutions=_SERVICE={CLOUD_RUN_SERVICE},_OUTPUT_BUCKET={OUTPUT_BUCKET}", |
| 41 | + "--config=e2e_test_setup.yaml", "--quiet"], |
| 42 | + check=True |
| 43 | + ) |
| 44 | + |
| 45 | + yield |
| 46 | + |
| 47 | + # Delete Cloud Run service and image container |
| 48 | + subprocess.run( |
| 49 | + ["gcloud", "run", "services", "delete", CLOUD_RUN_SERVICE, |
| 50 | + "--project", PROJECT, "--platform", "managed", "--region", |
| 51 | + "us-central1", "--quiet"], |
| 52 | + check=True |
| 53 | + ) |
| 54 | + |
| 55 | + subprocess.run( |
| 56 | + ["gcloud", "container", "images", "delete", |
| 57 | + f"gcr.io/{PROJECT}/{CLOUD_RUN_SERVICE}", "--quiet"], |
| 58 | + check=True |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def service_url(cloud_run_service): |
| 64 | + # Get the URL for the cloud run service |
| 65 | + service_url = subprocess.run( |
| 66 | + [ |
| 67 | + "gcloud", |
| 68 | + "run", |
| 69 | + "--project", |
| 70 | + PROJECT, |
| 71 | + "--platform=managed", |
| 72 | + "--region=us-central1", |
| 73 | + "services", |
| 74 | + "describe", |
| 75 | + CLOUD_RUN_SERVICE, |
| 76 | + "--format=value(status.url)", |
| 77 | + ], |
| 78 | + stdout=subprocess.PIPE, |
| 79 | + check=True |
| 80 | + ).stdout.strip() |
| 81 | + |
| 82 | + yield service_url.decode() |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture() |
| 86 | +def pubsub_topic(service_url): |
| 87 | + # Create pub/sub topic |
| 88 | + topic = f"image_proc_{SUFFIX}" |
| 89 | + subprocess.run( |
| 90 | + ["gcloud", "pubsub", "topics", "create", topic, |
| 91 | + "--project", PROJECT, "--quiet"], check=True |
| 92 | + ) |
| 93 | + |
| 94 | + # Create pubsub push subscription to Cloud Run Service |
| 95 | + # Attach service account with Cloud Run Invoker role |
| 96 | + # See tutorial for details on setting up service-account: |
| 97 | + # https://cloud.google.com/run/docs/tutorials/pubsub |
| 98 | + subprocess.run( |
| 99 | + ["gcloud", "pubsub", "subscriptions", "create", f"{topic}_sub", |
| 100 | + "--topic", topic, "--push-endpoint", service_url, "--project", |
| 101 | + PROJECT, "--push-auth-service-account", |
| 102 | + f"cloud-run-invoker@{PROJECT}.iam.gserviceaccount.com", "--quiet"], check=True |
| 103 | + ) |
| 104 | + |
| 105 | + yield topic |
| 106 | + |
| 107 | + # Delete topic |
| 108 | + subprocess.run( |
| 109 | + ["gcloud", "pubsub", "topics", "delete", topic, |
| 110 | + "--project", PROJECT, "--quiet"], check=True |
| 111 | + ) |
| 112 | + |
| 113 | + # Delete subscription |
| 114 | + subprocess.run( |
| 115 | + ["gcloud", "pubsub", "subscriptions", "delete", f"{topic}_sub", |
| 116 | + "--project", PROJECT, "--quiet"], check=True |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +@pytest.fixture() |
| 121 | +def storage_buckets(pubsub_topic): |
| 122 | + # Create GCS Buckets |
| 123 | + storage_client = storage.Client() |
| 124 | + storage_client.create_bucket(INPUT_BUCKET) |
| 125 | + storage_client.create_bucket(OUTPUT_BUCKET) |
| 126 | + |
| 127 | + # Get input and output buckets |
| 128 | + input_bucket = storage_client.get_bucket(INPUT_BUCKET) |
| 129 | + output_bucket = storage_client.get_bucket(OUTPUT_BUCKET) |
| 130 | + |
| 131 | + # Create pub/sub notification on input_bucket |
| 132 | + notification.BucketNotification(input_bucket, topic_name=pubsub_topic, |
| 133 | + topic_project=PROJECT, payload_format="JSON_API_V1").create() |
| 134 | + |
| 135 | + yield input_bucket, output_bucket |
| 136 | + |
| 137 | + # Delete GCS buckets |
| 138 | + input_bucket.delete(force=True) |
| 139 | + output_bucket.delete(force=True) |
| 140 | + |
| 141 | + |
| 142 | +def test_end_to_end(storage_buckets): |
| 143 | + # Upload image to the input bucket |
| 144 | + input_bucket = storage_buckets[0] |
| 145 | + output_bucket = storage_buckets[1] |
| 146 | + |
| 147 | + blob = Blob("zombie.jpg", input_bucket) |
| 148 | + blob.upload_from_filename("test-images/zombie.jpg", content_type="image/jpeg") |
| 149 | + |
| 150 | + # Wait for image processing to complete |
| 151 | + time.sleep(30) |
| 152 | + |
| 153 | + for x in range(10): |
| 154 | + # Check for blurred image in output bucket |
| 155 | + output_blobs = list(output_bucket.list_blobs()) |
| 156 | + if len(output_blobs) > 0: |
| 157 | + break |
| 158 | + |
| 159 | + time.sleep(5) |
| 160 | + |
| 161 | + assert len(output_blobs) > 0 |
0 commit comments