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