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