aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils')
-rw-r--r--shared/extra-utils/miscs/email-child-process.js2
-rw-r--r--shared/extra-utils/miscs/email.ts12
-rw-r--r--shared/extra-utils/server/servers.ts51
3 files changed, 46 insertions, 19 deletions
diff --git a/shared/extra-utils/miscs/email-child-process.js b/shared/extra-utils/miscs/email-child-process.js
index 40ae37d70..088a5a08c 100644
--- a/shared/extra-utils/miscs/email-child-process.js
+++ b/shared/extra-utils/miscs/email-child-process.js
@@ -6,7 +6,7 @@ process.on('message', (msg) => {
6 if (msg.start) { 6 if (msg.start) {
7 const maildev = new MailDev({ 7 const maildev = new MailDev({
8 ip: '127.0.0.1', 8 ip: '127.0.0.1',
9 smtp: 1025, 9 smtp: msg.port,
10 disableWeb: true, 10 disableWeb: true,
11 silent: true 11 silent: true
12 }) 12 })
diff --git a/shared/extra-utils/miscs/email.ts b/shared/extra-utils/miscs/email.ts
index f9f1bd95b..b2a1093da 100644
--- a/shared/extra-utils/miscs/email.ts
+++ b/shared/extra-utils/miscs/email.ts
@@ -1,4 +1,6 @@
1import { fork, ChildProcess } from 'child_process' 1import { ChildProcess, fork } from 'child_process'
2import { randomInt } from '../../core-utils/miscs/miscs'
3import { parallelTests } from '../server/servers'
2 4
3class MockSmtpServer { 5class MockSmtpServer {
4 6
@@ -20,7 +22,9 @@ class MockSmtpServer {
20 } 22 }
21 23
22 collectEmails (emailsCollection: object[]) { 24 collectEmails (emailsCollection: object[]) {
23 return new Promise((res, rej) => { 25 return new Promise<number>((res, rej) => {
26 const port = parallelTests() ? randomInt(1000, 2000) : 1025
27
24 if (this.started) { 28 if (this.started) {
25 this.emails = emailsCollection 29 this.emails = emailsCollection
26 return res() 30 return res()
@@ -28,7 +32,7 @@ class MockSmtpServer {
28 32
29 // ensure maildev isn't started until 33 // ensure maildev isn't started until
30 // unexpected exit can be reported to test runner 34 // unexpected exit can be reported to test runner
31 this.emailChildProcess.send({ start: true }) 35 this.emailChildProcess.send({ start: true, port })
32 this.emailChildProcess.on('exit', () => { 36 this.emailChildProcess.on('exit', () => {
33 return rej(new Error('maildev exited unexpectedly, confirm port not in use')) 37 return rej(new Error('maildev exited unexpectedly, confirm port not in use'))
34 }) 38 })
@@ -38,7 +42,7 @@ class MockSmtpServer {
38 } 42 }
39 this.started = true 43 this.started = true
40 this.emails = emailsCollection 44 this.emails = emailsCollection
41 return res() 45 return res(port)
42 }) 46 })
43 }) 47 })
44 } 48 }
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index 3ef4b9746..ed41bfa48 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -3,10 +3,11 @@
3import { ChildProcess, exec, fork } from 'child_process' 3import { ChildProcess, exec, fork } from 'child_process'
4import { join } from 'path' 4import { join } from 'path'
5import { root, wait } from '../miscs/miscs' 5import { root, wait } from '../miscs/miscs'
6import { readdir, readFile } from 'fs-extra' 6import { copy, readdir, readFile, remove } from 'fs-extra'
7import { existsSync } from 'fs' 7import { existsSync } from 'fs'
8import { expect } from 'chai' 8import { expect } from 'chai'
9import { VideoChannel } from '../../models/videos' 9import { VideoChannel } from '../../models/videos'
10import { randomInt } from '../../core-utils/miscs/miscs'
10 11
11interface ServerInfo { 12interface ServerInfo {
12 app: ChildProcess, 13 app: ChildProcess,
@@ -29,6 +30,8 @@ interface ServerInfo {
29 email?: string 30 email?: string
30 } 31 }
31 32
33 customConfigFile?: string
34
32 accessToken?: string 35 accessToken?: string
33 videoChannel?: VideoChannel 36 videoChannel?: VideoChannel
34 37
@@ -49,6 +52,10 @@ interface ServerInfo {
49 videos?: { id: number, uuid: string }[] 52 videos?: { id: number, uuid: string }[]
50} 53}
51 54
55function parallelTests () {
56 return process.env.MOCHA_PARALLEL === 'true'
57}
58
52function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) { 59function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
53 let apps = [] 60 let apps = []
54 let i = 0 61 let i = 0
@@ -84,23 +91,23 @@ function randomServer () {
84 const low = 10 91 const low = 10
85 const high = 10000 92 const high = 10000
86 93
87 return Math.floor(Math.random() * (high - low) + low) 94 return randomInt(low, high)
88} 95}
89 96
90async function flushAndRunServer (serverNumber: number, configOverride?: Object, args = []) { 97async function flushAndRunServer (serverNumber: number, configOverride?: Object, args = []) {
91 const parallel = process.env.MOCHA_PARALLEL === 'true' 98 const parallel = parallelTests()
92 99
93 const internalServerNumber = parallel ? randomServer() : serverNumber 100 const internalServerNumber = parallel ? randomServer() : serverNumber
94 const port = 9000 + internalServerNumber 101 const port = 9000 + internalServerNumber
95 102
96 await flushTests(serverNumber) 103 await flushTests(internalServerNumber)
97 104
98 const server: ServerInfo = { 105 const server: ServerInfo = {
99 app: null, 106 app: null,
100 port, 107 port,
101 internalServerNumber, 108 internalServerNumber,
102 parallel, 109 parallel,
103 serverNumber: internalServerNumber, 110 serverNumber,
104 url: `http://localhost:${port}`, 111 url: `http://localhost:${port}`,
105 host: `localhost:${port}`, 112 host: `localhost:${port}`,
106 client: { 113 client: {
@@ -116,7 +123,7 @@ async function flushAndRunServer (serverNumber: number, configOverride?: Object,
116 return runServer(server, configOverride, args) 123 return runServer(server, configOverride, args)
117} 124}
118 125
119function runServer (server: ServerInfo, configOverrideArg?: any, args = []) { 126async function runServer (server: ServerInfo, configOverrideArg?: any, args = []) {
120 // These actions are async so we need to be sure that they have both been done 127 // These actions are async so we need to be sure that they have both been done
121 const serverRunString = { 128 const serverRunString = {
122 'Server listening': false 129 'Server listening': false
@@ -131,15 +138,19 @@ function runServer (server: ServerInfo, configOverrideArg?: any, args = []) {
131 user_password: 'User password: (.+)' 138 user_password: 'User password: (.+)'
132 } 139 }
133 140
134 // Share the environment 141 if (server.internalServerNumber !== server.serverNumber) {
135 const env = Object.create(process.env) 142 const basePath = join(root(), 'config')
136 env['NODE_ENV'] = 'test' 143
137 env['NODE_APP_INSTANCE'] = server.serverNumber.toString() 144 const tmpConfigFile = join(basePath, `test-${server.internalServerNumber}.yaml`)
145 await copy(join(basePath, `test-${server.serverNumber}.yaml`), tmpConfigFile)
146
147 server.customConfigFile = tmpConfigFile
148 }
138 149
139 let configOverride: any = {} 150 const configOverride: any = {}
140 151
141 if (server.parallel) { 152 if (server.parallel) {
142 configOverride = { 153 Object.assign(configOverride, {
143 listen: { 154 listen: {
144 port: server.port 155 port: server.port
145 }, 156 },
@@ -165,18 +176,22 @@ function runServer (server: ServerInfo, configOverrideArg?: any, args = []) {
165 admin: { 176 admin: {
166 email: `admin${server.internalServerNumber}@example.com` 177 email: `admin${server.internalServerNumber}@example.com`
167 } 178 }
168 } 179 })
169 } 180 }
170 181
171 if (configOverrideArg !== undefined) { 182 if (configOverrideArg !== undefined) {
172 Object.assign(configOverride, configOverrideArg) 183 Object.assign(configOverride, configOverrideArg)
173 } 184 }
174 185
186 // Share the environment
187 const env = Object.create(process.env)
188 env['NODE_ENV'] = 'test'
189 env['NODE_APP_INSTANCE'] = server.internalServerNumber.toString()
175 env['NODE_CONFIG'] = JSON.stringify(configOverride) 190 env['NODE_CONFIG'] = JSON.stringify(configOverride)
176 191
177 const options = { 192 const options = {
178 silent: true, 193 silent: true,
179 env: env, 194 env,
180 detached: true 195 detached: true
181 } 196 }
182 197
@@ -244,7 +259,10 @@ async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) {
244 259
245function killallServers (servers: ServerInfo[]) { 260function killallServers (servers: ServerInfo[]) {
246 for (const server of servers) { 261 for (const server of servers) {
262 if (!server.app) continue
263
247 process.kill(-server.app.pid) 264 process.kill(-server.app.pid)
265 server.app = null
248 } 266 }
249} 267}
250 268
@@ -256,6 +274,10 @@ function cleanupTests (servers: ServerInfo[]) {
256 if (server.parallel) { 274 if (server.parallel) {
257 p.push(flushTests(server.internalServerNumber)) 275 p.push(flushTests(server.internalServerNumber))
258 } 276 }
277
278 if (server.customConfigFile) {
279 p.push(remove(server.customConfigFile))
280 }
259 } 281 }
260 282
261 return Promise.all(p) 283 return Promise.all(p)
@@ -280,6 +302,7 @@ export {
280 checkDirectoryIsEmpty, 302 checkDirectoryIsEmpty,
281 checkTmpIsEmpty, 303 checkTmpIsEmpty,
282 ServerInfo, 304 ServerInfo,
305 parallelTests,
283 cleanupTests, 306 cleanupTests,
284 flushAndRunMultipleServers, 307 flushAndRunMultipleServers,
285 flushTests, 308 flushTests,