]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ |
89231874 | 2 | |
c655c9ef | 3 | import { expect } from 'chai' |
0e1dc3e7 | 4 | import { ChildProcess, exec, fork } from 'child_process' |
83ef31fe | 5 | import { copy, ensureDir, pathExists, readdir, readFile, remove } from 'fs-extra' |
c655c9ef | 6 | import { join } from 'path' |
7c3b7976 | 7 | import { randomInt } from '../../core-utils/miscs/miscs' |
a35a2279 | 8 | import { VideoChannel } from '../../models/videos' |
83ef31fe | 9 | import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' |
78d62f4d | 10 | import { makeGetRequest } from '../requests/requests' |
0e1dc3e7 C |
11 | |
12 | interface ServerInfo { | |
a1587156 | 13 | app: ChildProcess |
af4ae64f | 14 | |
0e1dc3e7 C |
15 | url: string |
16 | host: string | |
af4ae64f | 17 | hostname: string |
86ebdf8c | 18 | port: number |
af4ae64f | 19 | |
c655c9ef C |
20 | rtmpPort: number |
21 | ||
86ebdf8c C |
22 | parallel: boolean |
23 | internalServerNumber: number | |
fdbda9e3 | 24 | serverNumber: number |
0e1dc3e7 C |
25 | |
26 | client: { | |
a1587156 | 27 | id: string |
0e1dc3e7 C |
28 | secret: string |
29 | } | |
30 | ||
31 | user: { | |
a1587156 C |
32 | username: string |
33 | password: string | |
0e1dc3e7 C |
34 | email?: string |
35 | } | |
36 | ||
7c3b7976 C |
37 | customConfigFile?: string |
38 | ||
0e1dc3e7 | 39 | accessToken?: string |
f43db2f4 | 40 | refreshToken?: string |
df0b219d | 41 | videoChannel?: VideoChannel |
0e1dc3e7 C |
42 | |
43 | video?: { | |
44 | id: number | |
45 | uuid: string | |
310b5219 | 46 | name?: string |
a59f210f | 47 | url?: string |
aea0b0e7 | 48 | |
310b5219 | 49 | account?: { |
b64c950a C |
50 | name: string |
51 | } | |
aea0b0e7 C |
52 | |
53 | embedPath?: string | |
0e1dc3e7 C |
54 | } |
55 | ||
56 | remoteVideo?: { | |
57 | id: number | |
58 | uuid: string | |
59 | } | |
df0b219d C |
60 | |
61 | videos?: { id: number, uuid: string }[] | |
0e1dc3e7 C |
62 | } |
63 | ||
7c3b7976 C |
64 | function parallelTests () { |
65 | return process.env.MOCHA_PARALLEL === 'true' | |
66 | } | |
67 | ||
b36f41ca | 68 | function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) { |
a1587156 | 69 | const apps = [] |
0e1dc3e7 C |
70 | let i = 0 |
71 | ||
72 | return new Promise<ServerInfo[]>(res => { | |
73 | function anotherServerDone (serverNumber, app) { | |
74 | apps[serverNumber - 1] = app | |
75 | i++ | |
76 | if (i === totalServers) { | |
77 | return res(apps) | |
78 | } | |
79 | } | |
80 | ||
210feb6c C |
81 | for (let j = 1; j <= totalServers; j++) { |
82 | flushAndRunServer(j, configOverride).then(app => anotherServerDone(j, app)) | |
83 | } | |
0e1dc3e7 C |
84 | }) |
85 | } | |
86 | ||
210feb6c | 87 | function flushTests (serverNumber?: number) { |
0e1dc3e7 | 88 | return new Promise<void>((res, rej) => { |
210feb6c C |
89 | const suffix = serverNumber ? ` -- ${serverNumber}` : '' |
90 | ||
2284f202 C |
91 | return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => { |
92 | if (err || stderr) return rej(err || new Error(stderr)) | |
0e1dc3e7 C |
93 | |
94 | return res() | |
95 | }) | |
96 | }) | |
97 | } | |
98 | ||
86ebdf8c C |
99 | function randomServer () { |
100 | const low = 10 | |
101 | const high = 10000 | |
102 | ||
7c3b7976 | 103 | return randomInt(low, high) |
86ebdf8c C |
104 | } |
105 | ||
c655c9ef C |
106 | function randomRTMP () { |
107 | const low = 1900 | |
108 | const high = 2100 | |
109 | ||
110 | return randomInt(low, high) | |
111 | } | |
112 | ||
bd2e2f11 C |
113 | type RunServerOptions = { |
114 | hideLogs?: boolean | |
115 | execArgv?: string[] | |
116 | } | |
117 | ||
118 | async function flushAndRunServer (serverNumber: number, configOverride?: Object, args = [], options: RunServerOptions = {}) { | |
7c3b7976 | 119 | const parallel = parallelTests() |
86ebdf8c C |
120 | |
121 | const internalServerNumber = parallel ? randomServer() : serverNumber | |
3e8584b9 | 122 | const rtmpPort = parallel ? randomRTMP() : 1936 |
86ebdf8c C |
123 | const port = 9000 + internalServerNumber |
124 | ||
7c3b7976 | 125 | await flushTests(internalServerNumber) |
42e1ec25 | 126 | |
0e1dc3e7 C |
127 | const server: ServerInfo = { |
128 | app: null, | |
86ebdf8c C |
129 | port, |
130 | internalServerNumber, | |
c655c9ef | 131 | rtmpPort, |
86ebdf8c | 132 | parallel, |
7c3b7976 | 133 | serverNumber, |
86ebdf8c C |
134 | url: `http://localhost:${port}`, |
135 | host: `localhost:${port}`, | |
af4ae64f | 136 | hostname: 'localhost', |
0e1dc3e7 C |
137 | client: { |
138 | id: null, | |
139 | secret: null | |
140 | }, | |
141 | user: { | |
142 | username: null, | |
143 | password: null | |
144 | } | |
145 | } | |
146 | ||
bd2e2f11 | 147 | return runServer(server, configOverride, args, options) |
913b1d71 C |
148 | } |
149 | ||
bd2e2f11 | 150 | async function runServer (server: ServerInfo, configOverrideArg?: any, args = [], options: RunServerOptions = {}) { |
0e1dc3e7 C |
151 | // These actions are async so we need to be sure that they have both been done |
152 | const serverRunString = { | |
fcb77122 | 153 | 'HTTP server listening': false |
0e1dc3e7 | 154 | } |
913b1d71 | 155 | const key = 'Database peertube_test' + server.internalServerNumber + ' is ready' |
0e1dc3e7 C |
156 | serverRunString[key] = false |
157 | ||
158 | const regexps = { | |
159 | client_id: 'Client id: (.+)', | |
160 | client_secret: 'Client secret: (.+)', | |
161 | user_username: 'Username: (.+)', | |
162 | user_password: 'User password: (.+)' | |
163 | } | |
164 | ||
7c3b7976 C |
165 | if (server.internalServerNumber !== server.serverNumber) { |
166 | const basePath = join(root(), 'config') | |
167 | ||
168 | const tmpConfigFile = join(basePath, `test-${server.internalServerNumber}.yaml`) | |
169 | await copy(join(basePath, `test-${server.serverNumber}.yaml`), tmpConfigFile) | |
170 | ||
171 | server.customConfigFile = tmpConfigFile | |
172 | } | |
fdbda9e3 | 173 | |
7c3b7976 | 174 | const configOverride: any = {} |
86ebdf8c | 175 | |
913b1d71 | 176 | if (server.parallel) { |
7c3b7976 | 177 | Object.assign(configOverride, { |
86ebdf8c | 178 | listen: { |
913b1d71 | 179 | port: server.port |
86ebdf8c C |
180 | }, |
181 | webserver: { | |
913b1d71 | 182 | port: server.port |
86ebdf8c C |
183 | }, |
184 | database: { | |
913b1d71 | 185 | suffix: '_test' + server.internalServerNumber |
86ebdf8c C |
186 | }, |
187 | storage: { | |
913b1d71 C |
188 | tmp: `test${server.internalServerNumber}/tmp/`, |
189 | avatars: `test${server.internalServerNumber}/avatars/`, | |
190 | videos: `test${server.internalServerNumber}/videos/`, | |
191 | streaming_playlists: `test${server.internalServerNumber}/streaming-playlists/`, | |
192 | redundancy: `test${server.internalServerNumber}/redundancy/`, | |
193 | logs: `test${server.internalServerNumber}/logs/`, | |
194 | previews: `test${server.internalServerNumber}/previews/`, | |
195 | thumbnails: `test${server.internalServerNumber}/thumbnails/`, | |
196 | torrents: `test${server.internalServerNumber}/torrents/`, | |
197 | captions: `test${server.internalServerNumber}/captions/`, | |
89cd1275 C |
198 | cache: `test${server.internalServerNumber}/cache/`, |
199 | plugins: `test${server.internalServerNumber}/plugins/` | |
86ebdf8c C |
200 | }, |
201 | admin: { | |
913b1d71 | 202 | email: `admin${server.internalServerNumber}@example.com` |
c655c9ef C |
203 | }, |
204 | live: { | |
205 | rtmp: { | |
206 | port: server.rtmpPort | |
207 | } | |
86ebdf8c | 208 | } |
7c3b7976 | 209 | }) |
86ebdf8c C |
210 | } |
211 | ||
212 | if (configOverrideArg !== undefined) { | |
213 | Object.assign(configOverride, configOverrideArg) | |
fdbda9e3 C |
214 | } |
215 | ||
7c3b7976 C |
216 | // Share the environment |
217 | const env = Object.create(process.env) | |
218 | env['NODE_ENV'] = 'test' | |
219 | env['NODE_APP_INSTANCE'] = server.internalServerNumber.toString() | |
86ebdf8c C |
220 | env['NODE_CONFIG'] = JSON.stringify(configOverride) |
221 | ||
bd2e2f11 | 222 | const forkOptions = { |
0e1dc3e7 | 223 | silent: true, |
7c3b7976 | 224 | env, |
bd2e2f11 C |
225 | detached: true, |
226 | execArgv: options.execArgv || [] | |
0e1dc3e7 C |
227 | } |
228 | ||
229 | return new Promise<ServerInfo>(res => { | |
bd2e2f11 | 230 | server.app = fork(join(root(), 'dist', 'server.js'), args, forkOptions) |
42e1ec25 C |
231 | server.app.stdout.on('data', function onStdout (data) { |
232 | let dontContinue = false | |
233 | ||
234 | // Capture things if we want to | |
235 | for (const key of Object.keys(regexps)) { | |
a1587156 | 236 | const regexp = regexps[key] |
42e1ec25 C |
237 | const matches = data.toString().match(regexp) |
238 | if (matches !== null) { | |
a1587156 C |
239 | if (key === 'client_id') server.client.id = matches[1] |
240 | else if (key === 'client_secret') server.client.secret = matches[1] | |
241 | else if (key === 'user_username') server.user.username = matches[1] | |
242 | else if (key === 'user_password') server.user.password = matches[1] | |
42e1ec25 C |
243 | } |
244 | } | |
245 | ||
246 | // Check if all required sentences are here | |
247 | for (const key of Object.keys(serverRunString)) { | |
a1587156 C |
248 | if (data.toString().indexOf(key) !== -1) serverRunString[key] = true |
249 | if (serverRunString[key] === false) dontContinue = true | |
42e1ec25 C |
250 | } |
251 | ||
252 | // If no, there is maybe one thing not already initialized (client/user credentials generation...) | |
253 | if (dontContinue === true) return | |
254 | ||
bd2e2f11 | 255 | if (options.hideLogs === false) { |
5a547f69 C |
256 | console.log(data.toString()) |
257 | } else { | |
258 | server.app.stdout.removeListener('data', onStdout) | |
259 | } | |
42e1ec25 C |
260 | |
261 | process.on('exit', () => { | |
262 | try { | |
263 | process.kill(server.app.pid) | |
264 | } catch { /* empty */ } | |
dc094603 | 265 | }) |
42e1ec25 C |
266 | |
267 | res(server) | |
268 | }) | |
0e1dc3e7 C |
269 | }) |
270 | } | |
271 | ||
e5565833 | 272 | async function reRunServer (server: ServerInfo, configOverride?: any) { |
913b1d71 | 273 | const newServer = await runServer(server, configOverride) |
7bc29171 C |
274 | server.app = newServer.app |
275 | ||
276 | return server | |
277 | } | |
278 | ||
d1a2ce5e | 279 | async function checkTmpIsEmpty (server: ServerInfo) { |
f6d6e7f8 | 280 | await checkDirectoryIsEmpty(server, 'tmp', [ 'plugins-global.css', 'hls', 'resumable-uploads' ]) |
d1a2ce5e C |
281 | |
282 | if (await pathExists(join('test' + server.internalServerNumber, 'tmp', 'hls'))) { | |
283 | await checkDirectoryIsEmpty(server, 'tmp/hls') | |
284 | } | |
09209296 C |
285 | } |
286 | ||
8d2be0ed | 287 | async function checkDirectoryIsEmpty (server: ServerInfo, directory: string, exceptions: string[] = []) { |
48f07b4a | 288 | const testDirectory = 'test' + server.internalServerNumber |
89231874 | 289 | |
09209296 | 290 | const directoryPath = join(root(), testDirectory, directory) |
89231874 | 291 | |
8d2be0ed | 292 | const directoryExists = await pathExists(directoryPath) |
89231874 C |
293 | expect(directoryExists).to.be.true |
294 | ||
295 | const files = await readdir(directoryPath) | |
8d2be0ed C |
296 | const filtered = files.filter(f => exceptions.includes(f) === false) |
297 | ||
298 | expect(filtered).to.have.lengthOf(0) | |
89231874 C |
299 | } |
300 | ||
0e1dc3e7 C |
301 | function killallServers (servers: ServerInfo[]) { |
302 | for (const server of servers) { | |
7c3b7976 C |
303 | if (!server.app) continue |
304 | ||
0e1dc3e7 | 305 | process.kill(-server.app.pid) |
7c3b7976 | 306 | server.app = null |
0e1dc3e7 C |
307 | } |
308 | } | |
309 | ||
83ef31fe | 310 | async function cleanupTests (servers: ServerInfo[]) { |
86ebdf8c C |
311 | killallServers(servers) |
312 | ||
83ef31fe C |
313 | if (isGithubCI()) { |
314 | await ensureDir('artifacts') | |
315 | } | |
316 | ||
86ebdf8c C |
317 | const p: Promise<any>[] = [] |
318 | for (const server of servers) { | |
83ef31fe C |
319 | if (isGithubCI()) { |
320 | const origin = await buildServerDirectory(server, 'logs/peertube.log') | |
321 | const destname = `peertube-${server.internalServerNumber}.log` | |
322 | console.log('Saving logs %s.', destname) | |
323 | ||
324 | await copy(origin, join('artifacts', destname)) | |
325 | } | |
326 | ||
86ebdf8c C |
327 | if (server.parallel) { |
328 | p.push(flushTests(server.internalServerNumber)) | |
329 | } | |
7c3b7976 C |
330 | |
331 | if (server.customConfigFile) { | |
332 | p.push(remove(server.customConfigFile)) | |
333 | } | |
86ebdf8c C |
334 | } |
335 | ||
336 | return Promise.all(p) | |
337 | } | |
338 | ||
1b05d82d | 339 | async function waitUntilLog (server: ServerInfo, str: string, count = 1, strictCount = true) { |
ca5c612b | 340 | const logfile = buildServerDirectory(server, 'logs/peertube.log') |
792e5b8e C |
341 | |
342 | while (true) { | |
343 | const buf = await readFile(logfile) | |
344 | ||
345 | const matches = buf.toString().match(new RegExp(str, 'g')) | |
346 | if (matches && matches.length === count) return | |
1b05d82d | 347 | if (matches && strictCount === false && matches.length >= count) return |
792e5b8e C |
348 | |
349 | await wait(1000) | |
350 | } | |
351 | } | |
352 | ||
d218e7de | 353 | async function getServerFileSize (server: ServerInfo, subPath: string) { |
ca5c612b | 354 | const path = buildServerDirectory(server, subPath) |
d218e7de C |
355 | |
356 | return getFileSize(path) | |
357 | } | |
358 | ||
78d62f4d C |
359 | function makePingRequest (server: ServerInfo) { |
360 | return makeGetRequest({ | |
361 | url: server.url, | |
362 | path: '/api/v1/ping', | |
363 | statusCodeExpected: 200 | |
364 | }) | |
365 | } | |
366 | ||
0e1dc3e7 C |
367 | // --------------------------------------------------------------------------- |
368 | ||
369 | export { | |
09209296 | 370 | checkDirectoryIsEmpty, |
89231874 | 371 | checkTmpIsEmpty, |
d218e7de | 372 | getServerFileSize, |
0e1dc3e7 | 373 | ServerInfo, |
7c3b7976 | 374 | parallelTests, |
86ebdf8c | 375 | cleanupTests, |
0e1dc3e7 C |
376 | flushAndRunMultipleServers, |
377 | flushTests, | |
78d62f4d | 378 | makePingRequest, |
210feb6c | 379 | flushAndRunServer, |
7bc29171 | 380 | killallServers, |
792e5b8e C |
381 | reRunServer, |
382 | waitUntilLog | |
0e1dc3e7 | 383 | } |