Merge branch 'develop' of https://github.com/Chocobozzz/PeerTube into move-utils...
[oweals/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/webseed/' + 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       // Only servers 1 and 2 have the video
153       if (server.serverNumber !== 3) {
154         await makeGetRequest({
155           url: server.url,
156           statusCodeExpected: 200,
157           path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
158           contentType: null
159         })
160       }
161     }
162   }
163
164   for (const directory of [ 'test1', 'test2' ]) {
165     const files = await readdir(join(root(), directory, 'videos'))
166     expect(files).to.have.length.at.least(4)
167
168     for (const resolution of [ 240, 360, 480, 720 ]) {
169       expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
170     }
171   }
172 }
173
174 async function enableRedundancyOnServer1 () {
175   await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
176
177   const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
178   const follows: ActorFollow[] = res.body.data
179   const server2 = follows.find(f => f.following.host === 'localhost:9002')
180   const server3 = follows.find(f => f.following.host === 'localhost:9003')
181
182   expect(server3).to.not.be.undefined
183   expect(server3.following.hostRedundancyAllowed).to.be.false
184
185   expect(server2).to.not.be.undefined
186   expect(server2.following.hostRedundancyAllowed).to.be.true
187 }
188
189 async function disableRedundancyOnServer1 () {
190   await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
191
192   const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
193   const follows: ActorFollow[] = res.body.data
194   const server2 = follows.find(f => f.following.host === 'localhost:9002')
195   const server3 = follows.find(f => f.following.host === 'localhost:9003')
196
197   expect(server3).to.not.be.undefined
198   expect(server3.following.hostRedundancyAllowed).to.be.false
199
200   expect(server2).to.not.be.undefined
201   expect(server2.following.hostRedundancyAllowed).to.be.false
202 }
203
204 async function cleanServers () {
205   killallServers(servers)
206 }
207
208 describe('Test videos redundancy', function () {
209
210   describe('With most-views strategy', function () {
211     const strategy = 'most-views'
212
213     before(function () {
214       this.timeout(120000)
215
216       return runServers(strategy)
217     })
218
219     it('Should have 1 webseed on the first video', async function () {
220       await check1WebSeed(strategy)
221       await checkStatsWith1Webseed(strategy)
222     })
223
224     it('Should enable redundancy on server 1', function () {
225       return enableRedundancyOnServer1()
226     })
227
228     it('Should have 2 webseeds on the first video', async function () {
229       this.timeout(40000)
230
231       await waitJobs(servers)
232       await waitUntilLog(servers[0], 'Duplicated ', 4)
233       await waitJobs(servers)
234
235       await check2Webseeds(strategy)
236       await checkStatsWith2Webseed(strategy)
237     })
238
239     it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
240       this.timeout(40000)
241
242       await disableRedundancyOnServer1()
243
244       await waitJobs(servers)
245       await wait(5000)
246
247       await check1WebSeed(strategy)
248
249       await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
250     })
251
252     after(function () {
253       return cleanServers()
254     })
255   })
256
257   describe('With trending strategy', function () {
258     const strategy = 'trending'
259
260     before(function () {
261       this.timeout(120000)
262
263       return runServers(strategy)
264     })
265
266     it('Should have 1 webseed on the first video', async function () {
267       await check1WebSeed(strategy)
268       await checkStatsWith1Webseed(strategy)
269     })
270
271     it('Should enable redundancy on server 1', function () {
272       return enableRedundancyOnServer1()
273     })
274
275     it('Should have 2 webseeds on the first video', async function () {
276       this.timeout(40000)
277
278       await waitJobs(servers)
279       await waitUntilLog(servers[0], 'Duplicated ', 4)
280       await waitJobs(servers)
281
282       await check2Webseeds(strategy)
283       await checkStatsWith2Webseed(strategy)
284     })
285
286     it('Should unfollow on server 1 and remove duplicated videos', async function () {
287       this.timeout(40000)
288
289       await unfollow(servers[0].url, servers[0].accessToken, servers[1])
290
291       await waitJobs(servers)
292       await wait(5000)
293
294       await check1WebSeed(strategy)
295
296       await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
297     })
298
299     after(function () {
300       return cleanServers()
301     })
302   })
303
304   describe('With recently added strategy', function () {
305     const strategy = 'recently-added'
306
307     before(function () {
308       this.timeout(120000)
309
310       return runServers(strategy, { min_views: 3 })
311     })
312
313     it('Should have 1 webseed on the first video', async function () {
314       await check1WebSeed(strategy)
315       await checkStatsWith1Webseed(strategy)
316     })
317
318     it('Should enable redundancy on server 1', function () {
319       return enableRedundancyOnServer1()
320     })
321
322     it('Should still have 1 webseed on the first video', async function () {
323       this.timeout(40000)
324
325       await waitJobs(servers)
326       await wait(15000)
327       await waitJobs(servers)
328
329       await check1WebSeed(strategy)
330       await checkStatsWith1Webseed(strategy)
331     })
332
333     it('Should view 2 times the first video to have > min_views config', async function () {
334       this.timeout(40000)
335
336       await viewVideo(servers[ 0 ].url, video1Server2UUID)
337       await viewVideo(servers[ 2 ].url, video1Server2UUID)
338
339       await wait(10000)
340       await waitJobs(servers)
341     })
342
343     it('Should have 2 webseeds on the first video', async function () {
344       this.timeout(40000)
345
346       await waitJobs(servers)
347       await waitUntilLog(servers[0], 'Duplicated ', 4)
348       await waitJobs(servers)
349
350       await check2Webseeds(strategy)
351       await checkStatsWith2Webseed(strategy)
352     })
353
354     it('Should remove the video and the redundancy files', async function () {
355       this.timeout(20000)
356
357       await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID)
358
359       await waitJobs(servers)
360
361       for (const server of servers) {
362         await checkVideoFilesWereRemoved(video1Server2UUID, server.serverNumber)
363       }
364     })
365
366     after(function () {
367       return cleanServers()
368     })
369   })
370
371   describe('Test expiration', function () {
372     const strategy = 'recently-added'
373
374     async function checkContains (servers: ServerInfo[], str: string) {
375       for (const server of servers) {
376         const res = await getVideo(server.url, video1Server2UUID)
377         const video: VideoDetails = res.body
378
379         for (const f of video.files) {
380           expect(f.magnetUri).to.contain(str)
381         }
382       }
383     }
384
385     async function checkNotContains (servers: ServerInfo[], str: string) {
386       for (const server of servers) {
387         const res = await getVideo(server.url, video1Server2UUID)
388         const video: VideoDetails = res.body
389
390         for (const f of video.files) {
391           expect(f.magnetUri).to.not.contain(str)
392         }
393       }
394     }
395
396     before(async function () {
397       this.timeout(120000)
398
399       await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
400
401       await enableRedundancyOnServer1()
402     })
403
404     it('Should still have 2 webseeds after 10 seconds', async function () {
405       this.timeout(40000)
406
407       await wait(10000)
408
409       try {
410         await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
411       } catch {
412         // Maybe a server deleted a redundancy in the scheduler
413         await wait(2000)
414
415         await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
416       }
417     })
418
419     it('Should stop server 1 and expire video redundancy', async function () {
420       this.timeout(40000)
421
422       killallServers([ servers[0] ])
423
424       await wait(15000)
425
426       await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
427     })
428
429     after(function () {
430       return killallServers([ servers[1], servers[2] ])
431     })
432   })
433
434   describe('Test file replacement', function () {
435     let video2Server2UUID: string
436     const strategy = 'recently-added'
437
438     before(async function () {
439       this.timeout(120000)
440
441       await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
442
443       await enableRedundancyOnServer1()
444
445       await waitJobs(servers)
446       await waitUntilLog(servers[0], 'Duplicated ', 4)
447       await waitJobs(servers)
448
449       await check2Webseeds(strategy)
450       await checkStatsWith2Webseed(strategy)
451
452       const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
453       video2Server2UUID = res.body.video.uuid
454     })
455
456     it('Should cache video 2 webseeds on the first video', async function () {
457       this.timeout(120000)
458
459       await waitJobs(servers)
460
461       let checked = false
462
463       while (checked === false) {
464         await wait(1000)
465
466         try {
467           await check1WebSeed(strategy, video1Server2UUID)
468           await check2Webseeds(strategy, video2Server2UUID)
469
470           checked = true
471         } catch {
472           checked = false
473         }
474       }
475     })
476
477     after(function () {
478       return cleanServers()
479     })
480   })
481 })