import 'mocha'
import {
+ cleanupTests,
createUser,
doubleFollow,
flushAndRunMultipleServers,
- flushTests,
- killallServers,
makeDeleteRequest,
makeGetRequest,
makePostBodyRequest,
ServerInfo,
- setAccessTokensToServers, userLogin
+ setAccessTokensToServers,
+ userLogin
} from '../../../../shared/extra-utils'
import {
checkBadCountPagination,
url: server.url,
token: server.accessToken,
path,
- fields: { host: 'localhost:9001' },
+ fields: { host: 'localhost:' + server.port },
statusCodeExpected: 409
})
})
url: server.url,
token: server.accessToken,
path,
- fields: { host: 'localhost:9002' },
+ fields: { host: 'localhost:' + servers[1].port },
statusCodeExpected: 204
})
})
it('Should fail with an unauthenticated user', async function () {
await makeDeleteRequest({
url: server.url,
- path: path + '/localhost:9002',
+ path: path + '/localhost:' + servers[1].port,
statusCodeExpected: 401
})
})
it('Should succeed with the correct params', async function () {
await makeDeleteRequest({
url: server.url,
- path: path + '/localhost:9002',
+ path: path + '/localhost:' + servers[1].port,
token: server.accessToken,
statusCodeExpected: 204
})
await makePostBodyRequest({
url: server.url,
path,
- fields: { host: 'localhost:9002' },
+ fields: { host: 'localhost:' + servers[1].port },
statusCodeExpected: 401
})
})
url: server.url,
token: userAccessToken,
path,
- fields: { host: 'localhost:9002' },
+ fields: { host: 'localhost:' + servers[1].port },
statusCodeExpected: 403
})
})
url: server.url,
token: server.accessToken,
path,
- fields: { host: 'localhost:9001' },
+ fields: { host: 'localhost:' + server.port },
statusCodeExpected: 409
})
})
url: server.url,
token: server.accessToken,
path,
- fields: { host: 'localhost:9002' },
+ fields: { host: 'localhost:' + servers[1].port },
statusCodeExpected: 204
})
})
it('Should fail with an unauthenticated user', async function () {
await makeDeleteRequest({
url: server.url,
- path: path + '/localhost:9002',
+ path: path + '/localhost:' + servers[1].port,
statusCodeExpected: 401
})
})
it('Should fail with a user without the appropriate rights', async function () {
await makeDeleteRequest({
url: server.url,
- path: path + '/localhost:9002',
+ path: path + '/localhost:' + servers[1].port,
token: userAccessToken,
statusCodeExpected: 403
})
it('Should succeed with the correct params', async function () {
await makeDeleteRequest({
url: server.url,
- path: path + '/localhost:9002',
+ path: path + '/localhost:' + servers[1].port,
token: server.accessToken,
statusCodeExpected: 204
})
})
})
- after(function () {
- killallServers(servers)
+ after(async function () {
+ await cleanupTests(servers)
})
})
app: ChildProcess,
url: string
host: string
+
+ port: number
+ parallel: boolean
+ internalServerNumber: number
serverNumber: number
client: {
})
}
-function flushAndRunServer (serverNumber: number, configOverride?: Object, args = []) {
+function randomServer () {
+ const low = 10
+ const high = 10000
+
+ return Math.floor(Math.random() * (high - low) + low)
+}
+
+function flushAndRunServer (serverNumber: number, configOverrideArg?: Object, args = []) {
+ const parallel = process.env.MOCHA_PARALLEL === 'true'
+
+ const internalServerNumber = parallel ? randomServer() : serverNumber
+ const port = 9000 + internalServerNumber
+
const server: ServerInfo = {
app: null,
- serverNumber: serverNumber,
- url: `http://localhost:${9000 + serverNumber}`,
- host: `localhost:${9000 + serverNumber}`,
+ port,
+ internalServerNumber,
+ parallel,
+ serverNumber: internalServerNumber,
+ url: `http://localhost:${port}`,
+ host: `localhost:${port}`,
client: {
id: null,
secret: null
const serverRunString = {
'Server listening': false
}
- const key = 'Database peertube_test' + serverNumber + ' is ready'
+ const key = 'Database peertube_test' + internalServerNumber + ' is ready'
serverRunString[key] = false
const regexps = {
env['NODE_ENV'] = 'test'
env['NODE_APP_INSTANCE'] = serverNumber.toString()
- if (configOverride !== undefined) {
- env['NODE_CONFIG'] = JSON.stringify(configOverride)
+ let configOverride: any = {}
+
+ if (parallel) {
+ configOverride = {
+ listen: {
+ port: port
+ },
+ webserver: {
+ port: port
+ },
+ database: {
+ suffix: '_test' + internalServerNumber
+ },
+ storage: {
+ tmp: `test${internalServerNumber}/tmp/`,
+ avatars: `test${internalServerNumber}/avatars/`,
+ videos: `test${internalServerNumber}/videos/`,
+ streaming_playlists: `test${internalServerNumber}/streaming-playlists/`,
+ redundancy: `test${internalServerNumber}/redundancy/`,
+ logs: `test${internalServerNumber}/logs/`,
+ previews: `test${internalServerNumber}/previews/`,
+ thumbnails: `test${internalServerNumber}/thumbnails/`,
+ torrents: `test${internalServerNumber}/torrents/`,
+ captions: `test${internalServerNumber}/captions/`,
+ cache: `test${internalServerNumber}/cache/`
+ },
+ admin: {
+ email: `admin${internalServerNumber}@example.com`
+ }
+ }
+ }
+
+ if (configOverrideArg !== undefined) {
+ Object.assign(configOverride, configOverrideArg)
}
+ env['NODE_CONFIG'] = JSON.stringify(configOverride)
+
const options = {
silent: true,
env: env,
}
return new Promise<ServerInfo>(res => {
- flushTests(serverNumber)
+ flushTests(internalServerNumber)
.then(() => {
server.app = fork(join(root(), 'dist', 'server.js'), args, options)
process.on('exit', () => {
try {
process.kill(server.app.pid)
- } catch { /* empty */
- }
+ } catch { /* empty */ }
})
res(server)
}
}
+function cleanupTests (servers: ServerInfo[]) {
+ killallServers(servers)
+
+ const p: Promise<any>[] = []
+ for (const server of servers) {
+ if (server.parallel) {
+ p.push(flushTests(server.internalServerNumber))
+ }
+ }
+
+ return Promise.all(p)
+}
+
async function waitUntilLog (server: ServerInfo, str: string, count = 1) {
const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log')
checkDirectoryIsEmpty,
checkTmpIsEmpty,
ServerInfo,
+ cleanupTests,
flushAndRunMultipleServers,
flushTests,
flushAndRunServer,