]>
Commit | Line | Data |
---|---|---|
8d309058 C |
1 | 'use strict' |
2 | ||
3 | const childProcess = require('child_process') | |
4 | const exec = childProcess.exec | |
5 | const fork = childProcess.fork | |
6 | const pathUtils = require('path') | |
7 | ||
8 | const serversUtils = { | |
c4403b29 C |
9 | flushAndRunMultipleServers, |
10 | flushTests, | |
11 | runServer | |
8d309058 C |
12 | } |
13 | ||
14 | // ---------------------- Export functions -------------------- | |
15 | ||
16 | function 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 | }) | |
efe923bc | 37 | }, 1000 * (j - 1)) |
8d309058 C |
38 | } |
39 | }) | |
40 | } | |
41 | ||
42 | function flushTests (callback) { | |
43 | exec('npm run clean:server:test', callback) | |
44 | } | |
45 | ||
46 | function runServer (number, callback) { | |
47 | const server = { | |
48 | app: null, | |
49 | url: `http://localhost:${9000 + number}`, | |
a4254ea1 | 50 | host: `localhost:${9000 + number}`, |
8d309058 C |
51 | client: { |
52 | id: null, | |
53 | secret: null | |
54 | }, | |
55 | user: { | |
56 | username: null, | |
57 | password: null | |
58 | } | |
59 | } | |
60 | ||
61 | // These actions are async so we need to be sure that they have both been done | |
62 | const serverRunString = { | |
8d309058 C |
63 | 'Server listening on port': false |
64 | } | |
84c07f8a C |
65 | const key = 'Database peertube_test' + number + ' is ready' |
66 | serverRunString[key] = false | |
8d309058 C |
67 | |
68 | const regexps = { | |
feb4bdfd | 69 | client_id: 'Client id: (.+)', |
8d309058 C |
70 | client_secret: 'Client secret: (.+)', |
71 | user_username: 'Username: (.+)', | |
72 | user_password: 'User password: (.+)' | |
73 | } | |
74 | ||
75 | // Share the environment | |
76 | const env = Object.create(process.env) | |
77 | env.NODE_ENV = 'test' | |
78 | env.NODE_APP_INSTANCE = number | |
79 | const options = { | |
80 | silent: true, | |
81 | env: env, | |
82 | detached: true | |
83 | } | |
84 | ||
15103f11 | 85 | server.app = fork(pathUtils.join(__dirname, '..', '..', '..', 'server.js'), [], options) |
8d309058 C |
86 | server.app.stdout.on('data', function onStdout (data) { |
87 | let dontContinue = false | |
88 | ||
89 | // Capture things if we want to | |
90 | for (const key of Object.keys(regexps)) { | |
91 | const regexp = regexps[key] | |
92 | const matches = data.toString().match(regexp) | |
93 | if (matches !== null) { | |
94 | if (key === 'client_id') server.client.id = matches[1] | |
95 | else if (key === 'client_secret') server.client.secret = matches[1] | |
96 | else if (key === 'user_username') server.user.username = matches[1] | |
97 | else if (key === 'user_password') server.user.password = matches[1] | |
98 | } | |
99 | } | |
100 | ||
101 | // Check if all required sentences are here | |
102 | for (const key of Object.keys(serverRunString)) { | |
103 | if (data.toString().indexOf(key) !== -1) serverRunString[key] = true | |
104 | if (serverRunString[key] === false) dontContinue = true | |
105 | } | |
106 | ||
b769007f | 107 | // If no, there is maybe one thing not already initialized (client/user credentials generation...) |
8d309058 C |
108 | if (dontContinue === true) return |
109 | ||
110 | server.app.stdout.removeListener('data', onStdout) | |
111 | callback(server) | |
112 | }) | |
113 | } | |
114 | ||
115 | // --------------------------------------------------------------------------- | |
116 | ||
117 | module.exports = serversUtils |