1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { expect } from 'chai'
4 import { ChildProcess, exec, fork } from 'child_process'
5 import { copy, ensureDir, pathExists, readdir, readFile, remove } from 'fs-extra'
6 import { join } from 'path'
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 { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs'
13 import { makeGetRequest } from '../requests/requests'
15 interface ServerInfo {
26 internalServerNumber: number
40 customConfigFile?: string
44 videoChannel?: VideoChannel
65 videos?: { id: number, uuid: string }[]
67 bulkCommand?: BulkCommand
68 cliCommand?: CLICommand
69 customPageCommand?: CustomPagesCommand
72 function parallelTests () {
73 return process.env.MOCHA_PARALLEL === 'true'
76 function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
80 return new Promise<ServerInfo[]>(res => {
81 function anotherServerDone (serverNumber, app) {
82 apps[serverNumber - 1] = app
84 if (i === totalServers) {
89 for (let j = 1; j <= totalServers; j++) {
90 flushAndRunServer(j, configOverride).then(app => anotherServerDone(j, app))
95 function flushTests (serverNumber?: number) {
96 return new Promise<void>((res, rej) => {
97 const suffix = serverNumber ? ` -- ${serverNumber}` : ''
99 return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => {
100 if (err || stderr) return rej(err || new Error(stderr))
107 function randomServer () {
111 return randomInt(low, high)
114 function randomRTMP () {
118 return randomInt(low, high)
121 type RunServerOptions = {
126 async function flushAndRunServer (serverNumber: number, configOverride?: Object, args = [], options: RunServerOptions = {}) {
127 const parallel = parallelTests()
129 const internalServerNumber = parallel ? randomServer() : serverNumber
130 const rtmpPort = parallel ? randomRTMP() : 1936
131 const port = 9000 + internalServerNumber
133 await flushTests(internalServerNumber)
135 const server: ServerInfo = {
138 internalServerNumber,
142 url: `http://localhost:${port}`,
143 host: `localhost:${port}`,
144 hostname: 'localhost',
155 return runServer(server, configOverride, args, options)
158 async function runServer (server: ServerInfo, configOverrideArg?: any, args = [], options: RunServerOptions = {}) {
159 // These actions are async so we need to be sure that they have both been done
160 const serverRunString = {
161 'HTTP server listening': false
163 const key = 'Database peertube_test' + server.internalServerNumber + ' is ready'
164 serverRunString[key] = false
167 client_id: 'Client id: (.+)',
168 client_secret: 'Client secret: (.+)',
169 user_username: 'Username: (.+)',
170 user_password: 'User password: (.+)'
173 if (server.internalServerNumber !== server.serverNumber) {
174 const basePath = join(root(), 'config')
176 const tmpConfigFile = join(basePath, `test-${server.internalServerNumber}.yaml`)
177 await copy(join(basePath, `test-${server.serverNumber}.yaml`), tmpConfigFile)
179 server.customConfigFile = tmpConfigFile
182 const configOverride: any = {}
184 if (server.parallel) {
185 Object.assign(configOverride, {
193 suffix: '_test' + server.internalServerNumber
196 tmp: `test${server.internalServerNumber}/tmp/`,
197 avatars: `test${server.internalServerNumber}/avatars/`,
198 videos: `test${server.internalServerNumber}/videos/`,
199 streaming_playlists: `test${server.internalServerNumber}/streaming-playlists/`,
200 redundancy: `test${server.internalServerNumber}/redundancy/`,
201 logs: `test${server.internalServerNumber}/logs/`,
202 previews: `test${server.internalServerNumber}/previews/`,
203 thumbnails: `test${server.internalServerNumber}/thumbnails/`,
204 torrents: `test${server.internalServerNumber}/torrents/`,
205 captions: `test${server.internalServerNumber}/captions/`,
206 cache: `test${server.internalServerNumber}/cache/`,
207 plugins: `test${server.internalServerNumber}/plugins/`
210 email: `admin${server.internalServerNumber}@example.com`
214 port: server.rtmpPort
220 if (configOverrideArg !== undefined) {
221 Object.assign(configOverride, configOverrideArg)
224 // Share the environment
225 const env = Object.create(process.env)
226 env['NODE_ENV'] = 'test'
227 env['NODE_APP_INSTANCE'] = server.internalServerNumber.toString()
228 env['NODE_CONFIG'] = JSON.stringify(configOverride)
230 const forkOptions = {
234 execArgv: options.execArgv || []
237 return new Promise<ServerInfo>(res => {
238 server.app = fork(join(root(), 'dist', 'server.js'), args, forkOptions)
239 server.app.stdout.on('data', function onStdout (data) {
240 let dontContinue = false
242 // Capture things if we want to
243 for (const key of Object.keys(regexps)) {
244 const regexp = regexps[key]
245 const matches = data.toString().match(regexp)
246 if (matches !== null) {
247 if (key === 'client_id') server.client.id = matches[1]
248 else if (key === 'client_secret') server.client.secret = matches[1]
249 else if (key === 'user_username') server.user.username = matches[1]
250 else if (key === 'user_password') server.user.password = matches[1]
254 // Check if all required sentences are here
255 for (const key of Object.keys(serverRunString)) {
256 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
257 if (serverRunString[key] === false) dontContinue = true
260 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
261 if (dontContinue === true) return
263 if (options.hideLogs === false) {
264 console.log(data.toString())
266 server.app.stdout.removeListener('data', onStdout)
269 process.on('exit', () => {
271 process.kill(server.app.pid)
272 } catch { /* empty */ }
275 server.bulkCommand = new BulkCommand(server)
276 server.cliCommand = new CLICommand(server)
277 server.customPageCommand = new CustomPagesCommand(server)
284 async function reRunServer (server: ServerInfo, configOverride?: any) {
285 const newServer = await runServer(server, configOverride)
286 server.app = newServer.app
291 async function checkTmpIsEmpty (server: ServerInfo) {
292 await checkDirectoryIsEmpty(server, 'tmp', [ 'plugins-global.css', 'hls', 'resumable-uploads' ])
294 if (await pathExists(join('test' + server.internalServerNumber, 'tmp', 'hls'))) {
295 await checkDirectoryIsEmpty(server, 'tmp/hls')
299 async function checkDirectoryIsEmpty (server: ServerInfo, directory: string, exceptions: string[] = []) {
300 const testDirectory = 'test' + server.internalServerNumber
302 const directoryPath = join(root(), testDirectory, directory)
304 const directoryExists = await pathExists(directoryPath)
305 expect(directoryExists).to.be.true
307 const files = await readdir(directoryPath)
308 const filtered = files.filter(f => exceptions.includes(f) === false)
310 expect(filtered).to.have.lengthOf(0)
313 function killallServers (servers: ServerInfo[]) {
314 for (const server of servers) {
315 if (!server.app) continue
317 process.kill(-server.app.pid)
322 async function cleanupTests (servers: ServerInfo[]) {
323 killallServers(servers)
326 await ensureDir('artifacts')
329 const p: Promise<any>[] = []
330 for (const server of servers) {
332 const origin = await buildServerDirectory(server, 'logs/peertube.log')
333 const destname = `peertube-${server.internalServerNumber}.log`
334 console.log('Saving logs %s.', destname)
336 await copy(origin, join('artifacts', destname))
339 if (server.parallel) {
340 p.push(flushTests(server.internalServerNumber))
343 if (server.customConfigFile) {
344 p.push(remove(server.customConfigFile))
348 return Promise.all(p)
351 async function waitUntilLog (server: ServerInfo, str: string, count = 1, strictCount = true) {
352 const logfile = buildServerDirectory(server, 'logs/peertube.log')
355 const buf = await readFile(logfile)
357 const matches = buf.toString().match(new RegExp(str, 'g'))
358 if (matches && matches.length === count) return
359 if (matches && strictCount === false && matches.length >= count) return
365 async function getServerFileSize (server: ServerInfo, subPath: string) {
366 const path = buildServerDirectory(server, subPath)
368 return getFileSize(path)
371 function makePingRequest (server: ServerInfo) {
372 return makeGetRequest({
374 path: '/api/v1/ping',
375 statusCodeExpected: 200
379 // ---------------------------------------------------------------------------
382 checkDirectoryIsEmpty,
388 flushAndRunMultipleServers,