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