blob: b1f6333f3163f8d01a5da3b4f31bfc6cafbdf4e2 [file] [log] [blame]
Illyoung Choi59820ed2019-06-24 17:01:00 -07001/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18(function () {
19 'use strict';
20
21 const path = require('path');
22 const chai = require('chai');
23 const expect = chai.expect;
24 const sinonChai = require('sinon-chai');
25 chai.use(sinonChai);
26 const io = require('socket.io-client');
27 const async = require('async');
28 const _ = require('lodash');
29 const server = require('../src/server.js');
30 const port = 4000;
31 const eventrouter = require('../src/controllers/eventrouter.js');
32 const essenceLoader = require('../src/workflows/loader.js');
33 const essenceFileName = path.join(__dirname, 'test_clients_workflow_essence.json');
34 const workflowIdInEssence = 'test_clients_workflow'
35
36 describe('Simple websocket client test', function() {
37
38 var probeClient;
39 var workflowManagerClient;
40 var workflowRunClient;
41 var workflowId;
42 var workflowRunId;
43
Illyoung Choie3ce4cf2019-06-28 11:07:47 -070044 this.slow(5000);
45
Illyoung Choi59820ed2019-06-24 17:01:00 -070046 before(function() {
47 // Start our server
48 server.start(port);
49 });
50
51 after(function() {
52 server.stop();
53 });
54
55 beforeEach(function(done) {
56 let workflowCheckResults = [];
57 async.series([
58 (callback) => {
59 // connect a probe to the server
60 // to send events for test
61 probeClient = io.connect(`http://localhost:${port}`, {
62 query: 'id=probe_id&type=probe' +
63 '&name=probe@xos.org'
64 });
65
66 probeClient.on('connect', () => {
67 callback(null, true);
68 });
69 return;
70 },
71 (callback) => {
72 // connect a workflow manager to the server
73 // to register a test workflow
74 workflowManagerClient = io.connect(`http://localhost:${port}`, {
75 query: 'id=workflow_manager_id&type=workflow_manager' +
76 '&name=manager@xos.org'
77 });
78
79 workflowManagerClient.on(eventrouter.serviceEvents.WORKFLOW_KICKSTART, (message) => {
80 workflowRunId = message.workflow_run_id;
81
82 // call-back
Illyoung Choic707c052019-07-18 13:50:49 -070083 workflowManagerClient.emit(eventrouter.serviceEvents.WORKFLOW_REPORT_NEW_RUN, {
Illyoung Choi59820ed2019-06-24 17:01:00 -070084 workflow_id: workflowId,
85 workflow_run_id: workflowRunId
86 })
87 });
88
89 workflowManagerClient.on('connect', () => {
90 callback(null, true);
91 });
92 return;
93 },
94 (callback) => {
95 // check existance of the workflow
96 let essence = essenceLoader.loadEssence(essenceFileName, true);
97 let workflowCnt=0;
98
99 _.forOwn(essence, (_value, essenceWorkflowId) => {
100 workflowId = essenceWorkflowId; // preseve only the last one for test
101
102 workflowCnt++;
103
Illyoung Choie3ce4cf2019-06-28 11:07:47 -0700104 workflowManagerClient.emit(eventrouter.serviceEvents.WORKFLOW_CHECK, {
105 workflow_id: essenceWorkflowId
106 });
Illyoung Choi59820ed2019-06-24 17:01:00 -0700107
108 workflowManagerClient.on(eventrouter.serviceEvents.WORKFLOW_CHECK, (workflowCheckResult) => {
109 workflowCnt--;
110 workflowCheckResults.push(workflowCheckResult.result);
111
112 if(workflowCnt <= 0) {
113 callback(null, workflowCheckResults);
114 }
115 });
116 });
117 return;
118 },
119 (callback) => {
120 // register the workflow
121 let register = false;
122 workflowCheckResults.forEach((workflowCheckResult) => {
123 if(!workflowCheckResult) {
124 register = true;
125 }
126 });
127
128 if(register) {
129 let essence = essenceLoader.loadEssence(essenceFileName, true);
130
Illyoung Choib4fc0d82019-07-16 10:29:39 -0700131 workflowManagerClient.emit(eventrouter.serviceEvents.WORKFLOW_REGISTER_ESSENCE, {
Illyoung Choie3ce4cf2019-06-28 11:07:47 -0700132 essence: essence
133 });
Illyoung Choi59820ed2019-06-24 17:01:00 -0700134
135 workflowManagerClient.on(
Illyoung Choib4fc0d82019-07-16 10:29:39 -0700136 eventrouter.serviceEvents.WORKFLOW_REGISTER_ESSENCE,
Illyoung Choi59820ed2019-06-24 17:01:00 -0700137 (workflowRegResult) => {
138 callback(null, workflowRegResult);
139 }
140 );
141 }
142 else {
143 callback(null, true);
144 }
145 return;
146 },
147 (callback) => {
148 // kickstart the test workflow
149 probeClient.emit('onu.events', {serialNumber: 'testSerialXXX', other: 'test_other_field'});
150 setTimeout(() => {
151 expect(workflowRunId).to.not.be.undefined;
152 callback(null, true);
Illyoung Choie3ce4cf2019-06-28 11:07:47 -0700153 }, 1000);
Illyoung Choi59820ed2019-06-24 17:01:00 -0700154 return;
155 },
156 (callback) => {
157 // connect a workflow run client to the server
158 workflowRunClient = io.connect(`http://localhost:${port}`, {
159 query: 'id=workflow_run_id&type=workflow_run' +
160 `&workflow_id=${workflowIdInEssence}&workflow_run_id=${workflowRunId}` +
161 '&name=run@xos.org'
162 });
163
164 // when is connected start testing
165 workflowRunClient.on('connect', () => {
166 callback(null, true);
167 });
168 return;
169 }
170 ],
171 function(err, results) {
172 // we do not actually check results
173 if(results.includes(false)) {
174 done.fail(err);
175 }
176 else {
177 done();
178 }
179 });
180 return;
181 });
182
183 afterEach(function(done) {
184 // remove workflow run
Illyoung Choib4fc0d82019-07-16 10:29:39 -0700185 workflowManagerClient.emit(server.serviceEvents.WORKFLOW_REMOVE_RUN, {
Illyoung Choi59820ed2019-06-24 17:01:00 -0700186 workflow_id: workflowId,
187 workflow_run_id: workflowRunId
188 });
189
190 // remove workflow
Illyoung Choie3ce4cf2019-06-28 11:07:47 -0700191 workflowManagerClient.emit(server.serviceEvents.WORKFLOW_REMOVE, {
192 workflow_id: workflowId
193 });
Illyoung Choi59820ed2019-06-24 17:01:00 -0700194
195 workflowId = null;
196 workflowRunId = null;
197
198 // disconnect clients
199 if(workflowManagerClient.connected) {
200 workflowManagerClient.disconnect();
201 }
202 workflowManagerClient = null;
203
204 if(workflowRunClient.connected) {
205 workflowRunClient.disconnect();
206 }
207 workflowRunClient = null;
208
209 if(probeClient.connected) {
210 probeClient.disconnect();
211 }
212 probeClient = null;
213
214 done();
215 });
216
217 it('should have a probe, a workflow manager and a workflow run', function(done) {
218 const eventrouter = require('../src/controllers/eventrouter.js');
219 expect(
220 Object.keys(eventrouter.getWorkflowRunClients()).length,
221 'num of workflow run clients'
222 ).to.equal(1);
223 expect(
224 Object.keys(eventrouter.getWorkflowManagerClients()).length,
225 'num of workflow manager clients'
226 ).to.equal(1);
227 expect(
228 Object.keys(eventrouter.getProbeClients()).length,
229 'num of probe clients'
230 ).to.equal(1);
231 expect(
232 Object.keys(eventrouter.getClients()).length,
233 'total num of clients'
234 ).to.equal(3);
235
236 expect(
237 'probe_id' in eventrouter.getClients(),
238 'a client called prove_id exists'
239 ).to.equal(true);
240 expect(
241 'workflow_manager_id' in eventrouter.getClients(),
242 'a client called workflow_manager_id exists'
243 ).to.equal(true);
244 expect(
245 'workflow_run_id' in eventrouter.getClients(),
246 'a client called workflow_run_id exists'
247 ).to.equal(true);
248 done();
249 });
250
251 it('should store user details for a new connection', function() {
252 const eventrouter = require('../src/controllers/eventrouter.js');
253
254 const probe = eventrouter.getClients()['probe_id'];
255 expect(probe.getParams().name).to.equal('probe@xos.org');
256
257 const manager = eventrouter.getClients()['workflow_manager_id'];
258 expect(manager.getParams().name).to.equal('manager@xos.org');
259
260 const run = eventrouter.getClients()['workflow_run_id'];
261 expect(run.getParams().name).to.equal('run@xos.org');
262 });
263
264 it('should not store the same user twice', function(done) {
265 // This test case makes cleaning up process taking long time because it leaves
266 // a client socket. It seems there's no way to release it from server-side.
267
268 // connect a client to the server
269 const client2 = io.connect(`http://localhost:${port}`, {
270 query: 'id=probe_id&type=probe' +
271 '&name=probe@xos.org&value=different_value'
272 });
273
274 // when is connected start testing
275 client2.on('connect', () => {
276 setTimeout(() => {
277 const eventrouter = require('../src/controllers/eventrouter.js');
278 expect(
279 Object.keys(eventrouter.getWorkflowRunClients()).length,
280 'num of workflow run clients'
281 ).to.equal(1);
282 expect(
283 Object.keys(eventrouter.getWorkflowManagerClients()).length,
284 'num of workflow manager clients'
285 ).to.equal(1);
286 expect(
287 Object.keys(eventrouter.getProbeClients()).length,
288 'num of probe clients'
289 ).to.equal(1);
290 expect(
291 Object.keys(eventrouter.getClients()).length,
292 'total num of clients'
293 ).to.equal(3);
294
295 done();
296 }, 100);
297 });
298 });
299
300 it('should remove a user on disconnect', function(done) {
301 workflowManagerClient.disconnect();
302 workflowRunClient.disconnect();
303 probeClient.disconnect();
304
305 // we need to wait for the event to be dispatched
306 setTimeout(() => {
307 const eventrouter = require('../src/controllers/eventrouter.js');
308 expect(
309 Object.keys(eventrouter.getWorkflowRunClients()).length,
310 'num of workflow run clients'
311 ).to.equal(0);
312 expect(
313 Object.keys(eventrouter.getWorkflowManagerClients()).length,
314 'num of workflow manager clients'
315 ).to.equal(0);
316 expect(
317 Object.keys(eventrouter.getProbeClients()).length,
318 'num of probe clients'
319 ).to.equal(0);
320 expect(
321 Object.keys(eventrouter.getClients()).length,
322 'total num of clients'
323 ).to.equal(0);
324 done();
325 }, 100);
326 });
327 });
Illyoung Choib4fc0d82019-07-16 10:29:39 -0700328})();