]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/utils/server/servers.ts
Add ability to unfederate a local video (on blacklist)
[github/Chocobozzz/PeerTube.git] / shared / utils / server / servers.ts
1 /* tslint:disable:no-unused-expression */
2
3 import { ChildProcess, exec, fork } from 'child_process'
4 import { join } from 'path'
5 import { root, wait } from '../miscs/miscs'
6 import { readdir, readFile } from 'fs-extra'
7 import { existsSync } from 'fs'
8 import { expect } from 'chai'
9
10 interface ServerInfo {
11 app: ChildProcess,
12 url: string
13 host: string
14 serverNumber: number
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
32 name: string
33 account: {
34 name: string
35 }
36 }
37
38 remoteVideo?: {
39 id: number
40 uuid: string
41 }
42 }
43
44 function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
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++) {
60 runServer(j, configOverride).then(app => anotherServerDone(j, app))
61 }
62 })
63 })
64 }
65
66 function 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
76 function runServer (serverNumber: number, configOverride?: Object, args = []) {
77 const server: ServerInfo = {
78 app: null,
79 serverNumber: serverNumber,
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 = {
94 'Server listening': false
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()
110
111 if (configOverride !== undefined) {
112 env['NODE_CONFIG'] = JSON.stringify(configOverride)
113 }
114
115 const options = {
116 silent: true,
117 env: env,
118 detached: true
119 }
120
121 return new Promise<ServerInfo>(res => {
122 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
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)
148
149 process.on('exit', () => process.kill(server.app.pid))
150
151 res(server)
152 })
153
154 })
155 }
156
157 async function reRunServer (server: ServerInfo, configOverride?: any) {
158 const newServer = await runServer(server.serverNumber, configOverride)
159 server.app = newServer.app
160
161 return server
162 }
163
164 async 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
176 function killallServers (servers: ServerInfo[]) {
177 for (const server of servers) {
178 process.kill(-server.app.pid)
179 }
180 }
181
182 async 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
195 // ---------------------------------------------------------------------------
196
197 export {
198 checkTmpIsEmpty,
199 ServerInfo,
200 flushAndRunMultipleServers,
201 flushTests,
202 runServer,
203 killallServers,
204 reRunServer,
205 waitUntilLog
206 }