diff options
Diffstat (limited to 'server/tests/utils/servers.ts')
-rw-r--r-- | server/tests/utils/servers.ts | 162 |
1 files changed, 0 insertions, 162 deletions
diff --git a/server/tests/utils/servers.ts b/server/tests/utils/servers.ts deleted file mode 100644 index 8340fbc18..000000000 --- a/server/tests/utils/servers.ts +++ /dev/null | |||
@@ -1,162 +0,0 @@ | |||
1 | import { ChildProcess, exec, fork } from 'child_process' | ||
2 | import { join } from 'path' | ||
3 | |||
4 | interface ServerInfo { | ||
5 | app: ChildProcess, | ||
6 | url: string | ||
7 | host: string | ||
8 | serverNumber: number | ||
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 | ||
26 | name: string | ||
27 | accountName: string | ||
28 | } | ||
29 | |||
30 | remoteVideo?: { | ||
31 | id: number | ||
32 | uuid: string | ||
33 | } | ||
34 | } | ||
35 | |||
36 | function flushAndRunMultipleServers (totalServers) { | ||
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 | |||
61 | function 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 | |||
71 | function runServer (serverNumber: number, configOverride?: Object) { | ||
72 | const server: ServerInfo = { | ||
73 | app: null, | ||
74 | serverNumber: serverNumber, | ||
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() | ||
105 | |||
106 | if (configOverride !== undefined) { | ||
107 | env['NODE_CONFIG'] = JSON.stringify(configOverride) | ||
108 | } | ||
109 | |||
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 | |||
148 | function killallServers (servers: ServerInfo[]) { | ||
149 | for (const server of servers) { | ||
150 | process.kill(-server.app.pid) | ||
151 | } | ||
152 | } | ||
153 | |||
154 | // --------------------------------------------------------------------------- | ||
155 | |||
156 | export { | ||
157 | ServerInfo, | ||
158 | flushAndRunMultipleServers, | ||
159 | flushTests, | ||
160 | runServer, | ||
161 | killallServers | ||
162 | } | ||