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