aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/utils/server/servers.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-04-15 15:26:15 +0200
committerChocobozzz <me@florianbigard.com>2019-04-24 16:25:52 +0200
commit94565d52bb2883e09f16d1363170ac9c0dccb7a1 (patch)
tree3dcd20cd7b5a5cca80bce32b655cdbfaddf7aa59 /shared/utils/server/servers.ts
parent4ee7a4c9ac9280cda930a281c2d5a9a4c409cc14 (diff)
downloadPeerTube-94565d52bb2883e09f16d1363170ac9c0dccb7a1.tar.gz
PeerTube-94565d52bb2883e09f16d1363170ac9c0dccb7a1.tar.zst
PeerTube-94565d52bb2883e09f16d1363170ac9c0dccb7a1.zip
Shared utils -> extra-utils
Because they need dev dependencies
Diffstat (limited to 'shared/utils/server/servers.ts')
-rw-r--r--shared/utils/server/servers.ts219
1 files changed, 0 insertions, 219 deletions
diff --git a/shared/utils/server/servers.ts b/shared/utils/server/servers.ts
deleted file mode 100644
index 5288d253a..000000000
--- a/shared/utils/server/servers.ts
+++ /dev/null
@@ -1,219 +0,0 @@
1/* tslint:disable:no-unused-expression */
2
3import { ChildProcess, exec, fork } from 'child_process'
4import { join } from 'path'
5import { root, wait } from '../miscs/miscs'
6import { readdir, readFile } from 'fs-extra'
7import { existsSync } from 'fs'
8import { expect } from 'chai'
9import { VideoChannel } from '../../models/videos'
10
11interface 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
48function 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 flushTests()
62 .then(() => {
63 for (let j = 1; j <= totalServers; j++) {
64 runServer(j, configOverride).then(app => anotherServerDone(j, app))
65 }
66 })
67 })
68}
69
70function 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
80function runServer (serverNumber: number, configOverride?: Object, args = []) {
81 const server: ServerInfo = {
82 app: null,
83 serverNumber: serverNumber,
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 = {
98 'Server listening': false
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()
114
115 if (configOverride !== undefined) {
116 env['NODE_CONFIG'] = JSON.stringify(configOverride)
117 }
118
119 const options = {
120 silent: true,
121 env: env,
122 detached: true
123 }
124
125 return new Promise<ServerInfo>(res => {
126 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
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)
152
153 process.on('exit', () => {
154 try {
155 process.kill(server.app.pid)
156 } catch { /* empty */ }
157 })
158
159 res(server)
160 })
161
162 })
163}
164
165async function reRunServer (server: ServerInfo, configOverride?: any) {
166 const newServer = await runServer(server.serverNumber, configOverride)
167 server.app = newServer.app
168
169 return server
170}
171
172async function checkTmpIsEmpty (server: ServerInfo) {
173 return checkDirectoryIsEmpty(server, 'tmp')
174}
175
176async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) {
177 const testDirectory = 'test' + server.serverNumber
178
179 const directoryPath = join(root(), testDirectory, directory)
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
188function killallServers (servers: ServerInfo[]) {
189 for (const server of servers) {
190 process.kill(-server.app.pid)
191 }
192}
193
194async 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
207// ---------------------------------------------------------------------------
208
209export {
210 checkDirectoryIsEmpty,
211 checkTmpIsEmpty,
212 ServerInfo,
213 flushAndRunMultipleServers,
214 flushTests,
215 runServer,
216 killallServers,
217 reRunServer,
218 waitUntilLog
219}