1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { ChildProcess, fork } from 'child_process'
4 import { copy, ensureDir } from 'fs-extra'
5 import { join } from 'path'
6 import { root } from '@server/helpers/core-utils'
7 import { randomInt } from '../../core-utils/miscs/miscs'
8 import { VideoChannel } from '../../models/videos'
9 import { BulkCommand } from '../bulk'
10 import { CLICommand } from '../cli'
11 import { CustomPagesCommand } from '../custom-pages'
12 import { FeedCommand } from '../feeds'
13 import { LogsCommand } from '../logs'
14 import { isGithubCI, parallelTests, SQLCommand } from '../miscs'
15 import { AbusesCommand } from '../moderation'
16 import { OverviewsCommand } from '../overviews'
17 import { SearchCommand } from '../search'
18 import { SocketIOCommand } from '../socket'
19 import { AccountsCommand, BlocklistCommand, LoginCommand, NotificationsCommand, SubscriptionsCommand, UsersCommand } from '../users'
23 ChangeOwnershipCommand,
30 StreamingPlaylistsCommand,
33 import { CommentsCommand } from '../videos/comments-command'
34 import { ConfigCommand } from './config-command'
35 import { ContactFormCommand } from './contact-form-command'
36 import { DebugCommand } from './debug-command'
37 import { FollowsCommand } from './follows-command'
38 import { JobsCommand } from './jobs-command'
39 import { PluginsCommand } from './plugins-command'
40 import { RedundancyCommand } from './redundancy-command'
41 import { ServersCommand } from './servers-command'
42 import { StatsCommand } from './stats-command'
44 interface ServerInfo {
55 internalServerNumber: number
58 customConfigFile?: string
72 channel?: VideoChannel
88 videos?: { id: number, uuid: string }[]
96 customPage?: CustomPagesCommand
99 abuses?: AbusesCommand
100 overviews?: OverviewsCommand
101 search?: SearchCommand
102 contactForm?: ContactFormCommand
104 follows?: FollowsCommand
106 plugins?: PluginsCommand
107 redundancy?: RedundancyCommand
109 config?: ConfigCommand
110 socketIO?: SocketIOCommand
111 accounts?: AccountsCommand
112 blocklist?: BlocklistCommand
113 subscriptions?: SubscriptionsCommand
115 services?: ServicesCommand
116 blacklist?: BlacklistCommand
117 captions?: CaptionsCommand
118 changeOwnership?: ChangeOwnershipCommand
119 playlists?: PlaylistsCommand
120 history?: HistoryCommand
121 imports?: ImportsCommand
122 streamingPlaylists?: StreamingPlaylistsCommand
123 channels?: ChannelsCommand
124 comments?: CommentsCommand
126 notifications?: NotificationsCommand
127 servers?: ServersCommand
130 videos?: VideosCommand
133 function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
137 return new Promise<ServerInfo[]>(res => {
138 function anotherServerDone (serverNumber, app) {
139 apps[serverNumber - 1] = app
141 if (i === totalServers) {
146 for (let j = 1; j <= totalServers; j++) {
147 flushAndRunServer(j, configOverride).then(app => anotherServerDone(j, app))
152 function randomServer () {
156 return randomInt(low, high)
159 function randomRTMP () {
163 return randomInt(low, high)
166 type RunServerOptions = {
171 async function flushAndRunServer (serverNumber: number, configOverride?: Object, args = [], options: RunServerOptions = {}) {
172 const parallel = parallelTests()
174 const internalServerNumber = parallel ? randomServer() : serverNumber
175 const rtmpPort = parallel ? randomRTMP() : 1936
176 const port = 9000 + internalServerNumber
178 await ServersCommand.flushTests(internalServerNumber)
180 const server: ServerInfo = {
183 internalServerNumber,
187 url: `http://localhost:${port}`,
188 host: `localhost:${port}`,
189 hostname: 'localhost',
202 return runServer(server, configOverride, args, options)
205 async function runServer (server: ServerInfo, configOverrideArg?: any, args = [], options: RunServerOptions = {}) {
206 // These actions are async so we need to be sure that they have both been done
207 const serverRunString = {
208 'HTTP server listening': false
210 const key = 'Database peertube_test' + server.internalServerNumber + ' is ready'
211 serverRunString[key] = false
214 client_id: 'Client id: (.+)',
215 client_secret: 'Client secret: (.+)',
216 user_username: 'Username: (.+)',
217 user_password: 'User password: (.+)'
220 if (server.internalServerNumber !== server.serverNumber) {
221 const basePath = join(root(), 'config')
223 const tmpConfigFile = join(basePath, `test-${server.internalServerNumber}.yaml`)
224 await copy(join(basePath, `test-${server.serverNumber}.yaml`), tmpConfigFile)
226 server.customConfigFile = tmpConfigFile
229 const configOverride: any = {}
231 if (server.parallel) {
232 Object.assign(configOverride, {
240 suffix: '_test' + server.internalServerNumber
243 tmp: `test${server.internalServerNumber}/tmp/`,
244 avatars: `test${server.internalServerNumber}/avatars/`,
245 videos: `test${server.internalServerNumber}/videos/`,
246 streaming_playlists: `test${server.internalServerNumber}/streaming-playlists/`,
247 redundancy: `test${server.internalServerNumber}/redundancy/`,
248 logs: `test${server.internalServerNumber}/logs/`,
249 previews: `test${server.internalServerNumber}/previews/`,
250 thumbnails: `test${server.internalServerNumber}/thumbnails/`,
251 torrents: `test${server.internalServerNumber}/torrents/`,
252 captions: `test${server.internalServerNumber}/captions/`,
253 cache: `test${server.internalServerNumber}/cache/`,
254 plugins: `test${server.internalServerNumber}/plugins/`
257 email: `admin${server.internalServerNumber}@example.com`
261 port: server.rtmpPort
267 if (configOverrideArg !== undefined) {
268 Object.assign(configOverride, configOverrideArg)
271 // Share the environment
272 const env = Object.create(process.env)
273 env['NODE_ENV'] = 'test'
274 env['NODE_APP_INSTANCE'] = server.internalServerNumber.toString()
275 env['NODE_CONFIG'] = JSON.stringify(configOverride)
277 const forkOptions = {
281 execArgv: options.execArgv || []
284 return new Promise<ServerInfo>(res => {
285 server.app = fork(join(root(), 'dist', 'server.js'), args, forkOptions)
286 server.app.stdout.on('data', function onStdout (data) {
287 let dontContinue = false
289 // Capture things if we want to
290 for (const key of Object.keys(regexps)) {
291 const regexp = regexps[key]
292 const matches = data.toString().match(regexp)
293 if (matches !== null) {
294 if (key === 'client_id') server.store.client.id = matches[1]
295 else if (key === 'client_secret') server.store.client.secret = matches[1]
296 else if (key === 'user_username') server.store.user.username = matches[1]
297 else if (key === 'user_password') server.store.user.password = matches[1]
301 // Check if all required sentences are here
302 for (const key of Object.keys(serverRunString)) {
303 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
304 if (serverRunString[key] === false) dontContinue = true
307 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
308 if (dontContinue === true) return
310 if (options.hideLogs === false) {
311 console.log(data.toString())
313 server.app.stdout.removeListener('data', onStdout)
316 process.on('exit', () => {
318 process.kill(server.app.pid)
319 } catch { /* empty */ }
322 assignCommands(server)
329 function assignCommands (server: ServerInfo) {
330 server.bulk = new BulkCommand(server)
331 server.cli = new CLICommand(server)
332 server.customPage = new CustomPagesCommand(server)
333 server.feed = new FeedCommand(server)
334 server.logs = new LogsCommand(server)
335 server.abuses = new AbusesCommand(server)
336 server.overviews = new OverviewsCommand(server)
337 server.search = new SearchCommand(server)
338 server.contactForm = new ContactFormCommand(server)
339 server.debug = new DebugCommand(server)
340 server.follows = new FollowsCommand(server)
341 server.jobs = new JobsCommand(server)
342 server.plugins = new PluginsCommand(server)
343 server.redundancy = new RedundancyCommand(server)
344 server.stats = new StatsCommand(server)
345 server.config = new ConfigCommand(server)
346 server.socketIO = new SocketIOCommand(server)
347 server.accounts = new AccountsCommand(server)
348 server.blocklist = new BlocklistCommand(server)
349 server.subscriptions = new SubscriptionsCommand(server)
350 server.live = new LiveCommand(server)
351 server.services = new ServicesCommand(server)
352 server.blacklist = new BlacklistCommand(server)
353 server.captions = new CaptionsCommand(server)
354 server.changeOwnership = new ChangeOwnershipCommand(server)
355 server.playlists = new PlaylistsCommand(server)
356 server.history = new HistoryCommand(server)
357 server.imports = new ImportsCommand(server)
358 server.streamingPlaylists = new StreamingPlaylistsCommand(server)
359 server.channels = new ChannelsCommand(server)
360 server.comments = new CommentsCommand(server)
361 server.sql = new SQLCommand(server)
362 server.notifications = new NotificationsCommand(server)
363 server.servers = new ServersCommand(server)
364 server.login = new LoginCommand(server)
365 server.users = new UsersCommand(server)
366 server.videos = new VideosCommand(server)
369 async function reRunServer (server: ServerInfo, configOverride?: any) {
370 const newServer = await runServer(server, configOverride)
371 server.app = newServer.app
376 async function killallServers (servers: ServerInfo[]) {
377 for (const server of servers) {
378 if (!server.app) continue
380 await server.sql.cleanup()
382 process.kill(-server.app.pid)
388 async function cleanupTests (servers: ServerInfo[]) {
389 await killallServers(servers)
392 await ensureDir('artifacts')
395 let p: Promise<any>[] = []
396 for (const server of servers) {
397 p = p.concat(server.servers.cleanupTests())
400 return Promise.all(p)
403 // ---------------------------------------------------------------------------
408 flushAndRunMultipleServers,