1 import { ChildProcess, fork } from 'child_process'
2 import { copy } from 'fs-extra'
3 import { join } from 'path'
4 import { root } from '@server/helpers/core-utils'
5 import { randomInt } from '@shared/core-utils'
6 import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '../../models/videos'
7 import { BulkCommand } from '../bulk'
8 import { CLICommand } from '../cli'
9 import { CustomPagesCommand } from '../custom-pages'
10 import { FeedCommand } from '../feeds'
11 import { LogsCommand } from '../logs'
12 import { parallelTests, SQLCommand } from '../miscs'
13 import { AbusesCommand } from '../moderation'
14 import { OverviewsCommand } from '../overviews'
15 import { SearchCommand } from '../search'
16 import { SocketIOCommand } from '../socket'
17 import { AccountsCommand, BlocklistCommand, LoginCommand, NotificationsCommand, SubscriptionsCommand, UsersCommand } from '../users'
21 ChangeOwnershipCommand,
28 StreamingPlaylistsCommand,
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'
37 import { PluginsCommand } from './plugins-command'
38 import { RedundancyCommand } from './redundancy-command'
39 import { ServersCommand } from './servers-command'
40 import { StatsCommand } from './stats-command'
42 export type RunServerOptions = {
45 peertubeArgs?: string[]
48 export class PeerTubeServer {
59 internalServerNumber: number
62 customConfigFile?: string
76 channel?: VideoChannel
79 videoCreated?: VideoCreateResult
80 videoDetails?: VideoDetails
82 videos?: { id: number, uuid: string }[]
90 customPage?: CustomPagesCommand
93 abuses?: AbusesCommand
94 overviews?: OverviewsCommand
95 search?: SearchCommand
96 contactForm?: ContactFormCommand
98 follows?: FollowsCommand
100 plugins?: PluginsCommand
101 redundancy?: RedundancyCommand
103 config?: ConfigCommand
104 socketIO?: SocketIOCommand
105 accounts?: AccountsCommand
106 blocklist?: BlocklistCommand
107 subscriptions?: SubscriptionsCommand
109 services?: ServicesCommand
110 blacklist?: BlacklistCommand
111 captions?: CaptionsCommand
112 changeOwnership?: ChangeOwnershipCommand
113 playlists?: PlaylistsCommand
114 history?: HistoryCommand
115 imports?: ImportsCommand
116 streamingPlaylists?: StreamingPlaylistsCommand
117 channels?: ChannelsCommand
118 comments?: CommentsCommand
120 notifications?: NotificationsCommand
121 servers?: ServersCommand
124 videos?: VideosCommand
126 constructor (options: { serverNumber: number } | { url: string }) {
127 if ((options as any).url) {
128 this.setUrl((options as any).url)
130 this.setServerNumber((options as any).serverNumber)
144 this.assignCommands()
147 setServerNumber (serverNumber: number) {
148 this.serverNumber = serverNumber
150 this.parallel = parallelTests()
152 this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber
153 this.rtmpPort = this.parallel ? this.randomRTMP() : 1936
154 this.port = 9000 + this.internalServerNumber
156 this.url = `http://localhost:${this.port}`
157 this.host = `localhost:${this.port}`
158 this.hostname = 'localhost'
161 setUrl (url: string) {
162 const parsed = new URL(url)
165 this.host = parsed.host
166 this.hostname = parsed.hostname
167 this.port = parseInt(parsed.port)
170 async flushAndRun (configOverride?: Object, options: RunServerOptions = {}) {
171 await ServersCommand.flushTests(this.internalServerNumber)
173 return this.run(configOverride, options)
176 async run (configOverrideArg?: any, options: RunServerOptions = {}) {
177 // These actions are async so we need to be sure that they have both been done
178 const serverRunString = {
179 'HTTP server listening': false
181 const key = 'Database peertube_test' + this.internalServerNumber + ' is ready'
182 serverRunString[key] = false
185 client_id: 'Client id: (.+)',
186 client_secret: 'Client secret: (.+)',
187 user_username: 'Username: (.+)',
188 user_password: 'User password: (.+)'
191 await this.assignCustomConfigFile()
193 const configOverride = this.buildConfigOverride()
195 if (configOverrideArg !== undefined) {
196 Object.assign(configOverride, configOverrideArg)
199 // Share the environment
200 const env = Object.create(process.env)
201 env['NODE_ENV'] = 'test'
202 env['NODE_APP_INSTANCE'] = this.internalServerNumber.toString()
203 env['NODE_CONFIG'] = JSON.stringify(configOverride)
205 const forkOptions = {
209 execArgv: options.nodeArgs || []
212 return new Promise<void>(res => {
215 this.app = fork(join(root(), 'dist', 'server.js'), options.peertubeArgs || [], forkOptions)
216 this.app.stdout.on('data', function onStdout (data) {
217 let dontContinue = false
219 // Capture things if we want to
220 for (const key of Object.keys(regexps)) {
221 const regexp = regexps[key]
222 const matches = data.toString().match(regexp)
223 if (matches !== null) {
224 if (key === 'client_id') self.store.client.id = matches[1]
225 else if (key === 'client_secret') self.store.client.secret = matches[1]
226 else if (key === 'user_username') self.store.user.username = matches[1]
227 else if (key === 'user_password') self.store.user.password = matches[1]
231 // Check if all required sentences are here
232 for (const key of Object.keys(serverRunString)) {
233 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
234 if (serverRunString[key] === false) dontContinue = true
237 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
238 if (dontContinue === true) return
240 if (options.hideLogs === false) {
241 console.log(data.toString())
243 self.app.stdout.removeListener('data', onStdout)
246 process.on('exit', () => {
248 process.kill(self.app.pid)
249 } catch { /* empty */ }
258 if (!this.app) return
260 await this.sql.cleanup()
262 process.kill(-this.app.pid)
267 private randomServer () {
271 return randomInt(low, high)
274 private randomRTMP () {
278 return randomInt(low, high)
281 private async assignCustomConfigFile () {
282 if (this.internalServerNumber === this.serverNumber) return
284 const basePath = join(root(), 'config')
286 const tmpConfigFile = join(basePath, `test-${this.internalServerNumber}.yaml`)
287 await copy(join(basePath, `test-${this.serverNumber}.yaml`), tmpConfigFile)
289 this.customConfigFile = tmpConfigFile
292 private buildConfigOverride () {
293 if (!this.parallel) return {}
303 suffix: '_test' + this.internalServerNumber
306 tmp: `test${this.internalServerNumber}/tmp/`,
307 avatars: `test${this.internalServerNumber}/avatars/`,
308 videos: `test${this.internalServerNumber}/videos/`,
309 streaming_playlists: `test${this.internalServerNumber}/streaming-playlists/`,
310 redundancy: `test${this.internalServerNumber}/redundancy/`,
311 logs: `test${this.internalServerNumber}/logs/`,
312 previews: `test${this.internalServerNumber}/previews/`,
313 thumbnails: `test${this.internalServerNumber}/thumbnails/`,
314 torrents: `test${this.internalServerNumber}/torrents/`,
315 captions: `test${this.internalServerNumber}/captions/`,
316 cache: `test${this.internalServerNumber}/cache/`,
317 plugins: `test${this.internalServerNumber}/plugins/`
320 email: `admin${this.internalServerNumber}@example.com`
330 private assignCommands () {
331 this.bulk = new BulkCommand(this)
332 this.cli = new CLICommand(this)
333 this.customPage = new CustomPagesCommand(this)
334 this.feed = new FeedCommand(this)
335 this.logs = new LogsCommand(this)
336 this.abuses = new AbusesCommand(this)
337 this.overviews = new OverviewsCommand(this)
338 this.search = new SearchCommand(this)
339 this.contactForm = new ContactFormCommand(this)
340 this.debug = new DebugCommand(this)
341 this.follows = new FollowsCommand(this)
342 this.jobs = new JobsCommand(this)
343 this.plugins = new PluginsCommand(this)
344 this.redundancy = new RedundancyCommand(this)
345 this.stats = new StatsCommand(this)
346 this.config = new ConfigCommand(this)
347 this.socketIO = new SocketIOCommand(this)
348 this.accounts = new AccountsCommand(this)
349 this.blocklist = new BlocklistCommand(this)
350 this.subscriptions = new SubscriptionsCommand(this)
351 this.live = new LiveCommand(this)
352 this.services = new ServicesCommand(this)
353 this.blacklist = new BlacklistCommand(this)
354 this.captions = new CaptionsCommand(this)
355 this.changeOwnership = new ChangeOwnershipCommand(this)
356 this.playlists = new PlaylistsCommand(this)
357 this.history = new HistoryCommand(this)
358 this.imports = new ImportsCommand(this)
359 this.streamingPlaylists = new StreamingPlaylistsCommand(this)
360 this.channels = new ChannelsCommand(this)
361 this.comments = new CommentsCommand(this)
362 this.sql = new SQLCommand(this)
363 this.notifications = new NotificationsCommand(this)
364 this.servers = new ServersCommand(this)
365 this.login = new LoginCommand(this)
366 this.users = new UsersCommand(this)
367 this.videos = new VideosCommand(this)