Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/gcp-metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"fix": "gts fix",
"pretest": "npm run compile",
"prepare": "npm run compile",
"samples-test": "npm run compile && cd samples/ && npm link ../ && npm i && npm test",
"samples-test": "npm link && cd samples/ && npm link ../ && npm test && cd ../",
"presystem-test": "npm run compile",
"system-test": "npm run compile && c8 mocha --timeout 300000 build/system-test",
"system-test": "mocha build/system-test --timeout 600000",
"test": "c8 mocha --timeout=5000 build/test",
"docs": "jsdoc -c .jsdoc.js",
"lint": "gts check",
Expand Down Expand Up @@ -60,8 +60,8 @@
"gcx": "^2.0.27",
"gts": "^6.0.2",
"jsdoc": "^4.0.4",
"jsdoc-fresh": "^4.0.0",
"jsdoc-region-tag": "^3.0.0",
"jsdoc-fresh": "^5.0.0",
"jsdoc-region-tag": "^4.0.0",
"linkinator": "^6.1.2",
"mocha": "^11.1.0",
"ncp": "^2.0.0",
Expand All @@ -74,4 +74,4 @@
"node": ">=18"
},
"homepage": "https://github.com/googleapis/google-cloud-node-core/tree/main/packages/gcp-metadata"
}
}
61 changes: 61 additions & 0 deletions packages/gcp-metadata/samples/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2018 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('chai');
const {describe, it, before, after} = require('mocha');
const cp = require('child_process');
const http = require('http');
const {promisify} = require('util');

const exec = promisify(cp.exec);

function handleRequest(req, res) {
res.setHeader('Connection', 'close'); // Important to prevent hanging.
res.setHeader('Metadata-Flavor', 'Google');

if (req.url.includes('/instance')) {
res.end(JSON.stringify({id: 'mock-instance-id'}));
} else if (req.url.includes('/project')) {
res.end(JSON.stringify({projectId: 'mock-project-id'}));
} else {
// Handles the isAvailable() check which hits the root URL.
res.end('true');
}
}

describe('gcp-metadata samples', () => {
let server;
let port;

before(async () => {
// Start a local server to mock the metadata service.
server = http.createServer(handleRequest);
await promisify(server.listen.bind(server))(0);
port = server.address().port;
});

after(async () => {
await promisify(server.close.bind(server))();
});

it('should run the quickstart', async () => {
// Use process.execPath to ensure the same node executable is used to run the sample.
// This is more robust for CI/CD environments.
const command = `${process.execPath} quickstart.js`;
const {stdout} = await exec(command, {env: {GCE_METADATA_HOST: `localhost:${port}`}});
assert.match(stdout, /Is available: true/);
});
});