|
| 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 sample creates a secure two-service application running on Cloud Run. |
| 16 | +# This test builds and deploys the two secure services |
| 17 | +# to test that they interact properly together. |
| 18 | + |
| 19 | +import os |
| 20 | +import subprocess |
| 21 | +from urllib import request |
| 22 | +import uuid |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture() |
| 28 | +def services(): |
| 29 | + # Unique suffix to create distinct service names |
| 30 | + suffix = uuid.uuid4().hex |
| 31 | + project = os.environ["GOOGLE_CLOUD_PROJECT"] |
| 32 | + |
| 33 | + # Build and Deploy Cloud Run Services |
| 34 | + subprocess.run( |
| 35 | + [ |
| 36 | + "gcloud", |
| 37 | + "builds", |
| 38 | + "submit", |
| 39 | + f"--project={project}", |
| 40 | + f"--substitutions=_SUFFIX={suffix}", |
| 41 | + "--config=e2e_test_setup.yaml", |
| 42 | + "--quiet", |
| 43 | + ], |
| 44 | + check=True, |
| 45 | + ) |
| 46 | + |
| 47 | + # Get the URL for the service and the token |
| 48 | + service = subprocess.run( |
| 49 | + [ |
| 50 | + "gcloud", |
| 51 | + "run", |
| 52 | + f"--project={project}", |
| 53 | + "--platform=managed", |
| 54 | + "--region=us-central1", |
| 55 | + "services", |
| 56 | + "describe", |
| 57 | + f"helloworld-{suffix}", |
| 58 | + "--format=value(status.url)", |
| 59 | + ], |
| 60 | + stdout=subprocess.PIPE, |
| 61 | + check=True, |
| 62 | + ).stdout.strip() |
| 63 | + |
| 64 | + token = subprocess.run( |
| 65 | + ["gcloud", "auth", "print-identity-token"], stdout=subprocess.PIPE, check=True |
| 66 | + ).stdout.strip() |
| 67 | + |
| 68 | + yield service, token |
| 69 | + |
| 70 | + subprocess.run( |
| 71 | + [ |
| 72 | + "gcloud", |
| 73 | + "run", |
| 74 | + "services", |
| 75 | + "delete", |
| 76 | + f"helloworld-{suffix}", |
| 77 | + f"--project={project}", |
| 78 | + "--platform=managed", |
| 79 | + "--region=us-central1", |
| 80 | + "--quiet", |
| 81 | + ], |
| 82 | + check=True, |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def test_end_to_end(services): |
| 87 | + service = services[0].decode() |
| 88 | + token = services[1].decode() |
| 89 | + |
| 90 | + req = request.Request( |
| 91 | + f"{service}/", headers={"Authorization": f"Bearer {token}"} |
| 92 | + ) |
| 93 | + response = request.urlopen(req) |
| 94 | + assert response.status == 200 |
| 95 | + |
| 96 | + body = response.read() |
| 97 | + assert "Hello Test!" == body.decode() |
0 commit comments