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