Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Update gcp-metadata to the latest version 🚀 (#395)
Browse files Browse the repository at this point in the history
* fix(package): update gcp-metadata to version 0.5.0

gcp-metadata@0.5.0 is a semver major release. As it uses promises now,
we can get rid of our promise wrappers. Simplify the tests – we don't
need to test functionality provided by gcp-metadata.
  • Loading branch information
greenkeeper[bot] authored and ofrobots committed Feb 8, 2018
1 parent 9c93e7f commit e19e514
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 177 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"coffee-script": "^1.9.3",
"extend": "^3.0.1",
"findit2": "^2.2.3",
"gcp-metadata": "^0.4.0",
"gcp-metadata": "^0.5.0",
"lodash": "^4.12.0",
"proxyquire": "^1.8.0",
"semver": "^5.1.0",
Expand Down
30 changes: 6 additions & 24 deletions src/agent/debuglet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ const common: Common = require('@google-cloud/common');
import * as crypto from 'crypto';
import {EventEmitter} from 'events';
import * as extend from 'extend';
import * as dns from 'dns';
import * as fs from 'fs';

import * as metadata from 'gcp-metadata';
import * as request from 'request';

import * as _ from 'lodash';
import * as path from 'path';
Expand Down Expand Up @@ -514,32 +512,16 @@ export class Debuglet extends EventEmitter {
return project;
}

static async runningOnGCP(): Promise<boolean> {
const lookup = promisify(dns.lookup);
try {
await lookup('metadata.google.internal.');
return true;
} catch (err) {
// Take failure to resolve metadata service to indicate that we are not
// running on GCP.
return false;
}
static runningOnGCP(): Promise<boolean> {
return metadata.isAvailable();
}

static getProjectIdFromMetadata() {
return new Promise<string>((resolve, reject) => {
metadata.project('project-id', (err, res, projectId) => {
err ? reject(err) : resolve(projectId);
});
});
static async getProjectIdFromMetadata() : Promise<string> {
return (await metadata.project('project-id')).data as string;
}

static getClusterNameFromMetadata() {
return new Promise<string>((resolve, reject) => {
metadata.instance('attributes/cluster-name', (err, res, clusterName) => {
err ? reject(err) : resolve(clusterName);
});
});
static async getClusterNameFromMetadata() : Promise<string> {
return (await metadata.instance('attributes/cluster-name')).data as string;
}

getSourceContext_(
Expand Down
152 changes: 0 additions & 152 deletions test/test-debuglet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,158 +119,6 @@ describe('Debuglet', () => {
});
});

describe('runningOnGCP', () => {
// TODO: Make this more precise.
let savedLookup: Function;
before(() => {
savedLookup = dns.lookup;
});

after(() => {
// TODO: Fix this cast to any that is caused by the fact that `lookup`
// is a readonly property.
(dns as {lookup: {}}).lookup = savedLookup;
});

it('should resolve true if metadata service is resolveable', (done) => {
// TODO: Fix this cast to any that is caused by the fact that `lookup`
// is a readonly property.
// TODO: Determine if the hostname parameter should be used.
(dns as {lookup: {}}).lookup =
(hostname: string|null,
cb: (err: Error|null, param: {address: string, family: string}) =>
void) => {
setImmediate(() => {
cb(null, {address: '700.800.900.fake', family: 'Addams'});
});
};

Debuglet.runningOnGCP().then((onGCP) => {
assert.strictEqual(onGCP, true);
done();
});
});

it('should resolve false if metadata service not resolveable', (done) => {
// TODO: Fix this cast to any that is caused by the fact that `lookup`
// is a readonly property.
// TODO: Determine if the hostname parameter should be used.
// TODO: Determine if these types are correct
(dns as {lookup: {}}).lookup =
(hostname: string, cb: (err: Error) => void) => {
setImmediate(() => {
cb(new Error('resolution error'));
});
};

Debuglet.runningOnGCP().then((onGCP) => {
assert.strictEqual(onGCP, false);
done();
});
});
});

describe('getProjectIdFromMetadata', () => {
let savedProject: Function;
before(() => {
savedProject = metadata.project;
});
after(() => {
metadata.project = savedProject;
});

it('should return project retrived from metadata', (done) => {
const FAKE_PROJECT_ID = 'fake-project-id-from-metadata';
// TODO: Determine if the options to Debug should be optional so that
// new Debug() can be used instead of new Debug({}).
// TODO: This is never used. Determine if it should be used.
// const debug = new Debug({});
// TODO: This is never used. Determine if it should be used.
// const debuglet = new Debuglet(debug, defaultConfig);

// TODO: Determine if the path parameter should be used.
// TODO: Determine if these types are correct
metadata.project = (instancePath: string, cb: MetadataCallback) => {
setImmediate(() => {
cb(null, {}, FAKE_PROJECT_ID);
});
};

Debuglet.getProjectIdFromMetadata().then((projectId) => {
assert.strictEqual(projectId, FAKE_PROJECT_ID);
done();
});
});

it('should return null on error', (done) => {
// TODO: This is never used. Determine if it should be used.
// const debug = new Debug({});
// TODO: This is never used. Determine if it should be used.
// const debuglet = new Debuglet(debug, defaultConfig);

// TODO: Determine if the path parameter should be used.
metadata.project = (instancePath: string, cb: MetadataCallback) => {
setImmediate(() => {
cb(new Error());
});
};

// TODO: Determine if the err parameter should be used.
Debuglet.getProjectIdFromMetadata().catch((err) => {
done();
});
});
});

describe('getClusterNameFromMetadata', () => {
let savedInstance: Function;
before(() => {
savedInstance = metadata.instance;
});
after(() => {
metadata.instance = savedInstance;
});

it('should return project retrived from metadata', (done) => {
const FAKE_CLUSTER_NAME = 'fake-cluster-name-from-metadata';
// TODO: This is never used. Determine if it should be used.
// const debug = new Debug({});
// TODO: This is never used. Determine if it should be used.
// const debuglet = new Debuglet(debug, defaultConfig);

// TODO: Determine if the path parameter should be used.
metadata.instance = (instancePath: string, cb: MetadataCallback) => {
setImmediate(() => {
cb(null, {}, FAKE_CLUSTER_NAME);
});
};

Debuglet.getClusterNameFromMetadata().then((clusterName) => {
assert.strictEqual(clusterName, FAKE_CLUSTER_NAME);
done();
});
});

it('should return null on error', (done) => {
// TODO: This is never used. Determine if it should be used.
// const debug = new Debug({});
// TODO: This is never used. Determine if it should be used.
// const debuglet = new Debuglet(debug, defaultConfig);

// TODO: Determine if the path parameter should be used.
metadata.instance = (instancePath: string, cb: MetadataCallback) => {
setImmediate(() => {
cb(new Error());
});
};

// TODO: Determine if the err parameter should be used.
Debuglet.getClusterNameFromMetadata().catch((err) => {
done();
});
});
});

describe('getProjectId', () => {
let savedGetProjectIdFromMetadata: () => Promise<string>;

Expand Down

0 comments on commit e19e514

Please sign in to comment.