]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/redundancy/redundancy.ts
Merge branch 'release/v1.2.0'
[github/Chocobozzz/PeerTube.git] / server / tests / api / redundancy / redundancy.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoDetails } from '../../../../shared/models/videos'
6 import {
7 doubleFollow,
8 flushAndRunMultipleServers,
9 getFollowingListPaginationAndSort,
10 getVideo,
11 immutableAssign,
12 killallServers, makeGetRequest,
13 root,
14 ServerInfo,
15 setAccessTokensToServers, unfollow,
16 uploadVideo,
17 viewVideo,
18 wait,
19 waitUntilLog,
20 checkVideoFilesWereRemoved, removeVideo, getVideoWithToken
21 } from '../../../../shared/utils'
22 import { waitJobs } from '../../../../shared/utils/server/jobs'
23
24 import * as magnetUtil from 'magnet-uri'
25 import { updateRedundancy } from '../../../../shared/utils/server/redundancy'
26 import { ActorFollow } from '../../../../shared/models/actors'
27 import { readdir } from 'fs-extra'
28 import { join } from 'path'
29 import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
30 import { getStats } from '../../../../shared/utils/server/stats'
31 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
32
33 const expect = chai.expect
34
35 let servers: ServerInfo[] = []
36 let video1Server2UUID: string
37
38 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[], server: ServerInfo) {
39 const parsed = magnetUtil.decode(file.magnetUri)
40
41 for (const ws of baseWebseeds) {
42 const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
43 expect(found, `Webseed ${ws} not found in ${file.magnetUri} on server ${server.url}`).to.not.be.undefined
44 }
45
46 expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
47 }
48
49 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
50 const config = {
51 redundancy: {
52 videos: {
53 check_interval: '5 seconds',
54 strategies: [
55 immutableAssign({
56 min_lifetime: '1 hour',
57 strategy: strategy,
58 size: '200KB'
59 }, additionalParams)
60 ]
61 }
62 }
63 }
64 servers = await flushAndRunMultipleServers(3, config)
65
66 // Get the access tokens
67 await setAccessTokensToServers(servers)
68
69 {
70 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
71 video1Server2UUID = res.body.video.uuid
72
73 await viewVideo(servers[ 1 ].url, video1Server2UUID)
74 }
75
76 await waitJobs(servers)
77
78 // Server 1 and server 2 follow each other
79 await doubleFollow(servers[ 0 ], servers[ 1 ])
80 // Server 1 and server 3 follow each other
81 await doubleFollow(servers[ 0 ], servers[ 2 ])
82 // Server 2 and server 3 follow each other
83 await doubleFollow(servers[ 1 ], servers[ 2 ])
84
85 await waitJobs(servers)
86 }
87
88 async function check1WebSeed (strategy: VideoRedundancyStrategy, videoUUID?: string) {
89 if (!videoUUID) videoUUID = video1Server2UUID
90
91 const webseeds = [
92 'http://localhost:9002/static/webseed/' + videoUUID
93 ]
94
95 for (const server of servers) {
96 {
97 // With token to avoid issues with video follow constraints
98 const res = await getVideoWithToken(server.url, server.accessToken, videoUUID)
99
100 const video: VideoDetails = res.body
101 for (const f of video.files) {
102 checkMagnetWebseeds(f, webseeds, server)
103 }
104 }
105 }
106 }
107
108 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
109 const res = await getStats(servers[0].url)
110 const data: ServerStats = res.body
111
112 expect(data.videosRedundancy).to.have.lengthOf(1)
113 const stat = data.videosRedundancy[0]
114
115 expect(stat.strategy).to.equal(strategy)
116 expect(stat.totalSize).to.equal(204800)
117 expect(stat.totalUsed).to.be.at.least(1).and.below(204801)
118 expect(stat.totalVideoFiles).to.equal(4)
119 expect(stat.totalVideos).to.equal(1)
120 }
121
122 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
123 const res = await getStats(servers[0].url)
124 const data: ServerStats = res.body
125
126 expect(data.videosRedundancy).to.have.lengthOf(1)
127
128 const stat = data.videosRedundancy[0]
129 expect(stat.strategy).to.equal(strategy)
130 expect(stat.totalSize).to.equal(204800)
131 expect(stat.totalUsed).to.equal(0)
132 expect(stat.totalVideoFiles).to.equal(0)
133 expect(stat.totalVideos).to.equal(0)
134 }
135
136 async function check2Webseeds (strategy: VideoRedundancyStrategy, videoUUID?: string) {
137 if (!videoUUID) videoUUID = video1Server2UUID
138
139 const webseeds = [
140 'http://localhost:9001/static/redundancy/' + videoUUID,
141 'http://localhost:9002/static/webseed/' + videoUUID
142 ]
143
144 for (const server of servers) {
145 const res = await getVideo(server.url, videoUUID)
146
147 const video: VideoDetails = res.body
148
149 for (const file of video.files) {
150 checkMagnetWebseeds(file, webseeds, server)
151
152 await makeGetRequest({
153 url: servers[0].url,
154 statusCodeExpected: 200,
155 path: '/static/redundancy/' + `${videoUUID}-${file.resolution.id}.mp4`,
156 contentType: null
157 })
158 await makeGetRequest({
159 url: servers[1].url,
160 statusCodeExpected: 200,
161 path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
162 contentType: null
163 })
164 }
165 }
166
167 for (const directory of [ 'test1/redundancy', 'test2/videos' ]) {
168 const files = await readdir(join(root(), directory))
169 expect(files).to.have.length.at.least(4)
170
171 for (const resolution of [ 240, 360, 480, 720 ]) {
172 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
173 }
174 }
175 }
176
177 async function enableRedundancyOnServer1 () {
178 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
179
180 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
181 const follows: ActorFollow[] = res.body.data
182 const server2 = follows.find(f => f.following.host === 'localhost:9002')
183 const server3 = follows.find(f => f.following.host === 'localhost:9003')
184
185 expect(server3).to.not.be.undefined
186 expect(server3.following.hostRedundancyAllowed).to.be.false
187
188 expect(server2).to.not.be.undefined
189 expect(server2.following.hostRedundancyAllowed).to.be.true
190 }
191
192 async function disableRedundancyOnServer1 () {
193 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
194
195 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
196 const follows: ActorFollow[] = res.body.data
197 const server2 = follows.find(f => f.following.host === 'localhost:9002')
198 const server3 = follows.find(f => f.following.host === 'localhost:9003')
199
200 expect(server3).to.not.be.undefined
201 expect(server3.following.hostRedundancyAllowed).to.be.false
202
203 expect(server2).to.not.be.undefined
204 expect(server2.following.hostRedundancyAllowed).to.be.false
205 }
206
207 async function cleanServers () {
208 killallServers(servers)
209 }
210
211 describe('Test videos redundancy', function () {
212
213 describe('With most-views strategy', function () {
214 const strategy = 'most-views'
215
216 before(function () {
217 this.timeout(120000)
218
219 return runServers(strategy)
220 })
221
222 it('Should have 1 webseed on the first video', async function () {
223 await check1WebSeed(strategy)
224 await checkStatsWith1Webseed(strategy)
225 })
226
227 it('Should enable redundancy on server 1', function () {
228 return enableRedundancyOnServer1()
229 })
230
231 it('Should have 2 webseeds on the first video', async function () {
232 this.timeout(40000)
233
234 await waitJobs(servers)
235 await waitUntilLog(servers[0], 'Duplicated ', 4)
236 await waitJobs(servers)
237
238 await check2Webseeds(strategy)
239 await checkStatsWith2Webseed(strategy)
240 })
241
242 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
243 this.timeout(40000)
244
245 await disableRedundancyOnServer1()
246
247 await waitJobs(servers)
248 await wait(5000)
249
250 await check1WebSeed(strategy)
251
252 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
253 })
254
255 after(function () {
256 return cleanServers()
257 })
258 })
259
260 describe('With trending strategy', function () {
261 const strategy = 'trending'
262
263 before(function () {
264 this.timeout(120000)
265
266 return runServers(strategy)
267 })
268
269 it('Should have 1 webseed on the first video', async function () {
270 await check1WebSeed(strategy)
271 await checkStatsWith1Webseed(strategy)
272 })
273
274 it('Should enable redundancy on server 1', function () {
275 return enableRedundancyOnServer1()
276 })
277
278 it('Should have 2 webseeds on the first video', async function () {
279 this.timeout(40000)
280
281 await waitJobs(servers)
282 await waitUntilLog(servers[0], 'Duplicated ', 4)
283 await waitJobs(servers)
284
285 await check2Webseeds(strategy)
286 await checkStatsWith2Webseed(strategy)
287 })
288
289 it('Should unfollow on server 1 and remove duplicated videos', async function () {
290 this.timeout(40000)
291
292 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
293
294 await waitJobs(servers)
295 await wait(5000)
296
297 await check1WebSeed(strategy)
298
299 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
300 })
301
302 after(function () {
303 return cleanServers()
304 })
305 })
306
307 describe('With recently added strategy', function () {
308 const strategy = 'recently-added'
309
310 before(function () {
311 this.timeout(120000)
312
313 return runServers(strategy, { min_views: 3 })
314 })
315
316 it('Should have 1 webseed on the first video', async function () {
317 await check1WebSeed(strategy)
318 await checkStatsWith1Webseed(strategy)
319 })
320
321 it('Should enable redundancy on server 1', function () {
322 return enableRedundancyOnServer1()
323 })
324
325 it('Should still have 1 webseed on the first video', async function () {
326 this.timeout(40000)
327
328 await waitJobs(servers)
329 await wait(15000)
330 await waitJobs(servers)
331
332 await check1WebSeed(strategy)
333 await checkStatsWith1Webseed(strategy)
334 })
335
336 it('Should view 2 times the first video to have > min_views config', async function () {
337 this.timeout(40000)
338
339 await viewVideo(servers[ 0 ].url, video1Server2UUID)
340 await viewVideo(servers[ 2 ].url, video1Server2UUID)
341
342 await wait(10000)
343 await waitJobs(servers)
344 })
345
346 it('Should have 2 webseeds on the first video', async function () {
347 this.timeout(40000)
348
349 await waitJobs(servers)
350 await waitUntilLog(servers[0], 'Duplicated ', 4)
351 await waitJobs(servers)
352
353 await check2Webseeds(strategy)
354 await checkStatsWith2Webseed(strategy)
355 })
356
357 it('Should remove the video and the redundancy files', async function () {
358 this.timeout(20000)
359
360 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID)
361
362 await waitJobs(servers)
363
364 for (const server of servers) {
365 await checkVideoFilesWereRemoved(video1Server2UUID, server.serverNumber)
366 }
367 })
368
369 after(function () {
370 return cleanServers()
371 })
372 })
373
374 describe('Test expiration', function () {
375 const strategy = 'recently-added'
376
377 async function checkContains (servers: ServerInfo[], str: string) {
378 for (const server of servers) {
379 const res = await getVideo(server.url, video1Server2UUID)
380 const video: VideoDetails = res.body
381
382 for (const f of video.files) {
383 expect(f.magnetUri).to.contain(str)
384 }
385 }
386 }
387
388 async function checkNotContains (servers: ServerInfo[], str: string) {
389 for (const server of servers) {
390 const res = await getVideo(server.url, video1Server2UUID)
391 const video: VideoDetails = res.body
392
393 for (const f of video.files) {
394 expect(f.magnetUri).to.not.contain(str)
395 }
396 }
397 }
398
399 before(async function () {
400 this.timeout(120000)
401
402 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
403
404 await enableRedundancyOnServer1()
405 })
406
407 it('Should still have 2 webseeds after 10 seconds', async function () {
408 this.timeout(40000)
409
410 await wait(10000)
411
412 try {
413 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
414 } catch {
415 // Maybe a server deleted a redundancy in the scheduler
416 await wait(2000)
417
418 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
419 }
420 })
421
422 it('Should stop server 1 and expire video redundancy', async function () {
423 this.timeout(40000)
424
425 killallServers([ servers[0] ])
426
427 await wait(15000)
428
429 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
430 })
431
432 after(function () {
433 return killallServers([ servers[1], servers[2] ])
434 })
435 })
436
437 describe('Test file replacement', function () {
438 let video2Server2UUID: string
439 const strategy = 'recently-added'
440
441 before(async function () {
442 this.timeout(120000)
443
444 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
445
446 await enableRedundancyOnServer1()
447
448 await waitJobs(servers)
449 await waitUntilLog(servers[0], 'Duplicated ', 4)
450 await waitJobs(servers)
451
452 await check2Webseeds(strategy)
453 await checkStatsWith2Webseed(strategy)
454
455 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
456 video2Server2UUID = res.body.video.uuid
457 })
458
459 it('Should cache video 2 webseeds on the first video', async function () {
460 this.timeout(120000)
461
462 await waitJobs(servers)
463
464 let checked = false
465
466 while (checked === false) {
467 await wait(1000)
468
469 try {
470 await check1WebSeed(strategy, video1Server2UUID)
471 await check2Webseeds(strategy, video2Server2UUID)
472
473 checked = true
474 } catch {
475 checked = false
476 }
477 }
478 })
479
480 after(function () {
481 return cleanServers()
482 })
483 })
484 })