]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/servers.ts
Client: handle multiple file resolutions
[github/Chocobozzz/PeerTube.git] / server / tests / utils / servers.ts
1 import { ChildProcess, exec, fork } from 'child_process'
2 import { join } from 'path'
3
4 interface ServerInfo {
5 app: ChildProcess,
6 url: string
7 host: string
8 serverNumber: number
9
10 client: {
11 id: string,
12 secret: string
13 }
14
15 user: {
16 username: string,
17 password: string,
18 email?: string
19 }
20
21 accessToken?: string
22
23 video?: {
24 id: number
25 uuid: string
26 }
27
28 remoteVideo?: {
29 id: number
30 uuid: string
31 }
32 }
33
34 async function flushAndRunMultipleServers (totalServers) {
35 let apps = []
36 let i = 0
37
38 return new Promise<ServerInfo[]>(res => {
39 function anotherServerDone (serverNumber, app) {
40 apps[serverNumber - 1] = app
41 i++
42 if (i === totalServers) {
43 return res(apps)
44 }
45 }
46
47 flushTests()
48 .then(() => {
49 for (let j = 1; j <= totalServers; j++) {
50 // For the virtual buffer
51 setTimeout(() => {
52 runServer(j).then(app => anotherServerDone(j, app))
53 }, 1000 * (j - 1))
54 }
55 })
56 })
57 }
58
59 function flushTests () {
60 return new Promise<void>((res, rej) => {
61 return exec('npm run clean:server:test', err => {
62 if (err) return rej(err)
63
64 return res()
65 })
66 })
67 }
68
69 function runServer (serverNumber: number, configOverride?: Object) {
70 const server: ServerInfo = {
71 app: null,
72 serverNumber: serverNumber,
73 url: `http://localhost:${9000 + serverNumber}`,
74 host: `localhost:${9000 + serverNumber}`,
75 client: {
76 id: null,
77 secret: null
78 },
79 user: {
80 username: null,
81 password: null
82 }
83 }
84
85 // These actions are async so we need to be sure that they have both been done
86 const serverRunString = {
87 'Server listening on port': false
88 }
89 const key = 'Database peertube_test' + serverNumber + ' is ready'
90 serverRunString[key] = false
91
92 const regexps = {
93 client_id: 'Client id: (.+)',
94 client_secret: 'Client secret: (.+)',
95 user_username: 'Username: (.+)',
96 user_password: 'User password: (.+)'
97 }
98
99 // Share the environment
100 const env = Object.create(process.env)
101 env['NODE_ENV'] = 'test'
102 env['NODE_APP_INSTANCE'] = serverNumber.toString()
103
104 if (configOverride !== undefined) {
105 env['NODE_CONFIG'] = JSON.stringify(configOverride)
106 }
107
108 const options = {
109 silent: true,
110 env: env,
111 detached: true
112 }
113
114 return new Promise<ServerInfo>(res => {
115 server.app = fork(join(__dirname, '..', '..', '..', 'dist', 'server.js'), [], options)
116 server.app.stdout.on('data', function onStdout (data) {
117 let dontContinue = false
118
119 // Capture things if we want to
120 for (const key of Object.keys(regexps)) {
121 const regexp = regexps[key]
122 const matches = data.toString().match(regexp)
123 if (matches !== null) {
124 if (key === 'client_id') server.client.id = matches[1]
125 else if (key === 'client_secret') server.client.secret = matches[1]
126 else if (key === 'user_username') server.user.username = matches[1]
127 else if (key === 'user_password') server.user.password = matches[1]
128 }
129 }
130
131 // Check if all required sentences are here
132 for (const key of Object.keys(serverRunString)) {
133 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
134 if (serverRunString[key] === false) dontContinue = true
135 }
136
137 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
138 if (dontContinue === true) return
139
140 server.app.stdout.removeListener('data', onStdout)
141 res(server)
142 })
143 })
144 }
145
146 function killallServers (servers: ServerInfo[]) {
147 for (const server of servers) {
148 process.kill(-server.app.pid)
149 }
150 }
151
152 // ---------------------------------------------------------------------------
153
154 export {
155 ServerInfo,
156 flushAndRunMultipleServers,
157 flushTests,
158 runServer,
159 killallServers
160 }