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