]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ |
89231874 | 2 | |
c655c9ef | 3 | import { expect } from 'chai' |
0e1dc3e7 | 4 | import { ChildProcess, exec, fork } from 'child_process' |
8d2be0ed | 5 | import { copy, pathExists, readdir, readFile, remove } from 'fs-extra' |
c655c9ef | 6 | import { join } from 'path' |
7c3b7976 | 7 | import { randomInt } from '../../core-utils/miscs/miscs' |
c655c9ef | 8 | import { VideoChannel } from '../../models/videos' |
ca5c612b | 9 | import { buildServerDirectory, getFileSize, root, wait } from '../miscs/miscs' |
0e1dc3e7 C |
10 | |
11 | interface ServerInfo { | |
a1587156 | 12 | app: ChildProcess |
af4ae64f | 13 | |
0e1dc3e7 C |
14 | url: string |
15 | host: string | |
af4ae64f | 16 | hostname: string |
86ebdf8c | 17 | port: number |
af4ae64f | 18 | |
c655c9ef C |
19 | rtmpPort: number |
20 | ||
86ebdf8c C |
21 | parallel: boolean |
22 | internalServerNumber: number | |
fdbda9e3 | 23 | serverNumber: number |
0e1dc3e7 C |
24 | |
25 | client: { | |
a1587156 | 26 | id: string |
0e1dc3e7 C |
27 | secret: string |
28 | } | |
29 | ||
30 | user: { | |
a1587156 C |
31 | username: string |
32 | password: string | |
0e1dc3e7 C |
33 | email?: string |
34 | } | |
35 | ||
7c3b7976 C |
36 | customConfigFile?: string |
37 | ||
0e1dc3e7 | 38 | accessToken?: string |
df0b219d | 39 | videoChannel?: VideoChannel |
0e1dc3e7 C |
40 | |
41 | video?: { | |
42 | id: number | |
43 | uuid: string | |
310b5219 | 44 | name?: string |
a59f210f | 45 | url?: string |
310b5219 | 46 | account?: { |
b64c950a C |
47 | name: string |
48 | } | |
0e1dc3e7 C |
49 | } |
50 | ||
51 | remoteVideo?: { | |
52 | id: number | |
53 | uuid: string | |
54 | } | |
df0b219d C |
55 | |
56 | videos?: { id: number, uuid: string }[] | |
0e1dc3e7 C |
57 | } |
58 | ||
7c3b7976 C |
59 | function parallelTests () { |
60 | return process.env.MOCHA_PARALLEL === 'true' | |
61 | } | |
62 | ||
b36f41ca | 63 | function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) { |
a1587156 | 64 | const apps = [] |
0e1dc3e7 C |
65 | let i = 0 |
66 | ||
67 | return new Promise<ServerInfo[]>(res => { | |
68 | function anotherServerDone (serverNumber, app) { | |
69 | apps[serverNumber - 1] = app | |
70 | i++ | |
71 | if (i === totalServers) { | |
72 | return res(apps) | |
73 | } | |
74 | } | |
75 | ||
210feb6c C |
76 | for (let j = 1; j <= totalServers; j++) { |
77 | flushAndRunServer(j, configOverride).then(app => anotherServerDone(j, app)) | |
78 | } | |
0e1dc3e7 C |
79 | }) |
80 | } | |
81 | ||
210feb6c | 82 | function flushTests (serverNumber?: number) { |
0e1dc3e7 | 83 | return new Promise<void>((res, rej) => { |
210feb6c C |
84 | const suffix = serverNumber ? ` -- ${serverNumber}` : '' |
85 | ||
2284f202 C |
86 | return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => { |
87 | if (err || stderr) return rej(err || new Error(stderr)) | |
0e1dc3e7 C |
88 | |
89 | return res() | |
90 | }) | |
91 | }) | |
92 | } | |
93 | ||
86ebdf8c C |
94 | function randomServer () { |
95 | const low = 10 | |
96 | const high = 10000 | |
97 | ||
7c3b7976 | 98 | return randomInt(low, high) |
86ebdf8c C |
99 | } |
100 | ||
c655c9ef C |
101 | function randomRTMP () { |
102 | const low = 1900 | |
103 | const high = 2100 | |
104 | ||
105 | return randomInt(low, high) | |
106 | } | |
107 | ||
bd2e2f11 C |
108 | type RunServerOptions = { |
109 | hideLogs?: boolean | |
110 | execArgv?: string[] | |
111 | } | |
112 | ||
113 | async function flushAndRunServer (serverNumber: number, configOverride?: Object, args = [], options: RunServerOptions = {}) { | |
7c3b7976 | 114 | const parallel = parallelTests() |
86ebdf8c C |
115 | |
116 | const internalServerNumber = parallel ? randomServer() : serverNumber | |
3e8584b9 | 117 | const rtmpPort = parallel ? randomRTMP() : 1936 |
86ebdf8c C |
118 | const port = 9000 + internalServerNumber |
119 | ||
7c3b7976 | 120 | await flushTests(internalServerNumber) |
42e1ec25 | 121 | |
0e1dc3e7 C |
122 | const server: ServerInfo = { |
123 | app: null, | |
86ebdf8c C |
124 | port, |
125 | internalServerNumber, | |
c655c9ef | 126 | rtmpPort, |
86ebdf8c | 127 | parallel, |
7c3b7976 | 128 | serverNumber, |
86ebdf8c C |
129 | url: `http://localhost:${port}`, |
130 | host: `localhost:${port}`, | |
af4ae64f | 131 | hostname: 'localhost', |
0e1dc3e7 C |
132 | client: { |
133 | id: null, | |
134 | secret: null | |
135 | }, | |
136 | user: { | |
137 | username: null, | |
138 | password: null | |
139 | } | |
140 | } | |
141 | ||
bd2e2f11 | 142 | return runServer(server, configOverride, args, options) |
913b1d71 C |
143 | } |
144 | ||
bd2e2f11 | 145 | async function runServer (server: ServerInfo, configOverrideArg?: any, args = [], options: RunServerOptions = {}) { |
0e1dc3e7 C |
146 | // These actions are async so we need to be sure that they have both been done |
147 | const serverRunString = { | |
bf6e8e3e | 148 | 'Server listening': false |
0e1dc3e7 | 149 | } |
913b1d71 | 150 | const key = 'Database peertube_test' + server.internalServerNumber + ' is ready' |
0e1dc3e7 C |
151 | serverRunString[key] = false |
152 | ||
153 | const regexps = { | |
154 | client_id: 'Client id: (.+)', | |
155 | client_secret: 'Client secret: (.+)', | |
156 | user_username: 'Username: (.+)', | |
157 | user_password: 'User password: (.+)' | |
158 | } | |
159 | ||
7c3b7976 C |
160 | if (server.internalServerNumber !== server.serverNumber) { |
161 | const basePath = join(root(), 'config') | |
162 | ||
163 | const tmpConfigFile = join(basePath, `test-${server.internalServerNumber}.yaml`) | |
164 | await copy(join(basePath, `test-${server.serverNumber}.yaml`), tmpConfigFile) | |
165 | ||
166 | server.customConfigFile = tmpConfigFile | |
167 | } | |
fdbda9e3 | 168 | |
7c3b7976 | 169 | const configOverride: any = {} |
86ebdf8c | 170 | |
913b1d71 | 171 | if (server.parallel) { |
7c3b7976 | 172 | Object.assign(configOverride, { |
86ebdf8c | 173 | listen: { |
913b1d71 | 174 | port: server.port |
86ebdf8c C |
175 | }, |
176 | webserver: { | |
913b1d71 | 177 | port: server.port |
86ebdf8c C |
178 | }, |
179 | database: { | |
913b1d71 | 180 | suffix: '_test' + server.internalServerNumber |
86ebdf8c C |
181 | }, |
182 | storage: { | |
913b1d71 C |
183 | tmp: `test${server.internalServerNumber}/tmp/`, |
184 | avatars: `test${server.internalServerNumber}/avatars/`, | |
185 | videos: `test${server.internalServerNumber}/videos/`, | |
186 | streaming_playlists: `test${server.internalServerNumber}/streaming-playlists/`, | |
187 | redundancy: `test${server.internalServerNumber}/redundancy/`, | |
188 | logs: `test${server.internalServerNumber}/logs/`, | |
189 | previews: `test${server.internalServerNumber}/previews/`, | |
190 | thumbnails: `test${server.internalServerNumber}/thumbnails/`, | |
191 | torrents: `test${server.internalServerNumber}/torrents/`, | |
192 | captions: `test${server.internalServerNumber}/captions/`, | |
89cd1275 C |
193 | cache: `test${server.internalServerNumber}/cache/`, |
194 | plugins: `test${server.internalServerNumber}/plugins/` | |
86ebdf8c C |
195 | }, |
196 | admin: { | |
913b1d71 | 197 | email: `admin${server.internalServerNumber}@example.com` |
c655c9ef C |
198 | }, |
199 | live: { | |
200 | rtmp: { | |
201 | port: server.rtmpPort | |
202 | } | |
86ebdf8c | 203 | } |
7c3b7976 | 204 | }) |
86ebdf8c C |
205 | } |
206 | ||
207 | if (configOverrideArg !== undefined) { | |
208 | Object.assign(configOverride, configOverrideArg) | |
fdbda9e3 C |
209 | } |
210 | ||
7c3b7976 C |
211 | // Share the environment |
212 | const env = Object.create(process.env) | |
213 | env['NODE_ENV'] = 'test' | |
214 | env['NODE_APP_INSTANCE'] = server.internalServerNumber.toString() | |
86ebdf8c C |
215 | env['NODE_CONFIG'] = JSON.stringify(configOverride) |
216 | ||
bd2e2f11 | 217 | const forkOptions = { |
0e1dc3e7 | 218 | silent: true, |
7c3b7976 | 219 | env, |
bd2e2f11 C |
220 | detached: true, |
221 | execArgv: options.execArgv || [] | |
0e1dc3e7 C |
222 | } |
223 | ||
224 | return new Promise<ServerInfo>(res => { | |
bd2e2f11 | 225 | server.app = fork(join(root(), 'dist', 'server.js'), args, forkOptions) |
42e1ec25 C |
226 | server.app.stdout.on('data', function onStdout (data) { |
227 | let dontContinue = false | |
228 | ||
229 | // Capture things if we want to | |
230 | for (const key of Object.keys(regexps)) { | |
a1587156 | 231 | const regexp = regexps[key] |
42e1ec25 C |
232 | const matches = data.toString().match(regexp) |
233 | if (matches !== null) { | |
a1587156 C |
234 | if (key === 'client_id') server.client.id = matches[1] |
235 | else if (key === 'client_secret') server.client.secret = matches[1] | |
236 | else if (key === 'user_username') server.user.username = matches[1] | |
237 | else if (key === 'user_password') server.user.password = matches[1] | |
42e1ec25 C |
238 | } |
239 | } | |
240 | ||
241 | // Check if all required sentences are here | |
242 | for (const key of Object.keys(serverRunString)) { | |
a1587156 C |
243 | if (data.toString().indexOf(key) !== -1) serverRunString[key] = true |
244 | if (serverRunString[key] === false) dontContinue = true | |
42e1ec25 C |
245 | } |
246 | ||
247 | // If no, there is maybe one thing not already initialized (client/user credentials generation...) | |
248 | if (dontContinue === true) return | |
249 | ||
bd2e2f11 | 250 | if (options.hideLogs === false) { |
5a547f69 C |
251 | console.log(data.toString()) |
252 | } else { | |
253 | server.app.stdout.removeListener('data', onStdout) | |
254 | } | |
42e1ec25 C |
255 | |
256 | process.on('exit', () => { | |
257 | try { | |
258 | process.kill(server.app.pid) | |
259 | } catch { /* empty */ } | |
dc094603 | 260 | }) |
42e1ec25 C |
261 | |
262 | res(server) | |
263 | }) | |
0e1dc3e7 C |
264 | }) |
265 | } | |
266 | ||
e5565833 | 267 | async function reRunServer (server: ServerInfo, configOverride?: any) { |
913b1d71 | 268 | const newServer = await runServer(server, configOverride) |
7bc29171 C |
269 | server.app = newServer.app |
270 | ||
271 | return server | |
272 | } | |
273 | ||
8d2be0ed C |
274 | function checkTmpIsEmpty (server: ServerInfo) { |
275 | return checkDirectoryIsEmpty(server, 'tmp', [ 'plugins-global.css' ]) | |
09209296 C |
276 | } |
277 | ||
8d2be0ed | 278 | async function checkDirectoryIsEmpty (server: ServerInfo, directory: string, exceptions: string[] = []) { |
48f07b4a | 279 | const testDirectory = 'test' + server.internalServerNumber |
89231874 | 280 | |
09209296 | 281 | const directoryPath = join(root(), testDirectory, directory) |
89231874 | 282 | |
8d2be0ed | 283 | const directoryExists = await pathExists(directoryPath) |
89231874 C |
284 | expect(directoryExists).to.be.true |
285 | ||
286 | const files = await readdir(directoryPath) | |
8d2be0ed C |
287 | const filtered = files.filter(f => exceptions.includes(f) === false) |
288 | ||
289 | expect(filtered).to.have.lengthOf(0) | |
89231874 C |
290 | } |
291 | ||
0e1dc3e7 C |
292 | function killallServers (servers: ServerInfo[]) { |
293 | for (const server of servers) { | |
7c3b7976 C |
294 | if (!server.app) continue |
295 | ||
0e1dc3e7 | 296 | process.kill(-server.app.pid) |
7c3b7976 | 297 | server.app = null |
0e1dc3e7 C |
298 | } |
299 | } | |
300 | ||
86ebdf8c C |
301 | function cleanupTests (servers: ServerInfo[]) { |
302 | killallServers(servers) | |
303 | ||
304 | const p: Promise<any>[] = [] | |
305 | for (const server of servers) { | |
306 | if (server.parallel) { | |
307 | p.push(flushTests(server.internalServerNumber)) | |
308 | } | |
7c3b7976 C |
309 | |
310 | if (server.customConfigFile) { | |
311 | p.push(remove(server.customConfigFile)) | |
312 | } | |
86ebdf8c C |
313 | } |
314 | ||
315 | return Promise.all(p) | |
316 | } | |
317 | ||
1b05d82d | 318 | async function waitUntilLog (server: ServerInfo, str: string, count = 1, strictCount = true) { |
ca5c612b | 319 | const logfile = buildServerDirectory(server, 'logs/peertube.log') |
792e5b8e C |
320 | |
321 | while (true) { | |
322 | const buf = await readFile(logfile) | |
323 | ||
324 | const matches = buf.toString().match(new RegExp(str, 'g')) | |
325 | if (matches && matches.length === count) return | |
1b05d82d | 326 | if (matches && strictCount === false && matches.length >= count) return |
792e5b8e C |
327 | |
328 | await wait(1000) | |
329 | } | |
330 | } | |
331 | ||
d218e7de | 332 | async function getServerFileSize (server: ServerInfo, subPath: string) { |
ca5c612b | 333 | const path = buildServerDirectory(server, subPath) |
d218e7de C |
334 | |
335 | return getFileSize(path) | |
336 | } | |
337 | ||
0e1dc3e7 C |
338 | // --------------------------------------------------------------------------- |
339 | ||
340 | export { | |
09209296 | 341 | checkDirectoryIsEmpty, |
89231874 | 342 | checkTmpIsEmpty, |
d218e7de | 343 | getServerFileSize, |
0e1dc3e7 | 344 | ServerInfo, |
7c3b7976 | 345 | parallelTests, |
86ebdf8c | 346 | cleanupTests, |
0e1dc3e7 C |
347 | flushAndRunMultipleServers, |
348 | flushTests, | |
210feb6c | 349 | flushAndRunServer, |
7bc29171 | 350 | killallServers, |
792e5b8e C |
351 | reRunServer, |
352 | waitUntilLog | |
0e1dc3e7 | 353 | } |