diff --git a/core/dev-packages/pack-n-play/test/fixtures/esm-package/test/test.js b/core/dev-packages/pack-n-play/test/fixtures/esm-package/test/test.js index 037babedf227..2adbf53d289c 100644 --- a/core/dev-packages/pack-n-play/test/fixtures/esm-package/test/test.js +++ b/core/dev-packages/pack-n-play/test/fixtures/esm-package/test/test.js @@ -16,7 +16,8 @@ import {packNTest} from 'pack-n-play'; import * as assert from 'assert'; import {describe, it} from 'mocha'; -describe('ESM package', () => { +describe('ESM package', function () { + this.timeout(120000); it('should support esm property', () => packNTest({ sample: { diff --git a/core/dev-packages/pack-n-play/test/fixtures/leaky/test/test.ts b/core/dev-packages/pack-n-play/test/fixtures/leaky/test/test.ts index 3a9dc7f39b39..9c56bc9c0741 100644 --- a/core/dev-packages/pack-n-play/test/fixtures/leaky/test/test.ts +++ b/core/dev-packages/pack-n-play/test/fixtures/leaky/test/test.ts @@ -16,7 +16,8 @@ import {packNTest} from 'pack-n-play'; import * as assert from 'assert'; import {describe, it} from 'mocha'; -describe('leaky tests', () => { +describe('leaky tests', function () { + this.timeout(120000); it('should fail packing n testing', async () => { await assert.rejects( packNTest({ diff --git a/core/dev-packages/pack-n-play/test/fixtures/pass/test/test.ts b/core/dev-packages/pack-n-play/test/fixtures/pass/test/test.ts index 2dd02fb27cff..50d0d07d620d 100644 --- a/core/dev-packages/pack-n-play/test/fixtures/pass/test/test.ts +++ b/core/dev-packages/pack-n-play/test/fixtures/pass/test/test.ts @@ -15,7 +15,8 @@ import {packNTest} from 'pack-n-play'; import {describe, it} from 'mocha'; -describe('passing tests', () => { +describe('passing tests', function () { + this.timeout(120000); it('should pass the test', async () => { await packNTest({ sample: { diff --git a/core/dev-packages/pack-n-play/test/test.ts b/core/dev-packages/pack-n-play/test/test.ts index da954b94ba08..06aef544b4e7 100644 --- a/core/dev-packages/pack-n-play/test/test.ts +++ b/core/dev-packages/pack-n-play/test/test.ts @@ -18,7 +18,8 @@ import execa = require('execa'); import {describe, it} from 'mocha'; describe('pack-n-play', () => { - it('should run tests', async () => { + it('should run tests', async function () { + this.timeout(600000); // 10 minutes const fixturesPath = path.resolve('./test/fixtures'); const dirs = fs .readdirSync(fixturesPath) @@ -29,7 +30,7 @@ describe('pack-n-play', () => { stdio: 'inherit', cwd: dir, }; - await execa('npm', ['install'], opts); + await execa('npm', ['install', '--no-audit', '--no-fund'], opts); await execa('npm', ['link', '../../../'], opts); await execa('npm', ['test'], opts); } diff --git a/core/packages/gcp-metadata/src/index.ts b/core/packages/gcp-metadata/src/index.ts index 723fcf1d3f1e..3d98d38f9b03 100644 --- a/core/packages/gcp-metadata/src/index.ts +++ b/core/packages/gcp-metadata/src/index.ts @@ -383,12 +383,22 @@ export async function isAvailable() { if (err.response && err.response.status === 404) { return false; } else { - const codes = - e instanceof Error && e.name === 'AggregateError' - ? (e as any).errors.map((error: any) => - error.code ? error.code.toString() : 'UNKNOWN', - ) - : [err.code ? err.code.toString() : 'UNKNOWN']; + const errObj = e as any; + const getErrorCodes = (err: any): string[] => { + if (!err) return ['UNKNOWN']; + if (err.name === 'AggregateError' && Array.isArray(err.errors)) { + return err.errors.flatMap(getErrorCodes); + } + if (err.code) { + return [err.code.toString()]; + } + if (err.cause) { + return getErrorCodes(err.cause); + } + return ['UNKNOWN']; + }; + + const codes = getErrorCodes(errObj); const isExpected = codes.every((code: string) => [ diff --git a/core/packages/gcp-metadata/test/index.test.ts b/core/packages/gcp-metadata/test/index.test.ts index 83517852b622..f88a018efffa 100644 --- a/core/packages/gcp-metadata/test/index.test.ts +++ b/core/packages/gcp-metadata/test/index.test.ts @@ -493,6 +493,45 @@ describe('unit test', () => { }); }); + it('should fail on isAvailable if ENOTFOUND is wrapped in error.cause', async () => { + const secondary = secondaryHostRequest(500, 'ENOTFOUND'); + const innerErr = Object.assign(new Error('ENOTFOUND'), {code: 'ENOTFOUND'}); + const wrapperErr = Object.assign(new Error('Wrapper error'), { + cause: innerErr, + }); + const primary = nock(HOST) + .get(`${PATH}/${TYPE}`) + .replyWithError(wrapperErr); + const isGCE = await gcp.isAvailable(); + await secondary; + primary.done(); + assert.strictEqual(false, isGCE); + }); + + it('should fail on isAvailable if ENOTFOUND is wrapped inside an AggregateError or nested cause', async () => { + const secondary = secondaryHostRequest(500, 'ENOTFOUND'); + const innerErr1 = Object.assign(new Error('ENOTFOUND'), { + code: 'ENOTFOUND', + }); + const innerErr2 = Object.assign(new Error('EHOSTUNREACH'), { + code: 'EHOSTUNREACH', + }); + const wrapperErr = Object.assign(new Error('Wrapper error'), { + cause: innerErr1, + }); + const aggregateErr = new AggregateError( + [wrapperErr, innerErr2], + 'Aggregate error', + ); + const primary = nock(HOST) + .get(`${PATH}/${TYPE}`) + .replyWithError(aggregateErr); + const isGCE = await gcp.isAvailable(); + await secondary; + primary.done(); + assert.strictEqual(false, isGCE); + }); + it('should return first successful response', async () => { const secondary = secondaryHostRequest(500); const primary = nock(HOST).get(`${PATH}/${TYPE}`).reply(404); diff --git a/core/packages/nodejs-googleapis-common/src/discovery.ts b/core/packages/nodejs-googleapis-common/src/discovery.ts index c00c663bed28..2a665dba0291 100644 --- a/core/packages/nodejs-googleapis-common/src/discovery.ts +++ b/core/packages/nodejs-googleapis-common/src/discovery.ts @@ -13,7 +13,6 @@ import * as fs from 'fs'; import {Gaxios} from 'gaxios'; -import resolve = require('url'); import * as util from 'util'; import {GlobalOptions, ServiceOptions, APIRequestParams} from './api'; @@ -136,8 +135,15 @@ export class Discovery { apiDiscoveryUrl: string | {url?: string}, ): Promise { if (typeof apiDiscoveryUrl === 'string') { - const parts = resolve.parse(apiDiscoveryUrl); - if (apiDiscoveryUrl && !parts.protocol) { + let isUrl = false; + try { + const parsed = new URL(apiDiscoveryUrl); + isUrl = parsed.protocol === 'http:' || parsed.protocol === 'https:'; + } catch (e) { + // Not a valid URL + } + + if (apiDiscoveryUrl && !isUrl) { this.log('Reading from file ' + apiDiscoveryUrl); const file = await readFile(apiDiscoveryUrl, {encoding: 'utf8'}); return this.makeEndpoint(JSON.parse(file));