aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server/servers.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-13 09:43:59 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:18 +0200
commit6c5065a011b099618681a37bd77eaa7bd3db752e (patch)
tree352252a00b25013c4b1902f6bcd9668aba295c7b /shared/extra-utils/server/servers.ts
parent0d8ecb7592577f54012413a2b5a9b159cfc90399 (diff)
downloadPeerTube-6c5065a011b099618681a37bd77eaa7bd3db752e.tar.gz
PeerTube-6c5065a011b099618681a37bd77eaa7bd3db752e.tar.zst
PeerTube-6c5065a011b099618681a37bd77eaa7bd3db752e.zip
Introduce server commands
Diffstat (limited to 'shared/extra-utils/server/servers.ts')
-rw-r--r--shared/extra-utils/server/servers.ts108
1 files changed, 11 insertions, 97 deletions
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index e0e49d2c4..f5dc0326f 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -1,9 +1,9 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
2 2
3import { expect } from 'chai' 3import { ChildProcess, fork } from 'child_process'
4import { ChildProcess, exec, fork } from 'child_process' 4import { copy, ensureDir } from 'fs-extra'
5import { copy, ensureDir, pathExists, readdir, readFile, remove } from 'fs-extra'
6import { join } from 'path' 5import { join } from 'path'
6import { root } from '@server/helpers/core-utils'
7import { randomInt } from '../../core-utils/miscs/miscs' 7import { randomInt } from '../../core-utils/miscs/miscs'
8import { VideoChannel } from '../../models/videos' 8import { VideoChannel } from '../../models/videos'
9import { BulkCommand } from '../bulk' 9import { BulkCommand } from '../bulk'
@@ -11,11 +11,9 @@ import { CLICommand } from '../cli'
11import { CustomPagesCommand } from '../custom-pages' 11import { CustomPagesCommand } from '../custom-pages'
12import { FeedCommand } from '../feeds' 12import { FeedCommand } from '../feeds'
13import { LogsCommand } from '../logs' 13import { LogsCommand } from '../logs'
14import { SQLCommand } from '../miscs' 14import { isGithubCI, parallelTests, SQLCommand } from '../miscs'
15import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs'
16import { AbusesCommand } from '../moderation' 15import { AbusesCommand } from '../moderation'
17import { OverviewsCommand } from '../overviews' 16import { OverviewsCommand } from '../overviews'
18import { makeGetRequest } from '../requests/requests'
19import { SearchCommand } from '../search' 17import { SearchCommand } from '../search'
20import { SocketIOCommand } from '../socket' 18import { SocketIOCommand } from '../socket'
21import { AccountsCommand, BlocklistCommand, NotificationsCommand, SubscriptionsCommand } from '../users' 19import { AccountsCommand, BlocklistCommand, NotificationsCommand, SubscriptionsCommand } from '../users'
@@ -39,6 +37,7 @@ import { FollowsCommand } from './follows-command'
39import { JobsCommand } from './jobs-command' 37import { JobsCommand } from './jobs-command'
40import { PluginsCommand } from './plugins-command' 38import { PluginsCommand } from './plugins-command'
41import { RedundancyCommand } from './redundancy-command' 39import { RedundancyCommand } from './redundancy-command'
40import { ServersCommand } from './servers-command'
42import { StatsCommand } from './stats-command' 41import { StatsCommand } from './stats-command'
43 42
44interface ServerInfo { 43interface ServerInfo {
@@ -126,10 +125,7 @@ interface ServerInfo {
126 commentsCommand?: CommentsCommand 125 commentsCommand?: CommentsCommand
127 sqlCommand?: SQLCommand 126 sqlCommand?: SQLCommand
128 notificationsCommand?: NotificationsCommand 127 notificationsCommand?: NotificationsCommand
129} 128 serversCommand?: ServersCommand
130
131function parallelTests () {
132 return process.env.MOCHA_PARALLEL === 'true'
133} 129}
134 130
135function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) { 131function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
@@ -151,18 +147,6 @@ function flushAndRunMultipleServers (totalServers: number, configOverride?: Obje
151 }) 147 })
152} 148}
153 149
154function flushTests (serverNumber?: number) {
155 return new Promise<void>((res, rej) => {
156 const suffix = serverNumber ? ` -- ${serverNumber}` : ''
157
158 return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => {
159 if (err || stderr) return rej(err || new Error(stderr))
160
161 return res()
162 })
163 })
164}
165
166function randomServer () { 150function randomServer () {
167 const low = 10 151 const low = 10
168 const high = 10000 152 const high = 10000
@@ -189,7 +173,7 @@ async function flushAndRunServer (serverNumber: number, configOverride?: Object,
189 const rtmpPort = parallel ? randomRTMP() : 1936 173 const rtmpPort = parallel ? randomRTMP() : 1936
190 const port = 9000 + internalServerNumber 174 const port = 9000 + internalServerNumber
191 175
192 await flushTests(internalServerNumber) 176 await ServersCommand.flushTests(internalServerNumber)
193 177
194 const server: ServerInfo = { 178 const server: ServerInfo = {
195 app: null, 179 app: null,
@@ -372,6 +356,7 @@ function assignCommands (server: ServerInfo) {
372 server.commentsCommand = new CommentsCommand(server) 356 server.commentsCommand = new CommentsCommand(server)
373 server.sqlCommand = new SQLCommand(server) 357 server.sqlCommand = new SQLCommand(server)
374 server.notificationsCommand = new NotificationsCommand(server) 358 server.notificationsCommand = new NotificationsCommand(server)
359 server.serversCommand = new ServersCommand(server)
375} 360}
376 361
377async function reRunServer (server: ServerInfo, configOverride?: any) { 362async function reRunServer (server: ServerInfo, configOverride?: any) {
@@ -381,28 +366,6 @@ async function reRunServer (server: ServerInfo, configOverride?: any) {
381 return server 366 return server
382} 367}
383 368
384async function checkTmpIsEmpty (server: ServerInfo) {
385 await checkDirectoryIsEmpty(server, 'tmp', [ 'plugins-global.css', 'hls', 'resumable-uploads' ])
386
387 if (await pathExists(join('test' + server.internalServerNumber, 'tmp', 'hls'))) {
388 await checkDirectoryIsEmpty(server, 'tmp/hls')
389 }
390}
391
392async function checkDirectoryIsEmpty (server: ServerInfo, directory: string, exceptions: string[] = []) {
393 const testDirectory = 'test' + server.internalServerNumber
394
395 const directoryPath = join(root(), testDirectory, directory)
396
397 const directoryExists = await pathExists(directoryPath)
398 expect(directoryExists).to.be.true
399
400 const files = await readdir(directoryPath)
401 const filtered = files.filter(f => exceptions.includes(f) === false)
402
403 expect(filtered).to.have.lengthOf(0)
404}
405
406async function killallServers (servers: ServerInfo[]) { 369async function killallServers (servers: ServerInfo[]) {
407 for (const server of servers) { 370 for (const server of servers) {
408 if (!server.app) continue 371 if (!server.app) continue
@@ -422,71 +385,22 @@ async function cleanupTests (servers: ServerInfo[]) {
422 await ensureDir('artifacts') 385 await ensureDir('artifacts')
423 } 386 }
424 387
425 const p: Promise<any>[] = [] 388 let p: Promise<any>[] = []
426 for (const server of servers) { 389 for (const server of servers) {
427 if (isGithubCI()) { 390 p = p.concat(server.serversCommand.cleanupTests())
428 const origin = await buildServerDirectory(server, 'logs/peertube.log')
429 const destname = `peertube-${server.internalServerNumber}.log`
430 console.log('Saving logs %s.', destname)
431
432 await copy(origin, join('artifacts', destname))
433 }
434
435 if (server.parallel) {
436 p.push(flushTests(server.internalServerNumber))
437 }
438
439 if (server.customConfigFile) {
440 p.push(remove(server.customConfigFile))
441 }
442 } 391 }
443 392
444 return Promise.all(p) 393 return Promise.all(p)
445} 394}
446 395
447async function waitUntilLog (server: ServerInfo, str: string, count = 1, strictCount = true) {
448 const logfile = buildServerDirectory(server, 'logs/peertube.log')
449
450 while (true) {
451 const buf = await readFile(logfile)
452
453 const matches = buf.toString().match(new RegExp(str, 'g'))
454 if (matches && matches.length === count) return
455 if (matches && strictCount === false && matches.length >= count) return
456
457 await wait(1000)
458 }
459}
460
461async function getServerFileSize (server: ServerInfo, subPath: string) {
462 const path = buildServerDirectory(server, subPath)
463
464 return getFileSize(path)
465}
466
467function makePingRequest (server: ServerInfo) {
468 return makeGetRequest({
469 url: server.url,
470 path: '/api/v1/ping',
471 statusCodeExpected: 200
472 })
473}
474
475// --------------------------------------------------------------------------- 396// ---------------------------------------------------------------------------
476 397
477export { 398export {
478 checkDirectoryIsEmpty,
479 checkTmpIsEmpty,
480 getServerFileSize,
481 ServerInfo, 399 ServerInfo,
482 parallelTests,
483 cleanupTests, 400 cleanupTests,
484 flushAndRunMultipleServers, 401 flushAndRunMultipleServers,
485 flushTests,
486 makePingRequest,
487 flushAndRunServer, 402 flushAndRunServer,
488 killallServers, 403 killallServers,
489 reRunServer, 404 reRunServer,
490 assignCommands, 405 assignCommands
491 waitUntilLog
492} 406}