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