blob: 534797d69a081c50892a43adaa79b5178ce0fc40 [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
44 before(function() {
45 // Start our server
46 server.start(port);
47 });
48
49 after(function() {
50 server.stop();
51 });
52
53 beforeEach(function(done) {
54 let workflowCheckResults = [];
55 async.series([
56 (callback) => {
57 // connect a probe to the server
58 // to send events for test
59 probeClient = io.connect(`http://localhost:${port}`, {
60 query: 'id=probe_id&type=probe' +
61 '&name=probe@xos.org'
62 });
63
64 probeClient.on('connect', () => {
65 callback(null, true);
66 });
67 return;
68 },
69 (callback) => {
70 // connect a workflow manager to the server
71 // to register a test workflow
72 workflowManagerClient = io.connect(`http://localhost:${port}`, {
73 query: 'id=workflow_manager_id&type=workflow_manager' +
74 '&name=manager@xos.org'
75 });
76
77 workflowManagerClient.on(eventrouter.serviceEvents.WORKFLOW_KICKSTART, (message) => {
78 workflowRunId = message.workflow_run_id;
79
80 // call-back
81 workflowManagerClient.emit(eventrouter.serviceEvents.WORKFLOW_KICKSTART, {
82 workflow_id: workflowId,
83 workflow_run_id: workflowRunId
84 })
85 });
86
87 workflowManagerClient.on('connect', () => {
88 callback(null, true);
89 });
90 return;
91 },
92 (callback) => {
93 // check existance of the workflow
94 let essence = essenceLoader.loadEssence(essenceFileName, true);
95 let workflowCnt=0;
96
97 _.forOwn(essence, (_value, essenceWorkflowId) => {
98 workflowId = essenceWorkflowId; // preseve only the last one for test
99
100 workflowCnt++;
101
102 workflowManagerClient.emit(eventrouter.serviceEvents.WORKFLOW_CHECK, essenceWorkflowId);
103
104 workflowManagerClient.on(eventrouter.serviceEvents.WORKFLOW_CHECK, (workflowCheckResult) => {
105 workflowCnt--;
106 workflowCheckResults.push(workflowCheckResult.result);
107
108 if(workflowCnt <= 0) {
109 callback(null, workflowCheckResults);
110 }
111 });
112 });
113 return;
114 },
115 (callback) => {
116 // register the workflow
117 let register = false;
118 workflowCheckResults.forEach((workflowCheckResult) => {
119 if(!workflowCheckResult) {
120 register = true;
121 }
122 });
123
124 if(register) {
125 let essence = essenceLoader.loadEssence(essenceFileName, true);
126
127 workflowManagerClient.emit(eventrouter.serviceEvents.WORKFLOW_REG_ESSENCE, essence);
128
129 workflowManagerClient.on(
130 eventrouter.serviceEvents.WORKFLOW_REG_ESSENCE,
131 (workflowRegResult) => {
132 callback(null, workflowRegResult);
133 }
134 );
135 }
136 else {
137 callback(null, true);
138 }
139 return;
140 },
141 (callback) => {
142 // kickstart the test workflow
143 probeClient.emit('onu.events', {serialNumber: 'testSerialXXX', other: 'test_other_field'});
144 setTimeout(() => {
145 expect(workflowRunId).to.not.be.undefined;
146 callback(null, true);
147 }, 500);
148 return;
149 },
150 (callback) => {
151 // connect a workflow run client to the server
152 workflowRunClient = io.connect(`http://localhost:${port}`, {
153 query: 'id=workflow_run_id&type=workflow_run' +
154 `&workflow_id=${workflowIdInEssence}&workflow_run_id=${workflowRunId}` +
155 '&name=run@xos.org'
156 });
157
158 // when is connected start testing
159 workflowRunClient.on('connect', () => {
160 callback(null, true);
161 });
162 return;
163 }
164 ],
165 function(err, results) {
166 // we do not actually check results
167 if(results.includes(false)) {
168 done.fail(err);
169 }
170 else {
171 done();
172 }
173 });
174 return;
175 });
176
177 afterEach(function(done) {
178 // remove workflow run
179 workflowManagerClient.emit(server.serviceEvents.WORKFLOW_RUN_REMOVE, {
180 workflow_id: workflowId,
181 workflow_run_id: workflowRunId
182 });
183
184 // remove workflow
185 workflowManagerClient.emit(server.serviceEvents.WORKFLOW_REMOVE, workflowId);
186
187 workflowId = null;
188 workflowRunId = null;
189
190 // disconnect clients
191 if(workflowManagerClient.connected) {
192 workflowManagerClient.disconnect();
193 }
194 workflowManagerClient = null;
195
196 if(workflowRunClient.connected) {
197 workflowRunClient.disconnect();
198 }
199 workflowRunClient = null;
200
201 if(probeClient.connected) {
202 probeClient.disconnect();
203 }
204 probeClient = null;
205
206 done();
207 });
208
209 it('should have a probe, a workflow manager and a workflow run', function(done) {
210 const eventrouter = require('../src/controllers/eventrouter.js');
211 expect(
212 Object.keys(eventrouter.getWorkflowRunClients()).length,
213 'num of workflow run clients'
214 ).to.equal(1);
215 expect(
216 Object.keys(eventrouter.getWorkflowManagerClients()).length,
217 'num of workflow manager clients'
218 ).to.equal(1);
219 expect(
220 Object.keys(eventrouter.getProbeClients()).length,
221 'num of probe clients'
222 ).to.equal(1);
223 expect(
224 Object.keys(eventrouter.getClients()).length,
225 'total num of clients'
226 ).to.equal(3);
227
228 expect(
229 'probe_id' in eventrouter.getClients(),
230 'a client called prove_id exists'
231 ).to.equal(true);
232 expect(
233 'workflow_manager_id' in eventrouter.getClients(),
234 'a client called workflow_manager_id exists'
235 ).to.equal(true);
236 expect(
237 'workflow_run_id' in eventrouter.getClients(),
238 'a client called workflow_run_id exists'
239 ).to.equal(true);
240 done();
241 });
242
243 it('should store user details for a new connection', function() {
244 const eventrouter = require('../src/controllers/eventrouter.js');
245
246 const probe = eventrouter.getClients()['probe_id'];
247 expect(probe.getParams().name).to.equal('probe@xos.org');
248
249 const manager = eventrouter.getClients()['workflow_manager_id'];
250 expect(manager.getParams().name).to.equal('manager@xos.org');
251
252 const run = eventrouter.getClients()['workflow_run_id'];
253 expect(run.getParams().name).to.equal('run@xos.org');
254 });
255
256 it('should not store the same user twice', function(done) {
257 // This test case makes cleaning up process taking long time because it leaves
258 // a client socket. It seems there's no way to release it from server-side.
259
260 // connect a client to the server
261 const client2 = io.connect(`http://localhost:${port}`, {
262 query: 'id=probe_id&type=probe' +
263 '&name=probe@xos.org&value=different_value'
264 });
265
266 // when is connected start testing
267 client2.on('connect', () => {
268 setTimeout(() => {
269 const eventrouter = require('../src/controllers/eventrouter.js');
270 expect(
271 Object.keys(eventrouter.getWorkflowRunClients()).length,
272 'num of workflow run clients'
273 ).to.equal(1);
274 expect(
275 Object.keys(eventrouter.getWorkflowManagerClients()).length,
276 'num of workflow manager clients'
277 ).to.equal(1);
278 expect(
279 Object.keys(eventrouter.getProbeClients()).length,
280 'num of probe clients'
281 ).to.equal(1);
282 expect(
283 Object.keys(eventrouter.getClients()).length,
284 'total num of clients'
285 ).to.equal(3);
286
287 done();
288 }, 100);
289 });
290 });
291
292 it('should remove a user on disconnect', function(done) {
293 workflowManagerClient.disconnect();
294 workflowRunClient.disconnect();
295 probeClient.disconnect();
296
297 // we need to wait for the event to be dispatched
298 setTimeout(() => {
299 const eventrouter = require('../src/controllers/eventrouter.js');
300 expect(
301 Object.keys(eventrouter.getWorkflowRunClients()).length,
302 'num of workflow run clients'
303 ).to.equal(0);
304 expect(
305 Object.keys(eventrouter.getWorkflowManagerClients()).length,
306 'num of workflow manager clients'
307 ).to.equal(0);
308 expect(
309 Object.keys(eventrouter.getProbeClients()).length,
310 'num of probe clients'
311 ).to.equal(0);
312 expect(
313 Object.keys(eventrouter.getClients()).length,
314 'total num of clients'
315 ).to.equal(0);
316 done();
317 }, 100);
318 });
319 });
320})();