forked from googleapis/cloud-debug-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-controller.ts
278 lines (262 loc) · 9.85 KB
/
test-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as assert from 'assert';
import * as nock from 'nock';
import {Debug} from '../src/client/stackdriver/debug';
import {Debuggee} from '../src/debuggee';
import * as stackdriver from '../src/types/stackdriver';
import * as r from 'request'; // types only
import {teenyRequest} from 'teeny-request';
// the tests in this file rely on the GCLOUD_PROJECT environment variable
// not being set
delete process.env.GCLOUD_PROJECT;
import {Controller} from '../src/agent/controller';
// TODO: Fix fakeDebug to actually implement Debug.
const fakeDebug = ({
apiEndpoint: `clouddebugger.googleapis.com`,
request: (options: r.Options, cb: r.RequestCallback) => {
teenyRequest(options, (err, r) => {
cb(err, r ? r.body : undefined, r);
});
},
} as {}) as Debug;
const agentVersion = `SomeName/client/SomeVersion`;
const url = 'https://clouddebugger.googleapis.com';
const api = '/v2/controller';
nock.disableNetConnect();
describe('Controller API', () => {
describe('register', () => {
it('should get a debuggeeId', done => {
const scope = nock(url)
.post(api + '/debuggees/register')
.reply(200, {
debuggee: {id: 'fake-debuggee'},
activePeriodSec: 600,
});
const debuggee = new Debuggee({
project: 'fake-project',
uniquifier: 'fake-id',
description: 'unit test',
agentVersion,
});
const controller = new Controller(fakeDebug);
// TODO: Determine if this type signature is correct.
controller.register(debuggee, (err, result) => {
assert(!err, 'not expecting an error');
assert.ok(result);
assert.strictEqual(result!.debuggee.id, 'fake-debuggee');
scope.done();
done();
});
});
it('should not return an error when the debuggee isDisabled', done => {
const scope = nock(url)
.post(api + '/debuggees/register')
.reply(200, {
debuggee: {id: 'fake-debuggee', isDisabled: true},
activePeriodSec: 600,
});
const debuggee = new Debuggee({
project: 'fake-project',
uniquifier: 'fake-id',
description: 'unit test',
agentVersion,
});
const controller = new Controller(fakeDebug);
controller.register(debuggee, (err, result) => {
// TODO: Fix this incorrect method signature.
(assert as {ifError: Function}).ifError(err, 'not expecting an error');
assert.ok(result);
assert.strictEqual(result!.debuggee.id, 'fake-debuggee');
assert.ok(result!.debuggee.isDisabled);
scope.done();
done();
});
});
});
describe('listBreakpoints', () => {
// register before each test
before(done => {
nock(url)
.post(api + '/debuggees/register')
.reply(200, {
debuggee: {id: 'fake-debuggee'},
activePeriodSec: 600,
});
const debuggee = new Debuggee({
project: 'fake-project',
uniquifier: 'fake-id',
description: 'unit test',
agentVersion,
});
const controller = new Controller(fakeDebug);
controller.register(debuggee, (err /*, result*/) => {
assert.ifError(err);
done();
});
});
it('should deal with a missing breakpoints response', done => {
const scope = nock(url)
.get(api + '/debuggees/fake-debuggee/breakpoints?successOnTimeout=true')
.reply(200, {kind: 'whatever'});
const debuggee = {id: 'fake-debuggee'};
const controller = new Controller(fakeDebug);
// TODO: Fix debuggee to actually implement Debuggee
// TODO: Determine if the response parameter should be used.
controller.listBreakpoints(
debuggee as Debuggee,
(err, response, result?: stackdriver.ListBreakpointsResponse) => {
assert(!err, 'not expecting an error');
// TODO: Handle the case where result is undefined
assert(
!(result as {breakpoints: {}}).breakpoints,
'should not have a breakpoints property'
);
scope.done();
done();
}
);
});
describe('invalid responses', () => {
const tests: string | Array<{}> = ['', 'JSON, this is not', []];
tests.forEach((invalidResponse, index) => {
it('should pass test ' + index, done => {
const scope = nock(url)
.get(
api + '/debuggees/fake-debuggee/breakpoints?successOnTimeout=true'
)
.reply(200, invalidResponse);
const debuggee = {id: 'fake-debuggee'};
const controller = new Controller(fakeDebug);
// TODO: Fix debuggee to actually implement Debuggee
// TODO: Determine if the response parameter should be used.
controller.listBreakpoints(
debuggee as Debuggee,
(err, response, result?: stackdriver.ListBreakpointsResponse) => {
assert(!err, 'not expecting an error');
// TODO: Handle the case where result is undefined
assert(
!(result as {breakpoints: {}}).breakpoints,
'should not have breakpoints property'
);
scope.done();
done();
}
);
});
});
});
it('should throw error on http errors', done => {
const scope = nock(url)
.get(api + '/debuggees/fake-debuggee/breakpoints?successOnTimeout=true')
.reply(403);
// TODO: Fix debuggee to actually implement Debuggee
const debuggee: Debuggee = {id: 'fake-debuggee'} as Debuggee;
const controller = new Controller(fakeDebug);
// TODO: Determine if the response parameter should be used.
controller.listBreakpoints(debuggee, (err, response, result) => {
assert(err instanceof Error, 'expecting an error');
assert(!result, 'should not have a result');
scope.done();
done();
});
});
it('should work with waitTokens', done => {
const scope = nock(url)
.get(api + '/debuggees/fake-debuggee/breakpoints?successOnTimeout=true')
.reply(200, {waitExpired: true});
// TODO: Fix debuggee to actually implement Debuggee
const debuggee: Debuggee = {id: 'fake-debuggee'} as Debuggee;
const controller = new Controller(fakeDebug);
// TODO: Determine if the result parameter should be used.
controller.listBreakpoints(debuggee, (err, response, result) => {
// TODO: Fix this incorrect method signature.
(assert as {ifError: Function}).ifError(err, 'not expecting an error');
// TODO: Fix this error that states `body` is not a property
// of `ServerResponse`.
assert(
((response as {}) as {body: {waitExpired: {}}}).body.waitExpired,
'should have expired set'
);
scope.done();
done();
});
});
// TODO: Fix this so that each element of the array is actually an
// array of Breakpoints.
const testsBreakpoints: stackdriver.Breakpoint[][] = [
[],
[{id: 'breakpoint-0', location: {path: 'foo.js', line: 18}}],
] as stackdriver.Breakpoint[][];
testsBreakpoints.forEach(
(breakpoints: stackdriver.Breakpoint[], index: number) => {
it('should pass test ' + index, done => {
const scope = nock(url)
.get(
api + '/debuggees/fake-debuggee/breakpoints?successOnTimeout=true'
)
.reply(200, {breakpoints});
// TODO: Fix debuggee to actually implement Debuggee
const debuggee: Debuggee = {id: 'fake-debuggee'} as Debuggee;
const controller = new Controller(fakeDebug);
// TODO: Determine if the response parameter should be used.
controller.listBreakpoints(debuggee, (err, response, result) => {
assert(!err, 'not expecting an error');
assert.ok(result);
assert(result!.breakpoints, 'should have a breakpoints property');
const bps = result!.breakpoints;
assert.deepStrictEqual(bps, breakpoints, 'breakpoints mismatch');
scope.done();
done();
});
});
}
);
});
describe('updateBreakpoint', () => {
it('should PUT to server when a breakpoint is updated', done => {
// TODO: Fix breakpoint to actually Breakpoint
const breakpoint: stackdriver.Breakpoint = {
id: 'breakpoint-0',
location: {path: 'foo.js', line: 99},
} as stackdriver.Breakpoint;
const scope = nock(url)
.put(api + '/debuggees/fake-debuggee/breakpoints/breakpoint-0', {
debuggeeId: 'fake-debuggee',
breakpoint,
})
.reply(200, {
kind: 'debugletcontroller#updateActiveBreakpointResponse',
});
// TODO: Fix debuggee to actually implement Debuggee
const debuggee: Debuggee = {id: 'fake-debuggee'} as Debuggee;
const controller = new Controller(fakeDebug);
controller.updateBreakpoint(
debuggee as Debuggee,
breakpoint,
(err, result) => {
assert(!err, 'not expecting an error');
assert.strictEqual(
(result as {kind: {}}).kind,
'debugletcontroller#updateActiveBreakpointResponse'
);
scope.done();
done();
}
);
});
});
});