]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/server/servers.ts
Use parallel tests
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / servers.ts
CommitLineData
89231874
C
1/* tslint:disable:no-unused-expression */
2
0e1dc3e7
C
3import { ChildProcess, exec, fork } from 'child_process'
4import { join } from 'path'
792e5b8e 5import { root, wait } from '../miscs/miscs'
89231874
C
6import { readdir, readFile } from 'fs-extra'
7import { existsSync } from 'fs'
8import { expect } from 'chai'
df0b219d 9import { VideoChannel } from '../../models/videos'
0e1dc3e7
C
10
11interface ServerInfo {
12 app: ChildProcess,
13 url: string
14 host: string
86ebdf8c
C
15
16 port: number
17 parallel: boolean
18 internalServerNumber: number
fdbda9e3 19 serverNumber: number
0e1dc3e7
C
20
21 client: {
22 id: string,
23 secret: string
24 }
25
26 user: {
27 username: string,
28 password: string,
29 email?: string
30 }
31
32 accessToken?: string
df0b219d 33 videoChannel?: VideoChannel
0e1dc3e7
C
34
35 video?: {
36 id: number
37 uuid: string
d8755eed 38 name: string
b64c950a
C
39 account: {
40 name: string
41 }
0e1dc3e7
C
42 }
43
44 remoteVideo?: {
45 id: number
46 uuid: string
47 }
df0b219d
C
48
49 videos?: { id: number, uuid: string }[]
0e1dc3e7
C
50}
51
b36f41ca 52function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
0e1dc3e7
C
53 let apps = []
54 let i = 0
55
56 return new Promise<ServerInfo[]>(res => {
57 function anotherServerDone (serverNumber, app) {
58 apps[serverNumber - 1] = app
59 i++
60 if (i === totalServers) {
61 return res(apps)
62 }
63 }
64
210feb6c
C
65 for (let j = 1; j <= totalServers; j++) {
66 flushAndRunServer(j, configOverride).then(app => anotherServerDone(j, app))
67 }
0e1dc3e7
C
68 })
69}
70
210feb6c 71function flushTests (serverNumber?: number) {
0e1dc3e7 72 return new Promise<void>((res, rej) => {
210feb6c
C
73 const suffix = serverNumber ? ` -- ${serverNumber}` : ''
74
75 return exec('npm run clean:server:test' + suffix, err => {
0e1dc3e7
C
76 if (err) return rej(err)
77
78 return res()
79 })
80 })
81}
82
86ebdf8c
C
83function randomServer () {
84 const low = 10
85 const high = 10000
86
87 return Math.floor(Math.random() * (high - low) + low)
88}
89
90function flushAndRunServer (serverNumber: number, configOverrideArg?: Object, args = []) {
91 const parallel = process.env.MOCHA_PARALLEL === 'true'
92
93 const internalServerNumber = parallel ? randomServer() : serverNumber
94 const port = 9000 + internalServerNumber
95
0e1dc3e7
C
96 const server: ServerInfo = {
97 app: null,
86ebdf8c
C
98 port,
99 internalServerNumber,
100 parallel,
101 serverNumber: internalServerNumber,
102 url: `http://localhost:${port}`,
103 host: `localhost:${port}`,
0e1dc3e7
C
104 client: {
105 id: null,
106 secret: null
107 },
108 user: {
109 username: null,
110 password: null
111 }
112 }
113
114 // These actions are async so we need to be sure that they have both been done
115 const serverRunString = {
bf6e8e3e 116 'Server listening': false
0e1dc3e7 117 }
86ebdf8c 118 const key = 'Database peertube_test' + internalServerNumber + ' is ready'
0e1dc3e7
C
119 serverRunString[key] = false
120
121 const regexps = {
122 client_id: 'Client id: (.+)',
123 client_secret: 'Client secret: (.+)',
124 user_username: 'Username: (.+)',
125 user_password: 'User password: (.+)'
126 }
127
128 // Share the environment
129 const env = Object.create(process.env)
130 env['NODE_ENV'] = 'test'
131 env['NODE_APP_INSTANCE'] = serverNumber.toString()
fdbda9e3 132
86ebdf8c
C
133 let configOverride: any = {}
134
135 if (parallel) {
136 configOverride = {
137 listen: {
138 port: port
139 },
140 webserver: {
141 port: port
142 },
143 database: {
144 suffix: '_test' + internalServerNumber
145 },
146 storage: {
147 tmp: `test${internalServerNumber}/tmp/`,
148 avatars: `test${internalServerNumber}/avatars/`,
149 videos: `test${internalServerNumber}/videos/`,
150 streaming_playlists: `test${internalServerNumber}/streaming-playlists/`,
151 redundancy: `test${internalServerNumber}/redundancy/`,
152 logs: `test${internalServerNumber}/logs/`,
153 previews: `test${internalServerNumber}/previews/`,
154 thumbnails: `test${internalServerNumber}/thumbnails/`,
155 torrents: `test${internalServerNumber}/torrents/`,
156 captions: `test${internalServerNumber}/captions/`,
157 cache: `test${internalServerNumber}/cache/`
158 },
159 admin: {
160 email: `admin${internalServerNumber}@example.com`
161 }
162 }
163 }
164
165 if (configOverrideArg !== undefined) {
166 Object.assign(configOverride, configOverrideArg)
fdbda9e3
C
167 }
168
86ebdf8c
C
169 env['NODE_CONFIG'] = JSON.stringify(configOverride)
170
0e1dc3e7
C
171 const options = {
172 silent: true,
173 env: env,
174 detached: true
175 }
176
177 return new Promise<ServerInfo>(res => {
86ebdf8c 178 flushTests(internalServerNumber)
210feb6c 179 .then(() => {
5abb9fbb 180
210feb6c
C
181 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
182 server.app.stdout.on('data', function onStdout (data) {
183 let dontContinue = false
184
185 // Capture things if we want to
186 for (const key of Object.keys(regexps)) {
187 const regexp = regexps[ key ]
188 const matches = data.toString().match(regexp)
189 if (matches !== null) {
190 if (key === 'client_id') server.client.id = matches[ 1 ]
191 else if (key === 'client_secret') server.client.secret = matches[ 1 ]
192 else if (key === 'user_username') server.user.username = matches[ 1 ]
193 else if (key === 'user_password') server.user.password = matches[ 1 ]
194 }
195 }
196
197 // Check if all required sentences are here
198 for (const key of Object.keys(serverRunString)) {
199 if (data.toString().indexOf(key) !== -1) serverRunString[ key ] = true
200 if (serverRunString[ key ] === false) dontContinue = true
201 }
202
203 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
204 if (dontContinue === true) return
205
206 server.app.stdout.removeListener('data', onStdout)
207
208 process.on('exit', () => {
209 try {
210 process.kill(server.app.pid)
86ebdf8c 211 } catch { /* empty */ }
210feb6c
C
212 })
213
214 res(server)
215 })
dc094603 216 })
0e1dc3e7
C
217 })
218}
219
e5565833 220async function reRunServer (server: ServerInfo, configOverride?: any) {
210feb6c 221 const newServer = await flushAndRunServer(server.serverNumber, configOverride)
7bc29171
C
222 server.app = newServer.app
223
224 return server
225}
226
89231874 227async function checkTmpIsEmpty (server: ServerInfo) {
09209296
C
228 return checkDirectoryIsEmpty(server, 'tmp')
229}
230
231async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) {
89231874
C
232 const testDirectory = 'test' + server.serverNumber
233
09209296 234 const directoryPath = join(root(), testDirectory, directory)
89231874
C
235
236 const directoryExists = existsSync(directoryPath)
237 expect(directoryExists).to.be.true
238
239 const files = await readdir(directoryPath)
240 expect(files).to.have.lengthOf(0)
241}
242
0e1dc3e7
C
243function killallServers (servers: ServerInfo[]) {
244 for (const server of servers) {
245 process.kill(-server.app.pid)
246 }
247}
248
86ebdf8c
C
249function cleanupTests (servers: ServerInfo[]) {
250 killallServers(servers)
251
252 const p: Promise<any>[] = []
253 for (const server of servers) {
254 if (server.parallel) {
255 p.push(flushTests(server.internalServerNumber))
256 }
257 }
258
259 return Promise.all(p)
260}
261
792e5b8e
C
262async function waitUntilLog (server: ServerInfo, str: string, count = 1) {
263 const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log')
264
265 while (true) {
266 const buf = await readFile(logfile)
267
268 const matches = buf.toString().match(new RegExp(str, 'g'))
269 if (matches && matches.length === count) return
270
271 await wait(1000)
272 }
273}
274
0e1dc3e7
C
275// ---------------------------------------------------------------------------
276
277export {
09209296 278 checkDirectoryIsEmpty,
89231874 279 checkTmpIsEmpty,
0e1dc3e7 280 ServerInfo,
86ebdf8c 281 cleanupTests,
0e1dc3e7
C
282 flushAndRunMultipleServers,
283 flushTests,
210feb6c 284 flushAndRunServer,
7bc29171 285 killallServers,
792e5b8e
C
286 reRunServer,
287 waitUntilLog
0e1dc3e7 288}