]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/servers.js
Update migrations code
[github/Chocobozzz/PeerTube.git] / server / tests / utils / servers.js
CommitLineData
8d309058
C
1'use strict'
2
3const childProcess = require('child_process')
4const exec = childProcess.exec
5const fork = childProcess.fork
6const pathUtils = require('path')
7
8const serversUtils = {
c4403b29
C
9 flushAndRunMultipleServers,
10 flushTests,
11 runServer
8d309058
C
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}`,
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 = {
feb4bdfd 63 'Database is ready': false,
8d309058
C
64 'Server listening on port': false
65 }
66
67 const regexps = {
feb4bdfd 68 client_id: 'Client id: (.+)',
8d309058
C
69 client_secret: 'Client secret: (.+)',
70 user_username: 'Username: (.+)',
71 user_password: 'User password: (.+)'
72 }
73
74 // Share the environment
75 const env = Object.create(process.env)
76 env.NODE_ENV = 'test'
77 env.NODE_APP_INSTANCE = number
78 const options = {
79 silent: true,
80 env: env,
81 detached: true
82 }
83
84 server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options)
85 server.app.stdout.on('data', function onStdout (data) {
86 let dontContinue = false
87
88 // Capture things if we want to
89 for (const key of Object.keys(regexps)) {
90 const regexp = regexps[key]
91 const matches = data.toString().match(regexp)
92 if (matches !== null) {
93 if (key === 'client_id') server.client.id = matches[1]
94 else if (key === 'client_secret') server.client.secret = matches[1]
95 else if (key === 'user_username') server.user.username = matches[1]
96 else if (key === 'user_password') server.user.password = matches[1]
97 }
98 }
99
100 // Check if all required sentences are here
101 for (const key of Object.keys(serverRunString)) {
102 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
103 if (serverRunString[key] === false) dontContinue = true
104 }
105
b769007f 106 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
8d309058
C
107 if (dontContinue === true) return
108
109 server.app.stdout.removeListener('data', onStdout)
110 callback(server)
111 })
112}
113
114// ---------------------------------------------------------------------------
115
116module.exports = serversUtils