]>
Commit | Line | Data |
---|---|---|
254d3579 C |
1 | import { ChildProcess, fork } from 'child_process' |
2 | import { copy } from 'fs-extra' | |
3 | import { join } from 'path' | |
c55e3d72 C |
4 | import { parallelTests, randomInt, root } from '@shared/core-utils' |
5 | import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '@shared/models' | |
254d3579 C |
6 | import { BulkCommand } from '../bulk' |
7 | import { CLICommand } from '../cli' | |
8 | import { CustomPagesCommand } from '../custom-pages' | |
9 | import { FeedCommand } from '../feeds' | |
10 | import { LogsCommand } from '../logs' | |
c55e3d72 | 11 | import { SQLCommand } from '../miscs' |
254d3579 C |
12 | import { AbusesCommand } from '../moderation' |
13 | import { OverviewsCommand } from '../overviews' | |
14 | import { SearchCommand } from '../search' | |
15 | import { SocketIOCommand } from '../socket' | |
16 | import { AccountsCommand, BlocklistCommand, LoginCommand, NotificationsCommand, SubscriptionsCommand, UsersCommand } from '../users' | |
17 | import { | |
18 | BlacklistCommand, | |
19 | CaptionsCommand, | |
20 | ChangeOwnershipCommand, | |
21 | ChannelsCommand, | |
22 | HistoryCommand, | |
23 | ImportsCommand, | |
24 | LiveCommand, | |
25 | PlaylistsCommand, | |
26 | ServicesCommand, | |
27 | StreamingPlaylistsCommand, | |
c729caf6 | 28 | VideoEditorCommand, |
254d3579 C |
29 | VideosCommand |
30 | } from '../videos' | |
31 | import { CommentsCommand } from '../videos/comments-command' | |
32 | import { ConfigCommand } from './config-command' | |
33 | import { ContactFormCommand } from './contact-form-command' | |
34 | import { DebugCommand } from './debug-command' | |
35 | import { FollowsCommand } from './follows-command' | |
36 | import { JobsCommand } from './jobs-command' | |
c55e3d72 | 37 | import { ObjectStorageCommand } from './object-storage-command' |
254d3579 C |
38 | import { PluginsCommand } from './plugins-command' |
39 | import { RedundancyCommand } from './redundancy-command' | |
40 | import { ServersCommand } from './servers-command' | |
41 | import { StatsCommand } from './stats-command' | |
42 | ||
43 | export type RunServerOptions = { | |
44 | hideLogs?: boolean | |
2e980ed3 C |
45 | nodeArgs?: string[] |
46 | peertubeArgs?: string[] | |
0305db28 | 47 | env?: { [ id: string ]: string } |
254d3579 C |
48 | } |
49 | ||
50 | export class PeerTubeServer { | |
51 | app?: ChildProcess | |
52 | ||
53 | url: string | |
54 | host?: string | |
55 | hostname?: string | |
56 | port?: number | |
57 | ||
58 | rtmpPort?: number | |
df1db951 | 59 | rtmpsPort?: number |
254d3579 C |
60 | |
61 | parallel?: boolean | |
62 | internalServerNumber: number | |
63 | ||
64 | serverNumber?: number | |
65 | customConfigFile?: string | |
66 | ||
67 | store?: { | |
68 | client?: { | |
69 | id?: string | |
70 | secret?: string | |
71 | } | |
72 | ||
73 | user?: { | |
74 | username: string | |
75 | password: string | |
76 | email?: string | |
77 | } | |
78 | ||
79 | channel?: VideoChannel | |
80 | ||
83903cb6 C |
81 | video?: Video |
82 | videoCreated?: VideoCreateResult | |
83 | videoDetails?: VideoDetails | |
254d3579 C |
84 | |
85 | videos?: { id: number, uuid: string }[] | |
86 | } | |
87 | ||
88 | accessToken?: string | |
89 | refreshToken?: string | |
90 | ||
91 | bulk?: BulkCommand | |
92 | cli?: CLICommand | |
93 | customPage?: CustomPagesCommand | |
94 | feed?: FeedCommand | |
95 | logs?: LogsCommand | |
96 | abuses?: AbusesCommand | |
97 | overviews?: OverviewsCommand | |
98 | search?: SearchCommand | |
99 | contactForm?: ContactFormCommand | |
100 | debug?: DebugCommand | |
101 | follows?: FollowsCommand | |
102 | jobs?: JobsCommand | |
103 | plugins?: PluginsCommand | |
104 | redundancy?: RedundancyCommand | |
105 | stats?: StatsCommand | |
106 | config?: ConfigCommand | |
107 | socketIO?: SocketIOCommand | |
108 | accounts?: AccountsCommand | |
109 | blocklist?: BlocklistCommand | |
110 | subscriptions?: SubscriptionsCommand | |
111 | live?: LiveCommand | |
112 | services?: ServicesCommand | |
113 | blacklist?: BlacklistCommand | |
114 | captions?: CaptionsCommand | |
115 | changeOwnership?: ChangeOwnershipCommand | |
116 | playlists?: PlaylistsCommand | |
117 | history?: HistoryCommand | |
118 | imports?: ImportsCommand | |
119 | streamingPlaylists?: StreamingPlaylistsCommand | |
120 | channels?: ChannelsCommand | |
121 | comments?: CommentsCommand | |
122 | sql?: SQLCommand | |
123 | notifications?: NotificationsCommand | |
124 | servers?: ServersCommand | |
125 | login?: LoginCommand | |
126 | users?: UsersCommand | |
0305db28 | 127 | objectStorage?: ObjectStorageCommand |
c729caf6 | 128 | videoEditor?: VideoEditorCommand |
254d3579 C |
129 | videos?: VideosCommand |
130 | ||
131 | constructor (options: { serverNumber: number } | { url: string }) { | |
132 | if ((options as any).url) { | |
133 | this.setUrl((options as any).url) | |
134 | } else { | |
135 | this.setServerNumber((options as any).serverNumber) | |
136 | } | |
137 | ||
138 | this.store = { | |
139 | client: { | |
140 | id: null, | |
141 | secret: null | |
142 | }, | |
143 | user: { | |
144 | username: null, | |
145 | password: null | |
146 | } | |
147 | } | |
148 | ||
149 | this.assignCommands() | |
150 | } | |
151 | ||
152 | setServerNumber (serverNumber: number) { | |
153 | this.serverNumber = serverNumber | |
154 | ||
155 | this.parallel = parallelTests() | |
156 | ||
157 | this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber | |
158 | this.rtmpPort = this.parallel ? this.randomRTMP() : 1936 | |
df1db951 | 159 | this.rtmpsPort = this.parallel ? this.randomRTMP() : 1937 |
254d3579 C |
160 | this.port = 9000 + this.internalServerNumber |
161 | ||
162 | this.url = `http://localhost:${this.port}` | |
163 | this.host = `localhost:${this.port}` | |
164 | this.hostname = 'localhost' | |
165 | } | |
166 | ||
167 | setUrl (url: string) { | |
168 | const parsed = new URL(url) | |
169 | ||
170 | this.url = url | |
171 | this.host = parsed.host | |
172 | this.hostname = parsed.hostname | |
173 | this.port = parseInt(parsed.port) | |
174 | } | |
175 | ||
2e980ed3 | 176 | async flushAndRun (configOverride?: Object, options: RunServerOptions = {}) { |
254d3579 C |
177 | await ServersCommand.flushTests(this.internalServerNumber) |
178 | ||
2e980ed3 | 179 | return this.run(configOverride, options) |
254d3579 C |
180 | } |
181 | ||
2e980ed3 | 182 | async run (configOverrideArg?: any, options: RunServerOptions = {}) { |
254d3579 C |
183 | // These actions are async so we need to be sure that they have both been done |
184 | const serverRunString = { | |
185 | 'HTTP server listening': false | |
186 | } | |
187 | const key = 'Database peertube_test' + this.internalServerNumber + ' is ready' | |
188 | serverRunString[key] = false | |
189 | ||
190 | const regexps = { | |
191 | client_id: 'Client id: (.+)', | |
192 | client_secret: 'Client secret: (.+)', | |
193 | user_username: 'Username: (.+)', | |
194 | user_password: 'User password: (.+)' | |
195 | } | |
196 | ||
197 | await this.assignCustomConfigFile() | |
198 | ||
199 | const configOverride = this.buildConfigOverride() | |
200 | ||
201 | if (configOverrideArg !== undefined) { | |
202 | Object.assign(configOverride, configOverrideArg) | |
203 | } | |
204 | ||
205 | // Share the environment | |
206 | const env = Object.create(process.env) | |
207 | env['NODE_ENV'] = 'test' | |
208 | env['NODE_APP_INSTANCE'] = this.internalServerNumber.toString() | |
209 | env['NODE_CONFIG'] = JSON.stringify(configOverride) | |
210 | ||
0305db28 JB |
211 | if (options.env) { |
212 | Object.assign(env, options.env) | |
213 | } | |
214 | ||
7298cad6 | 215 | const execArgv = options.nodeArgs || [] |
8f5a1f36 C |
216 | // FIXME: too slow :/ |
217 | // execArgv.push('--enable-source-maps') | |
7298cad6 | 218 | |
254d3579 C |
219 | const forkOptions = { |
220 | silent: true, | |
221 | env, | |
222 | detached: true, | |
7298cad6 | 223 | execArgv |
254d3579 C |
224 | } |
225 | ||
9270bd3a | 226 | const peertubeArgs = options.peertubeArgs || [] |
9270bd3a | 227 | |
0305db28 | 228 | return new Promise<void>((res, rej) => { |
08642a76 | 229 | const self = this |
f307255e | 230 | let aggregatedLogs = '' |
08642a76 | 231 | |
9270bd3a | 232 | this.app = fork(join(root(), 'dist', 'server.js'), peertubeArgs, forkOptions) |
0305db28 | 233 | |
f307255e | 234 | const onPeerTubeExit = () => rej(new Error('Process exited:\n' + aggregatedLogs)) |
706a450f C |
235 | const onParentExit = () => { |
236 | if (!this.app || !this.app.pid) return | |
237 | ||
238 | try { | |
239 | process.kill(self.app.pid) | |
240 | } catch { /* empty */ } | |
0305db28 JB |
241 | } |
242 | ||
706a450f C |
243 | this.app.on('exit', onPeerTubeExit) |
244 | process.on('exit', onParentExit) | |
0305db28 | 245 | |
254d3579 C |
246 | this.app.stdout.on('data', function onStdout (data) { |
247 | let dontContinue = false | |
248 | ||
f307255e C |
249 | const log: string = data.toString() |
250 | aggregatedLogs += log | |
251 | ||
254d3579 C |
252 | // Capture things if we want to |
253 | for (const key of Object.keys(regexps)) { | |
254 | const regexp = regexps[key] | |
f307255e | 255 | const matches = log.match(regexp) |
254d3579 | 256 | if (matches !== null) { |
08642a76 C |
257 | if (key === 'client_id') self.store.client.id = matches[1] |
258 | else if (key === 'client_secret') self.store.client.secret = matches[1] | |
259 | else if (key === 'user_username') self.store.user.username = matches[1] | |
260 | else if (key === 'user_password') self.store.user.password = matches[1] | |
254d3579 C |
261 | } |
262 | } | |
263 | ||
264 | // Check if all required sentences are here | |
265 | for (const key of Object.keys(serverRunString)) { | |
f307255e | 266 | if (log.includes(key)) serverRunString[key] = true |
254d3579 C |
267 | if (serverRunString[key] === false) dontContinue = true |
268 | } | |
269 | ||
270 | // If no, there is maybe one thing not already initialized (client/user credentials generation...) | |
271 | if (dontContinue === true) return | |
272 | ||
273 | if (options.hideLogs === false) { | |
f307255e | 274 | console.log(log) |
254d3579 | 275 | } else { |
706a450f | 276 | process.removeListener('exit', onParentExit) |
08642a76 | 277 | self.app.stdout.removeListener('data', onStdout) |
706a450f | 278 | self.app.removeListener('exit', onPeerTubeExit) |
254d3579 C |
279 | } |
280 | ||
254d3579 C |
281 | res() |
282 | }) | |
283 | }) | |
284 | } | |
285 | ||
286 | async kill () { | |
287 | if (!this.app) return | |
288 | ||
289 | await this.sql.cleanup() | |
290 | ||
291 | process.kill(-this.app.pid) | |
292 | ||
293 | this.app = null | |
294 | } | |
295 | ||
296 | private randomServer () { | |
297 | const low = 10 | |
298 | const high = 10000 | |
299 | ||
300 | return randomInt(low, high) | |
301 | } | |
302 | ||
303 | private randomRTMP () { | |
304 | const low = 1900 | |
305 | const high = 2100 | |
306 | ||
307 | return randomInt(low, high) | |
308 | } | |
309 | ||
310 | private async assignCustomConfigFile () { | |
311 | if (this.internalServerNumber === this.serverNumber) return | |
312 | ||
313 | const basePath = join(root(), 'config') | |
314 | ||
315 | const tmpConfigFile = join(basePath, `test-${this.internalServerNumber}.yaml`) | |
316 | await copy(join(basePath, `test-${this.serverNumber}.yaml`), tmpConfigFile) | |
317 | ||
318 | this.customConfigFile = tmpConfigFile | |
319 | } | |
320 | ||
321 | private buildConfigOverride () { | |
322 | if (!this.parallel) return {} | |
323 | ||
324 | return { | |
325 | listen: { | |
326 | port: this.port | |
327 | }, | |
328 | webserver: { | |
329 | port: this.port | |
330 | }, | |
331 | database: { | |
332 | suffix: '_test' + this.internalServerNumber | |
333 | }, | |
334 | storage: { | |
335 | tmp: `test${this.internalServerNumber}/tmp/`, | |
5678353d | 336 | bin: `test${this.internalServerNumber}/bin/`, |
254d3579 C |
337 | avatars: `test${this.internalServerNumber}/avatars/`, |
338 | videos: `test${this.internalServerNumber}/videos/`, | |
339 | streaming_playlists: `test${this.internalServerNumber}/streaming-playlists/`, | |
340 | redundancy: `test${this.internalServerNumber}/redundancy/`, | |
341 | logs: `test${this.internalServerNumber}/logs/`, | |
342 | previews: `test${this.internalServerNumber}/previews/`, | |
343 | thumbnails: `test${this.internalServerNumber}/thumbnails/`, | |
344 | torrents: `test${this.internalServerNumber}/torrents/`, | |
345 | captions: `test${this.internalServerNumber}/captions/`, | |
346 | cache: `test${this.internalServerNumber}/cache/`, | |
347 | plugins: `test${this.internalServerNumber}/plugins/` | |
348 | }, | |
349 | admin: { | |
350 | email: `admin${this.internalServerNumber}@example.com` | |
351 | }, | |
352 | live: { | |
353 | rtmp: { | |
354 | port: this.rtmpPort | |
355 | } | |
356 | } | |
357 | } | |
358 | } | |
359 | ||
360 | private assignCommands () { | |
361 | this.bulk = new BulkCommand(this) | |
362 | this.cli = new CLICommand(this) | |
363 | this.customPage = new CustomPagesCommand(this) | |
364 | this.feed = new FeedCommand(this) | |
365 | this.logs = new LogsCommand(this) | |
366 | this.abuses = new AbusesCommand(this) | |
367 | this.overviews = new OverviewsCommand(this) | |
368 | this.search = new SearchCommand(this) | |
369 | this.contactForm = new ContactFormCommand(this) | |
370 | this.debug = new DebugCommand(this) | |
371 | this.follows = new FollowsCommand(this) | |
372 | this.jobs = new JobsCommand(this) | |
373 | this.plugins = new PluginsCommand(this) | |
374 | this.redundancy = new RedundancyCommand(this) | |
375 | this.stats = new StatsCommand(this) | |
376 | this.config = new ConfigCommand(this) | |
377 | this.socketIO = new SocketIOCommand(this) | |
378 | this.accounts = new AccountsCommand(this) | |
379 | this.blocklist = new BlocklistCommand(this) | |
380 | this.subscriptions = new SubscriptionsCommand(this) | |
381 | this.live = new LiveCommand(this) | |
382 | this.services = new ServicesCommand(this) | |
383 | this.blacklist = new BlacklistCommand(this) | |
384 | this.captions = new CaptionsCommand(this) | |
385 | this.changeOwnership = new ChangeOwnershipCommand(this) | |
386 | this.playlists = new PlaylistsCommand(this) | |
387 | this.history = new HistoryCommand(this) | |
388 | this.imports = new ImportsCommand(this) | |
389 | this.streamingPlaylists = new StreamingPlaylistsCommand(this) | |
390 | this.channels = new ChannelsCommand(this) | |
391 | this.comments = new CommentsCommand(this) | |
392 | this.sql = new SQLCommand(this) | |
393 | this.notifications = new NotificationsCommand(this) | |
394 | this.servers = new ServersCommand(this) | |
395 | this.login = new LoginCommand(this) | |
396 | this.users = new UsersCommand(this) | |
397 | this.videos = new VideosCommand(this) | |
0305db28 | 398 | this.objectStorage = new ObjectStorageCommand(this) |
c729caf6 | 399 | this.videoEditor = new VideoEditorCommand(this) |
254d3579 C |
400 | } |
401 | } |