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

Commit a95b308

Browse files
author
Matt Loring
committed
Test for source context
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.
1 parent 4de169b commit a95b308

File tree

3 files changed

+76
-80
lines changed

3 files changed

+76
-80
lines changed

lib/debugletapi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ DebugletApi.prototype.init = function(uid, callback) {
7272
}
7373
that.projectNumber_ = projectNumber;
7474

75-
fs.readFile('source-context.json', 'utf8', function(err, data) {
75+
fs.readFile('source-contexts.json', 'utf8', function(err, data) {
7676
try {
7777
that.sourceContext_ = JSON.parse(data);
7878
} catch (e) {

test/fixtures/source-contexts.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"a": 5
3+
}

test/standalone/test-debuglet.js

Lines changed: 72 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,42 @@ var logger = require('@google/cloud-diagnostics-common').logger;
2121
var config = require('../../config.js');
2222
var Debuglet = require('../../lib/debuglet.js');
2323

24+
var DEBUGGEE_ID = 'bar';
25+
var API = 'https://clouddebugger.googleapis.com';
26+
var REGISTER_PATH = '/v2/controller/debuggees/register';
27+
var BPS_PATH = '/v2/controller/debuggees/' + DEBUGGEE_ID + '/breakpoints';
28+
2429
var nock = require('nock');
2530
nock.disableNetConnect();
2631

32+
var debuglet;
33+
var bp = {
34+
id: 'test',
35+
location: { path: 'fixtures/foo.js', line: 2 }
36+
};
37+
2738
describe(__filename, function(){
28-
it('should not start unless we know the project num', function(done) {
29-
var debuglet = new Debuglet(
39+
beforeEach(function() {
40+
process.env.GCLOUD_PROJECT_NUM = 0;
41+
debuglet = new Debuglet(
3042
config, logger.create(config.logLevel, '@google/cloud-debug'));
43+
debuglet.once('started', function() {
44+
debuglet.debugletApi_.request_ = request; // Avoid authing.
45+
});
46+
});
47+
48+
afterEach(function() {
49+
debuglet.stop();
50+
});
3151

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

3758
debuglet.once('error', function(err) {
3859
assert(err);
39-
debuglet.stop();
4060
scope.done();
4161
done();
4262
});
@@ -47,15 +67,11 @@ describe(__filename, function(){
4767
});
4868

4969
it('should complain if GCLOUD_PROJECT_NUM is not numeric', function(done) {
50-
var debuglet = new Debuglet(
51-
config, logger.create(config.logLevel, '@google/cloud-debug'));
52-
5370
process.env.GCLOUD_PROJECT_NUM='11020304f2934';
5471

5572
debuglet.once('error', function(err) {
5673
assert(err);
5774
assert(err.message.indexOf('should be numeric') !== -1);
58-
debuglet.stop();
5975
done();
6076
});
6177
debuglet.once('started', function() {
@@ -67,28 +83,41 @@ describe(__filename, function(){
6783
it('should error if a package.json doesn\'t exist');
6884

6985
it('should register successfully otherwise', function(done) {
70-
var debuglet = new Debuglet(
71-
config, logger.create(config.logLevel, '@google/cloud-debug'));
86+
var scope = nock(API)
87+
.post(REGISTER_PATH)
88+
.reply(200, {
89+
debuggee: {
90+
id: DEBUGGEE_ID
91+
}
92+
});
93+
94+
debuglet.once('registered', function(id) {
95+
assert(id === DEBUGGEE_ID);
96+
scope.done();
97+
done();
98+
});
99+
100+
debuglet.start();
101+
});
72102

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

75-
var API = 'https://clouddebugger.googleapis.com';
76-
var PATH = '/v2/controller/debuggees/register';
77106
var scope = nock(API)
78-
.post(PATH)
107+
.post(REGISTER_PATH, function(body) {
108+
return body.debuggee.sourceContexts[0] &&
109+
body.debuggee.sourceContexts[0].a === 5;
110+
})
79111
.reply(200, {
80112
debuggee: {
81-
id: 'foo'
113+
id: DEBUGGEE_ID
82114
}
83115
});
84116

85-
debuglet.once('started', function() {
86-
debuglet.debugletApi_.request_ = request; // Avoid authing.
87-
});
88117
debuglet.once('registered', function(id) {
89-
assert(id === 'foo');
90-
debuglet.stop();
118+
assert(id === DEBUGGEE_ID);
91119
scope.done();
120+
process.chdir('../..');
92121
done();
93122
});
94123

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

104133
it('should re-fetch breakpoints on error', function(done) {
105134
this.timeout(6000);
106-
var debuglet = new Debuglet(
107-
config, logger.create(config.logLevel, '@google/cloud-debug'));
108-
109-
process.env.GCLOUD_PROJECT_NUM=0;
110-
111-
var API = 'https://clouddebugger.googleapis.com';
112135

113136
var scope = nock(API)
114-
.post('/v2/controller/debuggees/register')
137+
.post(REGISTER_PATH)
115138
.reply(200, {
116139
debuggee: {
117-
id: 'bar'
140+
id: DEBUGGEE_ID
118141
}
119142
})
120-
.post('/v2/controller/debuggees/register')
143+
.post(REGISTER_PATH)
121144
.reply(200, {
122145
debuggee: {
123-
id: 'bar'
146+
id: DEBUGGEE_ID
124147
}
125148
})
126-
.get('/v2/controller/debuggees/bar/breakpoints')
149+
.get(BPS_PATH)
127150
.reply(404)
128-
.get('/v2/controller/debuggees/bar/breakpoints')
151+
.get(BPS_PATH)
129152
.reply(409)
130-
.get('/v2/controller/debuggees/bar/breakpoints')
153+
.get(BPS_PATH)
131154
.reply(200, {
132-
breakpoints: [{
133-
id: 'test',
134-
location: { path: 'fixtures/foo.js', line: 2 }
135-
}]
155+
breakpoints: [bp]
136156
});
137157

138-
debuglet.once('started', function() {
139-
debuglet.debugletApi_.request_ = request; // Avoid authing.
140-
});
141158
debuglet.once('registered', function reg(id) {
142-
assert(id === 'bar');
159+
assert(id === DEBUGGEE_ID);
143160
setTimeout(function() {
144-
assert.deepEqual(debuglet.activeBreakpointMap_.test, {
145-
id: 'test',
146-
location: { path: 'fixtures/foo.js', line: 2 }
147-
});
148-
debuglet.stop();
161+
assert.deepEqual(debuglet.activeBreakpointMap_.test, bp);
149162
scope.done();
150163
done();
151164
}, 1000);
@@ -157,44 +170,33 @@ describe(__filename, function(){
157170
it('should add a breakpoint');
158171

159172
it('should report error on breakpoint set', function(done) {
160-
var debuglet = new Debuglet(
161-
config, logger.create(config.logLevel, '@google/cloud-debug'));
162-
163-
process.env.GCLOUD_PROJECT_NUM=0;
164-
165-
var bp = {
173+
var errorBp = {
166174
id: 'test',
167175
location: { path: 'fixtures/foo', line: 2 }
168176
};
169177

170-
var API = 'https://clouddebugger.googleapis.com';
171-
172178
var scope = nock(API)
173-
.post('/v2/controller/debuggees/register')
179+
.post(REGISTER_PATH)
174180
.reply(200, {
175181
debuggee: {
176-
id: 'bar'
182+
id: DEBUGGEE_ID
177183
}
178184
})
179-
.get('/v2/controller/debuggees/bar/breakpoints')
185+
.get(BPS_PATH)
180186
.reply(200, {
181-
breakpoints: [bp]
187+
breakpoints: [errorBp]
182188
})
183-
.put('/v2/controller/debuggees/bar/breakpoints/test', function(body) {
189+
.put(BPS_PATH + '/test', function(body) {
184190
var status = body.breakpoint.status;
185191
return status.isError &&
186192
status.description.format.indexOf('Only files with .js extensions') !== -1;
187193
})
188194
.reply(200);
189195

190-
debuglet.once('started', function() {
191-
debuglet.debugletApi_.request_ = request; // Avoid authing.
192-
});
193196
debuglet.once('registered', function(id) {
194-
assert(id === 'bar');
197+
assert(id === DEBUGGEE_ID);
195198
setTimeout(function() {
196199
assert(!debuglet.activeBreakpointMap_.test);
197-
debuglet.stop();
198200
scope.done();
199201
done();
200202
}, 200);
@@ -210,41 +212,32 @@ describe(__filename, function(){
210212
var debuglet = new Debuglet(
211213
config, logger.create(config.logLevel, '@google/cloud-debug'));
212214

213-
process.env.GCLOUD_PROJECT_NUM=0;
214-
215-
var bp = {
216-
id: 'test',
217-
location: { path: 'fixtures/foo.js', line: 2 }
218-
};
219-
220-
var API = 'https://clouddebugger.googleapis.com';
221-
222215
var scope = nock(API)
223-
.post('/v2/controller/debuggees/register')
216+
.post(REGISTER_PATH)
224217
.reply(200, {
225218
debuggee: {
226-
id: 'bar'
219+
id: DEBUGGEE_ID
227220
}
228221
})
229-
.get('/v2/controller/debuggees/bar/breakpoints')
222+
.get(BPS_PATH)
230223
.reply(200, {
231224
breakpoints: [bp]
232225
})
233-
.put('/v2/controller/debuggees/bar/breakpoints/test', function(body) {
234-
return body.breakpoint.status.description.format === 'The snapshot has expired';
226+
.put(BPS_PATH + '/test', function(body) {
227+
return body.breakpoint.status.description.format ===
228+
'The snapshot has expired';
235229
})
236230
.reply(200);
237231

238232
debuglet.once('started', function() {
239233
debuglet.debugletApi_.request_ = request; // Avoid authing.
240234
});
241235
debuglet.once('registered', function(id) {
242-
assert(id === 'bar');
236+
assert(id === DEBUGGEE_ID);
243237
setTimeout(function() {
244238
assert.deepEqual(debuglet.activeBreakpointMap_.test, bp);
245239
setTimeout(function() {
246240
assert(!debuglet.activeBreakpointMap_.test);
247-
debuglet.stop();
248241
scope.done();
249242
config.breakpointExpirationSec = oldTimeout;
250243
done();

0 commit comments

Comments
 (0)