Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 44 | this.slow(5000); |
| 45 | |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 46 | 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 Choi | c707c05 | 2019-07-18 13:50:49 -0700 | [diff] [blame] | 83 | workflowManagerClient.emit(eventrouter.serviceEvents.WORKFLOW_REPORT_NEW_RUN, { |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 84 | 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 Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 104 | workflowManagerClient.emit(eventrouter.serviceEvents.WORKFLOW_CHECK, { |
| 105 | workflow_id: essenceWorkflowId |
| 106 | }); |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 107 | |
| 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 Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 131 | workflowManagerClient.emit(eventrouter.serviceEvents.WORKFLOW_REGISTER_ESSENCE, { |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 132 | essence: essence |
| 133 | }); |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 134 | |
| 135 | workflowManagerClient.on( |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 136 | eventrouter.serviceEvents.WORKFLOW_REGISTER_ESSENCE, |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 137 | (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 |
Illyoung Choi | 16c6d4f | 2019-07-24 18:09:26 -0700 | [diff] [blame^] | 149 | probeClient.emit(eventrouter.serviceEvents.EVENT_EMIT, { |
| 150 | topic: 'onu.events', |
| 151 | message: {serialNumber: 'testSerialXXX', other: 'test_other_field'} |
| 152 | }); |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 153 | setTimeout(() => { |
| 154 | expect(workflowRunId).to.not.be.undefined; |
| 155 | callback(null, true); |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 156 | }, 1000); |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 157 | return; |
| 158 | }, |
| 159 | (callback) => { |
| 160 | // connect a workflow run client to the server |
| 161 | workflowRunClient = io.connect(`http://localhost:${port}`, { |
| 162 | query: 'id=workflow_run_id&type=workflow_run' + |
| 163 | `&workflow_id=${workflowIdInEssence}&workflow_run_id=${workflowRunId}` + |
| 164 | '&name=run@xos.org' |
| 165 | }); |
| 166 | |
| 167 | // when is connected start testing |
| 168 | workflowRunClient.on('connect', () => { |
| 169 | callback(null, true); |
| 170 | }); |
| 171 | return; |
| 172 | } |
| 173 | ], |
| 174 | function(err, results) { |
| 175 | // we do not actually check results |
| 176 | if(results.includes(false)) { |
| 177 | done.fail(err); |
| 178 | } |
| 179 | else { |
| 180 | done(); |
| 181 | } |
| 182 | }); |
| 183 | return; |
| 184 | }); |
| 185 | |
| 186 | afterEach(function(done) { |
| 187 | // remove workflow run |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 188 | workflowManagerClient.emit(server.serviceEvents.WORKFLOW_REMOVE_RUN, { |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 189 | workflow_id: workflowId, |
| 190 | workflow_run_id: workflowRunId |
| 191 | }); |
| 192 | |
| 193 | // remove workflow |
Illyoung Choi | e3ce4cf | 2019-06-28 11:07:47 -0700 | [diff] [blame] | 194 | workflowManagerClient.emit(server.serviceEvents.WORKFLOW_REMOVE, { |
| 195 | workflow_id: workflowId |
| 196 | }); |
Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 197 | |
| 198 | workflowId = null; |
| 199 | workflowRunId = null; |
| 200 | |
| 201 | // disconnect clients |
| 202 | if(workflowManagerClient.connected) { |
| 203 | workflowManagerClient.disconnect(); |
| 204 | } |
| 205 | workflowManagerClient = null; |
| 206 | |
| 207 | if(workflowRunClient.connected) { |
| 208 | workflowRunClient.disconnect(); |
| 209 | } |
| 210 | workflowRunClient = null; |
| 211 | |
| 212 | if(probeClient.connected) { |
| 213 | probeClient.disconnect(); |
| 214 | } |
| 215 | probeClient = null; |
| 216 | |
| 217 | done(); |
| 218 | }); |
| 219 | |
| 220 | it('should have a probe, a workflow manager and a workflow run', function(done) { |
| 221 | const eventrouter = require('../src/controllers/eventrouter.js'); |
| 222 | expect( |
| 223 | Object.keys(eventrouter.getWorkflowRunClients()).length, |
| 224 | 'num of workflow run clients' |
| 225 | ).to.equal(1); |
| 226 | expect( |
| 227 | Object.keys(eventrouter.getWorkflowManagerClients()).length, |
| 228 | 'num of workflow manager clients' |
| 229 | ).to.equal(1); |
| 230 | expect( |
| 231 | Object.keys(eventrouter.getProbeClients()).length, |
| 232 | 'num of probe clients' |
| 233 | ).to.equal(1); |
| 234 | expect( |
| 235 | Object.keys(eventrouter.getClients()).length, |
| 236 | 'total num of clients' |
| 237 | ).to.equal(3); |
| 238 | |
| 239 | expect( |
| 240 | 'probe_id' in eventrouter.getClients(), |
| 241 | 'a client called prove_id exists' |
| 242 | ).to.equal(true); |
| 243 | expect( |
| 244 | 'workflow_manager_id' in eventrouter.getClients(), |
| 245 | 'a client called workflow_manager_id exists' |
| 246 | ).to.equal(true); |
| 247 | expect( |
| 248 | 'workflow_run_id' in eventrouter.getClients(), |
| 249 | 'a client called workflow_run_id exists' |
| 250 | ).to.equal(true); |
| 251 | done(); |
| 252 | }); |
| 253 | |
| 254 | it('should store user details for a new connection', function() { |
| 255 | const eventrouter = require('../src/controllers/eventrouter.js'); |
| 256 | |
| 257 | const probe = eventrouter.getClients()['probe_id']; |
| 258 | expect(probe.getParams().name).to.equal('probe@xos.org'); |
| 259 | |
| 260 | const manager = eventrouter.getClients()['workflow_manager_id']; |
| 261 | expect(manager.getParams().name).to.equal('manager@xos.org'); |
| 262 | |
| 263 | const run = eventrouter.getClients()['workflow_run_id']; |
| 264 | expect(run.getParams().name).to.equal('run@xos.org'); |
| 265 | }); |
| 266 | |
| 267 | it('should not store the same user twice', function(done) { |
| 268 | // This test case makes cleaning up process taking long time because it leaves |
| 269 | // a client socket. It seems there's no way to release it from server-side. |
| 270 | |
| 271 | // connect a client to the server |
| 272 | const client2 = io.connect(`http://localhost:${port}`, { |
| 273 | query: 'id=probe_id&type=probe' + |
| 274 | '&name=probe@xos.org&value=different_value' |
| 275 | }); |
| 276 | |
| 277 | // when is connected start testing |
| 278 | client2.on('connect', () => { |
| 279 | setTimeout(() => { |
| 280 | const eventrouter = require('../src/controllers/eventrouter.js'); |
| 281 | expect( |
| 282 | Object.keys(eventrouter.getWorkflowRunClients()).length, |
| 283 | 'num of workflow run clients' |
| 284 | ).to.equal(1); |
| 285 | expect( |
| 286 | Object.keys(eventrouter.getWorkflowManagerClients()).length, |
| 287 | 'num of workflow manager clients' |
| 288 | ).to.equal(1); |
| 289 | expect( |
| 290 | Object.keys(eventrouter.getProbeClients()).length, |
| 291 | 'num of probe clients' |
| 292 | ).to.equal(1); |
| 293 | expect( |
| 294 | Object.keys(eventrouter.getClients()).length, |
| 295 | 'total num of clients' |
| 296 | ).to.equal(3); |
| 297 | |
| 298 | done(); |
| 299 | }, 100); |
| 300 | }); |
| 301 | }); |
| 302 | |
| 303 | it('should remove a user on disconnect', function(done) { |
| 304 | workflowManagerClient.disconnect(); |
| 305 | workflowRunClient.disconnect(); |
| 306 | probeClient.disconnect(); |
| 307 | |
| 308 | // we need to wait for the event to be dispatched |
| 309 | setTimeout(() => { |
| 310 | const eventrouter = require('../src/controllers/eventrouter.js'); |
| 311 | expect( |
| 312 | Object.keys(eventrouter.getWorkflowRunClients()).length, |
| 313 | 'num of workflow run clients' |
| 314 | ).to.equal(0); |
| 315 | expect( |
| 316 | Object.keys(eventrouter.getWorkflowManagerClients()).length, |
| 317 | 'num of workflow manager clients' |
| 318 | ).to.equal(0); |
| 319 | expect( |
| 320 | Object.keys(eventrouter.getProbeClients()).length, |
| 321 | 'num of probe clients' |
| 322 | ).to.equal(0); |
| 323 | expect( |
| 324 | Object.keys(eventrouter.getClients()).length, |
| 325 | 'total num of clients' |
| 326 | ).to.equal(0); |
| 327 | done(); |
| 328 | }, 100); |
| 329 | }); |
| 330 | }); |
Illyoung Choi | b4fc0d8 | 2019-07-16 10:29:39 -0700 | [diff] [blame] | 331 | })(); |