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