]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/transcoding/create-transcoding.ts
e1972f275860694812abf884e1e97920393cbcb8
[github/Chocobozzz/PeerTube.git] / server / tests / api / transcoding / create-transcoding.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { checkResolutionsInMasterPlaylist, expectStartWith } from '@server/tests/shared'
5 import { areObjectStorageTestsDisabled } from '@shared/core-utils'
6 import { HttpStatusCode, VideoDetails } from '@shared/models'
7 import {
8 cleanupTests,
9 ConfigCommand,
10 createMultipleServers,
11 doubleFollow,
12 expectNoFailedTranscodingJob,
13 makeRawRequest,
14 ObjectStorageCommand,
15 PeerTubeServer,
16 setAccessTokensToServers,
17 waitJobs
18 } from '@shared/server-commands'
19
20 const expect = chai.expect
21
22 async function checkFilesInObjectStorage (video: VideoDetails) {
23 for (const file of video.files) {
24 expectStartWith(file.fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl())
25 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
26 }
27
28 if (video.streamingPlaylists.length === 0) return
29
30 const hlsPlaylist = video.streamingPlaylists[0]
31 for (const file of hlsPlaylist.files) {
32 expectStartWith(file.fileUrl, ObjectStorageCommand.getPlaylistBaseUrl())
33 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
34 }
35
36 expectStartWith(hlsPlaylist.playlistUrl, ObjectStorageCommand.getPlaylistBaseUrl())
37 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
38
39 expectStartWith(hlsPlaylist.segmentsSha256Url, ObjectStorageCommand.getPlaylistBaseUrl())
40 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
41 }
42
43 function runTests (objectStorage: boolean) {
44 let servers: PeerTubeServer[] = []
45 let videoUUID: string
46 let publishedAt: string
47
48 let shouldBeDeleted: string[]
49
50 before(async function () {
51 this.timeout(120000)
52
53 const config = objectStorage
54 ? ObjectStorageCommand.getDefaultConfig()
55 : {}
56
57 // Run server 2 to have transcoding enabled
58 servers = await createMultipleServers(2, config)
59 await setAccessTokensToServers(servers)
60
61 await servers[0].config.disableTranscoding()
62
63 await doubleFollow(servers[0], servers[1])
64
65 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
66
67 const { shortUUID } = await servers[0].videos.quickUpload({ name: 'video' })
68 videoUUID = shortUUID
69
70 await waitJobs(servers)
71
72 const video = await servers[0].videos.get({ id: videoUUID })
73 publishedAt = video.publishedAt as string
74
75 await servers[0].config.enableTranscoding()
76 })
77
78 it('Should generate HLS', async function () {
79 this.timeout(60000)
80
81 await servers[0].videos.runTranscoding({
82 videoId: videoUUID,
83 transcodingType: 'hls'
84 })
85
86 await waitJobs(servers)
87 await expectNoFailedTranscodingJob(servers[0])
88
89 for (const server of servers) {
90 const videoDetails = await server.videos.get({ id: videoUUID })
91
92 expect(videoDetails.files).to.have.lengthOf(1)
93 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
94 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
95
96 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
97 }
98 })
99
100 it('Should generate WebTorrent', async function () {
101 this.timeout(60000)
102
103 await servers[0].videos.runTranscoding({
104 videoId: videoUUID,
105 transcodingType: 'webtorrent'
106 })
107
108 await waitJobs(servers)
109
110 for (const server of servers) {
111 const videoDetails = await server.videos.get({ id: videoUUID })
112
113 expect(videoDetails.files).to.have.lengthOf(5)
114 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
115 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
116
117 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
118 }
119 })
120
121 it('Should generate WebTorrent from HLS only video', async function () {
122 this.timeout(60000)
123
124 await servers[0].videos.removeAllWebTorrentFiles({ videoId: videoUUID })
125 await waitJobs(servers)
126
127 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
128 await waitJobs(servers)
129
130 for (const server of servers) {
131 const videoDetails = await server.videos.get({ id: videoUUID })
132
133 expect(videoDetails.files).to.have.lengthOf(5)
134 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
135 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
136
137 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
138 }
139 })
140
141 it('Should only generate WebTorrent', async function () {
142 this.timeout(60000)
143
144 await servers[0].videos.removeHLSPlaylist({ videoId: videoUUID })
145 await waitJobs(servers)
146
147 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
148 await waitJobs(servers)
149
150 for (const server of servers) {
151 const videoDetails = await server.videos.get({ id: videoUUID })
152
153 expect(videoDetails.files).to.have.lengthOf(5)
154 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
155
156 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
157 }
158 })
159
160 it('Should correctly update HLS playlist on resolution change', async function () {
161 this.timeout(120000)
162
163 await servers[0].config.updateExistingSubConfig({
164 newConfig: {
165 transcoding: {
166 enabled: true,
167 resolutions: ConfigCommand.getCustomConfigResolutions(false),
168
169 webtorrent: {
170 enabled: true
171 },
172 hls: {
173 enabled: true
174 }
175 }
176 }
177 })
178
179 const { uuid } = await servers[0].videos.quickUpload({ name: 'quick' })
180
181 await waitJobs(servers)
182
183 for (const server of servers) {
184 const videoDetails = await server.videos.get({ id: uuid })
185
186 expect(videoDetails.files).to.have.lengthOf(1)
187 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
188 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(1)
189
190 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
191
192 shouldBeDeleted = [
193 videoDetails.streamingPlaylists[0].files[0].fileUrl,
194 videoDetails.streamingPlaylists[0].playlistUrl,
195 videoDetails.streamingPlaylists[0].segmentsSha256Url
196 ]
197 }
198
199 await servers[0].config.updateExistingSubConfig({
200 newConfig: {
201 transcoding: {
202 enabled: true,
203 resolutions: ConfigCommand.getCustomConfigResolutions(true),
204
205 webtorrent: {
206 enabled: true
207 },
208 hls: {
209 enabled: true
210 }
211 }
212 }
213 })
214
215 await servers[0].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
216 await waitJobs(servers)
217
218 for (const server of servers) {
219 const videoDetails = await server.videos.get({ id: uuid })
220
221 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
222 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
223
224 if (objectStorage) {
225 await checkFilesInObjectStorage(videoDetails)
226
227 const hlsPlaylist = videoDetails.streamingPlaylists[0]
228 const resolutions = hlsPlaylist.files.map(f => f.resolution.id)
229 await checkResolutionsInMasterPlaylist({ server: servers[0], playlistUrl: hlsPlaylist.playlistUrl, resolutions })
230
231 const shaBody = await servers[0].streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
232 expect(Object.keys(shaBody)).to.have.lengthOf(5)
233 }
234 }
235 })
236
237 it('Should have correctly deleted previous files', async function () {
238 for (const fileUrl of shouldBeDeleted) {
239 await makeRawRequest(fileUrl, HttpStatusCode.NOT_FOUND_404)
240 }
241 })
242
243 it('Should not have updated published at attributes', async function () {
244 const video = await servers[0].videos.get({ id: videoUUID })
245
246 expect(video.publishedAt).to.equal(publishedAt)
247 })
248
249 after(async function () {
250 await cleanupTests(servers)
251 })
252 }
253
254 describe('Test create transcoding jobs from API', function () {
255
256 describe('On filesystem', function () {
257 runTests(false)
258 })
259
260 describe('On object storage', function () {
261 if (areObjectStorageTestsDisabled()) return
262
263 runTests(true)
264 })
265 })