aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server/tests/api/check-params/accounts.ts6
-rw-r--r--server/tests/api/check-params/blocklist.ts32
-rw-r--r--shared/extra-utils/server/servers.ts86
3 files changed, 95 insertions, 29 deletions
diff --git a/server/tests/api/check-params/accounts.ts b/server/tests/api/check-params/accounts.ts
index 086090533..4f79685bd 100644
--- a/server/tests/api/check-params/accounts.ts
+++ b/server/tests/api/check-params/accounts.ts
@@ -2,7 +2,7 @@
2 2
3import 'mocha' 3import 'mocha'
4 4
5import { flushTests, killallServers, flushAndRunServer, ServerInfo } from '../../../../shared/extra-utils' 5import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../../shared/extra-utils'
6import { 6import {
7 checkBadCountPagination, 7 checkBadCountPagination,
8 checkBadSortPagination, 8 checkBadSortPagination,
@@ -42,7 +42,7 @@ describe('Test accounts API validators', function () {
42 }) 42 })
43 }) 43 })
44 44
45 after(function () { 45 after(async function () {
46 killallServers([ server ]) 46 await cleanupTests([ server ])
47 }) 47 })
48}) 48})
diff --git a/server/tests/api/check-params/blocklist.ts b/server/tests/api/check-params/blocklist.ts
index d815a8363..0661676ce 100644
--- a/server/tests/api/check-params/blocklist.ts
+++ b/server/tests/api/check-params/blocklist.ts
@@ -3,16 +3,16 @@
3import 'mocha' 3import 'mocha'
4 4
5import { 5import {
6 cleanupTests,
6 createUser, 7 createUser,
7 doubleFollow, 8 doubleFollow,
8 flushAndRunMultipleServers, 9 flushAndRunMultipleServers,
9 flushTests,
10 killallServers,
11 makeDeleteRequest, 10 makeDeleteRequest,
12 makeGetRequest, 11 makeGetRequest,
13 makePostBodyRequest, 12 makePostBodyRequest,
14 ServerInfo, 13 ServerInfo,
15 setAccessTokensToServers, userLogin 14 setAccessTokensToServers,
15 userLogin
16} from '../../../../shared/extra-utils' 16} from '../../../../shared/extra-utils'
17import { 17import {
18 checkBadCountPagination, 18 checkBadCountPagination,
@@ -190,7 +190,7 @@ describe('Test blocklist API validators', function () {
190 url: server.url, 190 url: server.url,
191 token: server.accessToken, 191 token: server.accessToken,
192 path, 192 path,
193 fields: { host: 'localhost:9001' }, 193 fields: { host: 'localhost:' + server.port },
194 statusCodeExpected: 409 194 statusCodeExpected: 409
195 }) 195 })
196 }) 196 })
@@ -200,7 +200,7 @@ describe('Test blocklist API validators', function () {
200 url: server.url, 200 url: server.url,
201 token: server.accessToken, 201 token: server.accessToken,
202 path, 202 path,
203 fields: { host: 'localhost:9002' }, 203 fields: { host: 'localhost:' + servers[1].port },
204 statusCodeExpected: 204 204 statusCodeExpected: 204
205 }) 205 })
206 }) 206 })
@@ -210,7 +210,7 @@ describe('Test blocklist API validators', function () {
210 it('Should fail with an unauthenticated user', async function () { 210 it('Should fail with an unauthenticated user', async function () {
211 await makeDeleteRequest({ 211 await makeDeleteRequest({
212 url: server.url, 212 url: server.url,
213 path: path + '/localhost:9002', 213 path: path + '/localhost:' + servers[1].port,
214 statusCodeExpected: 401 214 statusCodeExpected: 401
215 }) 215 })
216 }) 216 })
@@ -227,7 +227,7 @@ describe('Test blocklist API validators', function () {
227 it('Should succeed with the correct params', async function () { 227 it('Should succeed with the correct params', async function () {
228 await makeDeleteRequest({ 228 await makeDeleteRequest({
229 url: server.url, 229 url: server.url,
230 path: path + '/localhost:9002', 230 path: path + '/localhost:' + servers[1].port,
231 token: server.accessToken, 231 token: server.accessToken,
232 statusCodeExpected: 204 232 statusCodeExpected: 204
233 }) 233 })
@@ -400,7 +400,7 @@ describe('Test blocklist API validators', function () {
400 await makePostBodyRequest({ 400 await makePostBodyRequest({
401 url: server.url, 401 url: server.url,
402 path, 402 path,
403 fields: { host: 'localhost:9002' }, 403 fields: { host: 'localhost:' + servers[1].port },
404 statusCodeExpected: 401 404 statusCodeExpected: 401
405 }) 405 })
406 }) 406 })
@@ -410,7 +410,7 @@ describe('Test blocklist API validators', function () {
410 url: server.url, 410 url: server.url,
411 token: userAccessToken, 411 token: userAccessToken,
412 path, 412 path,
413 fields: { host: 'localhost:9002' }, 413 fields: { host: 'localhost:' + servers[1].port },
414 statusCodeExpected: 403 414 statusCodeExpected: 403
415 }) 415 })
416 }) 416 })
@@ -430,7 +430,7 @@ describe('Test blocklist API validators', function () {
430 url: server.url, 430 url: server.url,
431 token: server.accessToken, 431 token: server.accessToken,
432 path, 432 path,
433 fields: { host: 'localhost:9001' }, 433 fields: { host: 'localhost:' + server.port },
434 statusCodeExpected: 409 434 statusCodeExpected: 409
435 }) 435 })
436 }) 436 })
@@ -440,7 +440,7 @@ describe('Test blocklist API validators', function () {
440 url: server.url, 440 url: server.url,
441 token: server.accessToken, 441 token: server.accessToken,
442 path, 442 path,
443 fields: { host: 'localhost:9002' }, 443 fields: { host: 'localhost:' + servers[1].port },
444 statusCodeExpected: 204 444 statusCodeExpected: 204
445 }) 445 })
446 }) 446 })
@@ -450,7 +450,7 @@ describe('Test blocklist API validators', function () {
450 it('Should fail with an unauthenticated user', async function () { 450 it('Should fail with an unauthenticated user', async function () {
451 await makeDeleteRequest({ 451 await makeDeleteRequest({
452 url: server.url, 452 url: server.url,
453 path: path + '/localhost:9002', 453 path: path + '/localhost:' + servers[1].port,
454 statusCodeExpected: 401 454 statusCodeExpected: 401
455 }) 455 })
456 }) 456 })
@@ -458,7 +458,7 @@ describe('Test blocklist API validators', function () {
458 it('Should fail with a user without the appropriate rights', async function () { 458 it('Should fail with a user without the appropriate rights', async function () {
459 await makeDeleteRequest({ 459 await makeDeleteRequest({
460 url: server.url, 460 url: server.url,
461 path: path + '/localhost:9002', 461 path: path + '/localhost:' + servers[1].port,
462 token: userAccessToken, 462 token: userAccessToken,
463 statusCodeExpected: 403 463 statusCodeExpected: 403
464 }) 464 })
@@ -476,7 +476,7 @@ describe('Test blocklist API validators', function () {
476 it('Should succeed with the correct params', async function () { 476 it('Should succeed with the correct params', async function () {
477 await makeDeleteRequest({ 477 await makeDeleteRequest({
478 url: server.url, 478 url: server.url,
479 path: path + '/localhost:9002', 479 path: path + '/localhost:' + servers[1].port,
480 token: server.accessToken, 480 token: server.accessToken,
481 statusCodeExpected: 204 481 statusCodeExpected: 204
482 }) 482 })
@@ -485,7 +485,7 @@ describe('Test blocklist API validators', function () {
485 }) 485 })
486 }) 486 })
487 487
488 after(function () { 488 after(async function () {
489 killallServers(servers) 489 await cleanupTests(servers)
490 }) 490 })
491}) 491})
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index b0cb869f4..480d12330 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -12,6 +12,10 @@ interface ServerInfo {
12 app: ChildProcess, 12 app: ChildProcess,
13 url: string 13 url: string
14 host: string 14 host: string
15
16 port: number
17 parallel: boolean
18 internalServerNumber: number
15 serverNumber: number 19 serverNumber: number
16 20
17 client: { 21 client: {
@@ -76,12 +80,27 @@ function flushTests (serverNumber?: number) {
76 }) 80 })
77} 81}
78 82
79function flushAndRunServer (serverNumber: number, configOverride?: Object, args = []) { 83function randomServer () {
84 const low = 10
85 const high = 10000
86
87 return Math.floor(Math.random() * (high - low) + low)
88}
89
90function flushAndRunServer (serverNumber: number, configOverrideArg?: Object, args = []) {
91 const parallel = process.env.MOCHA_PARALLEL === 'true'
92
93 const internalServerNumber = parallel ? randomServer() : serverNumber
94 const port = 9000 + internalServerNumber
95
80 const server: ServerInfo = { 96 const server: ServerInfo = {
81 app: null, 97 app: null,
82 serverNumber: serverNumber, 98 port,
83 url: `http://localhost:${9000 + serverNumber}`, 99 internalServerNumber,
84 host: `localhost:${9000 + serverNumber}`, 100 parallel,
101 serverNumber: internalServerNumber,
102 url: `http://localhost:${port}`,
103 host: `localhost:${port}`,
85 client: { 104 client: {
86 id: null, 105 id: null,
87 secret: null 106 secret: null
@@ -96,7 +115,7 @@ function flushAndRunServer (serverNumber: number, configOverride?: Object, args
96 const serverRunString = { 115 const serverRunString = {
97 'Server listening': false 116 'Server listening': false
98 } 117 }
99 const key = 'Database peertube_test' + serverNumber + ' is ready' 118 const key = 'Database peertube_test' + internalServerNumber + ' is ready'
100 serverRunString[key] = false 119 serverRunString[key] = false
101 120
102 const regexps = { 121 const regexps = {
@@ -111,10 +130,44 @@ function flushAndRunServer (serverNumber: number, configOverride?: Object, args
111 env['NODE_ENV'] = 'test' 130 env['NODE_ENV'] = 'test'
112 env['NODE_APP_INSTANCE'] = serverNumber.toString() 131 env['NODE_APP_INSTANCE'] = serverNumber.toString()
113 132
114 if (configOverride !== undefined) { 133 let configOverride: any = {}
115 env['NODE_CONFIG'] = JSON.stringify(configOverride) 134
135 if (parallel) {
136 configOverride = {
137 listen: {
138 port: port
139 },
140 webserver: {
141 port: port
142 },
143 database: {
144 suffix: '_test' + internalServerNumber
145 },
146 storage: {
147 tmp: `test${internalServerNumber}/tmp/`,
148 avatars: `test${internalServerNumber}/avatars/`,
149 videos: `test${internalServerNumber}/videos/`,
150 streaming_playlists: `test${internalServerNumber}/streaming-playlists/`,
151 redundancy: `test${internalServerNumber}/redundancy/`,
152 logs: `test${internalServerNumber}/logs/`,
153 previews: `test${internalServerNumber}/previews/`,
154 thumbnails: `test${internalServerNumber}/thumbnails/`,
155 torrents: `test${internalServerNumber}/torrents/`,
156 captions: `test${internalServerNumber}/captions/`,
157 cache: `test${internalServerNumber}/cache/`
158 },
159 admin: {
160 email: `admin${internalServerNumber}@example.com`
161 }
162 }
163 }
164
165 if (configOverrideArg !== undefined) {
166 Object.assign(configOverride, configOverrideArg)
116 } 167 }
117 168
169 env['NODE_CONFIG'] = JSON.stringify(configOverride)
170
118 const options = { 171 const options = {
119 silent: true, 172 silent: true,
120 env: env, 173 env: env,
@@ -122,7 +175,7 @@ function flushAndRunServer (serverNumber: number, configOverride?: Object, args
122 } 175 }
123 176
124 return new Promise<ServerInfo>(res => { 177 return new Promise<ServerInfo>(res => {
125 flushTests(serverNumber) 178 flushTests(internalServerNumber)
126 .then(() => { 179 .then(() => {
127 180
128 server.app = fork(join(root(), 'dist', 'server.js'), args, options) 181 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
@@ -155,8 +208,7 @@ function flushAndRunServer (serverNumber: number, configOverride?: Object, args
155 process.on('exit', () => { 208 process.on('exit', () => {
156 try { 209 try {
157 process.kill(server.app.pid) 210 process.kill(server.app.pid)
158 } catch { /* empty */ 211 } catch { /* empty */ }
159 }
160 }) 212 })
161 213
162 res(server) 214 res(server)
@@ -194,6 +246,19 @@ function killallServers (servers: ServerInfo[]) {
194 } 246 }
195} 247}
196 248
249function cleanupTests (servers: ServerInfo[]) {
250 killallServers(servers)
251
252 const p: Promise<any>[] = []
253 for (const server of servers) {
254 if (server.parallel) {
255 p.push(flushTests(server.internalServerNumber))
256 }
257 }
258
259 return Promise.all(p)
260}
261
197async function waitUntilLog (server: ServerInfo, str: string, count = 1) { 262async function waitUntilLog (server: ServerInfo, str: string, count = 1) {
198 const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log') 263 const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log')
199 264
@@ -213,6 +278,7 @@ export {
213 checkDirectoryIsEmpty, 278 checkDirectoryIsEmpty,
214 checkTmpIsEmpty, 279 checkTmpIsEmpty,
215 ServerInfo, 280 ServerInfo,
281 cleanupTests,
216 flushAndRunMultipleServers, 282 flushAndRunMultipleServers,
217 flushTests, 283 flushTests,
218 flushAndRunServer, 284 flushAndRunServer,