aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils/servers.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/utils/servers.js')
-rw-r--r--server/tests/utils/servers.js115
1 files changed, 115 insertions, 0 deletions
diff --git a/server/tests/utils/servers.js b/server/tests/utils/servers.js
new file mode 100644
index 000000000..d62838bc7
--- /dev/null
+++ b/server/tests/utils/servers.js
@@ -0,0 +1,115 @@
1'use strict'
2
3const childProcess = require('child_process')
4const exec = childProcess.exec
5const fork = childProcess.fork
6const pathUtils = require('path')
7
8const serversUtils = {
9 flushAndRunMultipleServers,
10 flushTests,
11 runServer
12}
13
14// ---------------------- Export functions --------------------
15
16function flushAndRunMultipleServers (totalServers, serversRun) {
17 let apps = []
18 let urls = []
19 let i = 0
20
21 function anotherServerDone (number, app, url) {
22 apps[number - 1] = app
23 urls[number - 1] = url
24 i++
25 if (i === totalServers) {
26 serversRun(apps, urls)
27 }
28 }
29
30 flushTests(function () {
31 for (let j = 1; j <= totalServers; j++) {
32 // For the virtual buffer
33 setTimeout(function () {
34 runServer(j, function (app, url) {
35 anotherServerDone(j, app, url)
36 })
37 }, 1000 * j)
38 }
39 })
40}
41
42function flushTests (callback) {
43 exec('npm run clean:server:test', callback)
44}
45
46function runServer (number, callback) {
47 const server = {
48 app: null,
49 url: `http://localhost:${9000 + number}`,
50 client: {
51 id: null,
52 secret: null
53 },
54 user: {
55 username: null,
56 password: null
57 }
58 }
59
60 // These actions are async so we need to be sure that they have both been done
61 const serverRunString = {
62 'Connected to mongodb': false,
63 'Server listening on port': false
64 }
65
66 const regexps = {
67 client_id: 'Client id: ([a-f0-9]+)',
68 client_secret: 'Client secret: (.+)',
69 user_username: 'Username: (.+)',
70 user_password: 'User password: (.+)'
71 }
72
73 // Share the environment
74 const env = Object.create(process.env)
75 env.NODE_ENV = 'test'
76 env.NODE_APP_INSTANCE = number
77 const options = {
78 silent: true,
79 env: env,
80 detached: true
81 }
82
83 server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options)
84 server.app.stdout.on('data', function onStdout (data) {
85 let dontContinue = false
86
87 // Capture things if we want to
88 for (const key of Object.keys(regexps)) {
89 const regexp = regexps[key]
90 const matches = data.toString().match(regexp)
91 if (matches !== null) {
92 if (key === 'client_id') server.client.id = matches[1]
93 else if (key === 'client_secret') server.client.secret = matches[1]
94 else if (key === 'user_username') server.user.username = matches[1]
95 else if (key === 'user_password') server.user.password = matches[1]
96 }
97 }
98
99 // Check if all required sentences are here
100 for (const key of Object.keys(serverRunString)) {
101 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
102 if (serverRunString[key] === false) dontContinue = true
103 }
104
105 // If no, there is maybe one thing not already initialized (mongodb...)
106 if (dontContinue === true) return
107
108 server.app.stdout.removeListener('data', onStdout)
109 callback(server)
110 })
111}
112
113// ---------------------------------------------------------------------------
114
115module.exports = serversUtils