]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live-save-replay.ts
Fix tests timeout
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live-save-replay.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 { FfmpegCommand } from 'fluent-ffmpeg'
6 import { LiveVideoCreate, VideoDetails, VideoPrivacy, VideoState } from '@shared/models'
7 import {
8 addVideoToBlacklist,
9 checkLiveCleanup,
10 cleanupTests,
11 createLive,
12 doubleFollow,
13 flushAndRunMultipleServers,
14 getVideo,
15 getVideosList,
16 removeVideo,
17 sendRTMPStreamInVideo,
18 ServerInfo,
19 setAccessTokensToServers,
20 setDefaultVideoChannel,
21 stopFfmpeg,
22 testFfmpegStreamError,
23 updateCustomSubConfig,
24 updateVideo,
25 waitJobs,
26 waitUntilLivePublished
27 } from '../../../../shared/extra-utils'
28 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
29
30 const expect = chai.expect
31
32 describe('Save replay setting', function () {
33 let servers: ServerInfo[] = []
34 let liveVideoUUID: string
35 let ffmpegCommand: FfmpegCommand
36
37 async function createLiveWrapper (saveReplay: boolean) {
38 if (liveVideoUUID) {
39 try {
40 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
41 await waitJobs(servers)
42 } catch {}
43 }
44
45 const attributes: LiveVideoCreate = {
46 channelId: servers[0].videoChannel.id,
47 privacy: VideoPrivacy.PUBLIC,
48 name: 'my super live',
49 saveReplay
50 }
51
52 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
53 return res.body.video.uuid
54 }
55
56 async function checkVideosExist (videoId: string, existsInList: boolean, getStatus?: number) {
57 for (const server of servers) {
58 const length = existsInList ? 1 : 0
59
60 const resVideos = await getVideosList(server.url)
61 expect(resVideos.body.data).to.have.lengthOf(length)
62 expect(resVideos.body.total).to.equal(length)
63
64 if (getStatus) {
65 await getVideo(server.url, videoId, getStatus)
66 }
67 }
68 }
69
70 async function checkVideoState (videoId: string, state: VideoState) {
71 for (const server of servers) {
72 const res = await getVideo(server.url, videoId)
73 expect((res.body as VideoDetails).state.id).to.equal(state)
74 }
75 }
76
77 before(async function () {
78 this.timeout(120000)
79
80 servers = await flushAndRunMultipleServers(2)
81
82 // Get the access tokens
83 await setAccessTokensToServers(servers)
84 await setDefaultVideoChannel(servers)
85
86 // Server 1 and server 2 follow each other
87 await doubleFollow(servers[0], servers[1])
88
89 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
90 live: {
91 enabled: true,
92 allowReplay: true,
93 maxDuration: null,
94 transcoding: {
95 enabled: false,
96 resolutions: {
97 '240p': true,
98 '360p': true,
99 '480p': true,
100 '720p': true,
101 '1080p': true,
102 '2160p': true
103 }
104 }
105 }
106 })
107 })
108
109 describe('With save replay disabled', function () {
110
111 before(async function () {
112 this.timeout(10000)
113 })
114
115 it('Should correctly create and federate the "waiting for stream" live', async function () {
116 this.timeout(20000)
117
118 liveVideoUUID = await createLiveWrapper(false)
119
120 await waitJobs(servers)
121
122 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
123 await checkVideoState(liveVideoUUID, VideoState.WAITING_FOR_LIVE)
124 })
125
126 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
127 this.timeout(20000)
128
129 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
130 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
131
132 await waitJobs(servers)
133
134 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
135 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
136 })
137
138 it('Should correctly delete the video files after the stream ended', async function () {
139 this.timeout(40000)
140
141 await stopFfmpeg(ffmpegCommand)
142
143 await waitJobs(servers)
144
145 // Live still exist, but cannot be played anymore
146 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
147 await checkVideoState(liveVideoUUID, VideoState.LIVE_ENDED)
148
149 await waitJobs(servers)
150
151 // No resolutions saved since we did not save replay
152 await checkLiveCleanup(servers[0], liveVideoUUID, [])
153 })
154
155 it('Should correctly terminate the stream on blacklist and delete the live', async function () {
156 this.timeout(40000)
157
158 liveVideoUUID = await createLiveWrapper(false)
159
160 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
161 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
162
163 await waitJobs(servers)
164 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
165
166 await Promise.all([
167 addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true),
168 testFfmpegStreamError(ffmpegCommand, true)
169 ])
170
171 await waitJobs(servers)
172
173 await checkVideosExist(liveVideoUUID, false)
174
175 await getVideo(servers[0].url, liveVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
176 await getVideo(servers[1].url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
177
178 await checkLiveCleanup(servers[0], liveVideoUUID, [])
179 })
180
181 it('Should correctly terminate the stream on delete and delete the video', async function () {
182 this.timeout(40000)
183
184 liveVideoUUID = await createLiveWrapper(false)
185
186 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
187 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
188
189 await waitJobs(servers)
190 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
191
192 await Promise.all([
193 testFfmpegStreamError(ffmpegCommand, true),
194 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
195 ])
196
197 await waitJobs(servers)
198
199 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404)
200 await checkLiveCleanup(servers[0], liveVideoUUID, [])
201 })
202 })
203
204 describe('With save replay enabled', function () {
205
206 it('Should correctly create and federate the "waiting for stream" live', async function () {
207 this.timeout(20000)
208
209 liveVideoUUID = await createLiveWrapper(true)
210
211 await waitJobs(servers)
212
213 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
214 await checkVideoState(liveVideoUUID, VideoState.WAITING_FOR_LIVE)
215 })
216
217 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
218 this.timeout(20000)
219
220 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
221 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
222
223 await waitJobs(servers)
224
225 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
226 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
227 })
228
229 it('Should correctly have saved the live and federated it after the streaming', async function () {
230 this.timeout(30000)
231
232 await stopFfmpeg(ffmpegCommand)
233
234 await waitJobs(servers)
235
236 // Live has been transcoded
237 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
238 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
239 })
240
241 it('Should update the saved live and correctly federate the updated attributes', async function () {
242 this.timeout(30000)
243
244 await updateVideo(servers[0].url, servers[0].accessToken, liveVideoUUID, { name: 'video updated' })
245 await waitJobs(servers)
246
247 for (const server of servers) {
248 const res = await getVideo(server.url, liveVideoUUID)
249 expect(res.body.name).to.equal('video updated')
250 expect(res.body.isLive).to.be.false
251 }
252 })
253
254 it('Should have cleaned up the live files', async function () {
255 await checkLiveCleanup(servers[0], liveVideoUUID, [ 720 ])
256 })
257
258 it('Should correctly terminate the stream on blacklist and blacklist the saved replay video', async function () {
259 this.timeout(40000)
260
261 liveVideoUUID = await createLiveWrapper(true)
262
263 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
264 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
265
266 await waitJobs(servers)
267 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
268
269 await Promise.all([
270 addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true),
271 testFfmpegStreamError(ffmpegCommand, true)
272 ])
273
274 await waitJobs(servers)
275
276 await checkVideosExist(liveVideoUUID, false)
277
278 await getVideo(servers[0].url, liveVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
279 await getVideo(servers[1].url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
280
281 await checkLiveCleanup(servers[0], liveVideoUUID, [ 720 ])
282 })
283
284 it('Should correctly terminate the stream on delete and delete the video', async function () {
285 this.timeout(40000)
286
287 liveVideoUUID = await createLiveWrapper(true)
288
289 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
290 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
291
292 await waitJobs(servers)
293 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
294
295 await Promise.all([
296 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID),
297 testFfmpegStreamError(ffmpegCommand, true)
298 ])
299
300 await waitJobs(servers)
301
302 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404)
303 await checkLiveCleanup(servers[0], liveVideoUUID, [])
304 })
305 })
306
307 after(async function () {
308 await cleanupTests(servers)
309 })
310 })