]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/server/server.ts
Add bin directory creation for parallel tests
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / server.ts
index 3c335b8e448ab77f7b52b6c8a663f59f0218fdbf..31224ebe9f70f1a0b2149da9f5206addca049f83 100644 (file)
@@ -38,11 +38,13 @@ import { PluginsCommand } from './plugins-command'
 import { RedundancyCommand } from './redundancy-command'
 import { ServersCommand } from './servers-command'
 import { StatsCommand } from './stats-command'
+import { ObjectStorageCommand } from './object-storage-command'
 
 export type RunServerOptions = {
   hideLogs?: boolean
   nodeArgs?: string[]
   peertubeArgs?: string[]
+  env?: { [ id: string ]: string }
 }
 
 export class PeerTubeServer {
@@ -54,6 +56,7 @@ export class PeerTubeServer {
   port?: number
 
   rtmpPort?: number
+  rtmpsPort?: number
 
   parallel?: boolean
   internalServerNumber: number
@@ -121,6 +124,7 @@ export class PeerTubeServer {
   servers?: ServersCommand
   login?: LoginCommand
   users?: UsersCommand
+  objectStorage?: ObjectStorageCommand
   videos?: VideosCommand
 
   constructor (options: { serverNumber: number } | { url: string }) {
@@ -151,6 +155,7 @@ export class PeerTubeServer {
 
     this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber
     this.rtmpPort = this.parallel ? this.randomRTMP() : 1936
+    this.rtmpsPort = this.parallel ? this.randomRTMP() : 1937
     this.port = 9000 + this.internalServerNumber
 
     this.url = `http://localhost:${this.port}`
@@ -202,6 +207,10 @@ export class PeerTubeServer {
     env['NODE_APP_INSTANCE'] = this.internalServerNumber.toString()
     env['NODE_CONFIG'] = JSON.stringify(configOverride)
 
+    if (options.env) {
+      Object.assign(env, options.env)
+    }
+
     const forkOptions = {
       silent: true,
       env,
@@ -209,10 +218,23 @@ export class PeerTubeServer {
       execArgv: options.nodeArgs || []
     }
 
-    return new Promise<void>(res => {
+    return new Promise<void>((res, rej) => {
       const self = this
 
       this.app = fork(join(root(), 'dist', 'server.js'), options.peertubeArgs || [], forkOptions)
+
+      const onPeerTubeExit = () => rej(new Error('Process exited'))
+      const onParentExit = () => {
+        if (!this.app || !this.app.pid) return
+
+        try {
+          process.kill(self.app.pid)
+        } catch { /* empty */ }
+      }
+
+      this.app.on('exit', onPeerTubeExit)
+      process.on('exit', onParentExit)
+
       this.app.stdout.on('data', function onStdout (data) {
         let dontContinue = false
 
@@ -240,15 +262,11 @@ export class PeerTubeServer {
         if (options.hideLogs === false) {
           console.log(data.toString())
         } else {
+          process.removeListener('exit', onParentExit)
           self.app.stdout.removeListener('data', onStdout)
+          self.app.removeListener('exit', onPeerTubeExit)
         }
 
-        process.on('exit', () => {
-          try {
-            process.kill(self.app.pid)
-          } catch { /* empty */ }
-        })
-
         res()
       })
     })
@@ -304,6 +322,7 @@ export class PeerTubeServer {
       },
       storage: {
         tmp: `test${this.internalServerNumber}/tmp/`,
+        bin: `test${this.internalServerNumber}/bin/`,
         avatars: `test${this.internalServerNumber}/avatars/`,
         videos: `test${this.internalServerNumber}/videos/`,
         streaming_playlists: `test${this.internalServerNumber}/streaming-playlists/`,
@@ -365,5 +384,6 @@ export class PeerTubeServer {
     this.login = new LoginCommand(this)
     this.users = new UsersCommand(this)
     this.videos = new VideosCommand(this)
+    this.objectStorage = new ObjectStorageCommand(this)
   }
 }