]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/utils/server/servers.ts
Add test regarding tmp directory
[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 res(server)
149 })
150 })
151 }
152
153 async function reRunServer (server: ServerInfo, configOverride?: any) {
154 const newServer = await runServer(server.serverNumber, configOverride)
155 server.app = newServer.app
156
157 return server
158 }
159
160 async 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
172 function killallServers (servers: ServerInfo[]) {
173 for (const server of servers) {
174 process.kill(-server.app.pid)
175 }
176 }
177
178 async 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
191 // ---------------------------------------------------------------------------
192
193 export {
194 checkTmpIsEmpty,
195 ServerInfo,
196 flushAndRunMultipleServers,
197 flushTests,
198 runServer,
199 killallServers,
200 reRunServer,
201 waitUntilLog
202 }