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 { SQLCommand } from '../miscs'
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'
20 ChangeOwnershipCommand,
28 StreamingPlaylistsCommand,
33 import { CommentsCommand } from '../videos/comments-command'
34 import { VideoStatsCommand } from '../videos/video-stats-command'
35 import { ConfigCommand } from './config-command'
36 import { ContactFormCommand } from './contact-form-command'
37 import { DebugCommand } from './debug-command'
38 import { FollowsCommand } from './follows-command'
39 import { JobsCommand } from './jobs-command'
40 import { MetricsCommand } from './metrics-command'
41 import { ObjectStorageCommand } from './object-storage-command'
42 import { PluginsCommand } from './plugins-command'
43 import { RedundancyCommand } from './redundancy-command'
44 import { ServersCommand } from './servers-command'
45 import { StatsCommand } from './stats-command'
47 export type RunServerOptions = {
50 peertubeArgs?: string[]
51 env?: { [ id: string ]: string }
54 export class PeerTubeServer {
66 internalServerNumber: number
69 customConfigFile?: string
83 channel?: VideoChannel
84 videoChannelSync?: Partial<VideoChannelSync>
87 videoCreated?: VideoCreateResult
88 videoDetails?: VideoDetails
90 videos?: { id: number, uuid: string }[]
98 customPage?: CustomPagesCommand
101 abuses?: AbusesCommand
102 overviews?: OverviewsCommand
103 search?: SearchCommand
104 contactForm?: ContactFormCommand
106 follows?: FollowsCommand
108 metrics?: MetricsCommand
109 plugins?: PluginsCommand
110 redundancy?: RedundancyCommand
112 config?: ConfigCommand
113 socketIO?: SocketIOCommand
114 accounts?: AccountsCommand
115 blocklist?: BlocklistCommand
116 subscriptions?: SubscriptionsCommand
118 services?: ServicesCommand
119 blacklist?: BlacklistCommand
120 captions?: CaptionsCommand
121 changeOwnership?: ChangeOwnershipCommand
122 playlists?: PlaylistsCommand
123 history?: HistoryCommand
124 imports?: ImportsCommand
125 channelSyncs?: ChannelSyncsCommand
126 streamingPlaylists?: StreamingPlaylistsCommand
127 channels?: ChannelsCommand
128 comments?: CommentsCommand
130 notifications?: NotificationsCommand
131 servers?: ServersCommand
134 objectStorage?: ObjectStorageCommand
135 videoStudio?: VideoStudioCommand
136 videos?: VideosCommand
137 videoStats?: VideoStatsCommand
140 constructor (options: { serverNumber: number } | { url: string }) {
141 if ((options as any).url) {
142 this.setUrl((options as any).url)
144 this.setServerNumber((options as any).serverNumber)
158 this.assignCommands()
161 setServerNumber (serverNumber: number) {
162 this.serverNumber = serverNumber
164 this.parallel = parallelTests()
166 this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber
167 this.rtmpPort = this.parallel ? this.randomRTMP() : 1936
168 this.rtmpsPort = this.parallel ? this.randomRTMP() : 1937
169 this.port = 9000 + this.internalServerNumber
171 this.url = `http://localhost:${this.port}`
172 this.host = `localhost:${this.port}`
173 this.hostname = 'localhost'
176 setUrl (url: string) {
177 const parsed = new URL(url)
180 this.host = parsed.host
181 this.hostname = parsed.hostname
182 this.port = parseInt(parsed.port)
185 getDirectoryPath (directoryName: string) {
186 const testDirectory = 'test' + this.internalServerNumber
188 return join(root(), testDirectory, directoryName)
191 async flushAndRun (configOverride?: Object, options: RunServerOptions = {}) {
192 await ServersCommand.flushTests(this.internalServerNumber)
194 return this.run(configOverride, options)
197 async run (configOverrideArg?: any, options: RunServerOptions = {}) {
198 // These actions are async so we need to be sure that they have both been done
199 const serverRunString = {
200 'HTTP server listening': false
202 const key = 'Database peertube_test' + this.internalServerNumber + ' is ready'
203 serverRunString[key] = false
206 client_id: 'Client id: (.+)',
207 client_secret: 'Client secret: (.+)',
208 user_username: 'Username: (.+)',
209 user_password: 'User password: (.+)'
212 await this.assignCustomConfigFile()
214 const configOverride = this.buildConfigOverride()
216 if (configOverrideArg !== undefined) {
217 Object.assign(configOverride, configOverrideArg)
220 // Share the environment
221 const env = Object.create(process.env)
222 env['NODE_ENV'] = 'test'
223 env['NODE_APP_INSTANCE'] = this.internalServerNumber.toString()
224 env['NODE_CONFIG'] = JSON.stringify(configOverride)
227 Object.assign(env, options.env)
230 const execArgv = options.nodeArgs || []
231 // FIXME: too slow :/
232 // execArgv.push('--enable-source-maps')
234 const forkOptions = {
241 const peertubeArgs = options.peertubeArgs || []
243 return new Promise<void>((res, rej) => {
245 let aggregatedLogs = ''
247 this.app = fork(join(root(), 'dist', 'server.js'), peertubeArgs, forkOptions)
249 const onPeerTubeExit = () => rej(new Error('Process exited:\n' + aggregatedLogs))
250 const onParentExit = () => {
251 if (!this.app || !this.app.pid) return
254 process.kill(self.app.pid)
255 } catch { /* empty */ }
258 this.app.on('exit', onPeerTubeExit)
259 process.on('exit', onParentExit)
261 this.app.stdout.on('data', function onStdout (data) {
262 let dontContinue = false
264 const log: string = data.toString()
265 aggregatedLogs += log
267 // Capture things if we want to
268 for (const key of Object.keys(regexps)) {
269 const regexp = regexps[key]
270 const matches = log.match(regexp)
271 if (matches !== null) {
272 if (key === 'client_id') self.store.client.id = matches[1]
273 else if (key === 'client_secret') self.store.client.secret = matches[1]
274 else if (key === 'user_username') self.store.user.username = matches[1]
275 else if (key === 'user_password') self.store.user.password = matches[1]
279 // Check if all required sentences are here
280 for (const key of Object.keys(serverRunString)) {
281 if (log.includes(key)) serverRunString[key] = true
282 if (serverRunString[key] === false) dontContinue = true
285 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
286 if (dontContinue === true) return
288 if (options.hideLogs === false) {
291 process.removeListener('exit', onParentExit)
292 self.app.stdout.removeListener('data', onStdout)
293 self.app.removeListener('exit', onPeerTubeExit)
302 if (!this.app) return
304 await this.sql.cleanup()
306 process.kill(-this.app.pid)
311 private randomServer () {
315 return randomInt(low, high)
318 private randomRTMP () {
322 return randomInt(low, high)
325 private async assignCustomConfigFile () {
326 if (this.internalServerNumber === this.serverNumber) return
328 const basePath = join(root(), 'config')
330 const tmpConfigFile = join(basePath, `test-${this.internalServerNumber}.yaml`)
331 await copy(join(basePath, `test-${this.serverNumber}.yaml`), tmpConfigFile)
333 this.customConfigFile = tmpConfigFile
336 private buildConfigOverride () {
337 if (!this.parallel) return {}
347 suffix: '_test' + this.internalServerNumber
350 tmp: this.getDirectoryPath('tmp') + '/',
351 bin: this.getDirectoryPath('bin') + '/',
352 avatars: this.getDirectoryPath('avatars') + '/',
353 videos: this.getDirectoryPath('videos') + '/',
354 streaming_playlists: this.getDirectoryPath('streaming-playlists') + '/',
355 redundancy: this.getDirectoryPath('redundancy') + '/',
356 logs: this.getDirectoryPath('logs') + '/',
357 previews: this.getDirectoryPath('previews') + '/',
358 thumbnails: this.getDirectoryPath('thumbnails') + '/',
359 torrents: this.getDirectoryPath('torrents') + '/',
360 captions: this.getDirectoryPath('captions') + '/',
361 cache: this.getDirectoryPath('cache') + '/',
362 plugins: this.getDirectoryPath('plugins') + '/',
363 well_known: this.getDirectoryPath('well-known') + '/'
366 email: `admin${this.internalServerNumber}@example.com`
376 private assignCommands () {
377 this.bulk = new BulkCommand(this)
378 this.cli = new CLICommand(this)
379 this.customPage = new CustomPagesCommand(this)
380 this.feed = new FeedCommand(this)
381 this.logs = new LogsCommand(this)
382 this.abuses = new AbusesCommand(this)
383 this.overviews = new OverviewsCommand(this)
384 this.search = new SearchCommand(this)
385 this.contactForm = new ContactFormCommand(this)
386 this.debug = new DebugCommand(this)
387 this.follows = new FollowsCommand(this)
388 this.jobs = new JobsCommand(this)
389 this.metrics = new MetricsCommand(this)
390 this.plugins = new PluginsCommand(this)
391 this.redundancy = new RedundancyCommand(this)
392 this.stats = new StatsCommand(this)
393 this.config = new ConfigCommand(this)
394 this.socketIO = new SocketIOCommand(this)
395 this.accounts = new AccountsCommand(this)
396 this.blocklist = new BlocklistCommand(this)
397 this.subscriptions = new SubscriptionsCommand(this)
398 this.live = new LiveCommand(this)
399 this.services = new ServicesCommand(this)
400 this.blacklist = new BlacklistCommand(this)
401 this.captions = new CaptionsCommand(this)
402 this.changeOwnership = new ChangeOwnershipCommand(this)
403 this.playlists = new PlaylistsCommand(this)
404 this.history = new HistoryCommand(this)
405 this.imports = new ImportsCommand(this)
406 this.channelSyncs = new ChannelSyncsCommand(this)
407 this.streamingPlaylists = new StreamingPlaylistsCommand(this)
408 this.channels = new ChannelsCommand(this)
409 this.comments = new CommentsCommand(this)
410 this.sql = new SQLCommand(this)
411 this.notifications = new NotificationsCommand(this)
412 this.servers = new ServersCommand(this)
413 this.login = new LoginCommand(this)
414 this.users = new UsersCommand(this)
415 this.videos = new VideosCommand(this)
416 this.objectStorage = new ObjectStorageCommand(this)
417 this.videoStudio = new VideoStudioCommand(this)
418 this.videoStats = new VideoStatsCommand(this)
419 this.views = new ViewsCommand(this)