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