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