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

Commit

Permalink
Test for source context
Browse files Browse the repository at this point in the history
This ensures that the source context read from the source-contexts.json
file in the working directory is passed to the api when the debuggee
registers.

Also look for a file named source-contexts.json instead of
source-context.json since the gcloud tool now generates this file name
by default and deprecated the ability to use other names.

Fixes #15.
  • Loading branch information
Matt Loring committed Jan 13, 2016
1 parent 4de169b commit a95b308
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 80 deletions.
2 changes: 1 addition & 1 deletion lib/debugletapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ DebugletApi.prototype.init = function(uid, callback) {
}
that.projectNumber_ = projectNumber;

fs.readFile('source-context.json', 'utf8', function(err, data) {
fs.readFile('source-contexts.json', 'utf8', function(err, data) {
try {
that.sourceContext_ = JSON.parse(data);
} catch (e) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/source-contexts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": 5
}
151 changes: 72 additions & 79 deletions test/standalone/test-debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,42 @@ var logger = require('@google/cloud-diagnostics-common').logger;
var config = require('../../config.js');
var Debuglet = require('../../lib/debuglet.js');

var DEBUGGEE_ID = 'bar';
var API = 'https://clouddebugger.googleapis.com';
var REGISTER_PATH = '/v2/controller/debuggees/register';
var BPS_PATH = '/v2/controller/debuggees/' + DEBUGGEE_ID + '/breakpoints';

var nock = require('nock');
nock.disableNetConnect();

var debuglet;
var bp = {
id: 'test',
location: { path: 'fixtures/foo.js', line: 2 }
};

describe(__filename, function(){
it('should not start unless we know the project num', function(done) {
var debuglet = new Debuglet(
beforeEach(function() {
process.env.GCLOUD_PROJECT_NUM = 0;
debuglet = new Debuglet(
config, logger.create(config.logLevel, '@google/cloud-debug'));
debuglet.once('started', function() {
debuglet.debugletApi_.request_ = request; // Avoid authing.
});
});

afterEach(function() {
debuglet.stop();
});

it('should not start unless we know the project num', function(done) {
delete process.env.GCLOUD_PROJECT_NUM;
var scope = nock('http://metadata.google.internal')
.get('/computeMetadata/v1/project/numeric-project-id')
.reply(404);

debuglet.once('error', function(err) {
assert(err);
debuglet.stop();
scope.done();
done();
});
Expand All @@ -47,15 +67,11 @@ describe(__filename, function(){
});

it('should complain if GCLOUD_PROJECT_NUM is not numeric', function(done) {
var debuglet = new Debuglet(
config, logger.create(config.logLevel, '@google/cloud-debug'));

process.env.GCLOUD_PROJECT_NUM='11020304f2934';

debuglet.once('error', function(err) {
assert(err);
assert(err.message.indexOf('should be numeric') !== -1);
debuglet.stop();
done();
});
debuglet.once('started', function() {
Expand All @@ -67,28 +83,41 @@ describe(__filename, function(){
it('should error if a package.json doesn\'t exist');

it('should register successfully otherwise', function(done) {
var debuglet = new Debuglet(
config, logger.create(config.logLevel, '@google/cloud-debug'));
var scope = nock(API)
.post(REGISTER_PATH)
.reply(200, {
debuggee: {
id: DEBUGGEE_ID
}
});

debuglet.once('registered', function(id) {
assert(id === DEBUGGEE_ID);
scope.done();
done();
});

debuglet.start();
});

process.env.GCLOUD_PROJECT_NUM=0;
it('should pass source context to api if present', function(done) {
process.chdir('test/fixtures');

var API = 'https://clouddebugger.googleapis.com';
var PATH = '/v2/controller/debuggees/register';
var scope = nock(API)
.post(PATH)
.post(REGISTER_PATH, function(body) {
return body.debuggee.sourceContexts[0] &&
body.debuggee.sourceContexts[0].a === 5;
})
.reply(200, {
debuggee: {
id: 'foo'
id: DEBUGGEE_ID
}
});

debuglet.once('started', function() {
debuglet.debugletApi_.request_ = request; // Avoid authing.
});
debuglet.once('registered', function(id) {
assert(id === 'foo');
debuglet.stop();
assert(id === DEBUGGEE_ID);
scope.done();
process.chdir('../..');
done();
});

Expand All @@ -103,49 +132,33 @@ describe(__filename, function(){

it('should re-fetch breakpoints on error', function(done) {
this.timeout(6000);
var debuglet = new Debuglet(
config, logger.create(config.logLevel, '@google/cloud-debug'));

process.env.GCLOUD_PROJECT_NUM=0;

var API = 'https://clouddebugger.googleapis.com';

var scope = nock(API)
.post('/v2/controller/debuggees/register')
.post(REGISTER_PATH)
.reply(200, {
debuggee: {
id: 'bar'
id: DEBUGGEE_ID
}
})
.post('/v2/controller/debuggees/register')
.post(REGISTER_PATH)
.reply(200, {
debuggee: {
id: 'bar'
id: DEBUGGEE_ID
}
})
.get('/v2/controller/debuggees/bar/breakpoints')
.get(BPS_PATH)
.reply(404)
.get('/v2/controller/debuggees/bar/breakpoints')
.get(BPS_PATH)
.reply(409)
.get('/v2/controller/debuggees/bar/breakpoints')
.get(BPS_PATH)
.reply(200, {
breakpoints: [{
id: 'test',
location: { path: 'fixtures/foo.js', line: 2 }
}]
breakpoints: [bp]
});

debuglet.once('started', function() {
debuglet.debugletApi_.request_ = request; // Avoid authing.
});
debuglet.once('registered', function reg(id) {
assert(id === 'bar');
assert(id === DEBUGGEE_ID);
setTimeout(function() {
assert.deepEqual(debuglet.activeBreakpointMap_.test, {
id: 'test',
location: { path: 'fixtures/foo.js', line: 2 }
});
debuglet.stop();
assert.deepEqual(debuglet.activeBreakpointMap_.test, bp);
scope.done();
done();
}, 1000);
Expand All @@ -157,44 +170,33 @@ describe(__filename, function(){
it('should add a breakpoint');

it('should report error on breakpoint set', function(done) {
var debuglet = new Debuglet(
config, logger.create(config.logLevel, '@google/cloud-debug'));

process.env.GCLOUD_PROJECT_NUM=0;

var bp = {
var errorBp = {
id: 'test',
location: { path: 'fixtures/foo', line: 2 }
};

var API = 'https://clouddebugger.googleapis.com';

var scope = nock(API)
.post('/v2/controller/debuggees/register')
.post(REGISTER_PATH)
.reply(200, {
debuggee: {
id: 'bar'
id: DEBUGGEE_ID
}
})
.get('/v2/controller/debuggees/bar/breakpoints')
.get(BPS_PATH)
.reply(200, {
breakpoints: [bp]
breakpoints: [errorBp]
})
.put('/v2/controller/debuggees/bar/breakpoints/test', function(body) {
.put(BPS_PATH + '/test', function(body) {
var status = body.breakpoint.status;
return status.isError &&
status.description.format.indexOf('Only files with .js extensions') !== -1;
})
.reply(200);

debuglet.once('started', function() {
debuglet.debugletApi_.request_ = request; // Avoid authing.
});
debuglet.once('registered', function(id) {
assert(id === 'bar');
assert(id === DEBUGGEE_ID);
setTimeout(function() {
assert(!debuglet.activeBreakpointMap_.test);
debuglet.stop();
scope.done();
done();
}, 200);
Expand All @@ -210,41 +212,32 @@ describe(__filename, function(){
var debuglet = new Debuglet(
config, logger.create(config.logLevel, '@google/cloud-debug'));

process.env.GCLOUD_PROJECT_NUM=0;

var bp = {
id: 'test',
location: { path: 'fixtures/foo.js', line: 2 }
};

var API = 'https://clouddebugger.googleapis.com';

var scope = nock(API)
.post('/v2/controller/debuggees/register')
.post(REGISTER_PATH)
.reply(200, {
debuggee: {
id: 'bar'
id: DEBUGGEE_ID
}
})
.get('/v2/controller/debuggees/bar/breakpoints')
.get(BPS_PATH)
.reply(200, {
breakpoints: [bp]
})
.put('/v2/controller/debuggees/bar/breakpoints/test', function(body) {
return body.breakpoint.status.description.format === 'The snapshot has expired';
.put(BPS_PATH + '/test', function(body) {
return body.breakpoint.status.description.format ===
'The snapshot has expired';
})
.reply(200);

debuglet.once('started', function() {
debuglet.debugletApi_.request_ = request; // Avoid authing.
});
debuglet.once('registered', function(id) {
assert(id === 'bar');
assert(id === DEBUGGEE_ID);
setTimeout(function() {
assert.deepEqual(debuglet.activeBreakpointMap_.test, bp);
setTimeout(function() {
assert(!debuglet.activeBreakpointMap_.test);
debuglet.stop();
scope.done();
config.breakpointExpirationSec = oldTimeout;
done();
Expand Down

0 comments on commit a95b308

Please sign in to comment.