diff options
author | Chocobozzz <me@florianbigard.com> | 2021-10-22 14:31:38 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-10-22 14:31:38 +0200 |
commit | d1bfbdeb203b0e4f37e9468861c690171156ee29 (patch) | |
tree | 2a6051ef212b964eb2ded1021a625acd0ba32053 /shared/extra-utils | |
parent | 5480933b7f088bf099b25fb467faace814f0da58 (diff) | |
download | PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.tar.gz PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.tar.zst PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.zip |
Random listen for mocked servers
Diffstat (limited to 'shared/extra-utils')
6 files changed, 86 insertions, 78 deletions
diff --git a/shared/extra-utils/mock-servers/mock-instances-index.ts b/shared/extra-utils/mock-servers/mock-instances-index.ts index 43c2e9f6e..92b12d6f3 100644 --- a/shared/extra-utils/mock-servers/mock-instances-index.ts +++ b/shared/extra-utils/mock-servers/mock-instances-index.ts | |||
@@ -1,41 +1,39 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { Server } from 'http' | 2 | import { Server } from 'http' |
3 | import { randomInt } from '@shared/core-utils' | 3 | import { getPort, randomListen, terminateServer } from './utils' |
4 | import { terminateServer } from './utils' | ||
5 | 4 | ||
6 | export class MockInstancesIndex { | 5 | export class MockInstancesIndex { |
7 | private server: Server | 6 | private server: Server |
8 | 7 | ||
9 | private readonly indexInstances: { host: string, createdAt: string }[] = [] | 8 | private readonly indexInstances: { host: string, createdAt: string }[] = [] |
10 | 9 | ||
11 | initialize () { | 10 | async initialize () { |
12 | return new Promise<number>(res => { | 11 | const app = express() |
13 | const app = express() | ||
14 | 12 | ||
15 | app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { | 13 | app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { |
16 | if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) | 14 | if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) |
17 | 15 | ||
18 | return next() | 16 | return next() |
19 | }) | 17 | }) |
20 | |||
21 | app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => { | ||
22 | const since = req.query.since | ||
23 | 18 | ||
24 | const filtered = this.indexInstances.filter(i => { | 19 | app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => { |
25 | if (!since) return true | 20 | const since = req.query.since |
26 | 21 | ||
27 | return i.createdAt > since | 22 | const filtered = this.indexInstances.filter(i => { |
28 | }) | 23 | if (!since) return true |
29 | 24 | ||
30 | return res.json({ | 25 | return i.createdAt > since |
31 | total: filtered.length, | ||
32 | data: filtered | ||
33 | }) | ||
34 | }) | 26 | }) |
35 | 27 | ||
36 | const port = 42000 + randomInt(1, 1000) | 28 | return res.json({ |
37 | this.server = app.listen(port, () => res(port)) | 29 | total: filtered.length, |
30 | data: filtered | ||
31 | }) | ||
38 | }) | 32 | }) |
33 | |||
34 | this.server = await randomListen(app) | ||
35 | |||
36 | return getPort(this.server) | ||
39 | } | 37 | } |
40 | 38 | ||
41 | addInstance (host: string) { | 39 | addInstance (host: string) { |
diff --git a/shared/extra-utils/mock-servers/mock-joinpeertube-versions.ts b/shared/extra-utils/mock-servers/mock-joinpeertube-versions.ts index 79be31f61..e7906ea56 100644 --- a/shared/extra-utils/mock-servers/mock-joinpeertube-versions.ts +++ b/shared/extra-utils/mock-servers/mock-joinpeertube-versions.ts | |||
@@ -1,30 +1,31 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { randomInt } from '@shared/core-utils' | 2 | import { Server } from 'http' |
3 | import { getPort, randomListen } from './utils' | ||
3 | 4 | ||
4 | export class MockJoinPeerTubeVersions { | 5 | export class MockJoinPeerTubeVersions { |
6 | private server: Server | ||
5 | private latestVersion: string | 7 | private latestVersion: string |
6 | 8 | ||
7 | initialize () { | 9 | async initialize () { |
8 | return new Promise<number>(res => { | 10 | const app = express() |
9 | const app = express() | ||
10 | 11 | ||
11 | app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { | 12 | app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { |
12 | if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) | 13 | if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) |
13 | 14 | ||
14 | return next() | 15 | return next() |
15 | }) | 16 | }) |
16 | 17 | ||
17 | app.get('/versions.json', (req: express.Request, res: express.Response) => { | 18 | app.get('/versions.json', (req: express.Request, res: express.Response) => { |
18 | return res.json({ | 19 | return res.json({ |
19 | peertube: { | 20 | peertube: { |
20 | latestVersion: this.latestVersion | 21 | latestVersion: this.latestVersion |
21 | } | 22 | } |
22 | }) | ||
23 | }) | 23 | }) |
24 | |||
25 | const port = 43000 + randomInt(1, 1000) | ||
26 | app.listen(port, () => res(port)) | ||
27 | }) | 24 | }) |
25 | |||
26 | this.server = await randomListen(app) | ||
27 | |||
28 | return getPort(this.server) | ||
28 | } | 29 | } |
29 | 30 | ||
30 | setLatestVersion (latestVersion: string) { | 31 | setLatestVersion (latestVersion: string) { |
diff --git a/shared/extra-utils/mock-servers/mock-object-storage.ts b/shared/extra-utils/mock-servers/mock-object-storage.ts index 144f2819d..d135c2631 100644 --- a/shared/extra-utils/mock-servers/mock-object-storage.ts +++ b/shared/extra-utils/mock-servers/mock-object-storage.ts | |||
@@ -2,39 +2,37 @@ import express from 'express' | |||
2 | import got, { RequestError } from 'got' | 2 | import got, { RequestError } from 'got' |
3 | import { Server } from 'http' | 3 | import { Server } from 'http' |
4 | import { pipeline } from 'stream' | 4 | import { pipeline } from 'stream' |
5 | import { randomInt } from '@shared/core-utils' | ||
6 | import { ObjectStorageCommand } from '../server' | 5 | import { ObjectStorageCommand } from '../server' |
7 | import { terminateServer } from './utils' | 6 | import { getPort, randomListen, terminateServer } from './utils' |
8 | 7 | ||
9 | export class MockObjectStorage { | 8 | export class MockObjectStorage { |
10 | private server: Server | 9 | private server: Server |
11 | 10 | ||
12 | initialize () { | 11 | async initialize () { |
13 | return new Promise<number>(res => { | 12 | const app = express() |
14 | const app = express() | ||
15 | 13 | ||
16 | app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => { | 14 | app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => { |
17 | const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getEndpointHost()}/${req.params.path}` | 15 | const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getEndpointHost()}/${req.params.path}` |
18 | 16 | ||
19 | if (process.env.DEBUG) { | 17 | if (process.env.DEBUG) { |
20 | console.log('Receiving request on mocked server %s.', req.url) | 18 | console.log('Receiving request on mocked server %s.', req.url) |
21 | console.log('Proxifying request to %s', url) | 19 | console.log('Proxifying request to %s', url) |
22 | } | 20 | } |
23 | |||
24 | return pipeline( | ||
25 | got.stream(url, { throwHttpErrors: false }), | ||
26 | res, | ||
27 | (err: RequestError) => { | ||
28 | if (!err) return | ||
29 | 21 | ||
30 | console.error('Pipeline failed.', err) | 22 | return pipeline( |
31 | } | 23 | got.stream(url, { throwHttpErrors: false }), |
32 | ) | 24 | res, |
33 | }) | 25 | (err: RequestError) => { |
26 | if (!err) return | ||
34 | 27 | ||
35 | const port = 44000 + randomInt(1, 1000) | 28 | console.error('Pipeline failed.', err) |
36 | this.server = app.listen(port, () => res(port)) | 29 | } |
30 | ) | ||
37 | }) | 31 | }) |
32 | |||
33 | this.server = await randomListen(app) | ||
34 | |||
35 | return getPort(this.server) | ||
38 | } | 36 | } |
39 | 37 | ||
40 | terminate () { | 38 | terminate () { |
diff --git a/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts b/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts index 344d4bdbb..f8a271cba 100644 --- a/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts +++ b/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts | |||
@@ -1,7 +1,6 @@ | |||
1 | import express, { Request, Response } from 'express' | 1 | import express, { Request, Response } from 'express' |
2 | import { Server } from 'http' | 2 | import { Server } from 'http' |
3 | import { randomInt } from '@shared/core-utils' | 3 | import { getPort, randomListen, terminateServer } from './utils' |
4 | import { terminateServer } from './utils' | ||
5 | 4 | ||
6 | type BlocklistResponse = { | 5 | type BlocklistResponse = { |
7 | data: { | 6 | data: { |
@@ -15,17 +14,16 @@ export class MockBlocklist { | |||
15 | private body: BlocklistResponse | 14 | private body: BlocklistResponse |
16 | private server: Server | 15 | private server: Server |
17 | 16 | ||
18 | initialize () { | 17 | async initialize () { |
19 | return new Promise<number>(res => { | 18 | const app = express() |
20 | const app = express() | ||
21 | 19 | ||
22 | app.get('/blocklist', (req: Request, res: Response) => { | 20 | app.get('/blocklist', (req: Request, res: Response) => { |
23 | return res.json(this.body) | 21 | return res.json(this.body) |
24 | }) | ||
25 | |||
26 | const port = 45000 + randomInt(1, 1000) | ||
27 | this.server = app.listen(port, () => res(port)) | ||
28 | }) | 22 | }) |
23 | |||
24 | this.server = await randomListen(app) | ||
25 | |||
26 | return getPort(this.server) | ||
29 | } | 27 | } |
30 | 28 | ||
31 | replace (body: BlocklistResponse) { | 29 | replace (body: BlocklistResponse) { |
diff --git a/shared/extra-utils/mock-servers/mock-proxy.ts b/shared/extra-utils/mock-servers/mock-proxy.ts index 8583250f3..75ac79055 100644 --- a/shared/extra-utils/mock-servers/mock-proxy.ts +++ b/shared/extra-utils/mock-servers/mock-proxy.ts | |||
@@ -1,18 +1,15 @@ | |||
1 | 1 | ||
2 | import { createServer, Server } from 'http' | 2 | import { createServer, Server } from 'http' |
3 | import proxy from 'proxy' | 3 | import proxy from 'proxy' |
4 | import { randomInt } from '@shared/core-utils' | 4 | import { getPort, terminateServer } from './utils' |
5 | import { terminateServer } from './utils' | ||
6 | 5 | ||
7 | class MockProxy { | 6 | class MockProxy { |
8 | private server: Server | 7 | private server: Server |
9 | 8 | ||
10 | initialize () { | 9 | initialize () { |
11 | return new Promise<number>(res => { | 10 | return new Promise<number>(res => { |
12 | const port = 46000 + randomInt(1, 1000) | ||
13 | |||
14 | this.server = proxy(createServer()) | 11 | this.server = proxy(createServer()) |
15 | this.server.listen(port, () => res(port)) | 12 | this.server.listen(0, () => res(getPort(this.server))) |
16 | }) | 13 | }) |
17 | } | 14 | } |
18 | 15 | ||
diff --git a/shared/extra-utils/mock-servers/utils.ts b/shared/extra-utils/mock-servers/utils.ts index 64d94c868..235642439 100644 --- a/shared/extra-utils/mock-servers/utils.ts +++ b/shared/extra-utils/mock-servers/utils.ts | |||
@@ -1,4 +1,18 @@ | |||
1 | import { Express } from 'express' | ||
1 | import { Server } from 'http' | 2 | import { Server } from 'http' |
3 | import { AddressInfo } from 'net' | ||
4 | |||
5 | function randomListen (app: Express) { | ||
6 | return new Promise<Server>(res => { | ||
7 | const server = app.listen(0, () => res(server)) | ||
8 | }) | ||
9 | } | ||
10 | |||
11 | function getPort (server: Server) { | ||
12 | const address = server.address() as AddressInfo | ||
13 | |||
14 | return address.port | ||
15 | } | ||
2 | 16 | ||
3 | function terminateServer (server: Server) { | 17 | function terminateServer (server: Server) { |
4 | if (!server) return Promise.resolve() | 18 | if (!server) return Promise.resolve() |
@@ -13,5 +27,7 @@ function terminateServer (server: Server) { | |||
13 | } | 27 | } |
14 | 28 | ||
15 | export { | 29 | export { |
30 | randomListen, | ||
31 | getPort, | ||
16 | terminateServer | 32 | terminateServer |
17 | } | 33 | } |