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