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