aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/server')
-rw-r--r--shared/extra-utils/server/directories.ts6
-rw-r--r--shared/extra-utils/server/follows-command.ts6
-rw-r--r--shared/extra-utils/server/follows.ts4
-rw-r--r--shared/extra-utils/server/index.ts1
-rw-r--r--shared/extra-utils/server/jobs.ts10
-rw-r--r--shared/extra-utils/server/plugins.ts4
-rw-r--r--shared/extra-utils/server/server.ts376
-rw-r--r--shared/extra-utils/server/servers-command.ts2
-rw-r--r--shared/extra-utils/server/servers.ts400
9 files changed, 411 insertions, 398 deletions
diff --git a/shared/extra-utils/server/directories.ts b/shared/extra-utils/server/directories.ts
index 3cd38a561..b6465cbf4 100644
--- a/shared/extra-utils/server/directories.ts
+++ b/shared/extra-utils/server/directories.ts
@@ -4,9 +4,9 @@ import { expect } from 'chai'
4import { pathExists, readdir } from 'fs-extra' 4import { pathExists, readdir } from 'fs-extra'
5import { join } from 'path' 5import { join } from 'path'
6import { root } from '@server/helpers/core-utils' 6import { root } from '@server/helpers/core-utils'
7import { ServerInfo } from './servers' 7import { PeerTubeServer } from './server'
8 8
9async function checkTmpIsEmpty (server: ServerInfo) { 9async function checkTmpIsEmpty (server: PeerTubeServer) {
10 await checkDirectoryIsEmpty(server, 'tmp', [ 'plugins-global.css', 'hls', 'resumable-uploads' ]) 10 await checkDirectoryIsEmpty(server, 'tmp', [ 'plugins-global.css', 'hls', 'resumable-uploads' ])
11 11
12 if (await pathExists(join('test' + server.internalServerNumber, 'tmp', 'hls'))) { 12 if (await pathExists(join('test' + server.internalServerNumber, 'tmp', 'hls'))) {
@@ -14,7 +14,7 @@ async function checkTmpIsEmpty (server: ServerInfo) {
14 } 14 }
15} 15}
16 16
17async function checkDirectoryIsEmpty (server: ServerInfo, directory: string, exceptions: string[] = []) { 17async function checkDirectoryIsEmpty (server: PeerTubeServer, directory: string, exceptions: string[] = []) {
18 const testDirectory = 'test' + server.internalServerNumber 18 const testDirectory = 'test' + server.internalServerNumber
19 19
20 const directoryPath = join(root(), testDirectory, directory) 20 const directoryPath = join(root(), testDirectory, directory)
diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts
index 4e1e56d7a..4e9ed9494 100644
--- a/shared/extra-utils/server/follows-command.ts
+++ b/shared/extra-utils/server/follows-command.ts
@@ -2,7 +2,7 @@ import { pick } from 'lodash'
2import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models' 2import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models'
3import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' 3import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
4import { AbstractCommand, OverrideCommandOptions } from '../shared' 4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5import { ServerInfo } from './servers' 5import { PeerTubeServer } from './server'
6 6
7export class FollowsCommand extends AbstractCommand { 7export class FollowsCommand extends AbstractCommand {
8 8
@@ -70,7 +70,7 @@ export class FollowsCommand extends AbstractCommand {
70 } 70 }
71 71
72 async unfollow (options: OverrideCommandOptions & { 72 async unfollow (options: OverrideCommandOptions & {
73 target: ServerInfo 73 target: PeerTubeServer
74 }) { 74 }) {
75 const path = '/api/v1/server/following/' + options.target.host 75 const path = '/api/v1/server/following/' + options.target.host
76 76
@@ -112,7 +112,7 @@ export class FollowsCommand extends AbstractCommand {
112 } 112 }
113 113
114 removeFollower (options: OverrideCommandOptions & { 114 removeFollower (options: OverrideCommandOptions & {
115 follower: ServerInfo 115 follower: PeerTubeServer
116 }) { 116 }) {
117 const path = '/api/v1/server/followers/peertube@' + options.follower.host 117 const path = '/api/v1/server/followers/peertube@' + options.follower.host
118 118
diff --git a/shared/extra-utils/server/follows.ts b/shared/extra-utils/server/follows.ts
index 50ae898cc..0188be1aa 100644
--- a/shared/extra-utils/server/follows.ts
+++ b/shared/extra-utils/server/follows.ts
@@ -1,7 +1,7 @@
1import { waitJobs } from './jobs' 1import { waitJobs } from './jobs'
2import { ServerInfo } from './servers' 2import { PeerTubeServer } from './server'
3 3
4async function doubleFollow (server1: ServerInfo, server2: ServerInfo) { 4async function doubleFollow (server1: PeerTubeServer, server2: PeerTubeServer) {
5 await Promise.all([ 5 await Promise.all([
6 server1.follows.follow({ targets: [ server2.url ] }), 6 server1.follows.follow({ targets: [ server2.url ] }),
7 server2.follows.follow({ targets: [ server1.url ] }) 7 server2.follows.follow({ targets: [ server1.url ] })
diff --git a/shared/extra-utils/server/index.ts b/shared/extra-utils/server/index.ts
index 669b004cd..9055dfc57 100644
--- a/shared/extra-utils/server/index.ts
+++ b/shared/extra-utils/server/index.ts
@@ -9,6 +9,7 @@ export * from './jobs-command'
9export * from './plugins-command' 9export * from './plugins-command'
10export * from './plugins' 10export * from './plugins'
11export * from './redundancy-command' 11export * from './redundancy-command'
12export * from './server'
12export * from './servers-command' 13export * from './servers-command'
13export * from './servers' 14export * from './servers'
14export * from './stats-command' 15export * from './stats-command'
diff --git a/shared/extra-utils/server/jobs.ts b/shared/extra-utils/server/jobs.ts
index 754530977..64a0353eb 100644
--- a/shared/extra-utils/server/jobs.ts
+++ b/shared/extra-utils/server/jobs.ts
@@ -1,17 +1,17 @@
1 1
2import { JobState } from '../../models' 2import { JobState } from '../../models'
3import { wait } from '../miscs' 3import { wait } from '../miscs'
4import { ServerInfo } from './servers' 4import { PeerTubeServer } from './server'
5 5
6async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { 6async function waitJobs (serversArg: PeerTubeServer[] | PeerTubeServer) {
7 const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT 7 const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT
8 ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10) 8 ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10)
9 : 250 9 : 250
10 10
11 let servers: ServerInfo[] 11 let servers: PeerTubeServer[]
12 12
13 if (Array.isArray(serversArg) === false) servers = [ serversArg as ServerInfo ] 13 if (Array.isArray(serversArg) === false) servers = [ serversArg as PeerTubeServer ]
14 else servers = serversArg as ServerInfo[] 14 else servers = serversArg as PeerTubeServer[]
15 15
16 const states: JobState[] = [ 'waiting', 'active', 'delayed' ] 16 const states: JobState[] = [ 'waiting', 'active', 'delayed' ]
17 const repeatableJobs = [ 'videos-views', 'activitypub-cleaner' ] 17 const repeatableJobs = [ 'videos-views', 'activitypub-cleaner' ]
diff --git a/shared/extra-utils/server/plugins.ts b/shared/extra-utils/server/plugins.ts
index d1cc7e383..0f5fabd5a 100644
--- a/shared/extra-utils/server/plugins.ts
+++ b/shared/extra-utils/server/plugins.ts
@@ -1,9 +1,9 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import { expect } from 'chai' 3import { expect } from 'chai'
4import { ServerInfo } from '../server/servers' 4import { PeerTubeServer } from '../server/server'
5 5
6async function testHelloWorldRegisteredSettings (server: ServerInfo) { 6async function testHelloWorldRegisteredSettings (server: PeerTubeServer) {
7 const body = await server.plugins.getRegisteredSettings({ npmName: 'peertube-plugin-hello-world' }) 7 const body = await server.plugins.getRegisteredSettings({ npmName: 'peertube-plugin-hello-world' })
8 8
9 const registeredSettings = body.registeredSettings 9 const registeredSettings = body.registeredSettings
diff --git a/shared/extra-utils/server/server.ts b/shared/extra-utils/server/server.ts
new file mode 100644
index 000000000..b1347661f
--- /dev/null
+++ b/shared/extra-utils/server/server.ts
@@ -0,0 +1,376 @@
1import { ChildProcess, fork } from 'child_process'
2import { copy } from 'fs-extra'
3import { join } from 'path'
4import { root } from '@server/helpers/core-utils'
5import { randomInt } from '../../core-utils/miscs/miscs'
6import { VideoChannel } from '../../models/videos'
7import { BulkCommand } from '../bulk'
8import { CLICommand } from '../cli'
9import { CustomPagesCommand } from '../custom-pages'
10import { FeedCommand } from '../feeds'
11import { LogsCommand } from '../logs'
12import { parallelTests, SQLCommand } from '../miscs'
13import { AbusesCommand } from '../moderation'
14import { OverviewsCommand } from '../overviews'
15import { SearchCommand } from '../search'
16import { SocketIOCommand } from '../socket'
17import { AccountsCommand, BlocklistCommand, LoginCommand, NotificationsCommand, SubscriptionsCommand, UsersCommand } from '../users'
18import {
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'
31import { CommentsCommand } from '../videos/comments-command'
32import { ConfigCommand } from './config-command'
33import { ContactFormCommand } from './contact-form-command'
34import { DebugCommand } from './debug-command'
35import { FollowsCommand } from './follows-command'
36import { JobsCommand } from './jobs-command'
37import { PluginsCommand } from './plugins-command'
38import { RedundancyCommand } from './redundancy-command'
39import { ServersCommand } from './servers-command'
40import { StatsCommand } from './stats-command'
41
42export type RunServerOptions = {
43 hideLogs?: boolean
44 execArgv?: string[]
45}
46
47export class PeerTubeServer {
48 app?: ChildProcess
49
50 url: string
51 host?: string
52 hostname?: string
53 port?: number
54
55 rtmpPort?: number
56
57 parallel?: boolean
58 internalServerNumber: number
59
60 serverNumber?: number
61 customConfigFile?: string
62
63 store?: {
64 client?: {
65 id?: string
66 secret?: string
67 }
68
69 user?: {
70 username: string
71 password: string
72 email?: string
73 }
74
75 channel?: VideoChannel
76
77 video?: {
78 id: number
79 uuid: string
80 shortUUID: string
81 name?: string
82 url?: string
83
84 account?: {
85 name: string
86 }
87
88 embedPath?: string
89 }
90
91 videos?: { id: number, uuid: string }[]
92 }
93
94 accessToken?: string
95 refreshToken?: string
96
97 bulk?: BulkCommand
98 cli?: CLICommand
99 customPage?: CustomPagesCommand
100 feed?: FeedCommand
101 logs?: LogsCommand
102 abuses?: AbusesCommand
103 overviews?: OverviewsCommand
104 search?: SearchCommand
105 contactForm?: ContactFormCommand
106 debug?: DebugCommand
107 follows?: FollowsCommand
108 jobs?: JobsCommand
109 plugins?: PluginsCommand
110 redundancy?: RedundancyCommand
111 stats?: StatsCommand
112 config?: ConfigCommand
113 socketIO?: SocketIOCommand
114 accounts?: AccountsCommand
115 blocklist?: BlocklistCommand
116 subscriptions?: SubscriptionsCommand
117 live?: LiveCommand
118 services?: ServicesCommand
119 blacklist?: BlacklistCommand
120 captions?: CaptionsCommand
121 changeOwnership?: ChangeOwnershipCommand
122 playlists?: PlaylistsCommand
123 history?: HistoryCommand
124 imports?: ImportsCommand
125 streamingPlaylists?: StreamingPlaylistsCommand
126 channels?: ChannelsCommand
127 comments?: CommentsCommand
128 sql?: SQLCommand
129 notifications?: NotificationsCommand
130 servers?: ServersCommand
131 login?: LoginCommand
132 users?: UsersCommand
133 videos?: VideosCommand
134
135 constructor (options: { serverNumber: number } | { url: string }) {
136 if ((options as any).url) {
137 this.setUrl((options as any).url)
138 } else {
139 this.setServerNumber((options as any).serverNumber)
140 }
141
142 this.store = {
143 client: {
144 id: null,
145 secret: null
146 },
147 user: {
148 username: null,
149 password: null
150 }
151 }
152
153 this.assignCommands()
154 }
155
156 setServerNumber (serverNumber: number) {
157 this.serverNumber = serverNumber
158
159 this.parallel = parallelTests()
160
161 this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber
162 this.rtmpPort = this.parallel ? this.randomRTMP() : 1936
163 this.port = 9000 + this.internalServerNumber
164
165 this.url = `http://localhost:${this.port}`
166 this.host = `localhost:${this.port}`
167 this.hostname = 'localhost'
168 }
169
170 setUrl (url: string) {
171 const parsed = new URL(url)
172
173 this.url = url
174 this.host = parsed.host
175 this.hostname = parsed.hostname
176 this.port = parseInt(parsed.port)
177 }
178
179 async flushAndRun (configOverride?: Object, args = [], options: RunServerOptions = {}) {
180 await ServersCommand.flushTests(this.internalServerNumber)
181
182 return this.run(configOverride, args, options)
183 }
184
185 async run (configOverrideArg?: any, args = [], options: RunServerOptions = {}) {
186 // These actions are async so we need to be sure that they have both been done
187 const serverRunString = {
188 'HTTP server listening': false
189 }
190 const key = 'Database peertube_test' + this.internalServerNumber + ' is ready'
191 serverRunString[key] = false
192
193 const regexps = {
194 client_id: 'Client id: (.+)',
195 client_secret: 'Client secret: (.+)',
196 user_username: 'Username: (.+)',
197 user_password: 'User password: (.+)'
198 }
199
200 await this.assignCustomConfigFile()
201
202 const configOverride = this.buildConfigOverride()
203
204 if (configOverrideArg !== undefined) {
205 Object.assign(configOverride, configOverrideArg)
206 }
207
208 // Share the environment
209 const env = Object.create(process.env)
210 env['NODE_ENV'] = 'test'
211 env['NODE_APP_INSTANCE'] = this.internalServerNumber.toString()
212 env['NODE_CONFIG'] = JSON.stringify(configOverride)
213
214 const forkOptions = {
215 silent: true,
216 env,
217 detached: true,
218 execArgv: options.execArgv || []
219 }
220
221 return new Promise<void>(res => {
222 this.app = fork(join(root(), 'dist', 'server.js'), args, forkOptions)
223 this.app.stdout.on('data', function onStdout (data) {
224 let dontContinue = false
225
226 // Capture things if we want to
227 for (const key of Object.keys(regexps)) {
228 const regexp = regexps[key]
229 const matches = data.toString().match(regexp)
230 if (matches !== null) {
231 if (key === 'client_id') this.store.client.id = matches[1]
232 else if (key === 'client_secret') this.store.client.secret = matches[1]
233 else if (key === 'user_username') this.store.user.username = matches[1]
234 else if (key === 'user_password') this.store.user.password = matches[1]
235 }
236 }
237
238 // Check if all required sentences are here
239 for (const key of Object.keys(serverRunString)) {
240 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
241 if (serverRunString[key] === false) dontContinue = true
242 }
243
244 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
245 if (dontContinue === true) return
246
247 if (options.hideLogs === false) {
248 console.log(data.toString())
249 } else {
250 this.app.stdout.removeListener('data', onStdout)
251 }
252
253 process.on('exit', () => {
254 try {
255 process.kill(this.server.app.pid)
256 } catch { /* empty */ }
257 })
258
259 res()
260 })
261 })
262 }
263
264 async kill () {
265 if (!this.app) return
266
267 await this.sql.cleanup()
268
269 process.kill(-this.app.pid)
270
271 this.app = null
272 }
273
274 private randomServer () {
275 const low = 10
276 const high = 10000
277
278 return randomInt(low, high)
279 }
280
281 private randomRTMP () {
282 const low = 1900
283 const high = 2100
284
285 return randomInt(low, high)
286 }
287
288 private async assignCustomConfigFile () {
289 if (this.internalServerNumber === this.serverNumber) return
290
291 const basePath = join(root(), 'config')
292
293 const tmpConfigFile = join(basePath, `test-${this.internalServerNumber}.yaml`)
294 await copy(join(basePath, `test-${this.serverNumber}.yaml`), tmpConfigFile)
295
296 this.customConfigFile = tmpConfigFile
297 }
298
299 private buildConfigOverride () {
300 if (!this.parallel) return {}
301
302 return {
303 listen: {
304 port: this.port
305 },
306 webserver: {
307 port: this.port
308 },
309 database: {
310 suffix: '_test' + this.internalServerNumber
311 },
312 storage: {
313 tmp: `test${this.internalServerNumber}/tmp/`,
314 avatars: `test${this.internalServerNumber}/avatars/`,
315 videos: `test${this.internalServerNumber}/videos/`,
316 streaming_playlists: `test${this.internalServerNumber}/streaming-playlists/`,
317 redundancy: `test${this.internalServerNumber}/redundancy/`,
318 logs: `test${this.internalServerNumber}/logs/`,
319 previews: `test${this.internalServerNumber}/previews/`,
320 thumbnails: `test${this.internalServerNumber}/thumbnails/`,
321 torrents: `test${this.internalServerNumber}/torrents/`,
322 captions: `test${this.internalServerNumber}/captions/`,
323 cache: `test${this.internalServerNumber}/cache/`,
324 plugins: `test${this.internalServerNumber}/plugins/`
325 },
326 admin: {
327 email: `admin${this.internalServerNumber}@example.com`
328 },
329 live: {
330 rtmp: {
331 port: this.rtmpPort
332 }
333 }
334 }
335 }
336
337 private assignCommands () {
338 this.bulk = new BulkCommand(this)
339 this.cli = new CLICommand(this)
340 this.customPage = new CustomPagesCommand(this)
341 this.feed = new FeedCommand(this)
342 this.logs = new LogsCommand(this)
343 this.abuses = new AbusesCommand(this)
344 this.overviews = new OverviewsCommand(this)
345 this.search = new SearchCommand(this)
346 this.contactForm = new ContactFormCommand(this)
347 this.debug = new DebugCommand(this)
348 this.follows = new FollowsCommand(this)
349 this.jobs = new JobsCommand(this)
350 this.plugins = new PluginsCommand(this)
351 this.redundancy = new RedundancyCommand(this)
352 this.stats = new StatsCommand(this)
353 this.config = new ConfigCommand(this)
354 this.socketIO = new SocketIOCommand(this)
355 this.accounts = new AccountsCommand(this)
356 this.blocklist = new BlocklistCommand(this)
357 this.subscriptions = new SubscriptionsCommand(this)
358 this.live = new LiveCommand(this)
359 this.services = new ServicesCommand(this)
360 this.blacklist = new BlacklistCommand(this)
361 this.captions = new CaptionsCommand(this)
362 this.changeOwnership = new ChangeOwnershipCommand(this)
363 this.playlists = new PlaylistsCommand(this)
364 this.history = new HistoryCommand(this)
365 this.imports = new ImportsCommand(this)
366 this.streamingPlaylists = new StreamingPlaylistsCommand(this)
367 this.channels = new ChannelsCommand(this)
368 this.comments = new CommentsCommand(this)
369 this.sql = new SQLCommand(this)
370 this.notifications = new NotificationsCommand(this)
371 this.servers = new ServersCommand(this)
372 this.login = new LoginCommand(this)
373 this.users = new UsersCommand(this)
374 this.videos = new VideosCommand(this)
375 }
376}
diff --git a/shared/extra-utils/server/servers-command.ts b/shared/extra-utils/server/servers-command.ts
index a7c5a868d..1a7b2aade 100644
--- a/shared/extra-utils/server/servers-command.ts
+++ b/shared/extra-utils/server/servers-command.ts
@@ -37,7 +37,7 @@ export class ServersCommand extends AbstractCommand {
37 if (isGithubCI()) { 37 if (isGithubCI()) {
38 await ensureDir('artifacts') 38 await ensureDir('artifacts')
39 39
40 const origin = this.server.servers.buildDirectory('logs/peertube.log') 40 const origin = this.buildDirectory('logs/peertube.log')
41 const destname = `peertube-${this.server.internalServerNumber}.log` 41 const destname = `peertube-${this.server.internalServerNumber}.log`
42 console.log('Saving logs %s.', destname) 42 console.log('Saving logs %s.', destname)
43 43
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index ea3f19a92..87d7e9449 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -1,391 +1,30 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ 1import { ensureDir } from 'fs-extra'
2import { isGithubCI } from '../miscs'
3import { PeerTubeServer, RunServerOptions } from './server'
2 4
3import { ChildProcess, fork } from 'child_process' 5async function createSingleServer (serverNumber: number, configOverride?: Object, args = [], options: RunServerOptions = {}) {
4import { copy, ensureDir } from 'fs-extra' 6 const server = new PeerTubeServer({ serverNumber })
5import { join } from 'path'
6import { root } from '@server/helpers/core-utils'
7import { randomInt } from '../../core-utils/miscs/miscs'
8import { VideoChannel } from '../../models/videos'
9import { BulkCommand } from '../bulk'
10import { CLICommand } from '../cli'
11import { CustomPagesCommand } from '../custom-pages'
12import { FeedCommand } from '../feeds'
13import { LogsCommand } from '../logs'
14import { isGithubCI, parallelTests, SQLCommand } from '../miscs'
15import { AbusesCommand } from '../moderation'
16import { OverviewsCommand } from '../overviews'
17import { SearchCommand } from '../search'
18import { SocketIOCommand } from '../socket'
19import { AccountsCommand, BlocklistCommand, LoginCommand, NotificationsCommand, SubscriptionsCommand, UsersCommand } from '../users'
20import {
21 BlacklistCommand,
22 CaptionsCommand,
23 ChangeOwnershipCommand,
24 ChannelsCommand,
25 HistoryCommand,
26 ImportsCommand,
27 LiveCommand,
28 PlaylistsCommand,
29 ServicesCommand,
30 StreamingPlaylistsCommand,
31 VideosCommand
32} from '../videos'
33import { CommentsCommand } from '../videos/comments-command'
34import { ConfigCommand } from './config-command'
35import { ContactFormCommand } from './contact-form-command'
36import { DebugCommand } from './debug-command'
37import { FollowsCommand } from './follows-command'
38import { JobsCommand } from './jobs-command'
39import { PluginsCommand } from './plugins-command'
40import { RedundancyCommand } from './redundancy-command'
41import { ServersCommand } from './servers-command'
42import { StatsCommand } from './stats-command'
43 7
44interface ServerInfo { 8 await server.flushAndRun(configOverride, args, options)
45 app?: ChildProcess
46 9
47 url: string 10 return server
48 host?: string
49 hostname?: string
50 port?: number
51
52 rtmpPort?: number
53
54 parallel?: boolean
55 internalServerNumber: number
56
57 serverNumber?: number
58 customConfigFile?: string
59
60 store?: {
61 client?: {
62 id?: string
63 secret?: string
64 }
65
66 user?: {
67 username: string
68 password: string
69 email?: string
70 }
71
72 channel?: VideoChannel
73
74 video?: {
75 id: number
76 uuid: string
77 shortUUID: string
78 name?: string
79 url?: string
80
81 account?: {
82 name: string
83 }
84
85 embedPath?: string
86 }
87
88 videos?: { id: number, uuid: string }[]
89 }
90
91 accessToken?: string
92 refreshToken?: string
93
94 bulk?: BulkCommand
95 cli?: CLICommand
96 customPage?: CustomPagesCommand
97 feed?: FeedCommand
98 logs?: LogsCommand
99 abuses?: AbusesCommand
100 overviews?: OverviewsCommand
101 search?: SearchCommand
102 contactForm?: ContactFormCommand
103 debug?: DebugCommand
104 follows?: FollowsCommand
105 jobs?: JobsCommand
106 plugins?: PluginsCommand
107 redundancy?: RedundancyCommand
108 stats?: StatsCommand
109 config?: ConfigCommand
110 socketIO?: SocketIOCommand
111 accounts?: AccountsCommand
112 blocklist?: BlocklistCommand
113 subscriptions?: SubscriptionsCommand
114 live?: LiveCommand
115 services?: ServicesCommand
116 blacklist?: BlacklistCommand
117 captions?: CaptionsCommand
118 changeOwnership?: ChangeOwnershipCommand
119 playlists?: PlaylistsCommand
120 history?: HistoryCommand
121 imports?: ImportsCommand
122 streamingPlaylists?: StreamingPlaylistsCommand
123 channels?: ChannelsCommand
124 comments?: CommentsCommand
125 sql?: SQLCommand
126 notifications?: NotificationsCommand
127 servers?: ServersCommand
128 login?: LoginCommand
129 users?: UsersCommand
130 videos?: VideosCommand
131}
132
133function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
134 const apps = []
135 let i = 0
136
137 return new Promise<ServerInfo[]>(res => {
138 function anotherServerDone (serverNumber, app) {
139 apps[serverNumber - 1] = app
140 i++
141 if (i === totalServers) {
142 return res(apps)
143 }
144 }
145
146 for (let j = 1; j <= totalServers; j++) {
147 flushAndRunServer(j, configOverride).then(app => anotherServerDone(j, app))
148 }
149 })
150}
151
152function randomServer () {
153 const low = 10
154 const high = 10000
155
156 return randomInt(low, high)
157}
158
159function randomRTMP () {
160 const low = 1900
161 const high = 2100
162
163 return randomInt(low, high)
164}
165
166type RunServerOptions = {
167 hideLogs?: boolean
168 execArgv?: string[]
169}
170
171async function flushAndRunServer (serverNumber: number, configOverride?: Object, args = [], options: RunServerOptions = {}) {
172 const parallel = parallelTests()
173
174 const internalServerNumber = parallel ? randomServer() : serverNumber
175 const rtmpPort = parallel ? randomRTMP() : 1936
176 const port = 9000 + internalServerNumber
177
178 await ServersCommand.flushTests(internalServerNumber)
179
180 const server: ServerInfo = {
181 app: null,
182 port,
183 internalServerNumber,
184 rtmpPort,
185 parallel,
186 serverNumber,
187 url: `http://localhost:${port}`,
188 host: `localhost:${port}`,
189 hostname: 'localhost',
190 store: {
191 client: {
192 id: null,
193 secret: null
194 },
195 user: {
196 username: null,
197 password: null
198 }
199 }
200 }
201
202 return runServer(server, configOverride, args, options)
203} 11}
204 12
205async function runServer (server: ServerInfo, configOverrideArg?: any, args = [], options: RunServerOptions = {}) { 13function createMultipleServers (totalServers: number, configOverride?: Object) {
206 // These actions are async so we need to be sure that they have both been done 14 const serverPromises: Promise<PeerTubeServer>[] = []
207 const serverRunString = {
208 'HTTP server listening': false
209 }
210 const key = 'Database peertube_test' + server.internalServerNumber + ' is ready'
211 serverRunString[key] = false
212
213 const regexps = {
214 client_id: 'Client id: (.+)',
215 client_secret: 'Client secret: (.+)',
216 user_username: 'Username: (.+)',
217 user_password: 'User password: (.+)'
218 }
219
220 if (server.internalServerNumber !== server.serverNumber) {
221 const basePath = join(root(), 'config')
222
223 const tmpConfigFile = join(basePath, `test-${server.internalServerNumber}.yaml`)
224 await copy(join(basePath, `test-${server.serverNumber}.yaml`), tmpConfigFile)
225
226 server.customConfigFile = tmpConfigFile
227 }
228
229 const configOverride: any = {}
230
231 if (server.parallel) {
232 Object.assign(configOverride, {
233 listen: {
234 port: server.port
235 },
236 webserver: {
237 port: server.port
238 },
239 database: {
240 suffix: '_test' + server.internalServerNumber
241 },
242 storage: {
243 tmp: `test${server.internalServerNumber}/tmp/`,
244 avatars: `test${server.internalServerNumber}/avatars/`,
245 videos: `test${server.internalServerNumber}/videos/`,
246 streaming_playlists: `test${server.internalServerNumber}/streaming-playlists/`,
247 redundancy: `test${server.internalServerNumber}/redundancy/`,
248 logs: `test${server.internalServerNumber}/logs/`,
249 previews: `test${server.internalServerNumber}/previews/`,
250 thumbnails: `test${server.internalServerNumber}/thumbnails/`,
251 torrents: `test${server.internalServerNumber}/torrents/`,
252 captions: `test${server.internalServerNumber}/captions/`,
253 cache: `test${server.internalServerNumber}/cache/`,
254 plugins: `test${server.internalServerNumber}/plugins/`
255 },
256 admin: {
257 email: `admin${server.internalServerNumber}@example.com`
258 },
259 live: {
260 rtmp: {
261 port: server.rtmpPort
262 }
263 }
264 })
265 }
266
267 if (configOverrideArg !== undefined) {
268 Object.assign(configOverride, configOverrideArg)
269 }
270
271 // Share the environment
272 const env = Object.create(process.env)
273 env['NODE_ENV'] = 'test'
274 env['NODE_APP_INSTANCE'] = server.internalServerNumber.toString()
275 env['NODE_CONFIG'] = JSON.stringify(configOverride)
276 15
277 const forkOptions = { 16 for (let i = 1; i <= totalServers; i++) {
278 silent: true, 17 serverPromises.push(createSingleServer(i, configOverride))
279 env,
280 detached: true,
281 execArgv: options.execArgv || []
282 } 18 }
283 19
284 return new Promise<ServerInfo>(res => { 20 return Promise.all(serverPromises)
285 server.app = fork(join(root(), 'dist', 'server.js'), args, forkOptions)
286 server.app.stdout.on('data', function onStdout (data) {
287 let dontContinue = false
288
289 // Capture things if we want to
290 for (const key of Object.keys(regexps)) {
291 const regexp = regexps[key]
292 const matches = data.toString().match(regexp)
293 if (matches !== null) {
294 if (key === 'client_id') server.store.client.id = matches[1]
295 else if (key === 'client_secret') server.store.client.secret = matches[1]
296 else if (key === 'user_username') server.store.user.username = matches[1]
297 else if (key === 'user_password') server.store.user.password = matches[1]
298 }
299 }
300
301 // Check if all required sentences are here
302 for (const key of Object.keys(serverRunString)) {
303 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
304 if (serverRunString[key] === false) dontContinue = true
305 }
306
307 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
308 if (dontContinue === true) return
309
310 if (options.hideLogs === false) {
311 console.log(data.toString())
312 } else {
313 server.app.stdout.removeListener('data', onStdout)
314 }
315
316 process.on('exit', () => {
317 try {
318 process.kill(server.app.pid)
319 } catch { /* empty */ }
320 })
321
322 assignCommands(server)
323
324 res(server)
325 })
326 })
327}
328
329function assignCommands (server: ServerInfo) {
330 server.bulk = new BulkCommand(server)
331 server.cli = new CLICommand(server)
332 server.customPage = new CustomPagesCommand(server)
333 server.feed = new FeedCommand(server)
334 server.logs = new LogsCommand(server)
335 server.abuses = new AbusesCommand(server)
336 server.overviews = new OverviewsCommand(server)
337 server.search = new SearchCommand(server)
338 server.contactForm = new ContactFormCommand(server)
339 server.debug = new DebugCommand(server)
340 server.follows = new FollowsCommand(server)
341 server.jobs = new JobsCommand(server)
342 server.plugins = new PluginsCommand(server)
343 server.redundancy = new RedundancyCommand(server)
344 server.stats = new StatsCommand(server)
345 server.config = new ConfigCommand(server)
346 server.socketIO = new SocketIOCommand(server)
347 server.accounts = new AccountsCommand(server)
348 server.blocklist = new BlocklistCommand(server)
349 server.subscriptions = new SubscriptionsCommand(server)
350 server.live = new LiveCommand(server)
351 server.services = new ServicesCommand(server)
352 server.blacklist = new BlacklistCommand(server)
353 server.captions = new CaptionsCommand(server)
354 server.changeOwnership = new ChangeOwnershipCommand(server)
355 server.playlists = new PlaylistsCommand(server)
356 server.history = new HistoryCommand(server)
357 server.imports = new ImportsCommand(server)
358 server.streamingPlaylists = new StreamingPlaylistsCommand(server)
359 server.channels = new ChannelsCommand(server)
360 server.comments = new CommentsCommand(server)
361 server.sql = new SQLCommand(server)
362 server.notifications = new NotificationsCommand(server)
363 server.servers = new ServersCommand(server)
364 server.login = new LoginCommand(server)
365 server.users = new UsersCommand(server)
366 server.videos = new VideosCommand(server)
367}
368
369async function reRunServer (server: ServerInfo, configOverride?: any) {
370 const newServer = await runServer(server, configOverride)
371 server.app = newServer.app
372
373 return server
374} 21}
375 22
376async function killallServers (servers: ServerInfo[]) { 23async function killallServers (servers: PeerTubeServer[]) {
377 for (const server of servers) { 24 return Promise.all(servers.map(s => s.kill()))
378 if (!server.app) continue
379
380 await server.sql.cleanup()
381
382 process.kill(-server.app.pid)
383
384 server.app = null
385 }
386} 25}
387 26
388async function cleanupTests (servers: ServerInfo[]) { 27async function cleanupTests (servers: PeerTubeServer[]) {
389 await killallServers(servers) 28 await killallServers(servers)
390 29
391 if (isGithubCI()) { 30 if (isGithubCI()) {
@@ -403,11 +42,8 @@ async function cleanupTests (servers: ServerInfo[]) {
403// --------------------------------------------------------------------------- 42// ---------------------------------------------------------------------------
404 43
405export { 44export {
406 ServerInfo, 45 createSingleServer,
46 createMultipleServers,
407 cleanupTests, 47 cleanupTests,
408 flushAndRunMultipleServers, 48 killallServers
409 flushAndRunServer,
410 killallServers,
411 reRunServer,
412 assignCommands
413} 49}