]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/utils/server/servers.ts
Add ability to unfederate a local video (on blacklist)
[github/Chocobozzz/PeerTube.git] / shared / utils / server / servers.ts
CommitLineData
89231874
C
1/* tslint:disable:no-unused-expression */
2
0e1dc3e7
C
3import { ChildProcess, exec, fork } from 'child_process'
4import { join } from 'path'
792e5b8e 5import { root, wait } from '../miscs/miscs'
89231874
C
6import { readdir, readFile } from 'fs-extra'
7import { existsSync } from 'fs'
8import { expect } from 'chai'
0e1dc3e7
C
9
10interface ServerInfo {
11 app: ChildProcess,
12 url: string
13 host: string
fdbda9e3 14 serverNumber: number
0e1dc3e7
C
15
16 client: {
17 id: string,
18 secret: string
19 }
20
21 user: {
22 username: string,
23 password: string,
24 email?: string
25 }
26
27 accessToken?: string
28
29 video?: {
30 id: number
31 uuid: string
d8755eed 32 name: string
b64c950a
C
33 account: {
34 name: string
35 }
0e1dc3e7
C
36 }
37
38 remoteVideo?: {
39 id: number
40 uuid: string
41 }
42}
43
b36f41ca 44function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
0e1dc3e7
C
45 let apps = []
46 let i = 0
47
48 return new Promise<ServerInfo[]>(res => {
49 function anotherServerDone (serverNumber, app) {
50 apps[serverNumber - 1] = app
51 i++
52 if (i === totalServers) {
53 return res(apps)
54 }
55 }
56
57 flushTests()
58 .then(() => {
59 for (let j = 1; j <= totalServers; j++) {
d5f044ce 60 runServer(j, configOverride).then(app => anotherServerDone(j, app))
0e1dc3e7
C
61 }
62 })
63 })
64}
65
66function flushTests () {
67 return new Promise<void>((res, rej) => {
68 return exec('npm run clean:server:test', err => {
69 if (err) return rej(err)
70
71 return res()
72 })
73 })
74}
75
b83b8dd5 76function runServer (serverNumber: number, configOverride?: Object, args = []) {
0e1dc3e7
C
77 const server: ServerInfo = {
78 app: null,
fdbda9e3 79 serverNumber: serverNumber,
0e1dc3e7
C
80 url: `http://localhost:${9000 + serverNumber}`,
81 host: `localhost:${9000 + serverNumber}`,
82 client: {
83 id: null,
84 secret: null
85 },
86 user: {
87 username: null,
88 password: null
89 }
90 }
91
92 // These actions are async so we need to be sure that they have both been done
93 const serverRunString = {
bf6e8e3e 94 'Server listening': false
0e1dc3e7
C
95 }
96 const key = 'Database peertube_test' + serverNumber + ' is ready'
97 serverRunString[key] = false
98
99 const regexps = {
100 client_id: 'Client id: (.+)',
101 client_secret: 'Client secret: (.+)',
102 user_username: 'Username: (.+)',
103 user_password: 'User password: (.+)'
104 }
105
106 // Share the environment
107 const env = Object.create(process.env)
108 env['NODE_ENV'] = 'test'
109 env['NODE_APP_INSTANCE'] = serverNumber.toString()
fdbda9e3
C
110
111 if (configOverride !== undefined) {
112 env['NODE_CONFIG'] = JSON.stringify(configOverride)
113 }
114
0e1dc3e7
C
115 const options = {
116 silent: true,
117 env: env,
118 detached: true
119 }
120
121 return new Promise<ServerInfo>(res => {
2a8c5d0a 122 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
0e1dc3e7
C
123 server.app.stdout.on('data', function onStdout (data) {
124 let dontContinue = false
125
126 // Capture things if we want to
127 for (const key of Object.keys(regexps)) {
128 const regexp = regexps[key]
129 const matches = data.toString().match(regexp)
130 if (matches !== null) {
131 if (key === 'client_id') server.client.id = matches[1]
132 else if (key === 'client_secret') server.client.secret = matches[1]
133 else if (key === 'user_username') server.user.username = matches[1]
134 else if (key === 'user_password') server.user.password = matches[1]
135 }
136 }
137
138 // Check if all required sentences are here
139 for (const key of Object.keys(serverRunString)) {
140 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
141 if (serverRunString[key] === false) dontContinue = true
142 }
143
144 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
145 if (dontContinue === true) return
146
147 server.app.stdout.removeListener('data', onStdout)
5abb9fbb
C
148
149 process.on('exit', () => process.kill(server.app.pid))
150
0e1dc3e7
C
151 res(server)
152 })
5abb9fbb 153
0e1dc3e7
C
154 })
155}
156
e5565833
C
157async function reRunServer (server: ServerInfo, configOverride?: any) {
158 const newServer = await runServer(server.serverNumber, configOverride)
7bc29171
C
159 server.app = newServer.app
160
161 return server
162}
163
89231874
C
164async function checkTmpIsEmpty (server: ServerInfo) {
165 const testDirectory = 'test' + server.serverNumber
166
167 const directoryPath = join(root(), testDirectory, 'tmp')
168
169 const directoryExists = existsSync(directoryPath)
170 expect(directoryExists).to.be.true
171
172 const files = await readdir(directoryPath)
173 expect(files).to.have.lengthOf(0)
174}
175
0e1dc3e7
C
176function killallServers (servers: ServerInfo[]) {
177 for (const server of servers) {
178 process.kill(-server.app.pid)
179 }
180}
181
792e5b8e
C
182async function waitUntilLog (server: ServerInfo, str: string, count = 1) {
183 const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log')
184
185 while (true) {
186 const buf = await readFile(logfile)
187
188 const matches = buf.toString().match(new RegExp(str, 'g'))
189 if (matches && matches.length === count) return
190
191 await wait(1000)
192 }
193}
194
0e1dc3e7
C
195// ---------------------------------------------------------------------------
196
197export {
89231874 198 checkTmpIsEmpty,
0e1dc3e7
C
199 ServerInfo,
200 flushAndRunMultipleServers,
201 flushTests,
202 runServer,
7bc29171 203 killallServers,
792e5b8e
C
204 reRunServer,
205 waitUntilLog
0e1dc3e7 206}