]>
Commit | Line | Data |
---|---|---|
254d3579 C |
1 | import { ChildProcess, fork } from 'child_process' |
2 | import { copy } from 'fs-extra' | |
3 | import { join } from 'path' | |
4 | import { root } from '@server/helpers/core-utils' | |
15a7eafb | 5 | import { randomInt } from '@shared/core-utils' |
83903cb6 | 6 | import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '../../models/videos' |
254d3579 C |
7 | import { BulkCommand } from '../bulk' |
8 | import { CLICommand } from '../cli' | |
9 | import { CustomPagesCommand } from '../custom-pages' | |
10 | import { FeedCommand } from '../feeds' | |
11 | import { LogsCommand } from '../logs' | |
12 | import { parallelTests, SQLCommand } from '../miscs' | |
13 | import { AbusesCommand } from '../moderation' | |
14 | import { OverviewsCommand } from '../overviews' | |
15 | import { SearchCommand } from '../search' | |
16 | import { SocketIOCommand } from '../socket' | |
17 | import { AccountsCommand, BlocklistCommand, LoginCommand, NotificationsCommand, SubscriptionsCommand, UsersCommand } from '../users' | |
18 | import { | |
19 | BlacklistCommand, | |
20 | CaptionsCommand, | |
21 | ChangeOwnershipCommand, | |
22 | ChannelsCommand, | |
23 | HistoryCommand, | |
24 | ImportsCommand, | |
25 | LiveCommand, | |
26 | PlaylistsCommand, | |
27 | ServicesCommand, | |
28 | StreamingPlaylistsCommand, | |
29 | VideosCommand | |
30 | } from '../videos' | |
31 | import { CommentsCommand } from '../videos/comments-command' | |
32 | import { ConfigCommand } from './config-command' | |
33 | import { ContactFormCommand } from './contact-form-command' | |
34 | import { DebugCommand } from './debug-command' | |
35 | import { FollowsCommand } from './follows-command' | |
36 | import { JobsCommand } from './jobs-command' | |
37 | import { PluginsCommand } from './plugins-command' | |
38 | import { RedundancyCommand } from './redundancy-command' | |
39 | import { ServersCommand } from './servers-command' | |
40 | import { StatsCommand } from './stats-command' | |
0305db28 | 41 | import { ObjectStorageCommand } from './object-storage-command' |
254d3579 C |
42 | |
43 | export type RunServerOptions = { | |
44 | hideLogs?: boolean | |
2e980ed3 C |
45 | nodeArgs?: string[] |
46 | peertubeArgs?: string[] | |
0305db28 | 47 | env?: { [ id: string ]: string } |
254d3579 C |
48 | } |
49 | ||
50 | export class PeerTubeServer { | |
51 | app?: ChildProcess | |
52 | ||
53 | url: string | |
54 | host?: string | |
55 | hostname?: string | |
56 | port?: number | |
57 | ||
58 | rtmpPort?: number | |
df1db951 | 59 | rtmpsPort?: number |
254d3579 C |
60 | |
61 | parallel?: boolean | |
62 | internalServerNumber: number | |
63 | ||
64 | serverNumber?: number | |
65 | customConfigFile?: string | |
66 | ||
67 | store?: { | |
68 | client?: { | |
69 | id?: string | |
70 | secret?: string | |
71 | } | |
72 | ||
73 | user?: { | |
74 | username: string | |
75 | password: string | |
76 | email?: string | |
77 | } | |
78 | ||
79 | channel?: VideoChannel | |
80 | ||
83903cb6 C |
81 | video?: Video |
82 | videoCreated?: VideoCreateResult | |
83 | videoDetails?: VideoDetails | |
254d3579 C |
84 | |
85 | videos?: { id: number, uuid: string }[] | |
86 | } | |
87 | ||
88 | accessToken?: string | |
89 | refreshToken?: string | |
90 | ||
91 | bulk?: BulkCommand | |
92 | cli?: CLICommand | |
93 | customPage?: CustomPagesCommand | |
94 | feed?: FeedCommand | |
95 | logs?: LogsCommand | |
96 | abuses?: AbusesCommand | |
97 | overviews?: OverviewsCommand | |
98 | search?: SearchCommand | |
99 | contactForm?: ContactFormCommand | |
100 | debug?: DebugCommand | |
101 | follows?: FollowsCommand | |
102 | jobs?: JobsCommand | |
103 | plugins?: PluginsCommand | |
104 | redundancy?: RedundancyCommand | |
105 | stats?: StatsCommand | |
106 | config?: ConfigCommand | |
107 | socketIO?: SocketIOCommand | |
108 | accounts?: AccountsCommand | |
109 | blocklist?: BlocklistCommand | |
110 | subscriptions?: SubscriptionsCommand | |
111 | live?: LiveCommand | |
112 | services?: ServicesCommand | |
113 | blacklist?: BlacklistCommand | |
114 | captions?: CaptionsCommand | |
115 | changeOwnership?: ChangeOwnershipCommand | |
116 | playlists?: PlaylistsCommand | |
117 | history?: HistoryCommand | |
118 | imports?: ImportsCommand | |
119 | streamingPlaylists?: StreamingPlaylistsCommand | |
120 | channels?: ChannelsCommand | |
121 | comments?: CommentsCommand | |
122 | sql?: SQLCommand | |
123 | notifications?: NotificationsCommand | |
124 | servers?: ServersCommand | |
125 | login?: LoginCommand | |
126 | users?: UsersCommand | |
0305db28 | 127 | objectStorage?: ObjectStorageCommand |
254d3579 C |
128 | videos?: VideosCommand |
129 | ||
130 | constructor (options: { serverNumber: number } | { url: string }) { | |
131 | if ((options as any).url) { | |
132 | this.setUrl((options as any).url) | |
133 | } else { | |
134 | this.setServerNumber((options as any).serverNumber) | |
135 | } | |
136 | ||
137 | this.store = { | |
138 | client: { | |
139 | id: null, | |
140 | secret: null | |
141 | }, | |
142 | user: { | |
143 | username: null, | |
144 | password: null | |
145 | } | |
146 | } | |
147 | ||
148 | this.assignCommands() | |
149 | } | |
150 | ||
151 | setServerNumber (serverNumber: number) { | |
152 | this.serverNumber = serverNumber | |
153 | ||
154 | this.parallel = parallelTests() | |
155 | ||
156 | this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber | |
157 | this.rtmpPort = this.parallel ? this.randomRTMP() : 1936 | |
df1db951 | 158 | this.rtmpsPort = this.parallel ? this.randomRTMP() : 1937 |
254d3579 C |
159 | this.port = 9000 + this.internalServerNumber |
160 | ||
161 | this.url = `http://localhost:${this.port}` | |
162 | this.host = `localhost:${this.port}` | |
163 | this.hostname = 'localhost' | |
164 | } | |
165 | ||
166 | setUrl (url: string) { | |
167 | const parsed = new URL(url) | |
168 | ||
169 | this.url = url | |
170 | this.host = parsed.host | |
171 | this.hostname = parsed.hostname | |
172 | this.port = parseInt(parsed.port) | |
173 | } | |
174 | ||
2e980ed3 | 175 | async flushAndRun (configOverride?: Object, options: RunServerOptions = {}) { |
254d3579 C |
176 | await ServersCommand.flushTests(this.internalServerNumber) |
177 | ||
2e980ed3 | 178 | return this.run(configOverride, options) |
254d3579 C |
179 | } |
180 | ||
2e980ed3 | 181 | async run (configOverrideArg?: any, options: RunServerOptions = {}) { |
254d3579 C |
182 | // These actions are async so we need to be sure that they have both been done |
183 | const serverRunString = { | |
184 | 'HTTP server listening': false | |
185 | } | |
186 | const key = 'Database peertube_test' + this.internalServerNumber + ' is ready' | |
187 | serverRunString[key] = false | |
188 | ||
189 | const regexps = { | |
190 | client_id: 'Client id: (.+)', | |
191 | client_secret: 'Client secret: (.+)', | |
192 | user_username: 'Username: (.+)', | |
193 | user_password: 'User password: (.+)' | |
194 | } | |
195 | ||
196 | await this.assignCustomConfigFile() | |
197 | ||
198 | const configOverride = this.buildConfigOverride() | |
199 | ||
200 | if (configOverrideArg !== undefined) { | |
201 | Object.assign(configOverride, configOverrideArg) | |
202 | } | |
203 | ||
204 | // Share the environment | |
205 | const env = Object.create(process.env) | |
206 | env['NODE_ENV'] = 'test' | |
207 | env['NODE_APP_INSTANCE'] = this.internalServerNumber.toString() | |
208 | env['NODE_CONFIG'] = JSON.stringify(configOverride) | |
209 | ||
0305db28 JB |
210 | if (options.env) { |
211 | Object.assign(env, options.env) | |
212 | } | |
213 | ||
254d3579 C |
214 | const forkOptions = { |
215 | silent: true, | |
216 | env, | |
217 | detached: true, | |
2e980ed3 | 218 | execArgv: options.nodeArgs || [] |
254d3579 C |
219 | } |
220 | ||
0305db28 | 221 | return new Promise<void>((res, rej) => { |
08642a76 C |
222 | const self = this |
223 | ||
2e980ed3 | 224 | this.app = fork(join(root(), 'dist', 'server.js'), options.peertubeArgs || [], forkOptions) |
0305db28 | 225 | |
706a450f C |
226 | const onPeerTubeExit = () => rej(new Error('Process exited')) |
227 | const onParentExit = () => { | |
228 | if (!this.app || !this.app.pid) return | |
229 | ||
230 | try { | |
231 | process.kill(self.app.pid) | |
232 | } catch { /* empty */ } | |
0305db28 JB |
233 | } |
234 | ||
706a450f C |
235 | this.app.on('exit', onPeerTubeExit) |
236 | process.on('exit', onParentExit) | |
0305db28 | 237 | |
254d3579 C |
238 | this.app.stdout.on('data', function onStdout (data) { |
239 | let dontContinue = false | |
240 | ||
241 | // Capture things if we want to | |
242 | for (const key of Object.keys(regexps)) { | |
243 | const regexp = regexps[key] | |
244 | const matches = data.toString().match(regexp) | |
245 | if (matches !== null) { | |
08642a76 C |
246 | if (key === 'client_id') self.store.client.id = matches[1] |
247 | else if (key === 'client_secret') self.store.client.secret = matches[1] | |
248 | else if (key === 'user_username') self.store.user.username = matches[1] | |
249 | else if (key === 'user_password') self.store.user.password = matches[1] | |
254d3579 C |
250 | } |
251 | } | |
252 | ||
253 | // Check if all required sentences are here | |
254 | for (const key of Object.keys(serverRunString)) { | |
255 | if (data.toString().indexOf(key) !== -1) serverRunString[key] = true | |
256 | if (serverRunString[key] === false) dontContinue = true | |
257 | } | |
258 | ||
259 | // If no, there is maybe one thing not already initialized (client/user credentials generation...) | |
260 | if (dontContinue === true) return | |
261 | ||
262 | if (options.hideLogs === false) { | |
263 | console.log(data.toString()) | |
264 | } else { | |
706a450f | 265 | process.removeListener('exit', onParentExit) |
08642a76 | 266 | self.app.stdout.removeListener('data', onStdout) |
706a450f | 267 | self.app.removeListener('exit', onPeerTubeExit) |
254d3579 C |
268 | } |
269 | ||
254d3579 C |
270 | res() |
271 | }) | |
272 | }) | |
273 | } | |
274 | ||
275 | async kill () { | |
276 | if (!this.app) return | |
277 | ||
278 | await this.sql.cleanup() | |
279 | ||
280 | process.kill(-this.app.pid) | |
281 | ||
282 | this.app = null | |
283 | } | |
284 | ||
285 | private randomServer () { | |
286 | const low = 10 | |
287 | const high = 10000 | |
288 | ||
289 | return randomInt(low, high) | |
290 | } | |
291 | ||
292 | private randomRTMP () { | |
293 | const low = 1900 | |
294 | const high = 2100 | |
295 | ||
296 | return randomInt(low, high) | |
297 | } | |
298 | ||
299 | private async assignCustomConfigFile () { | |
300 | if (this.internalServerNumber === this.serverNumber) return | |
301 | ||
302 | const basePath = join(root(), 'config') | |
303 | ||
304 | const tmpConfigFile = join(basePath, `test-${this.internalServerNumber}.yaml`) | |
305 | await copy(join(basePath, `test-${this.serverNumber}.yaml`), tmpConfigFile) | |
306 | ||
307 | this.customConfigFile = tmpConfigFile | |
308 | } | |
309 | ||
310 | private buildConfigOverride () { | |
311 | if (!this.parallel) return {} | |
312 | ||
313 | return { | |
314 | listen: { | |
315 | port: this.port | |
316 | }, | |
317 | webserver: { | |
318 | port: this.port | |
319 | }, | |
320 | database: { | |
321 | suffix: '_test' + this.internalServerNumber | |
322 | }, | |
323 | storage: { | |
324 | tmp: `test${this.internalServerNumber}/tmp/`, | |
325 | avatars: `test${this.internalServerNumber}/avatars/`, | |
326 | videos: `test${this.internalServerNumber}/videos/`, | |
327 | streaming_playlists: `test${this.internalServerNumber}/streaming-playlists/`, | |
328 | redundancy: `test${this.internalServerNumber}/redundancy/`, | |
329 | logs: `test${this.internalServerNumber}/logs/`, | |
330 | previews: `test${this.internalServerNumber}/previews/`, | |
331 | thumbnails: `test${this.internalServerNumber}/thumbnails/`, | |
332 | torrents: `test${this.internalServerNumber}/torrents/`, | |
333 | captions: `test${this.internalServerNumber}/captions/`, | |
334 | cache: `test${this.internalServerNumber}/cache/`, | |
335 | plugins: `test${this.internalServerNumber}/plugins/` | |
336 | }, | |
337 | admin: { | |
338 | email: `admin${this.internalServerNumber}@example.com` | |
339 | }, | |
340 | live: { | |
341 | rtmp: { | |
342 | port: this.rtmpPort | |
343 | } | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
348 | private assignCommands () { | |
349 | this.bulk = new BulkCommand(this) | |
350 | this.cli = new CLICommand(this) | |
351 | this.customPage = new CustomPagesCommand(this) | |
352 | this.feed = new FeedCommand(this) | |
353 | this.logs = new LogsCommand(this) | |
354 | this.abuses = new AbusesCommand(this) | |
355 | this.overviews = new OverviewsCommand(this) | |
356 | this.search = new SearchCommand(this) | |
357 | this.contactForm = new ContactFormCommand(this) | |
358 | this.debug = new DebugCommand(this) | |
359 | this.follows = new FollowsCommand(this) | |
360 | this.jobs = new JobsCommand(this) | |
361 | this.plugins = new PluginsCommand(this) | |
362 | this.redundancy = new RedundancyCommand(this) | |
363 | this.stats = new StatsCommand(this) | |
364 | this.config = new ConfigCommand(this) | |
365 | this.socketIO = new SocketIOCommand(this) | |
366 | this.accounts = new AccountsCommand(this) | |
367 | this.blocklist = new BlocklistCommand(this) | |
368 | this.subscriptions = new SubscriptionsCommand(this) | |
369 | this.live = new LiveCommand(this) | |
370 | this.services = new ServicesCommand(this) | |
371 | this.blacklist = new BlacklistCommand(this) | |
372 | this.captions = new CaptionsCommand(this) | |
373 | this.changeOwnership = new ChangeOwnershipCommand(this) | |
374 | this.playlists = new PlaylistsCommand(this) | |
375 | this.history = new HistoryCommand(this) | |
376 | this.imports = new ImportsCommand(this) | |
377 | this.streamingPlaylists = new StreamingPlaylistsCommand(this) | |
378 | this.channels = new ChannelsCommand(this) | |
379 | this.comments = new CommentsCommand(this) | |
380 | this.sql = new SQLCommand(this) | |
381 | this.notifications = new NotificationsCommand(this) | |
382 | this.servers = new ServersCommand(this) | |
383 | this.login = new LoginCommand(this) | |
384 | this.users = new UsersCommand(this) | |
385 | this.videos = new VideosCommand(this) | |
0305db28 | 386 | this.objectStorage = new ObjectStorageCommand(this) |
254d3579 C |
387 | } |
388 | } |