aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server/servers.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-04-24 15:10:37 +0200
committerChocobozzz <me@florianbigard.com>2019-04-24 16:26:25 +0200
commit7c3b79768bd174b22154e8d2df0b1211e01ee56a (patch)
tree9e45c9302083f0438f7ea174f5fb7d6042be5712 /shared/extra-utils/server/servers.ts
parent913b1d71e630d5a959c763bf565a171b4dc61434 (diff)
downloadPeerTube-7c3b79768bd174b22154e8d2df0b1211e01ee56a.tar.gz
PeerTube-7c3b79768bd174b22154e8d2df0b1211e01ee56a.tar.zst
PeerTube-7c3b79768bd174b22154e8d2df0b1211e01ee56a.zip
Use test wrapper exit function
Diffstat (limited to 'shared/extra-utils/server/servers.ts')
-rw-r--r--shared/extra-utils/server/servers.ts51
1 files changed, 37 insertions, 14 deletions
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,