diff options
author | Chocobozzz <me@florianbigard.com> | 2021-12-17 11:58:15 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-12-17 12:24:03 +0100 |
commit | c55e3d7227fe1453869e309025996b9d75256d5d (patch) | |
tree | 08e9b0ca210d75c82c8606fef0852eca020e8e0e /shared/server-commands/miscs | |
parent | bf54587a3e2ad9c2c186828f2a5682b91ee2cc00 (diff) | |
download | PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.tar.gz PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.tar.zst PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.zip |
Move test functions outside extra-utils
Diffstat (limited to 'shared/server-commands/miscs')
-rw-r--r-- | shared/server-commands/miscs/checks.ts | 58 | ||||
-rw-r--r-- | shared/server-commands/miscs/generate.ts | 75 | ||||
-rw-r--r-- | shared/server-commands/miscs/index.ts | 3 | ||||
-rw-r--r-- | shared/server-commands/miscs/sql-command.ts | 2 | ||||
-rw-r--r-- | shared/server-commands/miscs/tests.ts | 101 |
5 files changed, 1 insertions, 238 deletions
diff --git a/shared/server-commands/miscs/checks.ts b/shared/server-commands/miscs/checks.ts deleted file mode 100644 index 589928997..000000000 --- a/shared/server-commands/miscs/checks.ts +++ /dev/null | |||
@@ -1,58 +0,0 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { pathExists, readFile } from 'fs-extra' | ||
5 | import { join } from 'path' | ||
6 | import { root } from '@shared/core-utils' | ||
7 | import { HttpStatusCode } from '@shared/models' | ||
8 | import { makeGetRequest } from '../requests' | ||
9 | import { PeerTubeServer } from '../server' | ||
10 | |||
11 | // Default interval -> 5 minutes | ||
12 | function dateIsValid (dateString: string, interval = 300000) { | ||
13 | const dateToCheck = new Date(dateString) | ||
14 | const now = new Date() | ||
15 | |||
16 | return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval | ||
17 | } | ||
18 | |||
19 | function expectStartWith (str: string, start: string) { | ||
20 | expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true | ||
21 | } | ||
22 | |||
23 | async function expectLogDoesNotContain (server: PeerTubeServer, str: string) { | ||
24 | const content = await server.servers.getLogContent() | ||
25 | |||
26 | expect(content.toString()).to.not.contain(str) | ||
27 | } | ||
28 | |||
29 | async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') { | ||
30 | const res = await makeGetRequest({ | ||
31 | url, | ||
32 | path: imagePath, | ||
33 | expectedStatus: HttpStatusCode.OK_200 | ||
34 | }) | ||
35 | |||
36 | const body = res.body | ||
37 | |||
38 | const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension)) | ||
39 | const minLength = body.length - ((30 * body.length) / 100) | ||
40 | const maxLength = body.length + ((30 * body.length) / 100) | ||
41 | |||
42 | expect(data.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture') | ||
43 | expect(data.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture') | ||
44 | } | ||
45 | |||
46 | async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) { | ||
47 | const base = server.servers.buildDirectory(directory) | ||
48 | |||
49 | expect(await pathExists(join(base, filePath))).to.equal(exist) | ||
50 | } | ||
51 | |||
52 | export { | ||
53 | dateIsValid, | ||
54 | testImage, | ||
55 | expectLogDoesNotContain, | ||
56 | testFileExistsOrNot, | ||
57 | expectStartWith | ||
58 | } | ||
diff --git a/shared/server-commands/miscs/generate.ts b/shared/server-commands/miscs/generate.ts deleted file mode 100644 index 93673a063..000000000 --- a/shared/server-commands/miscs/generate.ts +++ /dev/null | |||
@@ -1,75 +0,0 @@ | |||
1 | import { expect } from 'chai' | ||
2 | import ffmpeg from 'fluent-ffmpeg' | ||
3 | import { ensureDir, pathExists } from 'fs-extra' | ||
4 | import { dirname } from 'path' | ||
5 | import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '@shared/extra-utils/ffprobe' | ||
6 | import { getMaxBitrate } from '@shared/core-utils' | ||
7 | import { buildAbsoluteFixturePath } from './tests' | ||
8 | |||
9 | async function ensureHasTooBigBitrate (fixturePath: string) { | ||
10 | const bitrate = await getVideoFileBitrate(fixturePath) | ||
11 | const dataResolution = await getVideoFileResolution(fixturePath) | ||
12 | const fps = await getVideoFileFPS(fixturePath) | ||
13 | |||
14 | const maxBitrate = getMaxBitrate({ ...dataResolution, fps }) | ||
15 | expect(bitrate).to.be.above(maxBitrate) | ||
16 | } | ||
17 | |||
18 | async function generateHighBitrateVideo () { | ||
19 | const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true) | ||
20 | |||
21 | await ensureDir(dirname(tempFixturePath)) | ||
22 | |||
23 | const exists = await pathExists(tempFixturePath) | ||
24 | if (!exists) { | ||
25 | console.log('Generating high bitrate video.') | ||
26 | |||
27 | // Generate a random, high bitrate video on the fly, so we don't have to include | ||
28 | // a large file in the repo. The video needs to have a certain minimum length so | ||
29 | // that FFmpeg properly applies bitrate limits. | ||
30 | // https://stackoverflow.com/a/15795112 | ||
31 | return new Promise<string>((res, rej) => { | ||
32 | ffmpeg() | ||
33 | .outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ]) | ||
34 | .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ]) | ||
35 | .outputOptions([ '-maxrate 10M', '-bufsize 10M' ]) | ||
36 | .output(tempFixturePath) | ||
37 | .on('error', rej) | ||
38 | .on('end', () => res(tempFixturePath)) | ||
39 | .run() | ||
40 | }) | ||
41 | } | ||
42 | |||
43 | await ensureHasTooBigBitrate(tempFixturePath) | ||
44 | |||
45 | return tempFixturePath | ||
46 | } | ||
47 | |||
48 | async function generateVideoWithFramerate (fps = 60) { | ||
49 | const tempFixturePath = buildAbsoluteFixturePath(`video_${fps}fps.mp4`, true) | ||
50 | |||
51 | await ensureDir(dirname(tempFixturePath)) | ||
52 | |||
53 | const exists = await pathExists(tempFixturePath) | ||
54 | if (!exists) { | ||
55 | console.log('Generating video with framerate %d.', fps) | ||
56 | |||
57 | return new Promise<string>((res, rej) => { | ||
58 | ffmpeg() | ||
59 | .outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ]) | ||
60 | .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ]) | ||
61 | .outputOptions([ `-r ${fps}` ]) | ||
62 | .output(tempFixturePath) | ||
63 | .on('error', rej) | ||
64 | .on('end', () => res(tempFixturePath)) | ||
65 | .run() | ||
66 | }) | ||
67 | } | ||
68 | |||
69 | return tempFixturePath | ||
70 | } | ||
71 | |||
72 | export { | ||
73 | generateHighBitrateVideo, | ||
74 | generateVideoWithFramerate | ||
75 | } | ||
diff --git a/shared/server-commands/miscs/index.ts b/shared/server-commands/miscs/index.ts index 4474661de..a1d14e998 100644 --- a/shared/server-commands/miscs/index.ts +++ b/shared/server-commands/miscs/index.ts | |||
@@ -1,5 +1,2 @@ | |||
1 | export * from './checks' | ||
2 | export * from './generate' | ||
3 | export * from './sql-command' | 1 | export * from './sql-command' |
4 | export * from './tests' | ||
5 | export * from './webtorrent' | 2 | export * from './webtorrent' |
diff --git a/shared/server-commands/miscs/sql-command.ts b/shared/server-commands/miscs/sql-command.ts index bedb3349b..09a99f834 100644 --- a/shared/server-commands/miscs/sql-command.ts +++ b/shared/server-commands/miscs/sql-command.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { QueryTypes, Sequelize } from 'sequelize' | 1 | import { QueryTypes, Sequelize } from 'sequelize' |
2 | import { AbstractCommand } from '../shared/abstract-command' | 2 | import { AbstractCommand } from '../shared' |
3 | 3 | ||
4 | export class SQLCommand extends AbstractCommand { | 4 | export class SQLCommand extends AbstractCommand { |
5 | private sequelize: Sequelize | 5 | private sequelize: Sequelize |
diff --git a/shared/server-commands/miscs/tests.ts b/shared/server-commands/miscs/tests.ts deleted file mode 100644 index 658fe5fd3..000000000 --- a/shared/server-commands/miscs/tests.ts +++ /dev/null | |||
@@ -1,101 +0,0 @@ | |||
1 | import { stat } from 'fs-extra' | ||
2 | import { basename, isAbsolute, join, resolve } from 'path' | ||
3 | |||
4 | const FIXTURE_URLS = { | ||
5 | peertube_long: 'https://peertube2.cpy.re/videos/watch/122d093a-1ede-43bd-bd34-59d2931ffc5e', | ||
6 | peertube_short: 'https://peertube2.cpy.re/w/3fbif9S3WmtTP8gGsC5HBd', | ||
7 | |||
8 | youtube: 'https://www.youtube.com/watch?v=msX3jv1XdvM', | ||
9 | |||
10 | /** | ||
11 | * The video is used to check format-selection correctness wrt. HDR, | ||
12 | * which brings its own set of oddities outside of a MediaSource. | ||
13 | * | ||
14 | * The video needs to have the following format_ids: | ||
15 | * (which you can check by using `youtube-dl <url> -F`): | ||
16 | * - (webm vp9) | ||
17 | * - (mp4 avc1) | ||
18 | * - (webm vp9.2 HDR) | ||
19 | */ | ||
20 | youtubeHDR: 'https://www.youtube.com/watch?v=RQgnBB9z_N4', | ||
21 | |||
22 | // eslint-disable-next-line max-len | ||
23 | magnet: 'magnet:?xs=https%3A%2F%2Fpeertube2.cpy.re%2Flazy-static%2Ftorrents%2Fb209ca00-c8bb-4b2b-b421-1ede169f3dbc-720.torrent&xt=urn:btih:0f498834733e8057ed5c6f2ee2b4efd8d84a76ee&dn=super+peertube2+video&tr=https%3A%2F%2Fpeertube2.cpy.re%2Ftracker%2Fannounce&tr=wss%3A%2F%2Fpeertube2.cpy.re%3A443%2Ftracker%2Fsocket&ws=https%3A%2F%2Fpeertube2.cpy.re%2Fstatic%2Fwebseed%2Fb209ca00-c8bb-4b2b-b421-1ede169f3dbc-720.mp4', | ||
24 | |||
25 | badVideo: 'https://download.cpy.re/peertube/bad_video.mp4', | ||
26 | goodVideo: 'https://download.cpy.re/peertube/good_video.mp4', | ||
27 | goodVideo720: 'https://download.cpy.re/peertube/good_video_720.mp4', | ||
28 | |||
29 | file4K: 'https://download.cpy.re/peertube/4k_file.txt' | ||
30 | } | ||
31 | |||
32 | function parallelTests () { | ||
33 | return process.env.MOCHA_PARALLEL === 'true' | ||
34 | } | ||
35 | |||
36 | function isGithubCI () { | ||
37 | return !!process.env.GITHUB_WORKSPACE | ||
38 | } | ||
39 | |||
40 | function areHttpImportTestsDisabled () { | ||
41 | const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true' | ||
42 | |||
43 | if (disabled) console.log('DISABLE_HTTP_IMPORT_TESTS env set to "true" so import tests are disabled') | ||
44 | |||
45 | return disabled | ||
46 | } | ||
47 | |||
48 | function areObjectStorageTestsDisabled () { | ||
49 | const disabled = process.env.ENABLE_OBJECT_STORAGE_TESTS !== 'true' | ||
50 | |||
51 | if (disabled) console.log('ENABLE_OBJECT_STORAGE_TESTS env is not set to "true" so object storage tests are disabled') | ||
52 | |||
53 | return disabled | ||
54 | } | ||
55 | |||
56 | function buildAbsoluteFixturePath (path: string, customCIPath = false) { | ||
57 | if (isAbsolute(path)) return path | ||
58 | |||
59 | if (customCIPath && process.env.GITHUB_WORKSPACE) { | ||
60 | return join(process.env.GITHUB_WORKSPACE, 'fixtures', path) | ||
61 | } | ||
62 | |||
63 | return join(root(), 'server', 'tests', 'fixtures', path) | ||
64 | } | ||
65 | |||
66 | function root () { | ||
67 | // We are in /miscs | ||
68 | let root = join(__dirname, '..', '..', '..') | ||
69 | |||
70 | if (basename(root) === 'dist') root = resolve(root, '..') | ||
71 | |||
72 | return root | ||
73 | } | ||
74 | |||
75 | function wait (milliseconds: number) { | ||
76 | return new Promise(resolve => setTimeout(resolve, milliseconds)) | ||
77 | } | ||
78 | |||
79 | async function getFileSize (path: string) { | ||
80 | const stats = await stat(path) | ||
81 | |||
82 | return stats.size | ||
83 | } | ||
84 | |||
85 | function buildRequestStub (): any { | ||
86 | return { } | ||
87 | } | ||
88 | |||
89 | export { | ||
90 | FIXTURE_URLS, | ||
91 | |||
92 | parallelTests, | ||
93 | isGithubCI, | ||
94 | areHttpImportTestsDisabled, | ||
95 | buildAbsoluteFixturePath, | ||
96 | getFileSize, | ||
97 | buildRequestStub, | ||
98 | areObjectStorageTestsDisabled, | ||
99 | wait, | ||
100 | root | ||
101 | } | ||