]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/servers.ts
Begin video watch design
[github/Chocobozzz/PeerTube.git] / server / tests / utils / servers.ts
CommitLineData
0e1dc3e7
C
1import { ChildProcess, exec, fork } from 'child_process'
2import { join } from 'path'
3
4interface ServerInfo {
5 app: ChildProcess,
6 url: string
7 host: string
fdbda9e3 8 serverNumber: number
0e1dc3e7
C
9
10 client: {
11 id: string,
12 secret: string
13 }
14
15 user: {
16 username: string,
17 password: string,
18 email?: string
19 }
20
21 accessToken?: string
22
23 video?: {
24 id: number
25 uuid: string
d8755eed 26 name: string
b1fa3eba 27 accountName: string
0e1dc3e7
C
28 }
29
30 remoteVideo?: {
31 id: number
32 uuid: string
33 }
34}
35
53abc4c2 36function flushAndRunMultipleServers (totalServers) {
0e1dc3e7
C
37 let apps = []
38 let i = 0
39
40 return new Promise<ServerInfo[]>(res => {
41 function anotherServerDone (serverNumber, app) {
42 apps[serverNumber - 1] = app
43 i++
44 if (i === totalServers) {
45 return res(apps)
46 }
47 }
48
49 flushTests()
50 .then(() => {
51 for (let j = 1; j <= totalServers; j++) {
52 // For the virtual buffer
53 setTimeout(() => {
54 runServer(j).then(app => anotherServerDone(j, app))
55 }, 1000 * (j - 1))
56 }
57 })
58 })
59}
60
61function flushTests () {
62 return new Promise<void>((res, rej) => {
63 return exec('npm run clean:server:test', err => {
64 if (err) return rej(err)
65
66 return res()
67 })
68 })
69}
70
fdbda9e3 71function runServer (serverNumber: number, configOverride?: Object) {
0e1dc3e7
C
72 const server: ServerInfo = {
73 app: null,
fdbda9e3 74 serverNumber: serverNumber,
0e1dc3e7
C
75 url: `http://localhost:${9000 + serverNumber}`,
76 host: `localhost:${9000 + serverNumber}`,
77 client: {
78 id: null,
79 secret: null
80 },
81 user: {
82 username: null,
83 password: null
84 }
85 }
86
87 // These actions are async so we need to be sure that they have both been done
88 const serverRunString = {
89 'Server listening on port': false
90 }
91 const key = 'Database peertube_test' + serverNumber + ' is ready'
92 serverRunString[key] = false
93
94 const regexps = {
95 client_id: 'Client id: (.+)',
96 client_secret: 'Client secret: (.+)',
97 user_username: 'Username: (.+)',
98 user_password: 'User password: (.+)'
99 }
100
101 // Share the environment
102 const env = Object.create(process.env)
103 env['NODE_ENV'] = 'test'
104 env['NODE_APP_INSTANCE'] = serverNumber.toString()
fdbda9e3
C
105
106 if (configOverride !== undefined) {
107 env['NODE_CONFIG'] = JSON.stringify(configOverride)
108 }
109
0e1dc3e7
C
110 const options = {
111 silent: true,
112 env: env,
113 detached: true
114 }
115
116 return new Promise<ServerInfo>(res => {
117 server.app = fork(join(__dirname, '..', '..', '..', 'dist', 'server.js'), [], options)
118 server.app.stdout.on('data', function onStdout (data) {
119 let dontContinue = false
120
121 // Capture things if we want to
122 for (const key of Object.keys(regexps)) {
123 const regexp = regexps[key]
124 const matches = data.toString().match(regexp)
125 if (matches !== null) {
126 if (key === 'client_id') server.client.id = matches[1]
127 else if (key === 'client_secret') server.client.secret = matches[1]
128 else if (key === 'user_username') server.user.username = matches[1]
129 else if (key === 'user_password') server.user.password = matches[1]
130 }
131 }
132
133 // Check if all required sentences are here
134 for (const key of Object.keys(serverRunString)) {
135 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
136 if (serverRunString[key] === false) dontContinue = true
137 }
138
139 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
140 if (dontContinue === true) return
141
142 server.app.stdout.removeListener('data', onStdout)
143 res(server)
144 })
145 })
146}
147
148function killallServers (servers: ServerInfo[]) {
149 for (const server of servers) {
150 process.kill(-server.app.pid)
151 }
152}
153
154// ---------------------------------------------------------------------------
155
156export {
157 ServerInfo,
158 flushAndRunMultipleServers,
159 flushTests,
160 runServer,
161 killallServers
162}