|
| 1 | +// Copyright 2022 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 | +const assert = require('assert'); |
| 16 | +const {execSync} = require('child_process'); |
| 17 | +const {Logging} = require('@google-cloud/logging'); |
| 18 | + |
| 19 | +describe('End-to-End Tests', () => { |
| 20 | + const {GOOGLE_CLOUD_PROJECT} = process.env; |
| 21 | + if (!GOOGLE_CLOUD_PROJECT) { |
| 22 | + throw Error('"GOOGLE_CLOUD_PROJECT" env var not found.'); |
| 23 | + } |
| 24 | + let {SERVICE_NAME} = process.env; |
| 25 | + if (!SERVICE_NAME) { |
| 26 | + SERVICE_NAME = 'logger-job'; |
| 27 | + console.log( |
| 28 | + `"SERVICE_NAME" env var not found. Defaulting to "${SERVICE_NAME}"` |
| 29 | + ); |
| 30 | + } |
| 31 | + const {SAMPLE_VERSION} = process.env; |
| 32 | + const REGION = 'us-central1'; |
| 33 | + before(async () => { |
| 34 | + // Deploy service using Cloud Build |
| 35 | + let buildCmd = |
| 36 | + `gcloud builds submit --project ${GOOGLE_CLOUD_PROJECT} ` + |
| 37 | + '--config ./test/e2e_test_setup.yaml ' + |
| 38 | + `--substitutions _SERVICE=${SERVICE_NAME},_REGION=${REGION}`; |
| 39 | + if (SAMPLE_VERSION) buildCmd += `,_VERSION=${SAMPLE_VERSION}`; |
| 40 | + |
| 41 | + console.log('Starting Cloud Build...'); |
| 42 | + execSync(buildCmd); |
| 43 | + console.log('Cloud Build completed.'); |
| 44 | + }); |
| 45 | + |
| 46 | + after(() => { |
| 47 | + let cleanUpCmd = |
| 48 | + `gcloud builds submit --project ${GOOGLE_CLOUD_PROJECT} ` + |
| 49 | + '--config ./test/e2e_test_cleanup.yaml ' + |
| 50 | + `--substitutions _SERVICE=${SERVICE_NAME},_REGION=${REGION}`; |
| 51 | + if (SAMPLE_VERSION) cleanUpCmd += `,_VERSION=${SAMPLE_VERSION}`; |
| 52 | + |
| 53 | + execSync(cleanUpCmd); |
| 54 | + }); |
| 55 | + |
| 56 | + const dateMinutesAgo = (date, min_ago) => { |
| 57 | + date.setMinutes(date.getMinutes() - min_ago); |
| 58 | + return date.toISOString(); |
| 59 | + }; |
| 60 | + |
| 61 | + it('generates logs in Cloud Logging', async () => { |
| 62 | + const logging = new Logging({ |
| 63 | + projectId: process.env.GOOGLE_CLOUD_PROJECT, |
| 64 | + }); |
| 65 | + |
| 66 | + const preparedFilter = |
| 67 | + 'resource.type = "cloud_run_revision" ' + |
| 68 | + `resource.labels.service_name = "${SERVICE_NAME}" ` + |
| 69 | + `resource.labels.location = "${REGION}" ` + |
| 70 | + `timestamp>="${dateMinutesAgo(new Date(), 5)}"`; |
| 71 | + |
| 72 | + const entries = await logging.getEntries({ |
| 73 | + filter: preparedFilter, |
| 74 | + autoPaginate: false, |
| 75 | + pageSize: 3, |
| 76 | + }); |
| 77 | + |
| 78 | + assert(entries[0]); |
| 79 | + assert(entries[0].length > 0); |
| 80 | + const found = entries[0].find(entry => { |
| 81 | + return typeof entry.data === 'string' |
| 82 | + ? entry.data.includes('Task') |
| 83 | + : false; |
| 84 | + }); |
| 85 | + assert(found); |
| 86 | + }); |
| 87 | +}); |
0 commit comments