|
| 1 | +# Copyright 2021 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 datetime |
| 20 | +import os |
| 21 | +import subprocess |
| 22 | +from urllib import request |
| 23 | +import uuid |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +# Unique suffix to create distinct service names |
| 28 | +SUFFIX = uuid.uuid4().hex |
| 29 | +PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def deployed_service(): |
| 34 | + # Deploy image to Cloud Run |
| 35 | + service_name = f'filesystem-{SUFFIX}' |
| 36 | + connector = os.environ['CONNECTOR'] |
| 37 | + ip_address = os.environ['IP_ADDRESS'] |
| 38 | + |
| 39 | + subprocess.run( |
| 40 | + [ |
| 41 | + 'gcloud', |
| 42 | + 'builds', |
| 43 | + 'submit', |
| 44 | + '--config', |
| 45 | + 'cloudbuild.yaml', |
| 46 | + '--project', |
| 47 | + PROJECT, |
| 48 | + f'--substitutions=_SERVICE_NAME={service_name},_FILESTORE_IP_ADDRESS={ip_address},_CONNECTOR={connector}' |
| 49 | + ], |
| 50 | + check=True, |
| 51 | + ) |
| 52 | + |
| 53 | + yield service_name |
| 54 | + |
| 55 | + subprocess.run( |
| 56 | + [ |
| 57 | + 'gcloud', |
| 58 | + 'run', |
| 59 | + 'services', |
| 60 | + 'delete', |
| 61 | + service_name, |
| 62 | + '--region=us-central1', |
| 63 | + '--platform=managed', |
| 64 | + '--quiet', |
| 65 | + '--project', |
| 66 | + PROJECT, |
| 67 | + ], |
| 68 | + check=True, |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture |
| 73 | +def service_url_auth_token(deployed_service): |
| 74 | + # Get Cloud Run service URL and auth token |
| 75 | + service_url = ( |
| 76 | + subprocess.run( |
| 77 | + [ |
| 78 | + 'gcloud', |
| 79 | + 'run', |
| 80 | + 'services', |
| 81 | + 'describe', |
| 82 | + deployed_service, |
| 83 | + '--region=us-central1', |
| 84 | + '--platform=managed', |
| 85 | + '--format=value(status.url)', |
| 86 | + '--project', |
| 87 | + PROJECT, |
| 88 | + ], |
| 89 | + stdout=subprocess.PIPE, |
| 90 | + check=True, |
| 91 | + ) |
| 92 | + .stdout.strip() |
| 93 | + .decode() |
| 94 | + ) |
| 95 | + auth_token = ( |
| 96 | + subprocess.run( |
| 97 | + ['gcloud', 'auth', 'print-identity-token'], |
| 98 | + stdout=subprocess.PIPE, |
| 99 | + check=True, |
| 100 | + ) |
| 101 | + .stdout.strip() |
| 102 | + .decode() |
| 103 | + ) |
| 104 | + |
| 105 | + yield service_url, auth_token |
| 106 | + |
| 107 | + # no deletion needed |
| 108 | + |
| 109 | + |
| 110 | +def test_end_to_end(service_url_auth_token): |
| 111 | + service_url, auth_token = service_url_auth_token |
| 112 | + # Non mnt directory |
| 113 | + req = request.Request( |
| 114 | + service_url, headers={'Authorization': f'Bearer {auth_token}'} |
| 115 | + ) |
| 116 | + response = request.urlopen(req) |
| 117 | + assert response.status == 200 |
| 118 | + |
| 119 | + # Mnt directory |
| 120 | + mnt_url = f'{service_url}/mnt/nfs/filestore' |
| 121 | + req = request.Request( |
| 122 | + mnt_url, headers={'Authorization': f'Bearer {auth_token}'} |
| 123 | + ) |
| 124 | + response = request.urlopen(req) |
| 125 | + assert response.status == 200 |
| 126 | + |
| 127 | + date = datetime.datetime.utcnow() |
| 128 | + body = response.read() |
| 129 | + assert '{dt:%a}-{dt:%b}-{dt:%d}-{dt:%H}:{dt:%M}-{dt:%Y}'.format(dt=date) in body.decode() |
0 commit comments