]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/utils/server/servers.ts
bde7dd5c286ee22691ac34d5c705d5c96ce2fb0a
[github/Chocobozzz/PeerTube.git] / shared / utils / server / servers.ts
1 /* tslint:disable:no-unused-expression */
2
3 import { ChildProcess, exec, fork } from 'child_process'
4 import { join } from 'path'
5 import { root, wait } from '../miscs/miscs'
6 import { readdir, readFile } from 'fs-extra'
7 import { existsSync } from 'fs'
8 import { expect } from 'chai'
9
10 interface ServerInfo {
11 app: ChildProcess,
12 url: string
13 host: string
14 serverNumber: number
15
16 client: {
17 id: string,
18 secret: string
19 }
20
21 user: {
22 username: string,
23 password: string,
24 email?: string
25 }
26
27 accessToken?: string
28
29 video?: {
30 id: number
31 uuid: string
32 name: string
33 account: {
34 name: string
35 }
36 }
37
38 remoteVideo?: {
39 id: number
40 uuid: string
41 }
42 }
43
44 function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
45 let apps = []
46 let i = 0
47
48 return new Promise<ServerInfo[]>(res => {
49 function anotherServerDone (serverNumber, app) {
50 apps[serverNumber - 1] = app
51 i++
52 if (i === totalServers) {
53 return res(apps)
54 }
55 }
56
57 flushTests()
58 .then(() => {
59 for (let j = 1; j <= totalServers; j++) {
60 runServer(j, configOverride).then(app => anotherServerDone(j, app))
61 }
62 })
63 })
64 }
65
66 function flushTests () {
67 return new Promise<void>((res, rej) => {
68 return exec('npm run clean:server:test', err => {
69 if (err) return rej(err)
70
71 return res()
72 })
73 })
74 }
75
76 function runServer (serverNumber: number, configOverride?: Object, args = []) {
77 const server: ServerInfo = {
78 app: null,
79 serverNumber: serverNumber,
80 url: `http://localhost:${9000 + serverNumber}`,
81 host: `localhost:${9000 + serverNumber}`,
82 client: {
83 id: null,
84 secret: null
85 },
86 user: {
87 username: null,
88 password: null
89 }
90 }
91
92 // These actions are async so we need to be sure that they have both been done
93 const serverRunString = {
94 'Server listening': false
95 }
96 const key = 'Database peertube_test' + serverNumber + ' is ready'
97 serverRunString[key] = false
98
99 const regexps = {
100 client_id: 'Client id: (.+)',
101 client_secret: 'Client secret: (.+)',
102 user_username: 'Username: (.+)',
103 user_password: 'User password: (.+)'
104 }
105
106 // Share the environment
107 const env = Object.create(process.env)
108 env['NODE_ENV'] = 'test'
109 env['NODE_APP_INSTANCE'] = serverNumber.toString()
110
111 if (configOverride !== undefined) {
112 env['NODE_CONFIG'] = JSON.stringify(configOverride)
113 }
114
115 const options = {
116 silent: true,
117 env: env,
118 detached: true
119 }
120
121 return new Promise<ServerInfo>(res => {
122 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
123 server.app.stdout.on('data', function onStdout (data) {
124 let dontContinue = false
125
126 // Capture things if we want to
127 for (const key of Object.keys(regexps)) {
128 const regexp = regexps[key]
129 const matches = data.toString().match(regexp)
130 if (matches !== null) {
131 if (key === 'client_id') server.client.id = matches[1]
132 else if (key === 'client_secret') server.client.secret = matches[1]
133 else if (key === 'user_username') server.user.username = matches[1]
134 else if (key === 'user_password') server.user.password = matches[1]
135 }
136 }
137
138 // Check if all required sentences are here
139 for (const key of Object.keys(serverRunString)) {
140 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
141 if (serverRunString[key] === false) dontContinue = true
142 }
143
144 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
145 if (dontContinue === true) return
146
147 server.app.stdout.removeListener('data', onStdout)
148
149 process.on('exit', () => {
150 try {
151 process.kill(server.app.pid)
152 } catch { /* empty */ }
153 })
154
155 res(server)
156 })
157
158 })
159 }
160
161 async function reRunServer (server: ServerInfo, configOverride?: any) {
162 const newServer = await runServer(server.serverNumber, configOverride)
163 server.app = newServer.app
164
165 return server
166 }
167
168 async function checkTmpIsEmpty (server: ServerInfo) {
169 return checkDirectoryIsEmpty(server, 'tmp')
170 }
171
172 async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) {
173 const testDirectory = 'test' + server.serverNumber
174
175 const directoryPath = join(root(), testDirectory, directory)
176
177 const directoryExists = existsSync(directoryPath)
178 expect(directoryExists).to.be.true
179
180 const files = await readdir(directoryPath)
181 expect(files).to.have.lengthOf(0)
182 }
183
184 function killallServers (servers: ServerInfo[]) {
185 for (const server of servers) {
186 process.kill(-server.app.pid)
187 }
188 }
189
190 async function waitUntilLog (server: ServerInfo, str: string, count = 1) {
191 const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log')
192
193 while (true) {
194 const buf = await readFile(logfile)
195
196 const matches = buf.toString().match(new RegExp(str, 'g'))
197 if (matches && matches.length === count) return
198
199 await wait(1000)
200 }
201 }
202
203 // ---------------------------------------------------------------------------
204
205 export {
206 checkDirectoryIsEmpty,
207 checkTmpIsEmpty,
208 ServerInfo,
209 flushAndRunMultipleServers,
210 flushTests,
211 runServer,
212 killallServers,
213 reRunServer,
214 waitUntilLog
215 }