1 import { ChildProcess, fork } from 'child_process'
2 import { copy } from 'fs-extra'
3 import { join } from 'path'
4 import { parallelTests, randomInt, root } from '@shared/core-utils'
5 import { Video, VideoChannel, VideoChannelSync, VideoCreateResult, VideoDetails } from '@shared/models'
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'
11 import { AbusesCommand } from '../moderation'
12 import { OverviewsCommand } from '../overviews'
13 import { RunnerJobsCommand, RunnerRegistrationTokensCommand, RunnersCommand } from '../runners'
14 import { SearchCommand } from '../search'
15 import { SocketIOCommand } from '../socket'
29 ChangeOwnershipCommand,
37 StreamingPlaylistsCommand,
43 import { CommentsCommand } from '../videos/comments-command'
44 import { VideoStatsCommand } from '../videos/video-stats-command'
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'
50 import { MetricsCommand } from './metrics-command'
51 import { PluginsCommand } from './plugins-command'
52 import { RedundancyCommand } from './redundancy-command'
53 import { ServersCommand } from './servers-command'
54 import { StatsCommand } from './stats-command'
56 export type RunServerOptions = {
59 peertubeArgs?: string[]
60 env?: { [ id: string ]: string }
63 export class PeerTubeServer {
75 internalServerNumber: number
78 customConfigFile?: string
92 channel?: VideoChannel
93 videoChannelSync?: Partial<VideoChannelSync>
96 videoCreated?: VideoCreateResult
97 videoDetails?: VideoDetails
99 videos?: { id: number, uuid: string }[]
103 refreshToken?: string
107 customPage?: CustomPagesCommand
110 abuses?: AbusesCommand
111 overviews?: OverviewsCommand
112 search?: SearchCommand
113 contactForm?: ContactFormCommand
115 follows?: FollowsCommand
117 metrics?: MetricsCommand
118 plugins?: PluginsCommand
119 redundancy?: RedundancyCommand
121 config?: ConfigCommand
122 socketIO?: SocketIOCommand
123 accounts?: AccountsCommand
124 blocklist?: BlocklistCommand
125 subscriptions?: SubscriptionsCommand
127 services?: ServicesCommand
128 blacklist?: BlacklistCommand
129 captions?: CaptionsCommand
130 changeOwnership?: ChangeOwnershipCommand
131 playlists?: PlaylistsCommand
132 history?: HistoryCommand
133 imports?: ImportsCommand
134 channelSyncs?: ChannelSyncsCommand
135 streamingPlaylists?: StreamingPlaylistsCommand
136 channels?: ChannelsCommand
137 comments?: CommentsCommand
138 notifications?: NotificationsCommand
139 servers?: ServersCommand
142 videoStudio?: VideoStudioCommand
143 videos?: VideosCommand
144 videoStats?: VideoStatsCommand
146 twoFactor?: TwoFactorCommand
147 videoToken?: VideoTokenCommand
148 registrations?: RegistrationsCommand
150 runners?: RunnersCommand
151 runnerRegistrationTokens?: RunnerRegistrationTokensCommand
152 runnerJobs?: RunnerJobsCommand
154 constructor (options: { serverNumber: number } | { url: string }) {
155 if ((options as any).url) {
156 this.setUrl((options as any).url)
158 this.setServerNumber((options as any).serverNumber)
172 this.assignCommands()
175 setServerNumber (serverNumber: number) {
176 this.serverNumber = serverNumber
178 this.parallel = parallelTests()
180 this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber
181 this.rtmpPort = this.parallel ? this.randomRTMP() : 1936
182 this.rtmpsPort = this.parallel ? this.randomRTMP() : 1937
183 this.port = 9000 + this.internalServerNumber
185 this.url = `http://127.0.0.1:${this.port}`
186 this.host = `127.0.0.1:${this.port}`
187 this.hostname = '127.0.0.1'
190 setUrl (url: string) {
191 const parsed = new URL(url)
194 this.host = parsed.host
195 this.hostname = parsed.hostname
196 this.port = parseInt(parsed.port)
199 getDirectoryPath (directoryName: string) {
200 const testDirectory = 'test' + this.internalServerNumber
202 return join(root(), testDirectory, directoryName)
205 async flushAndRun (configOverride?: object, options: RunServerOptions = {}) {
206 await ServersCommand.flushTests(this.internalServerNumber)
208 return this.run(configOverride, options)
211 async run (configOverrideArg?: any, options: RunServerOptions = {}) {
212 // These actions are async so we need to be sure that they have both been done
213 const serverRunString = {
214 'HTTP server listening': false
216 const key = 'Database peertube_test' + this.internalServerNumber + ' is ready'
217 serverRunString[key] = false
220 client_id: 'Client id: (.+)',
221 client_secret: 'Client secret: (.+)',
222 user_username: 'Username: (.+)',
223 user_password: 'User password: (.+)'
226 await this.assignCustomConfigFile()
228 const configOverride = this.buildConfigOverride()
230 if (configOverrideArg !== undefined) {
231 Object.assign(configOverride, configOverrideArg)
234 // Share the environment
235 const env = Object.create(process.env)
236 env['NODE_ENV'] = 'test'
237 env['NODE_APP_INSTANCE'] = this.internalServerNumber.toString()
238 env['NODE_CONFIG'] = JSON.stringify(configOverride)
241 Object.assign(env, options.env)
244 const execArgv = options.nodeArgs || []
245 // FIXME: too slow :/
246 // execArgv.push('--enable-source-maps')
248 const forkOptions = {
255 const peertubeArgs = options.peertubeArgs || []
257 return new Promise<void>((res, rej) => {
259 let aggregatedLogs = ''
261 this.app = fork(join(root(), 'dist', 'server.js'), peertubeArgs, forkOptions)
263 const onPeerTubeExit = () => rej(new Error('Process exited:\n' + aggregatedLogs))
264 const onParentExit = () => {
265 if (!this.app?.pid) return
268 process.kill(self.app.pid)
269 } catch { /* empty */ }
272 this.app.on('exit', onPeerTubeExit)
273 process.on('exit', onParentExit)
275 this.app.stdout.on('data', function onStdout (data) {
276 let dontContinue = false
278 const log: string = data.toString()
279 aggregatedLogs += log
281 // Capture things if we want to
282 for (const key of Object.keys(regexps)) {
283 const regexp = regexps[key]
284 const matches = log.match(regexp)
285 if (matches !== null) {
286 if (key === 'client_id') self.store.client.id = matches[1]
287 else if (key === 'client_secret') self.store.client.secret = matches[1]
288 else if (key === 'user_username') self.store.user.username = matches[1]
289 else if (key === 'user_password') self.store.user.password = matches[1]
293 // Check if all required sentences are here
294 for (const key of Object.keys(serverRunString)) {
295 if (log.includes(key)) serverRunString[key] = true
296 if (serverRunString[key] === false) dontContinue = true
299 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
300 if (dontContinue === true) return
302 if (options.hideLogs === false) {
305 process.removeListener('exit', onParentExit)
306 self.app.stdout.removeListener('data', onStdout)
307 self.app.removeListener('exit', onPeerTubeExit)
316 if (!this.app) return Promise.resolve()
318 process.kill(this.app.pid)
322 return Promise.resolve()
325 private randomServer () {
329 return randomInt(low, high)
332 private randomRTMP () {
336 return randomInt(low, high)
339 private async assignCustomConfigFile () {
340 if (this.internalServerNumber === this.serverNumber) return
342 const basePath = join(root(), 'config')
344 const tmpConfigFile = join(basePath, `test-${this.internalServerNumber}.yaml`)
345 await copy(join(basePath, `test-${this.serverNumber}.yaml`), tmpConfigFile)
347 this.customConfigFile = tmpConfigFile
350 private buildConfigOverride () {
351 if (!this.parallel) return {}
361 suffix: '_test' + this.internalServerNumber
364 tmp: this.getDirectoryPath('tmp') + '/',
365 tmp_persistent: this.getDirectoryPath('tmp-persistent') + '/',
366 bin: this.getDirectoryPath('bin') + '/',
367 avatars: this.getDirectoryPath('avatars') + '/',
368 videos: this.getDirectoryPath('videos') + '/',
369 streaming_playlists: this.getDirectoryPath('streaming-playlists') + '/',
370 redundancy: this.getDirectoryPath('redundancy') + '/',
371 logs: this.getDirectoryPath('logs') + '/',
372 previews: this.getDirectoryPath('previews') + '/',
373 thumbnails: this.getDirectoryPath('thumbnails') + '/',
374 torrents: this.getDirectoryPath('torrents') + '/',
375 captions: this.getDirectoryPath('captions') + '/',
376 cache: this.getDirectoryPath('cache') + '/',
377 plugins: this.getDirectoryPath('plugins') + '/',
378 well_known: this.getDirectoryPath('well-known') + '/'
381 email: `admin${this.internalServerNumber}@example.com`
391 private assignCommands () {
392 this.bulk = new BulkCommand(this)
393 this.cli = new CLICommand(this)
394 this.customPage = new CustomPagesCommand(this)
395 this.feed = new FeedCommand(this)
396 this.logs = new LogsCommand(this)
397 this.abuses = new AbusesCommand(this)
398 this.overviews = new OverviewsCommand(this)
399 this.search = new SearchCommand(this)
400 this.contactForm = new ContactFormCommand(this)
401 this.debug = new DebugCommand(this)
402 this.follows = new FollowsCommand(this)
403 this.jobs = new JobsCommand(this)
404 this.metrics = new MetricsCommand(this)
405 this.plugins = new PluginsCommand(this)
406 this.redundancy = new RedundancyCommand(this)
407 this.stats = new StatsCommand(this)
408 this.config = new ConfigCommand(this)
409 this.socketIO = new SocketIOCommand(this)
410 this.accounts = new AccountsCommand(this)
411 this.blocklist = new BlocklistCommand(this)
412 this.subscriptions = new SubscriptionsCommand(this)
413 this.live = new LiveCommand(this)
414 this.services = new ServicesCommand(this)
415 this.blacklist = new BlacklistCommand(this)
416 this.captions = new CaptionsCommand(this)
417 this.changeOwnership = new ChangeOwnershipCommand(this)
418 this.playlists = new PlaylistsCommand(this)
419 this.history = new HistoryCommand(this)
420 this.imports = new ImportsCommand(this)
421 this.channelSyncs = new ChannelSyncsCommand(this)
422 this.streamingPlaylists = new StreamingPlaylistsCommand(this)
423 this.channels = new ChannelsCommand(this)
424 this.comments = new CommentsCommand(this)
425 this.notifications = new NotificationsCommand(this)
426 this.servers = new ServersCommand(this)
427 this.login = new LoginCommand(this)
428 this.users = new UsersCommand(this)
429 this.videos = new VideosCommand(this)
430 this.videoStudio = new VideoStudioCommand(this)
431 this.videoStats = new VideoStatsCommand(this)
432 this.views = new ViewsCommand(this)
433 this.twoFactor = new TwoFactorCommand(this)
434 this.videoToken = new VideoTokenCommand(this)
435 this.registrations = new RegistrationsCommand(this)
437 this.runners = new RunnersCommand(this)
438 this.runnerRegistrationTokens = new RunnerRegistrationTokensCommand(this)
439 this.runnerJobs = new RunnerJobsCommand(this)