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