blob: c37a21d69e914b8c44455254b699cbc0680096cc [file] [log] [blame]
Ali Ghassemifcab3c32014-09-12 14:24:12 -07001var test = require('prova');
2var _ = require('lodash');
3var proxyquire = require('proxyquireify')(require);
4var mockLRUCache = require('./mocks/lru-cache');
Ali Ghassemifcab3c32014-09-12 14:24:12 -07005
6// 8885 is the expected wspr port to be running for the tests
Ali Ghassemia35f0422014-09-26 09:04:04 -07007// @noCallThru ensures this completely overrdies the original config
8// instead of inheriting the properties that are not defined here from
9// the original dependency
Ali Ghassemifcab3c32014-09-12 14:24:12 -070010var veyronConfigForTest = {
Ali Ghassemia35f0422014-09-26 09:04:04 -070011 'authenticate': false,
12 'wspr': 'http://localhost:8885',
13 '@noCallThru': true
Ali Ghassemifcab3c32014-09-12 14:24:12 -070014};
15
16// Require namespaceService but using test specific mocks and configs
17var namespaceService =
18 proxyquire('../../../../src/services/namespace/service.js', {
19 '../../veyron-config': veyronConfigForTest,
20 'lru-cache': function() {
21 return mockLRUCache;
22 }
23 });
24
Ali Ghassemifcab3c32014-09-12 14:24:12 -070025test('getChildren of default namespace root', function(t) {
Alex Fandrianto4204a882014-09-19 13:34:32 -070026 t.plan(9+9);
Ali Ghassemifcab3c32014-09-12 14:24:12 -070027
Ali Ghassemibbb20532014-09-22 15:05:58 -070028 namespaceService.getChildren().
29 then(function assertResult(result) {
30 // Wait until we receive the 4 top level items,
Ali Ghassemif2540c92014-10-01 09:00:15 -070031 // cottage, house
Ali Ghassemibbb20532014-09-22 15:05:58 -070032 var numReturnedChildren;
33 result(function(children) {
34 numReturnedChildren = children.length;
Ali Ghassemif2540c92014-10-01 09:00:15 -070035 if (numReturnedChildren === 2) {
Ali Ghassemibbb20532014-09-22 15:05:58 -070036 children = _.sortBy(children, 'mountedName');
Ali Ghassemif2540c92014-10-01 09:00:15 -070037 assertCottage(children[0]);
38 assertHouse(children[1]);
Ali Ghassemibbb20532014-09-22 15:05:58 -070039 }
40 });
Ali Ghassemif2540c92014-10-01 09:00:15 -070041 }).catch();
Ali Ghassemifcab3c32014-09-12 14:24:12 -070042
Alex Fandrianto4204a882014-09-19 13:34:32 -070043 // 9 assertions
Ali Ghassemif2540c92014-10-01 09:00:15 -070044 function assertCottage(item) {
45 t.equal(item.mountedName, 'cottage');
46 t.equal(item.objectName, 'cottage');
Ali Ghassemifcab3c32014-09-12 14:24:12 -070047 t.equal(item.isServer, true);
Ali Ghassemif2540c92014-10-01 09:00:15 -070048 t.equal(item.isGlobbable, true);
Ali Ghassemifcab3c32014-09-12 14:24:12 -070049
50 t.ok(item.serverInfo.signature);
Ali Ghassemif2540c92014-10-01 09:00:15 -070051
52 assertMounttableServiceTypeInfo(t, item.serverInfo.typeInfo);
Ali Ghassemifcab3c32014-09-12 14:24:12 -070053 }
54
Alex Fandrianto4204a882014-09-19 13:34:32 -070055 // 9 assertions
Ali Ghassemifcab3c32014-09-12 14:24:12 -070056 function assertHouse(item) {
57 t.equal(item.mountedName, 'house');
58 t.equal(item.objectName, 'house');
59 t.equal(item.isServer, true);
60 t.equal(item.isGlobbable, true);
61
62 t.ok(item.serverInfo.signature);
63
64 assertMounttableServiceTypeInfo(t, item.serverInfo.typeInfo);
65 }
66});
67
68test('getChildren of cottage/lawn', function(t) {
Alex Fandrianto4204a882014-09-19 13:34:32 -070069 t.plan(9+5);
Ali Ghassemifcab3c32014-09-12 14:24:12 -070070
Ali Ghassemibbb20532014-09-22 15:05:58 -070071 namespaceService.getChildren('cottage/lawn').
72 then(function assertResult(result) {
73 // Wait until we receive the 3 items,
74 // back, front and master-sprinkler come back
75 var numReturnedChildren;
76 result(function(children) {
77 numReturnedChildren = children.length;
78 if (numReturnedChildren === 3) {
79 children = _.sortBy(children, 'mountedName');
80 assertBack(children[0]);
81 assertSprinkler(children[2]);
82 }
83 });
84 }).catch(t.end);
Ali Ghassemifcab3c32014-09-12 14:24:12 -070085
Alex Fandrianto4204a882014-09-19 13:34:32 -070086 // 9 assertions
Ali Ghassemifcab3c32014-09-12 14:24:12 -070087 function assertSprinkler(item) {
88 t.equal(item.mountedName, 'master-sprinkler');
89 t.equal(item.objectName, 'cottage/lawn/master-sprinkler');
90 t.equal(item.isServer, true);
91 t.equal(item.isGlobbable, false);
92
93 assertUnknownServiceTypeInfo(t, item.serverInfo.typeInfo);
94
95 t.ok(item.serverInfo.signature);
96 }
97
Alex Fandrianto4204a882014-09-19 13:34:32 -070098 // 5 assertions
Ali Ghassemifcab3c32014-09-12 14:24:12 -070099 function assertBack(item) {
100 t.equal(item.mountedName, 'back');
101 t.equal(item.objectName, 'cottage/lawn/back');
102 t.equal(item.isServer, false);
103 t.equal(item.isGlobbable, true);
104 t.equal(item.serverInfo, null);
105 }
106});
107
108test('getChildren of rooted /localhost:8881/house/kitchen', function(t) {
Alex Fandrianto4204a882014-09-19 13:34:32 -0700109 t.plan(9+9);
Ali Ghassemifcab3c32014-09-12 14:24:12 -0700110
111 // 8881 is the expected root mounttable port to be running for the tests
Ali Ghassemibbb20532014-09-22 15:05:58 -0700112 namespaceService.getChildren('/localhost:8881/house/kitchen').
113 then(function assertResult(result) {
Alex Fandrianto4204a882014-09-19 13:34:32 -0700114 // Wait until we receive the 2 items, lights and smoke-detector
Ali Ghassemibbb20532014-09-22 15:05:58 -0700115 var numReturnedChildren;
116 result(function(children) {
117 numReturnedChildren = children.length;
Alex Fandrianto4204a882014-09-19 13:34:32 -0700118 if (numReturnedChildren === 2) {
119 children = _.sortBy(children, 'mountedName');
120 assertLightSwitch(children[0]);
121 assertSmokeDetector(children[1]);
Ali Ghassemibbb20532014-09-22 15:05:58 -0700122 t.end();
123 }
124 });
125 }).catch(t.end);
Ali Ghassemifcab3c32014-09-12 14:24:12 -0700126
Alex Fandrianto4204a882014-09-19 13:34:32 -0700127 // 9 assertions
128 function assertLightSwitch(item) {
129 t.equal(item.mountedName, 'lights');
130 t.equal(item.objectName, '/localhost:8881/house/kitchen/lights');
131 t.equal(item.isServer, true);
132 t.equal(item.isGlobbable, false);
133
134 assertUnknownServiceTypeInfo(t, item.serverInfo.typeInfo);
135
136 t.ok(item.serverInfo.signature);
137 }
138
139 // 9 assertions
Ali Ghassemifcab3c32014-09-12 14:24:12 -0700140 function assertSmokeDetector(item) {
141 t.equal(item.mountedName, 'smoke-detector');
142 t.equal(item.objectName, '/localhost:8881/house/kitchen/smoke-detector');
143 t.equal(item.isServer, true);
144 t.equal(item.isGlobbable, false);
145
146 assertUnknownServiceTypeInfo(t, item.serverInfo.typeInfo);
147
148 t.ok(item.serverInfo.signature);
149 }
150});
151
Ali Ghassemibbb20532014-09-22 15:05:58 -0700152test('getChildren of non-existing mounttable', function(t) {
153 t.plan(1);
154
155 // Should get an error
156 namespaceService.getChildren('/DoesNotExist:666/What/Ever').
157 then(function shouldNotGetResult(result){
158 t.fail('Should have returned an error instead of result');
159 }).
160 catch(function assertThereIsError(err) {
161 t.ok(err);
162 });
163});
164
Ali Ghassemifcab3c32014-09-12 14:24:12 -0700165test('glob uses caching', function(t) {
166 t.plan(3);
167
168 mockLRUCache.reset();
169
Ali Ghassemibbb20532014-09-22 15:05:58 -0700170 namespaceService.glob('house', '*').
171 then(function assertNoCacheHit() {
172 // First time, no cache hit so a get call followed by a set call
173 t.notOk(mockLRUCache.lastCallWasCacheHit);
Ali Ghassemifcab3c32014-09-12 14:24:12 -0700174
Ali Ghassemibbb20532014-09-22 15:05:58 -0700175 // Call second time, there should have been a cache hit
176 return namespaceService.glob('house', '*');
177 }).then( function assertCacheHit() {
178 t.ok(mockLRUCache.lastCallWasCacheHit);
Ali Ghassemifcab3c32014-09-12 14:24:12 -0700179
Ali Ghassemibbb20532014-09-22 15:05:58 -0700180 // call glob with same name, different query, there should be no cache hit
181 return namespaceService.glob('house', 'foo*');
182 }).then(function assertNoCacheHit() {
183 t.notOk(mockLRUCache.lastCallWasCacheHit);
184 }).catch(t.end);
Ali Ghassemifcab3c32014-09-12 14:24:12 -0700185});
186
187test('getSignature uses caching', function(t) {
188 t.plan(3);
189
190 mockLRUCache.reset();
191
192 namespaceService.getSignature('house/alarm').then(function() {
193 // First time, no cache hit
194 t.notOk(mockLRUCache.lastCallWasCacheHit);
195 // Call a second time
196 return namespaceService.getSignature('house/alarm');
197 }).then(function() {
198 // Second time, must be a cache hit
199 t.ok(mockLRUCache.lastCallWasCacheHit);
200 // Call a different name
201 return namespaceService.getSignature('house/kitchen/smoke-detector');
202 }).then(function() {
203 // Different name, no cache hit
204 t.notOk(mockLRUCache.lastCallWasCacheHit);
205 });
206});
207
208//TODO(aghassemi)
209//Tests for:
210// Recursive glob
211// Glob with some keyword
212// Ensuring array is updated when nodes get mounted and unmounted (when we use
213// watchGlob)
214// makeRPC TODO(alexfandrianto)
215
216/*
217 * Test helpers
218 */
219
220/*
Alex Fandrianto4204a882014-09-19 13:34:32 -0700221 * Asserts that a ServiceTypeInfo is of predefined type of Unknown Service.
222 * 4 assertions
Ali Ghassemifcab3c32014-09-12 14:24:12 -0700223 */
224function assertUnknownServiceTypeInfo(t, typeInfo) {
225 t.equal(typeInfo.key, 'veyron-unknown');
226 t.equal(typeInfo.typeName, 'Service');
227 t.equal(typeInfo.description, null);
228 t.ok(typeInfo.icon);
229}
230
231/*
Alex Fandrianto4204a882014-09-19 13:34:32 -0700232 * Asserts that a ServiceTypeInfo is of predefined type of mounttable.
233 * 4 assertions
Ali Ghassemifcab3c32014-09-12 14:24:12 -0700234 */
235function assertMounttableServiceTypeInfo(t, typeInfo) {
236 t.equal(typeInfo.key, 'veyron-mounttable');
237 t.equal(typeInfo.typeName, 'Mount Table');
238 t.ok(typeInfo.description);
239 t.ok(typeInfo.icon);
240}