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