]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/cli/create-transcoding-job.ts
We don't need to import mocha
[github/Chocobozzz/PeerTube.git] / server / tests / cli / create-transcoding-job.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { areObjectStorageTestsDisabled } from '@shared/core-utils'
5 import { HttpStatusCode, VideoFile } 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 { checkResolutionsInMasterPlaylist, expectStartWith } from '../shared'
17
18 const expect = chai.expect
19
20 async function checkFilesInObjectStorage (files: VideoFile[], type: 'webtorrent' | 'playlist') {
21 for (const file of files) {
22 const shouldStartWith = type === 'webtorrent'
23 ? ObjectStorageCommand.getWebTorrentBaseUrl()
24 : ObjectStorageCommand.getPlaylistBaseUrl()
25
26 expectStartWith(file.fileUrl, shouldStartWith)
27
28 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
29 }
30 }
31
32 function runTests (objectStorage: boolean) {
33 let servers: PeerTubeServer[] = []
34 const videosUUID: string[] = []
35 const publishedAt: string[] = []
36
37 before(async function () {
38 this.timeout(120000)
39
40 const config = objectStorage
41 ? ObjectStorageCommand.getDefaultConfig()
42 : {}
43
44 // Run server 2 to have transcoding enabled
45 servers = await createMultipleServers(2, config)
46 await setAccessTokensToServers(servers)
47
48 await servers[0].config.disableTranscoding()
49
50 await doubleFollow(servers[0], servers[1])
51
52 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
53
54 for (let i = 1; i <= 5; i++) {
55 const { uuid, shortUUID } = await servers[0].videos.upload({ attributes: { name: 'video' + i } })
56
57 await waitJobs(servers)
58
59 const video = await servers[0].videos.get({ id: uuid })
60 publishedAt.push(video.publishedAt as string)
61
62 if (i > 2) {
63 videosUUID.push(uuid)
64 } else {
65 videosUUID.push(shortUUID)
66 }
67 }
68
69 await waitJobs(servers)
70 })
71
72 it('Should have two video files on each server', async function () {
73 this.timeout(30000)
74
75 for (const server of servers) {
76 const { data } = await server.videos.list()
77 expect(data).to.have.lengthOf(videosUUID.length)
78
79 for (const video of data) {
80 const videoDetail = await server.videos.get({ id: video.uuid })
81 expect(videoDetail.files).to.have.lengthOf(1)
82 expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
83 }
84 }
85 })
86
87 it('Should run a transcoding job on video 2', async function () {
88 this.timeout(60000)
89
90 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[1]}`)
91 await waitJobs(servers)
92
93 for (const server of servers) {
94 const { data } = await server.videos.list()
95
96 let infoHashes: { [id: number]: string }
97
98 for (const video of data) {
99 const videoDetails = await server.videos.get({ id: video.uuid })
100
101 if (video.shortUUID === videosUUID[1] || video.uuid === videosUUID[1]) {
102 expect(videoDetails.files).to.have.lengthOf(4)
103 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
104
105 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
106
107 if (!infoHashes) {
108 infoHashes = {}
109
110 for (const file of videoDetails.files) {
111 infoHashes[file.resolution.id.toString()] = file.magnetUri
112 }
113 } else {
114 for (const resolution of Object.keys(infoHashes)) {
115 const file = videoDetails.files.find(f => f.resolution.id.toString() === resolution)
116 expect(file.magnetUri).to.equal(infoHashes[resolution])
117 }
118 }
119 } else {
120 expect(videoDetails.files).to.have.lengthOf(1)
121 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
122 }
123 }
124 }
125 })
126
127 it('Should run a transcoding job on video 1 with resolution', async function () {
128 this.timeout(60000)
129
130 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[0]} -r 480`)
131
132 await waitJobs(servers)
133
134 for (const server of servers) {
135 const { data } = await server.videos.list()
136 expect(data).to.have.lengthOf(videosUUID.length)
137
138 const videoDetails = await server.videos.get({ id: videosUUID[0] })
139
140 expect(videoDetails.files).to.have.lengthOf(2)
141 expect(videoDetails.files[0].resolution.id).to.equal(720)
142 expect(videoDetails.files[1].resolution.id).to.equal(480)
143
144 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
145
146 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
147 }
148 })
149
150 it('Should generate an HLS resolution', async function () {
151 this.timeout(120000)
152
153 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
154
155 await waitJobs(servers)
156
157 for (const server of servers) {
158 const videoDetails = await server.videos.get({ id: videosUUID[2] })
159
160 expect(videoDetails.files).to.have.lengthOf(1)
161 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
162
163 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
164
165 const hlsPlaylist = videoDetails.streamingPlaylists[0]
166
167 const files = hlsPlaylist.files
168 expect(files).to.have.lengthOf(1)
169 expect(files[0].resolution.id).to.equal(480)
170
171 if (objectStorage) {
172 await checkFilesInObjectStorage(files, 'playlist')
173
174 const resolutions = files.map(f => f.resolution.id)
175 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
176 }
177 }
178 })
179
180 it('Should not duplicate an HLS resolution', async function () {
181 this.timeout(120000)
182
183 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
184
185 await waitJobs(servers)
186
187 for (const server of servers) {
188 const videoDetails = await server.videos.get({ id: videosUUID[2] })
189
190 const files = videoDetails.streamingPlaylists[0].files
191 expect(files).to.have.lengthOf(1)
192 expect(files[0].resolution.id).to.equal(480)
193
194 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
195 }
196 })
197
198 it('Should generate all HLS resolutions', async function () {
199 this.timeout(120000)
200
201 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`)
202
203 await waitJobs(servers)
204
205 for (const server of servers) {
206 const videoDetails = await server.videos.get({ id: videosUUID[3] })
207
208 expect(videoDetails.files).to.have.lengthOf(1)
209 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
210
211 const files = videoDetails.streamingPlaylists[0].files
212 expect(files).to.have.lengthOf(4)
213
214 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
215 }
216 })
217
218 it('Should optimize the video file and generate HLS videos if enabled in config', async function () {
219 this.timeout(120000)
220
221 await servers[0].config.enableTranscoding()
222 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`)
223
224 await waitJobs(servers)
225
226 for (const server of servers) {
227 const videoDetails = await server.videos.get({ id: videosUUID[4] })
228
229 expect(videoDetails.files).to.have.lengthOf(5)
230 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
231 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
232
233 if (objectStorage) {
234 await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
235 await checkFilesInObjectStorage(videoDetails.streamingPlaylists[0].files, 'playlist')
236 }
237 }
238 })
239
240 it('Should not have updated published at attributes', async function () {
241 for (const id of videosUUID) {
242 const video = await servers[0].videos.get({ id })
243
244 expect(publishedAt.some(p => video.publishedAt === p)).to.be.true
245 }
246 })
247
248 after(async function () {
249 await cleanupTests(servers)
250 })
251 }
252
253 describe('Test create transcoding jobs', function () {
254
255 describe('On filesystem', function () {
256 runTests(false)
257 })
258
259 describe('On object storage', function () {
260 if (areObjectStorageTestsDisabled()) return
261
262 runTests(true)
263 })
264 })