]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/cli/create-import-video-file-job.ts
Merge branch 'release/5.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / cli / create-import-video-file-job.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { areMockObjectStorageTestsDisabled } from '@shared/core-utils'
5 import { HttpStatusCode, VideoDetails, VideoFile, VideoInclude } from '@shared/models'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 doubleFollow,
10 makeRawRequest,
11 ObjectStorageCommand,
12 PeerTubeServer,
13 setAccessTokensToServers,
14 waitJobs
15 } from '@shared/server-commands'
16 import { expectStartWith } from '../shared'
17
18 function assertVideoProperties (video: VideoFile, resolution: number, extname: string, size?: number) {
19 expect(video).to.have.nested.property('resolution.id', resolution)
20 expect(video).to.have.property('torrentUrl').that.includes(`-${resolution}.torrent`)
21 expect(video).to.have.property('fileUrl').that.includes(`.${extname}`)
22 expect(video).to.have.property('magnetUri').that.includes(`.${extname}`)
23 expect(video).to.have.property('size').that.is.above(0)
24
25 if (size) expect(video.size).to.equal(size)
26 }
27
28 async function checkFiles (video: VideoDetails, objectStorage: boolean) {
29 for (const file of video.files) {
30 if (objectStorage) expectStartWith(file.fileUrl, ObjectStorageCommand.getMockWebTorrentBaseUrl())
31
32 await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
33 }
34 }
35
36 function runTests (objectStorage: boolean) {
37 let video1ShortId: string
38 let video2UUID: string
39
40 let servers: PeerTubeServer[] = []
41
42 before(async function () {
43 this.timeout(90000)
44
45 const config = objectStorage
46 ? ObjectStorageCommand.getDefaultMockConfig()
47 : {}
48
49 // Run server 2 to have transcoding enabled
50 servers = await createMultipleServers(2, config)
51 await setAccessTokensToServers(servers)
52
53 await doubleFollow(servers[0], servers[1])
54
55 if (objectStorage) await ObjectStorageCommand.prepareDefaultMockBuckets()
56
57 // Upload two videos for our needs
58 {
59 const { shortUUID } = await servers[0].videos.upload({ attributes: { name: 'video1' } })
60 video1ShortId = shortUUID
61 }
62
63 {
64 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video2' } })
65 video2UUID = uuid
66 }
67
68 await waitJobs(servers)
69
70 for (const server of servers) {
71 await server.config.enableTranscoding()
72 }
73 })
74
75 it('Should run a import job on video 1 with a lower resolution', async function () {
76 const command = `npm run create-import-video-file-job -- -v ${video1ShortId} -i server/tests/fixtures/video_short-480.webm`
77 await servers[0].cli.execWithEnv(command)
78
79 await waitJobs(servers)
80
81 for (const server of servers) {
82 const { data: videos } = await server.videos.list()
83 expect(videos).to.have.lengthOf(2)
84
85 const video = videos.find(({ shortUUID }) => shortUUID === video1ShortId)
86 const videoDetails = await server.videos.get({ id: video.shortUUID })
87
88 expect(videoDetails.files).to.have.lengthOf(2)
89 const [ originalVideo, transcodedVideo ] = videoDetails.files
90 assertVideoProperties(originalVideo, 720, 'webm', 218910)
91 assertVideoProperties(transcodedVideo, 480, 'webm', 69217)
92
93 await checkFiles(videoDetails, objectStorage)
94 }
95 })
96
97 it('Should run a import job on video 2 with the same resolution and a different extension', async function () {
98 const command = `npm run create-import-video-file-job -- -v ${video2UUID} -i server/tests/fixtures/video_short.ogv`
99 await servers[1].cli.execWithEnv(command)
100
101 await waitJobs(servers)
102
103 for (const server of servers) {
104 const { data: videos } = await server.videos.listWithToken({ include: VideoInclude.NOT_PUBLISHED_STATE })
105 expect(videos).to.have.lengthOf(2)
106
107 const video = videos.find(({ uuid }) => uuid === video2UUID)
108 const videoDetails = await server.videos.get({ id: video.uuid })
109
110 expect(videoDetails.files).to.have.lengthOf(4)
111 const [ originalVideo, transcodedVideo420, transcodedVideo320, transcodedVideo240 ] = videoDetails.files
112 assertVideoProperties(originalVideo, 720, 'ogv', 140849)
113 assertVideoProperties(transcodedVideo420, 480, 'mp4')
114 assertVideoProperties(transcodedVideo320, 360, 'mp4')
115 assertVideoProperties(transcodedVideo240, 240, 'mp4')
116
117 await checkFiles(videoDetails, objectStorage)
118 }
119 })
120
121 it('Should run a import job on video 2 with the same resolution and the same extension', async function () {
122 const command = `npm run create-import-video-file-job -- -v ${video1ShortId} -i server/tests/fixtures/video_short2.webm`
123 await servers[0].cli.execWithEnv(command)
124
125 await waitJobs(servers)
126
127 for (const server of servers) {
128 const { data: videos } = await server.videos.listWithToken({ include: VideoInclude.NOT_PUBLISHED_STATE })
129 expect(videos).to.have.lengthOf(2)
130
131 const video = videos.find(({ shortUUID }) => shortUUID === video1ShortId)
132 const videoDetails = await server.videos.get({ id: video.uuid })
133
134 expect(videoDetails.files).to.have.lengthOf(2)
135 const [ video720, video480 ] = videoDetails.files
136 assertVideoProperties(video720, 720, 'webm', 942961)
137 assertVideoProperties(video480, 480, 'webm', 69217)
138
139 await checkFiles(videoDetails, objectStorage)
140 }
141 })
142
143 it('Should not have run transcoding after an import job', async function () {
144 const { data } = await servers[0].jobs.list({ jobType: 'video-transcoding' })
145 expect(data).to.have.lengthOf(0)
146 })
147
148 after(async function () {
149 await cleanupTests(servers)
150 })
151 }
152
153 describe('Test create import video jobs', function () {
154
155 describe('On filesystem', function () {
156 runTests(false)
157 })
158
159 describe('On object storage', function () {
160 if (areMockObjectStorageTestsDisabled()) return
161
162 runTests(true)
163 })
164 })