]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/cli/create-transcoding-job.ts
Introduce config command
[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 'mocha'
4 import * as chai from 'chai'
5 import {
6 cleanupTests,
7 doubleFollow,
8 flushAndRunMultipleServers,
9 getVideo,
10 getVideosList,
11 ServerInfo,
12 setAccessTokensToServers,
13 uploadVideo
14 } from '../../../shared/extra-utils'
15 import { waitJobs } from '../../../shared/extra-utils/server/jobs'
16 import { VideoDetails } from '../../../shared/models/videos'
17
18 const expect = chai.expect
19
20 describe('Test create transcoding jobs', function () {
21 let servers: ServerInfo[] = []
22 const videosUUID: string[] = []
23
24 const config = {
25 transcoding: {
26 enabled: false,
27 resolutions: {
28 '240p': true,
29 '360p': true,
30 '480p': true,
31 '720p': true,
32 '1080p': true,
33 '1440p': true,
34 '2160p': true
35 },
36 hls: {
37 enabled: false
38 }
39 }
40 }
41
42 before(async function () {
43 this.timeout(60000)
44
45 // Run server 2 to have transcoding enabled
46 servers = await flushAndRunMultipleServers(2)
47 await setAccessTokensToServers(servers)
48
49 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
50
51 await doubleFollow(servers[0], servers[1])
52
53 for (let i = 1; i <= 5; i++) {
54 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' + i })
55 videosUUID.push(res.body.video.uuid)
56 }
57
58 await waitJobs(servers)
59 })
60
61 it('Should have two video files on each server', async function () {
62 this.timeout(30000)
63
64 for (const server of servers) {
65 const res = await getVideosList(server.url)
66 const videos = res.body.data
67 expect(videos).to.have.lengthOf(videosUUID.length)
68
69 for (const video of videos) {
70 const res2 = await getVideo(server.url, video.uuid)
71 const videoDetail: VideoDetails = res2.body
72 expect(videoDetail.files).to.have.lengthOf(1)
73 expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
74 }
75 }
76 })
77
78 it('Should run a transcoding job on video 2', async function () {
79 this.timeout(60000)
80
81 await servers[0].cliCommand.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[1]}`)
82 await waitJobs(servers)
83
84 for (const server of servers) {
85 const res = await getVideosList(server.url)
86 const videos = res.body.data
87
88 let infoHashes: { [id: number]: string }
89
90 for (const video of videos) {
91 const res2 = await getVideo(server.url, video.uuid)
92 const videoDetail: VideoDetails = res2.body
93
94 if (video.uuid === videosUUID[1]) {
95 expect(videoDetail.files).to.have.lengthOf(4)
96 expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
97
98 if (!infoHashes) {
99 infoHashes = {}
100
101 for (const file of videoDetail.files) {
102 infoHashes[file.resolution.id.toString()] = file.magnetUri
103 }
104 } else {
105 for (const resolution of Object.keys(infoHashes)) {
106 const file = videoDetail.files.find(f => f.resolution.id.toString() === resolution)
107 expect(file.magnetUri).to.equal(infoHashes[resolution])
108 }
109 }
110 } else {
111 expect(videoDetail.files).to.have.lengthOf(1)
112 expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
113 }
114 }
115 }
116 })
117
118 it('Should run a transcoding job on video 1 with resolution', async function () {
119 this.timeout(60000)
120
121 await servers[0].cliCommand.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[0]} -r 480`)
122
123 await waitJobs(servers)
124
125 for (const server of servers) {
126 const res = await getVideosList(server.url)
127 const videos = res.body.data
128 expect(videos).to.have.lengthOf(videosUUID.length)
129
130 const res2 = await getVideo(server.url, videosUUID[0])
131 const videoDetail: VideoDetails = res2.body
132
133 expect(videoDetail.files).to.have.lengthOf(2)
134 expect(videoDetail.files[0].resolution.id).to.equal(720)
135 expect(videoDetail.files[1].resolution.id).to.equal(480)
136
137 expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
138 }
139 })
140
141 it('Should generate an HLS resolution', async function () {
142 this.timeout(120000)
143
144 await servers[0].cliCommand.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
145
146 await waitJobs(servers)
147
148 for (const server of servers) {
149 const res = await getVideo(server.url, videosUUID[2])
150 const videoDetail: VideoDetails = res.body
151
152 expect(videoDetail.files).to.have.lengthOf(1)
153 expect(videoDetail.streamingPlaylists).to.have.lengthOf(1)
154
155 const files = videoDetail.streamingPlaylists[0].files
156 expect(files).to.have.lengthOf(1)
157 expect(files[0].resolution.id).to.equal(480)
158 }
159 })
160
161 it('Should not duplicate an HLS resolution', async function () {
162 this.timeout(120000)
163
164 await servers[0].cliCommand.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
165
166 await waitJobs(servers)
167
168 for (const server of servers) {
169 const res = await getVideo(server.url, videosUUID[2])
170 const videoDetail: VideoDetails = res.body
171
172 const files = videoDetail.streamingPlaylists[0].files
173 expect(files).to.have.lengthOf(1)
174 expect(files[0].resolution.id).to.equal(480)
175 }
176 })
177
178 it('Should generate all HLS resolutions', async function () {
179 this.timeout(120000)
180
181 await servers[0].cliCommand.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`)
182
183 await waitJobs(servers)
184
185 for (const server of servers) {
186 const res = await getVideo(server.url, videosUUID[3])
187 const videoDetail: VideoDetails = res.body
188
189 expect(videoDetail.files).to.have.lengthOf(1)
190 expect(videoDetail.streamingPlaylists).to.have.lengthOf(1)
191
192 const files = videoDetail.streamingPlaylists[0].files
193 expect(files).to.have.lengthOf(4)
194 }
195 })
196
197 it('Should optimize the video file and generate HLS videos if enabled in config', async function () {
198 this.timeout(120000)
199
200 config.transcoding.hls.enabled = true
201 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
202
203 await servers[0].cliCommand.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`)
204
205 await waitJobs(servers)
206
207 for (const server of servers) {
208 const res = await getVideo(server.url, videosUUID[4])
209 const videoDetail: VideoDetails = res.body
210
211 expect(videoDetail.files).to.have.lengthOf(4)
212 expect(videoDetail.streamingPlaylists).to.have.lengthOf(1)
213 expect(videoDetail.streamingPlaylists[0].files).to.have.lengthOf(4)
214 }
215 })
216
217 after(async function () {
218 await cleanupTests(servers)
219 })
220 })