1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { VideoDetails } from '../../../../shared/models/videos'
8 flushAndRunMultipleServers,
9 getFollowingListPaginationAndSort,
12 killallServers, makeGetRequest,
15 setAccessTokensToServers, unfollow,
20 checkVideoFilesWereRemoved, removeVideo, getVideoWithToken, reRunServer, checkSegmentHash
21 } from '../../../../shared/utils'
22 import { waitJobs } from '../../../../shared/utils/server/jobs'
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'
33 const expect = chai.expect
35 let servers: ServerInfo[] = []
36 let video1Server2UUID: string
38 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[], server: ServerInfo) {
39 const parsed = magnetUtil.decode(file.magnetUri)
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
46 expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
49 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
58 check_interval: '5 seconds',
61 min_lifetime: '1 hour',
69 servers = await flushAndRunMultipleServers(3, config)
71 // Get the access tokens
72 await setAccessTokensToServers(servers)
75 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
76 video1Server2UUID = res.body.video.uuid
78 await viewVideo(servers[ 1 ].url, video1Server2UUID)
81 await waitJobs(servers)
83 // Server 1 and server 2 follow each other
84 await doubleFollow(servers[ 0 ], servers[ 1 ])
85 // Server 1 and server 3 follow each other
86 await doubleFollow(servers[ 0 ], servers[ 2 ])
87 // Server 2 and server 3 follow each other
88 await doubleFollow(servers[ 1 ], servers[ 2 ])
90 await waitJobs(servers)
93 async function check1WebSeed (videoUUID?: string) {
94 if (!videoUUID) videoUUID = video1Server2UUID
97 'http://localhost:9002/static/webseed/' + videoUUID
100 for (const server of servers) {
101 // With token to avoid issues with video follow constraints
102 const res = await getVideoWithToken(server.url, server.accessToken, videoUUID)
104 const video: VideoDetails = res.body
105 for (const f of video.files) {
106 checkMagnetWebseeds(f, webseeds, server)
111 async function check2Webseeds (videoUUID?: string) {
112 if (!videoUUID) videoUUID = video1Server2UUID
115 'http://localhost:9001/static/redundancy/' + videoUUID,
116 'http://localhost:9002/static/webseed/' + videoUUID
119 for (const server of servers) {
120 const res = await getVideo(server.url, videoUUID)
122 const video: VideoDetails = res.body
124 for (const file of video.files) {
125 checkMagnetWebseeds(file, webseeds, server)
127 await makeGetRequest({
129 statusCodeExpected: 200,
130 path: '/static/redundancy/' + `${videoUUID}-${file.resolution.id}.mp4`,
133 await makeGetRequest({
135 statusCodeExpected: 200,
136 path: `/static/webseed/${videoUUID}-${file.resolution.id}.mp4`,
142 for (const directory of [ 'test1/redundancy', 'test2/videos' ]) {
143 const files = await readdir(join(root(), directory))
144 expect(files).to.have.length.at.least(4)
146 for (const resolution of [ 240, 360, 480, 720 ]) {
147 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
152 async function check0PlaylistRedundancies (videoUUID?: string) {
153 if (!videoUUID) videoUUID = video1Server2UUID
155 for (const server of servers) {
156 // With token to avoid issues with video follow constraints
157 const res = await getVideoWithToken(server.url, server.accessToken, videoUUID)
158 const video: VideoDetails = res.body
160 expect(video.streamingPlaylists).to.be.an('array')
161 expect(video.streamingPlaylists).to.have.lengthOf(1)
162 expect(video.streamingPlaylists[0].redundancies).to.have.lengthOf(0)
166 async function check1PlaylistRedundancies (videoUUID?: string) {
167 if (!videoUUID) videoUUID = video1Server2UUID
169 for (const server of servers) {
170 const res = await getVideo(server.url, videoUUID)
171 const video: VideoDetails = res.body
173 expect(video.streamingPlaylists).to.have.lengthOf(1)
174 expect(video.streamingPlaylists[0].redundancies).to.have.lengthOf(1)
176 const redundancy = video.streamingPlaylists[0].redundancies[0]
178 expect(redundancy.baseUrl).to.equal(servers[0].url + '/static/redundancy/hls/' + videoUUID)
181 const baseUrlPlaylist = servers[1].url + '/static/playlists/hls'
182 const baseUrlSegment = servers[0].url + '/static/redundancy/hls'
184 const res = await getVideo(servers[0].url, videoUUID)
185 const hlsPlaylist = (res.body as VideoDetails).streamingPlaylists[0]
187 for (const resolution of [ 240, 360, 480, 720 ]) {
188 await checkSegmentHash(baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist)
191 for (const directory of [ 'test1/redundancy/hls', 'test2/playlists/hls' ]) {
192 const files = await readdir(join(root(), directory, videoUUID))
193 expect(files).to.have.length.at.least(4)
195 for (const resolution of [ 240, 360, 480, 720 ]) {
196 const filename = `${videoUUID}-${resolution}-fragmented.mp4`
198 expect(files.find(f => f === filename)).to.not.be.undefined
203 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
204 const res = await getStats(servers[0].url)
205 const data: ServerStats = res.body
207 expect(data.videosRedundancy).to.have.lengthOf(1)
208 const stat = data.videosRedundancy[0]
210 expect(stat.strategy).to.equal(strategy)
211 expect(stat.totalSize).to.equal(204800)
212 expect(stat.totalUsed).to.be.at.least(1).and.below(204801)
213 expect(stat.totalVideoFiles).to.equal(4)
214 expect(stat.totalVideos).to.equal(1)
217 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
218 const res = await getStats(servers[0].url)
219 const data: ServerStats = res.body
221 expect(data.videosRedundancy).to.have.lengthOf(1)
223 const stat = data.videosRedundancy[0]
224 expect(stat.strategy).to.equal(strategy)
225 expect(stat.totalSize).to.equal(204800)
226 expect(stat.totalUsed).to.equal(0)
227 expect(stat.totalVideoFiles).to.equal(0)
228 expect(stat.totalVideos).to.equal(0)
231 async function enableRedundancyOnServer1 () {
232 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
234 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
235 const follows: ActorFollow[] = res.body.data
236 const server2 = follows.find(f => f.following.host === 'localhost:9002')
237 const server3 = follows.find(f => f.following.host === 'localhost:9003')
239 expect(server3).to.not.be.undefined
240 expect(server3.following.hostRedundancyAllowed).to.be.false
242 expect(server2).to.not.be.undefined
243 expect(server2.following.hostRedundancyAllowed).to.be.true
246 async function disableRedundancyOnServer1 () {
247 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
249 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
250 const follows: ActorFollow[] = res.body.data
251 const server2 = follows.find(f => f.following.host === 'localhost:9002')
252 const server3 = follows.find(f => f.following.host === 'localhost:9003')
254 expect(server3).to.not.be.undefined
255 expect(server3.following.hostRedundancyAllowed).to.be.false
257 expect(server2).to.not.be.undefined
258 expect(server2.following.hostRedundancyAllowed).to.be.false
261 async function cleanServers () {
262 killallServers(servers)
265 describe('Test videos redundancy', function () {
267 describe('With most-views strategy', function () {
268 const strategy = 'most-views'
273 return runServers(strategy)
276 it('Should have 1 webseed on the first video', async function () {
277 await check1WebSeed()
278 await check0PlaylistRedundancies()
279 await checkStatsWith1Webseed(strategy)
282 it('Should enable redundancy on server 1', function () {
283 return enableRedundancyOnServer1()
286 it('Should have 2 webseeds on the first video', async function () {
289 await waitJobs(servers)
290 await waitUntilLog(servers[0], 'Duplicated ', 5)
291 await waitJobs(servers)
293 await check2Webseeds()
294 await check1PlaylistRedundancies()
295 await checkStatsWith2Webseed(strategy)
298 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
301 await disableRedundancyOnServer1()
303 await waitJobs(servers)
306 await check1WebSeed()
307 await check0PlaylistRedundancies()
309 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos', join('playlists', 'hls') ])
313 return cleanServers()
317 describe('With trending strategy', function () {
318 const strategy = 'trending'
323 return runServers(strategy)
326 it('Should have 1 webseed on the first video', async function () {
327 await check1WebSeed()
328 await check0PlaylistRedundancies()
329 await checkStatsWith1Webseed(strategy)
332 it('Should enable redundancy on server 1', function () {
333 return enableRedundancyOnServer1()
336 it('Should have 2 webseeds on the first video', async function () {
339 await waitJobs(servers)
340 await waitUntilLog(servers[0], 'Duplicated ', 5)
341 await waitJobs(servers)
343 await check2Webseeds()
344 await check1PlaylistRedundancies()
345 await checkStatsWith2Webseed(strategy)
348 it('Should unfollow on server 1 and remove duplicated videos', async function () {
351 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
353 await waitJobs(servers)
356 await check1WebSeed()
357 await check0PlaylistRedundancies()
359 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
363 return cleanServers()
367 describe('With recently added strategy', function () {
368 const strategy = 'recently-added'
373 return runServers(strategy, { min_views: 3 })
376 it('Should have 1 webseed on the first video', async function () {
377 await check1WebSeed()
378 await check0PlaylistRedundancies()
379 await checkStatsWith1Webseed(strategy)
382 it('Should enable redundancy on server 1', function () {
383 return enableRedundancyOnServer1()
386 it('Should still have 1 webseed on the first video', async function () {
389 await waitJobs(servers)
391 await waitJobs(servers)
393 await check1WebSeed()
394 await check0PlaylistRedundancies()
395 await checkStatsWith1Webseed(strategy)
398 it('Should view 2 times the first video to have > min_views config', async function () {
401 await viewVideo(servers[ 0 ].url, video1Server2UUID)
402 await viewVideo(servers[ 2 ].url, video1Server2UUID)
405 await waitJobs(servers)
408 it('Should have 2 webseeds on the first video', async function () {
411 await waitJobs(servers)
412 await waitUntilLog(servers[0], 'Duplicated ', 5)
413 await waitJobs(servers)
415 await check2Webseeds()
416 await check1PlaylistRedundancies()
417 await checkStatsWith2Webseed(strategy)
420 it('Should remove the video and the redundancy files', async function () {
423 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID)
425 await waitJobs(servers)
427 for (const server of servers) {
428 await checkVideoFilesWereRemoved(video1Server2UUID, server.serverNumber)
433 return cleanServers()
437 describe('Test expiration', function () {
438 const strategy = 'recently-added'
440 async function checkContains (servers: ServerInfo[], str: string) {
441 for (const server of servers) {
442 const res = await getVideo(server.url, video1Server2UUID)
443 const video: VideoDetails = res.body
445 for (const f of video.files) {
446 expect(f.magnetUri).to.contain(str)
451 async function checkNotContains (servers: ServerInfo[], str: string) {
452 for (const server of servers) {
453 const res = await getVideo(server.url, video1Server2UUID)
454 const video: VideoDetails = res.body
456 for (const f of video.files) {
457 expect(f.magnetUri).to.not.contain(str)
462 before(async function () {
465 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
467 await enableRedundancyOnServer1()
470 it('Should still have 2 webseeds after 10 seconds', async function () {
476 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
478 // Maybe a server deleted a redundancy in the scheduler
481 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
485 it('Should stop server 1 and expire video redundancy', async function () {
488 killallServers([ servers[0] ])
492 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
496 return killallServers([ servers[1], servers[2] ])
500 describe('Test file replacement', function () {
501 let video2Server2UUID: string
502 const strategy = 'recently-added'
504 before(async function () {
507 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
509 await enableRedundancyOnServer1()
511 await waitJobs(servers)
512 await waitUntilLog(servers[0], 'Duplicated ', 5)
513 await waitJobs(servers)
515 await check2Webseeds()
516 await check1PlaylistRedundancies()
517 await checkStatsWith2Webseed(strategy)
519 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
520 video2Server2UUID = res.body.video.uuid
523 it('Should cache video 2 webseeds on the first video', async function () {
526 await waitJobs(servers)
530 while (checked === false) {
534 await check1WebSeed(video1Server2UUID)
535 await check0PlaylistRedundancies(video1Server2UUID)
536 await check2Webseeds(video2Server2UUID)
537 await check1PlaylistRedundancies(video2Server2UUID)
546 it('Should disable strategy and remove redundancies', async function () {
549 await waitJobs(servers)
551 killallServers([ servers[ 0 ] ])
552 await reRunServer(servers[ 0 ], {
555 check_interval: '1 second',
561 await waitJobs(servers)
563 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ join('redundancy', 'hls') ])
567 return cleanServers()