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