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