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 { ObjectStorageCommand } from './object-storage-command'
52 import { PluginsCommand } from './plugins-command'
53 import { RedundancyCommand } from './redundancy-command'
54 import { ServersCommand } from './servers-command'
55 import { StatsCommand } from './stats-command'
57 export type RunServerOptions = {
60 peertubeArgs?: string[]
61 env?: { [ id: string ]: string }
64 export class PeerTubeServer {
76 internalServerNumber: number
79 customConfigFile?: string
93 channel?: VideoChannel
94 videoChannelSync?: Partial<VideoChannelSync>
97 videoCreated?: VideoCreateResult
98 videoDetails?: VideoDetails
100 videos?: { id: number, uuid: string }[]
104 refreshToken?: string
108 customPage?: CustomPagesCommand
111 abuses?: AbusesCommand
112 overviews?: OverviewsCommand
113 search?: SearchCommand
114 contactForm?: ContactFormCommand
116 follows?: FollowsCommand
118 metrics?: MetricsCommand
119 plugins?: PluginsCommand
120 redundancy?: RedundancyCommand
122 config?: ConfigCommand
123 socketIO?: SocketIOCommand
124 accounts?: AccountsCommand
125 blocklist?: BlocklistCommand
126 subscriptions?: SubscriptionsCommand
128 services?: ServicesCommand
129 blacklist?: BlacklistCommand
130 captions?: CaptionsCommand
131 changeOwnership?: ChangeOwnershipCommand
132 playlists?: PlaylistsCommand
133 history?: HistoryCommand
134 imports?: ImportsCommand
135 channelSyncs?: ChannelSyncsCommand
136 streamingPlaylists?: StreamingPlaylistsCommand
137 channels?: ChannelsCommand
138 comments?: CommentsCommand
139 notifications?: NotificationsCommand
140 servers?: ServersCommand
143 objectStorage?: ObjectStorageCommand
144 videoStudio?: VideoStudioCommand
145 videos?: VideosCommand
146 videoStats?: VideoStatsCommand
148 twoFactor?: TwoFactorCommand
149 videoToken?: VideoTokenCommand
150 registrations?: RegistrationsCommand
152 runners?: RunnersCommand
153 runnerRegistrationTokens?: RunnerRegistrationTokensCommand
154 runnerJobs?: RunnerJobsCommand
156 constructor (options: { serverNumber: number } | { url: string }) {
157 if ((options as any).url) {
158 this.setUrl((options as any).url)
160 this.setServerNumber((options as any).serverNumber)
174 this.assignCommands()
177 setServerNumber (serverNumber: number) {
178 this.serverNumber = serverNumber
180 this.parallel = parallelTests()
182 this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber
183 this.rtmpPort = this.parallel ? this.randomRTMP() : 1936
184 this.rtmpsPort = this.parallel ? this.randomRTMP() : 1937
185 this.port = 9000 + this.internalServerNumber
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'
192 setUrl (url: string) {
193 const parsed = new URL(url)
196 this.host = parsed.host
197 this.hostname = parsed.hostname
198 this.port = parseInt(parsed.port)
201 getDirectoryPath (directoryName: string) {
202 const testDirectory = 'test' + this.internalServerNumber
204 return join(root(), testDirectory, directoryName)
207 async flushAndRun (configOverride?: object, options: RunServerOptions = {}) {
208 await ServersCommand.flushTests(this.internalServerNumber)
210 return this.run(configOverride, options)
213 async run (configOverrideArg?: any, options: RunServerOptions = {}) {
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
218 const key = 'Database peertube_test' + this.internalServerNumber + ' is ready'
219 serverRunString[key] = false
222 client_id: 'Client id: (.+)',
223 client_secret: 'Client secret: (.+)',
224 user_username: 'Username: (.+)',
225 user_password: 'User password: (.+)'
228 await this.assignCustomConfigFile()
230 const configOverride = this.buildConfigOverride()
232 if (configOverrideArg !== undefined) {
233 Object.assign(configOverride, configOverrideArg)
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)
243 Object.assign(env, options.env)
246 const execArgv = options.nodeArgs || []
247 // FIXME: too slow :/
248 // execArgv.push('--enable-source-maps')
250 const forkOptions = {
257 const peertubeArgs = options.peertubeArgs || []
259 return new Promise<void>((res, rej) => {
261 let aggregatedLogs = ''
263 this.app = fork(join(root(), 'dist', 'server.js'), peertubeArgs, forkOptions)
265 const onPeerTubeExit = () => rej(new Error('Process exited:\n' + aggregatedLogs))
266 const onParentExit = () => {
267 if (!this.app?.pid) return
270 process.kill(self.app.pid)
271 } catch { /* empty */ }
274 this.app.on('exit', onPeerTubeExit)
275 process.on('exit', onParentExit)
277 this.app.stdout.on('data', function onStdout (data) {
278 let dontContinue = false
280 const log: string = data.toString()
281 aggregatedLogs += log
283 // Capture things if we want to
284 for (const key of Object.keys(regexps)) {
285 const regexp = regexps[key]
286 const matches = log.match(regexp)
287 if (matches !== null) {
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]
295 // Check if all required sentences are here
296 for (const key of Object.keys(serverRunString)) {
297 if (log.includes(key)) serverRunString[key] = true
298 if (serverRunString[key] === false) dontContinue = true
301 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
302 if (dontContinue === true) return
304 if (options.hideLogs === false) {
307 process.removeListener('exit', onParentExit)
308 self.app.stdout.removeListener('data', onStdout)
309 self.app.removeListener('exit', onPeerTubeExit)
318 if (!this.app) return Promise.resolve()
320 process.kill(-this.app.pid)
324 return Promise.resolve()
327 private randomServer () {
331 return randomInt(low, high)
334 private randomRTMP () {
338 return randomInt(low, high)
341 private async assignCustomConfigFile () {
342 if (this.internalServerNumber === this.serverNumber) return
344 const basePath = join(root(), 'config')
346 const tmpConfigFile = join(basePath, `test-${this.internalServerNumber}.yaml`)
347 await copy(join(basePath, `test-${this.serverNumber}.yaml`), tmpConfigFile)
349 this.customConfigFile = tmpConfigFile
352 private buildConfigOverride () {
353 if (!this.parallel) return {}
363 suffix: '_test' + this.internalServerNumber
366 tmp: this.getDirectoryPath('tmp') + '/',
367 tmp_persistent: this.getDirectoryPath('tmp-persistent') + '/',
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') + '/'
383 email: `admin${this.internalServerNumber}@example.com`
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)
406 this.metrics = new MetricsCommand(this)
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)
423 this.channelSyncs = new ChannelSyncsCommand(this)
424 this.streamingPlaylists = new StreamingPlaylistsCommand(this)
425 this.channels = new ChannelsCommand(this)
426 this.comments = new CommentsCommand(this)
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)
432 this.objectStorage = new ObjectStorageCommand(this)
433 this.videoStudio = new VideoStudioCommand(this)
434 this.videoStats = new VideoStatsCommand(this)
435 this.views = new ViewsCommand(this)
436 this.twoFactor = new TwoFactorCommand(this)
437 this.videoToken = new VideoTokenCommand(this)
438 this.registrations = new RegistrationsCommand(this)
440 this.runners = new RunnersCommand(this)
441 this.runnerRegistrationTokens = new RunnerRegistrationTokensCommand(this)
442 this.runnerJobs = new RunnerJobsCommand(this)