From 211423560323acadd84a86ab10e78f2ea4847a98 Mon Sep 17 00:00:00 2001 From: eq19 Date: Fri, 27 Sep 2024 07:34:27 +0000 Subject: [PATCH] Update action.yml --- .github/action.yml | 16 ++++++ .../1-ubuntu/1-maps/2-build/action.yml | 6 --- .github/functions/index.js | 31 +++++++---- .github/functions/package.json | 27 ++++++---- .github/functions/test/index.test.js | 54 +++++++++++++++++++ 5 files changed, 109 insertions(+), 25 deletions(-) create mode 100644 .github/functions/test/index.test.js diff --git a/.github/action.yml b/.github/action.yml index 82fff35118579..20f8044e0e1c8 100644 --- a/.github/action.yml +++ b/.github/action.yml @@ -26,6 +26,16 @@ inputs: action_path: description: 'Path to the requirements file' required: true + hub_username: + description: Username for Docker Hub + default: ${{ github.actor }} + required: true + hub_password: + description: Docker Hub authentication token + required: true + hub_token: + description: Docker Hub authentication token + required: true runs: using: composite @@ -58,6 +68,12 @@ runs: if [ $? -eq 0 ]; then exit 1; fi + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ inputs.hub_username }} + password: ${{ inputs.hub_token }} + - uses: actions/setup-python@v5 if: runner.os != 'Windows' with: diff --git a/.github/actions/1-ubuntu/1-maps/2-build/action.yml b/.github/actions/1-ubuntu/1-maps/2-build/action.yml index 9518b89764a40..008669b0a752e 100644 --- a/.github/actions/1-ubuntu/1-maps/2-build/action.yml +++ b/.github/actions/1-ubuntu/1-maps/2-build/action.yml @@ -33,12 +33,6 @@ outputs: runs: using: composite steps: - - name: Login to DockerHub - uses: docker/login-action@v3 - with: - username: ${{ inputs.hub_username }} - password: ${{ inputs.hub_token }} - - name: 🚀 Initiate Lexer uses: devcontainers/ci@v0.3 with: diff --git a/.github/functions/index.js b/.github/functions/index.js index 184eb80fea8f5..e87fda1992568 100644 --- a/.github/functions/index.js +++ b/.github/functions/index.js @@ -1,12 +1,25 @@ -import express from 'express'; -const app = express(); +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -app.get('/', (req, res) => { - const name = process.env.NAME || 'World'; - res.send(`Hello ${name}!`); -}); +'use strict'; + +// [START functions_helloworld_get] +const functions = require('@google-cloud/functions-framework'); -const port = parseInt(process.env.PORT) || 8080; -app.listen(port, () => { - console.log(`helloworld: listening on port ${port}`); +// Register an HTTP function with the Functions Framework that will be executed +// when you make an HTTP request to the deployed function's endpoint. +functions.http('helloGET', (req, res) => { + res.send('Hello World!'); }); +// [END functions_helloworld_get] diff --git a/.github/functions/package.json b/.github/functions/package.json index 8661600a08036..d17681b8eb02b 100644 --- a/.github/functions/package.json +++ b/.github/functions/package.json @@ -1,19 +1,26 @@ { - "name": "helloworld", - "description": "Simple hello world sample in Node", - "version": "1.0.0", + "name": "nodejs-docs-samples-functions-hello-world-get", + "version": "0.0.1", "private": true, - "main": "index.js", - "type": "module", - "scripts": { - "start": "node index.js" + "license": "Apache-2.0", + "author": "Google Inc.", + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" }, "engines": { "node": ">=16.0.0" }, - "author": "Google LLC", - "license": "Apache-2.0", + "scripts": { + "test": "c8 mocha -p -j 2 test/*.test.js --timeout=6000 --exit" + }, "dependencies": { - "express": "^4.17.1" + "@google-cloud/functions-framework": "^3.1.0" + }, + "devDependencies": { + "c8": "^10.0.0", + "gaxios": "^6.0.0", + "mocha": "^10.0.0", + "wait-port": "^1.0.4" } } diff --git a/.github/functions/test/index.test.js b/.github/functions/test/index.test.js new file mode 100644 index 0000000000000..36e1e0de75c40 --- /dev/null +++ b/.github/functions/test/index.test.js @@ -0,0 +1,54 @@ +// Copyright 2017 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +const assert = require('assert'); +const {request} = require('gaxios'); +const {exec} = require('child_process'); +const waitPort = require('wait-port'); + +const startFF = async (target, signature, port) => { + const ff = exec( + `npx functions-framework --target=${target} --signature-type=${signature} --port=${port}` + ); + await waitPort({host: 'localhost', port}); + return ff; +}; + +const httpInvocation = (fnUrl, port) => { + const baseUrl = `http://localhost:${port}`; + + // GET request + return request({ + url: `${baseUrl}/${fnUrl}`, + }); +}; + +describe('index.test.js', () => { + describe('functions_helloworld_get helloGET', () => { + const PORT = 8081; + let ffProc; + + before(async () => { + ffProc = await startFF('helloGET', 'http', PORT); + }); + + after(() => ffProc.kill()); + + it('helloGET: should print hello world', async () => { + const response = await httpInvocation('helloGET', PORT); + assert.strictEqual(response.status, 200); + assert.strictEqual(response.data, 'Hello World!'); + }); + }); +});