]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/utils/server/servers.ts
Update credits
[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)
148 res(server)
149 })
150 })
151}
152
e5565833
C
153async function reRunServer (server: ServerInfo, configOverride?: any) {
154 const newServer = await runServer(server.serverNumber, configOverride)
7bc29171
C
155 server.app = newServer.app
156
157 return server
158}
159
89231874
C
160async function checkTmpIsEmpty (server: ServerInfo) {
161 const testDirectory = 'test' + server.serverNumber
162
163 const directoryPath = join(root(), testDirectory, 'tmp')
164
165 const directoryExists = existsSync(directoryPath)
166 expect(directoryExists).to.be.true
167
168 const files = await readdir(directoryPath)
169 expect(files).to.have.lengthOf(0)
170}
171
0e1dc3e7
C
172function killallServers (servers: ServerInfo[]) {
173 for (const server of servers) {
174 process.kill(-server.app.pid)
175 }
176}
177
792e5b8e
C
178async function waitUntilLog (server: ServerInfo, str: string, count = 1) {
179 const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log')
180
181 while (true) {
182 const buf = await readFile(logfile)
183
184 const matches = buf.toString().match(new RegExp(str, 'g'))
185 if (matches && matches.length === count) return
186
187 await wait(1000)
188 }
189}
190
0e1dc3e7
C
191// ---------------------------------------------------------------------------
192
193export {
89231874 194 checkTmpIsEmpty,
0e1dc3e7
C
195 ServerInfo,
196 flushAndRunMultipleServers,
197 flushTests,
198 runServer,
7bc29171 199 killallServers,
792e5b8e
C
200 reRunServer,
201 waitUntilLog
0e1dc3e7 202}