1 /* tslint:disable:no-unused-expression */
3 import { ChildProcess, exec, fork } from 'child_process'
4 import { join } from 'path'
5 import { root, wait } from '../miscs/miscs'
6 import { readdir, readFile } from 'fs-extra'
7 import { existsSync } from 'fs'
8 import { expect } from 'chai'
9 import { VideoChannel } from '../../models/videos'
11 interface ServerInfo {
29 videoChannel?: VideoChannel
45 videos?: { id: number, uuid: string }[]
48 function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
52 return new Promise<ServerInfo[]>(res => {
53 function anotherServerDone (serverNumber, app) {
54 apps[serverNumber - 1] = app
56 if (i === totalServers) {
63 for (let j = 1; j <= totalServers; j++) {
64 runServer(j, configOverride).then(app => anotherServerDone(j, app))
70 function flushTests () {
71 return new Promise<void>((res, rej) => {
72 return exec('npm run clean:server:test', err => {
73 if (err) return rej(err)
80 function runServer (serverNumber: number, configOverride?: Object, args = []) {
81 const server: ServerInfo = {
83 serverNumber: serverNumber,
84 url: `http://localhost:${9000 + serverNumber}`,
85 host: `localhost:${9000 + serverNumber}`,
96 // These actions are async so we need to be sure that they have both been done
97 const serverRunString = {
98 'Server listening': false
100 const key = 'Database peertube_test' + serverNumber + ' is ready'
101 serverRunString[key] = false
104 client_id: 'Client id: (.+)',
105 client_secret: 'Client secret: (.+)',
106 user_username: 'Username: (.+)',
107 user_password: 'User password: (.+)'
110 // Share the environment
111 const env = Object.create(process.env)
112 env['NODE_ENV'] = 'test'
113 env['NODE_APP_INSTANCE'] = serverNumber.toString()
115 if (configOverride !== undefined) {
116 env['NODE_CONFIG'] = JSON.stringify(configOverride)
125 return new Promise<ServerInfo>(res => {
126 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
127 server.app.stdout.on('data', function onStdout (data) {
128 let dontContinue = false
130 // Capture things if we want to
131 for (const key of Object.keys(regexps)) {
132 const regexp = regexps[key]
133 const matches = data.toString().match(regexp)
134 if (matches !== null) {
135 if (key === 'client_id') server.client.id = matches[1]
136 else if (key === 'client_secret') server.client.secret = matches[1]
137 else if (key === 'user_username') server.user.username = matches[1]
138 else if (key === 'user_password') server.user.password = matches[1]
142 // Check if all required sentences are here
143 for (const key of Object.keys(serverRunString)) {
144 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
145 if (serverRunString[key] === false) dontContinue = true
148 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
149 if (dontContinue === true) return
151 server.app.stdout.removeListener('data', onStdout)
153 process.on('exit', () => {
155 process.kill(server.app.pid)
156 } catch { /* empty */ }
165 async function reRunServer (server: ServerInfo, configOverride?: any) {
166 const newServer = await runServer(server.serverNumber, configOverride)
167 server.app = newServer.app
172 async function checkTmpIsEmpty (server: ServerInfo) {
173 return checkDirectoryIsEmpty(server, 'tmp')
176 async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) {
177 const testDirectory = 'test' + server.serverNumber
179 const directoryPath = join(root(), testDirectory, directory)
181 const directoryExists = existsSync(directoryPath)
182 expect(directoryExists).to.be.true
184 const files = await readdir(directoryPath)
185 expect(files).to.have.lengthOf(0)
188 function killallServers (servers: ServerInfo[]) {
189 for (const server of servers) {
190 process.kill(-server.app.pid)
194 async function waitUntilLog (server: ServerInfo, str: string, count = 1) {
195 const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log')
198 const buf = await readFile(logfile)
200 const matches = buf.toString().match(new RegExp(str, 'g'))
201 if (matches && matches.length === count) return
207 // ---------------------------------------------------------------------------
210 checkDirectoryIsEmpty,
213 flushAndRunMultipleServers,