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.js117
1 files changed, 0 insertions, 117 deletions
diff --git a/server/tests/utils/servers.js b/server/tests/utils/servers.js
deleted file mode 100644
index c753c1f1d..000000000
--- a/server/tests/utils/servers.js
+++ /dev/null
@@ -1,117 +0,0 @@
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 - 1))
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 host: `localhost:${9000 + number}`,
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 = {
63 'Server listening on port': false
64 }
65 const key = 'Database peertube_test' + number + ' is ready'
66 serverRunString[key] = false
67
68 const regexps = {
69 client_id: 'Client id: (.+)',
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
85 server.app = fork(pathUtils.join(__dirname, '..', '..', '..', 'dist', 'server.js'), [], options)
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
107 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
108 if (dontContinue === true) return
109
110 server.app.stdout.removeListener('data', onStdout)
111 callback(server)
112 })
113}
114
115// ---------------------------------------------------------------------------
116
117module.exports = serversUtils