1 | var assert = require('assert');
|
2 | var os = require('os');
|
3 | var request = require('supertest');
|
4 | var zetta = require('../');
|
5 | var Query = require('calypso').Query;
|
6 | var rels = require('zetta-rels');
|
7 | var Scout = require('./fixture/example_scout');
|
8 | var Driver = require('./fixture/example_driver');
|
9 | var HttpDriver = require('./fixture/example_http_driver');
|
10 | var Registry = require('./fixture/mem_registry');
|
11 | var PeerRegistry = require('./fixture/mem_peer_registry');
|
12 | var zettacluster = require('zetta-cluster');
|
13 |
|
14 | function getHttpServer(app) {
|
15 | return app.httpServer.server;
|
16 | }
|
17 |
|
18 | function getBody(fn) {
|
19 | return function(res) {
|
20 | try {
|
21 | if(res.text) {
|
22 | var body = JSON.parse(res.text);
|
23 | } else {
|
24 | var body = '';
|
25 | }
|
26 | } catch(err) {
|
27 | throw new Error('Failed to parse json body');
|
28 | }
|
29 |
|
30 | fn(res, body);
|
31 | }
|
32 | }
|
33 |
|
34 | function checkDeviceOnRootUri(entity) {
|
35 | assert.deepEqual(entity.class, ['device']);
|
36 | assert.deepEqual(entity.rel, ["http://rels.zettajs.io/device"]);
|
37 |
|
38 | assert(entity.properties.name);
|
39 | assert(entity.properties.type);
|
40 | assert(entity.properties.state);
|
41 | assert(!entity.actions);
|
42 |
|
43 | assert(entity.links);
|
44 | hasLinkRel(entity.links, rels.self);
|
45 | hasLinkRel(entity.links, rels.server);
|
46 | }
|
47 |
|
48 | function hasLinkRel(links, rel, title, href) {
|
49 | var found = false;
|
50 |
|
51 | links.forEach(function(link) {
|
52 | if(link.rel.indexOf(rel) != -1) {
|
53 | found = true;
|
54 |
|
55 | if(title !== undefined && link.title !== title) {
|
56 | throw new Error('link title does not match');
|
57 | }
|
58 |
|
59 | if(href !== undefined && link.href !== href) {
|
60 | throw new Error('link href does not match');
|
61 | }
|
62 | }
|
63 | });
|
64 |
|
65 | if(!found) {
|
66 | throw new Error('Link rel:'+rel+' not found in links');
|
67 | }
|
68 | }
|
69 |
|
70 |
|
71 | describe('Zetta Query Api', function() {
|
72 | var reg = null;
|
73 | var peerRegistry = null;
|
74 |
|
75 | beforeEach(function() {
|
76 | reg = new Registry();
|
77 | peerRegistry = new PeerRegistry();
|
78 | });
|
79 |
|
80 | describe('invalid query', function() {
|
81 | var app = null;
|
82 |
|
83 | beforeEach(function() {
|
84 | app = zetta({ registry: reg, peerRegistry: peerRegistry })
|
85 | .silent()
|
86 | .use(Scout)
|
87 | .name('local')
|
88 | .expose('*')
|
89 | ._run();
|
90 | });
|
91 |
|
92 | it('returns an error on /', function(done) {
|
93 | request(getHttpServer(app))
|
94 | .get('/?ql=where%20')
|
95 | .expect(getBody(function(res, body){
|
96 | assert.deepEqual(body.class, ['query-error']);
|
97 | }))
|
98 | .end(done);
|
99 | });
|
100 |
|
101 | it('returns an error on / when querying across servers', function(done) {
|
102 | request(getHttpServer(app))
|
103 | .get('/?server=*&ql=where%20')
|
104 | .expect(getBody(function(res, body){
|
105 | assert.deepEqual(body.class, ['query-error']);
|
106 | }))
|
107 | .end(done);
|
108 | });
|
109 |
|
110 | it('returns an error on /servers/<id>', function(done) {
|
111 | request(getHttpServer(app))
|
112 | .get('/servers/local?ql=where%20')
|
113 | .expect(getBody(function(res, body){
|
114 | assert.deepEqual(body.class, ['query-error']);
|
115 | }))
|
116 | .end(done);
|
117 | });
|
118 | });
|
119 |
|
120 | describe('queries on / with just a ql parameter', function() {
|
121 | var app = null;
|
122 |
|
123 | beforeEach(function() {
|
124 | app = zetta({ registry: reg, peerRegistry: peerRegistry })
|
125 | .silent()
|
126 | .use(Scout)
|
127 | .name('local')
|
128 | .expose('*')
|
129 | ._run();
|
130 | });
|
131 |
|
132 | it('should have two classes', function(done) {
|
133 | request(getHttpServer(app))
|
134 | .get('/?ql=where%20type%20=%20"testdriver"')
|
135 | .expect(getBody(function(res, body){
|
136 | assert.deepEqual(body.class, ['server', 'search-results']);
|
137 | }))
|
138 | .end(done);
|
139 | });
|
140 |
|
141 | it('should have two properties: server name and ql', function(done) {
|
142 | request(getHttpServer(app))
|
143 | .get('/?ql=where%20type%20=%20"testdriver"')
|
144 | .expect(getBody(function(res, body) {
|
145 | assert.equal(body.properties.name, 'local');
|
146 | assert.equal(body.properties.ql, 'where type = "testdriver"');
|
147 | }))
|
148 | .end(done);
|
149 | });
|
150 |
|
151 | it('should have one action.', function(done) {
|
152 | request(getHttpServer(app))
|
153 | .get('/?ql=where%20type%20=%20"testdriver"')
|
154 | .expect(getBody(function(res, body) {
|
155 | assert.equal(body.actions.length, 1);
|
156 | assert.equal(body.actions[0].name, 'query-devices');
|
157 | }))
|
158 | .end(done);
|
159 | });
|
160 |
|
161 | it('should have a websocket link to monitor the query.', function(done) {
|
162 | request(getHttpServer(app))
|
163 | .get('/?ql=where%20type%20=%20"testdriver"')
|
164 | .expect(getBody(function(res, body) {
|
165 | assert.equal(body.links.length, 4);
|
166 | hasLinkRel(body.links, 'http://rels.zettajs.io/query');
|
167 | assert.notEqual(body.links[3].href.indexOf("topic=query%2Fwhere%20type%20%3D%20%22testdriver%22"), -1);
|
168 | }))
|
169 | .end(done);
|
170 | });
|
171 | });
|
172 |
|
173 | describe('queries on / with a ql parameter and a server parameter', function() {
|
174 | var app = null;
|
175 |
|
176 | beforeEach(function() {
|
177 | app = zetta({ registry: reg, peerRegistry: peerRegistry })
|
178 | .silent()
|
179 | .use(Scout)
|
180 | .name('local')
|
181 | .expose('*')
|
182 | ._run();
|
183 | });
|
184 |
|
185 | it('should have two classes', function(done) {
|
186 | request(getHttpServer(app))
|
187 | .get('/?ql=where%20type%20=%20"testdriver"&server=local')
|
188 | .expect(getBody(function(res, body){
|
189 | assert.deepEqual(body.class, ['server', 'search-results']);
|
190 | }))
|
191 | .end(done);
|
192 | });
|
193 |
|
194 | it('should have two properties: server name and ql', function(done) {
|
195 | request(getHttpServer(app))
|
196 | .get('/?ql=where%20type%20=%20"testdriver"&server=local')
|
197 | .expect(getBody(function(res, body) {
|
198 | assert.equal(body.properties.name, 'local');
|
199 | assert.equal(body.properties.ql, 'where type = "testdriver"');
|
200 | }))
|
201 | .end(done);
|
202 | });
|
203 |
|
204 | it('should have no actions.', function(done) {
|
205 | request(getHttpServer(app))
|
206 | .get('/?ql=where%20type%20=%20"testdriver"&server=local')
|
207 | .expect(getBody(function(res, body) {
|
208 | assert.equal(body.actions.length, 1);
|
209 | }))
|
210 | .end(done);
|
211 | });
|
212 |
|
213 | it('should have a websocket link to monitor the query.', function(done) {
|
214 | request(getHttpServer(app))
|
215 | .get('/?ql=where%20type%20=%20"testdriver"&server=local')
|
216 | .expect(getBody(function(res, body) {
|
217 | assert.equal(body.links.length, 4);
|
218 | hasLinkRel(body.links, 'http://rels.zettajs.io/query');
|
219 | assert.notEqual(body.links[3].href.indexOf("topic=query%2Fwhere%20type%20%3D%20%22testdriver%22"), -1);
|
220 | }))
|
221 | .end(done);
|
222 | });
|
223 | });
|
224 |
|
225 | describe('queries on / with a ql parameter and a server parameter that is proxied to', function() {
|
226 | var app = null;
|
227 | var cluster = null;
|
228 |
|
229 | beforeEach(function(done) {
|
230 | cluster = zettacluster({ zetta: zetta })
|
231 | .server('cloud')
|
232 | .server('detroit1', [Scout], ['cloud'])
|
233 | .on('ready', function() {
|
234 | app = cluster.servers['cloud'];
|
235 | done();
|
236 | })
|
237 | .run(function(err){
|
238 | if (err) {
|
239 | return done(err);
|
240 | }
|
241 | });
|
242 | });
|
243 |
|
244 | it('should have two classes', function(done) {
|
245 | request(getHttpServer(app))
|
246 | .get('/?ql=where%20type%20=%20"testdriver"&server=detroit1')
|
247 | .expect(getBody(function(res, body){
|
248 | assert.deepEqual(body.class, ['root', 'search-results']);
|
249 | }))
|
250 | .end(done);
|
251 | });
|
252 |
|
253 | it('should have two properties: server name and ql', function(done) {
|
254 | request(getHttpServer(app))
|
255 | .get('/?ql=where%20type%20=%20"testdriver"&server=detroit1')
|
256 | .expect(getBody(function(res, body) {
|
257 | assert.equal(body.properties.server, 'detroit1');
|
258 | assert.equal(body.properties.ql, 'where type = "testdriver"');
|
259 | }))
|
260 | .end(done);
|
261 | });
|
262 |
|
263 | it('should have no actions.', function(done) {
|
264 | request(getHttpServer(app))
|
265 | .get('/?ql=where%20type%20=%20"testdriver"&server=detroit1')
|
266 | .expect(getBody(function(res, body) {
|
267 | assert.ok(!body.actions);
|
268 | }))
|
269 | .end(done);
|
270 | });
|
271 |
|
272 | it('should have a websocket link to monitor the query.', function(done) {
|
273 | request(getHttpServer(app))
|
274 | .get('/?ql=where%20type%20=%20"testdriver"&server=detroit1')
|
275 | .expect(getBody(function(res, body) {
|
276 | assert.equal(body.links.length, 3);
|
277 | hasLinkRel(body.links, 'http://rels.zettajs.io/query');
|
278 | }))
|
279 | .end(done);
|
280 | });
|
281 | });
|
282 |
|
283 | describe('queries on / for all peers', function() {
|
284 | var app = null;
|
285 | var cluster = null;
|
286 |
|
287 | beforeEach(function(done) {
|
288 | cluster = zettacluster({ zetta: zetta })
|
289 | .server('cloud')
|
290 | .server('detroit1', [Scout], ['cloud'])
|
291 | .server('detroit2', [Scout], ['cloud'])
|
292 | .on('ready', function() {
|
293 | app = cluster.servers['cloud'];
|
294 | done();
|
295 | })
|
296 | .run(function(err){
|
297 | if (err) {
|
298 | return done(err);
|
299 | }
|
300 | });
|
301 | });
|
302 |
|
303 | it('should return results from each server', function(done) {
|
304 | request(getHttpServer(app))
|
305 | .get('/?ql=where%20type%20=%20"testdriver"&server=*')
|
306 | .expect(getBody(function(res, body) {
|
307 | assert.equal(body.entities.length, 2);
|
308 | hasLinkRel(body.links, 'http://rels.zettajs.io/query');
|
309 | }))
|
310 | .end(done);
|
311 |
|
312 | });
|
313 | });
|
314 |
|
315 | describe('queries on /servers/<id>', function() {
|
316 | var app = null;
|
317 |
|
318 | beforeEach(function() {
|
319 | app = zetta({ registry: reg, peerRegistry: peerRegistry })
|
320 | .silent()
|
321 | .use(Scout)
|
322 | .name('local')
|
323 | .expose('*')
|
324 | ._run();
|
325 | });
|
326 |
|
327 | it('should have two classes', function(done) {
|
328 | request(getHttpServer(app))
|
329 | .get('/servers/local?ql=where%20type%20=%20"testdriver"')
|
330 | .expect(getBody(function(res, body){
|
331 | assert.deepEqual(body.class, ['server', 'search-results']);
|
332 | }))
|
333 | .end(done);
|
334 | });
|
335 |
|
336 | it('should have two properties: server name and ql', function(done) {
|
337 | request(getHttpServer(app))
|
338 | .get('/servers/local?ql=where%20type%20=%20"testdriver"')
|
339 | .expect(getBody(function(res, body) {
|
340 | assert.equal(body.properties.name, 'local');
|
341 | assert.equal(body.properties.ql, 'where type = "testdriver"');
|
342 | }))
|
343 | .end(done);
|
344 | });
|
345 |
|
346 | it('should have one action.', function(done) {
|
347 | request(getHttpServer(app))
|
348 | .get('/servers/local?ql=where%20type%20=%20"testdriver"')
|
349 | .expect(getBody(function(res, body) {
|
350 | assert.equal(body.actions.length, 1);
|
351 | assert.equal(body.actions[0].name, 'query-devices');
|
352 | }))
|
353 | .end(done);
|
354 | });
|
355 |
|
356 | it('should have a websocket link to monitor the query.', function(done) {
|
357 | request(getHttpServer(app))
|
358 | .get('/servers/local?ql=where%20type%20=%20"testdriver"')
|
359 | .expect(getBody(function(res, body) {
|
360 | assert.equal(body.links.length, 4);
|
361 | hasLinkRel(body.links, 'http://rels.zettajs.io/query');
|
362 | assert.notEqual(body.links[3].href.indexOf("topic=query%2Fwhere%20type%20%3D%20%22testdriver%22"), -1);
|
363 | }))
|
364 | .end(done);
|
365 | });
|
366 | });
|
367 |
|
368 | describe('proxied queries on /servers/<id>', function() {
|
369 | var app = null;
|
370 | var cluster = null;
|
371 |
|
372 | beforeEach(function(done) {
|
373 | cluster = zettacluster({ zetta: zetta })
|
374 | .server('cloud')
|
375 | .server('detroit1', [Scout], ['cloud'])
|
376 | .on('ready', function() {
|
377 | app = cluster.servers['cloud'];
|
378 | done();
|
379 | })
|
380 | .run(function(err){
|
381 | if (err) {
|
382 | return done(err);
|
383 | }
|
384 | });
|
385 |
|
386 | });
|
387 |
|
388 | it('should have two classes', function(done) {
|
389 | request(getHttpServer(app))
|
390 | .get('/servers/detroit1?ql=where%20type%20=%20"testdriver"')
|
391 | .expect(getBody(function(res, body){
|
392 | assert.deepEqual(body.class, ['server', 'search-results']);
|
393 | }))
|
394 | .end(done);
|
395 | });
|
396 |
|
397 | it('should have two properties: server name and ql', function(done) {
|
398 | request(getHttpServer(app))
|
399 | .get('/servers/detroit1?ql=where%20type%20=%20"testdriver"')
|
400 | .expect(getBody(function(res, body) {
|
401 | assert.equal(body.properties.name, 'detroit1');
|
402 | assert.equal(body.properties.ql, 'where type = "testdriver"');
|
403 | }))
|
404 | .end(done);
|
405 | });
|
406 |
|
407 | it('should have one action.', function(done) {
|
408 | request(getHttpServer(app))
|
409 | .get('/servers/detroit1?ql=where%20type%20=%20"testdriver"')
|
410 | .expect(getBody(function(res, body) {
|
411 | assert.equal(body.actions.length, 1);
|
412 | assert.equal(body.actions[0].name, 'query-devices');
|
413 | }))
|
414 | .end(done);
|
415 | });
|
416 |
|
417 | it('should have a websocket link to monitor the query.', function(done) {
|
418 | request(getHttpServer(app))
|
419 | .get('/servers/detroit1?ql=where%20type%20=%20"testdriver"')
|
420 | .expect(getBody(function(res, body) {
|
421 | assert.equal(body.links.length, 4);
|
422 | hasLinkRel(body.links, 'http://rels.zettajs.io/query');
|
423 | assert.notEqual(body.links[3].href.indexOf("topic=query%2Fwhere%20type%20%3D%20%22testdriver%22"), -1);
|
424 | }))
|
425 | .end(done);
|
426 | });
|
427 | });
|
428 | });
|