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
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
5 changes: 3 additions & 2 deletions core/dev-packages/pack-n-play/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
22 changes: 16 additions & 6 deletions core/packages/gcp-metadata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
westarle marked this conversation as resolved.

const isExpected = codes.every((code: string) =>
[
Expand Down
39 changes: 39 additions & 0 deletions core/packages/gcp-metadata/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions core/packages/nodejs-googleapis-common/src/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -136,8 +135,15 @@ export class Discovery {
apiDiscoveryUrl: string | {url?: string},
): Promise<EndpointCreator> {
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));
Expand Down
Loading