|
| 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 | +// https://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 | +import assert from 'assert'; |
| 16 | +import {execSync} from 'child_process'; |
| 17 | +import request from 'got'; |
| 18 | +import {GoogleAuth} from 'google-auth-library'; |
| 19 | +const auth = new GoogleAuth(); |
| 20 | + |
| 21 | +const get = (route, base_url) => { |
| 22 | + if (!ID_TOKEN) { |
| 23 | + throw Error('"ID_TOKEN" environment variable is required.'); |
| 24 | + } |
| 25 | + |
| 26 | + return request(new URL(route, base_url.trim()), { |
| 27 | + headers: { |
| 28 | + Authorization: `${ID_TOKEN.trim()}`, |
| 29 | + }, |
| 30 | + throwHttpErrors: false, |
| 31 | + }); |
| 32 | +}; |
| 33 | + |
| 34 | +let BASE_URL, ID_TOKEN; |
| 35 | +describe('End-to-End Tests', () => { |
| 36 | + const {GOOGLE_CLOUD_PROJECT} = process.env; |
| 37 | + if (!GOOGLE_CLOUD_PROJECT) { |
| 38 | + throw Error('"GOOGLE_CLOUD_PROJECT" env var not found.'); |
| 39 | + } |
| 40 | + let {SERVICE_NAME} = process.env; |
| 41 | + if (!SERVICE_NAME) { |
| 42 | + SERVICE_NAME = 'helloworld'; |
| 43 | + console.log( |
| 44 | + `"SERVICE_NAME" env var not found. Defaulting to "${SERVICE_NAME}"` |
| 45 | + ); |
| 46 | + } |
| 47 | + let {NAME} = process.env; |
| 48 | + if (!NAME) { |
| 49 | + NAME = 'Cloud'; |
| 50 | + console.log(`"NAME" env var not found. Defaulting to "${NAME}"`); |
| 51 | + } |
| 52 | + const {SAMPLE_VERSION} = process.env; |
| 53 | + const PLATFORM = 'managed'; |
| 54 | + const REGION = 'us-central1'; |
| 55 | + before(async () => { |
| 56 | + // Deploy service using Cloud Build |
| 57 | + let buildCmd = |
| 58 | + `gcloud builds submit --project ${GOOGLE_CLOUD_PROJECT} ` + |
| 59 | + '--config ./test/e2e_test_setup.yaml ' + |
| 60 | + `--substitutions _SERVICE=${SERVICE_NAME},_PLATFORM=${PLATFORM},_REGION=${REGION}` + |
| 61 | + `,_NAME=${NAME}`; |
| 62 | + if (SAMPLE_VERSION) buildCmd += `,_VERSION=${SAMPLE_VERSION}`; |
| 63 | + |
| 64 | + console.log('Starting Cloud Build...'); |
| 65 | + execSync(buildCmd, {timeout: 240000}); // timeout at 4 mins |
| 66 | + console.log('Cloud Build completed.'); |
| 67 | + |
| 68 | + // Retrieve URL of Cloud Run service |
| 69 | + const url = execSync( |
| 70 | + `gcloud run services describe ${SERVICE_NAME} --project=${GOOGLE_CLOUD_PROJECT} ` + |
| 71 | + `--platform=${PLATFORM} --region=${REGION} --format='value(status.url)'` |
| 72 | + ); |
| 73 | + |
| 74 | + BASE_URL = url.toString('utf-8').trim(); |
| 75 | + if (!BASE_URL) throw Error('Cloud Run service URL not found'); |
| 76 | + |
| 77 | + // Retrieve ID token for testing |
| 78 | + const client = await auth.getIdTokenClient(BASE_URL); |
| 79 | + const clientHeaders = await client.getRequestHeaders(); |
| 80 | + ID_TOKEN = clientHeaders['Authorization'].trim(); |
| 81 | + if (!ID_TOKEN) throw Error('Unable to acquire an ID token.'); |
| 82 | + }); |
| 83 | + |
| 84 | + after(() => { |
| 85 | + let cleanUpCmd = |
| 86 | + `gcloud builds submit --project ${GOOGLE_CLOUD_PROJECT} ` + |
| 87 | + '--config ./test/e2e_test_cleanup.yaml ' + |
| 88 | + `--substitutions _SERVICE=${SERVICE_NAME},_PLATFORM=${PLATFORM},_REGION=${REGION}`; |
| 89 | + if (SAMPLE_VERSION) cleanUpCmd += `,_VERSION=${SAMPLE_VERSION}`; |
| 90 | + |
| 91 | + execSync(cleanUpCmd); |
| 92 | + }); |
| 93 | + |
| 94 | + it('Service uses the NAME override', async () => { |
| 95 | + const response = await get('/', BASE_URL); |
| 96 | + assert.strictEqual( |
| 97 | + response.statusCode, |
| 98 | + 200, |
| 99 | + 'Did not fallback to default as expected' |
| 100 | + ); |
| 101 | + assert.strictEqual( |
| 102 | + response.body, |
| 103 | + `Hello ${NAME}!`, |
| 104 | + `Expected override "${NAME}" not found` |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
0 commit comments