1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { VideoDetails } from '../../../../shared/models/videos'
8 checkVideoFilesWereRemoved, cleanupTests,
10 flushAndRunMultipleServers,
11 getFollowingListPaginationAndSort,
21 setAccessTokensToServers,
27 } from '../../../../shared/extra-utils'
28 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
30 import * as magnetUtil from 'magnet-uri'
31 import { updateRedundancy } from '../../../../shared/extra-utils/server/redundancy'
32 import { ActorFollow } from '../../../../shared/models/actors'
33 import { readdir } from 'fs-extra'
34 import { join } from 'path'
35 import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
36 import { getStats } from '../../../../shared/extra-utils/server/stats'
37 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
39 const expect = chai.expect
41 let servers: ServerInfo[] = []
42 let video1Server2UUID: string
44 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[], server: ServerInfo) {
45 const parsed = magnetUtil.decode(file.magnetUri)
47 for (const ws of baseWebseeds) {
48 const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
49 expect(found, `Webseed ${ws} not found in ${file.magnetUri} on server ${server.url}`).to.not.be.undefined
52 expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
55 async function flushAndRunServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
64 check_interval: '5 seconds',
67 min_lifetime: '1 hour',
75 servers = await flushAndRunMultipleServers(3, config)
77 // Get the access tokens
78 await setAccessTokensToServers(servers)
81 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
82 video1Server2UUID = res.body.video.uuid
84 await viewVideo(servers[ 1 ].url, video1Server2UUID)
87 await waitJobs(servers)
89 // Server 1 and server 2 follow each other
90 await doubleFollow(servers[ 0 ], servers[ 1 ])
91 // Server 1 and server 3 follow each other
92 await doubleFollow(servers[ 0 ], servers[ 2 ])
93 // Server 2 and server 3 follow each other
94 await doubleFollow(servers[ 1 ], servers[ 2 ])
96 await waitJobs(servers)
99 async function check1WebSeed (videoUUID?: string) {
100 if (!videoUUID) videoUUID = video1Server2UUID
103 `http://localhost:${servers[ 1 ].port}/static/webseed/${videoUUID}`
106 for (const server of servers) {
107 // With token to avoid issues with video follow constraints
108 const res = await getVideoWithToken(server.url, server.accessToken, videoUUID)
110 const video: VideoDetails = res.body
111 for (const f of video.files) {
112 checkMagnetWebseeds(f, webseeds, server)
117 async function check2Webseeds (videoUUID?: string) {
118 if (!videoUUID) videoUUID = video1Server2UUID
121 `http://localhost:${servers[ 0 ].port}/static/redundancy/${videoUUID}`,
122 `http://localhost:${servers[ 1 ].port}/static/webseed/${videoUUID}`
125 for (const server of servers) {
126 const res = await getVideo(server.url, videoUUID)
128 const video: VideoDetails = res.body
130 for (const file of video.files) {
131 checkMagnetWebseeds(file, webseeds, server)
133 await makeGetRequest({
135 statusCodeExpected: 200,
136 path: '/static/redundancy/' + `${videoUUID}-${file.resolution.id}.mp4`,
139 await makeGetRequest({
141 statusCodeExpected: 200,
142 path: `/static/webseed/${videoUUID}-${file.resolution.id}.mp4`,
148 const directories = [
149 'test' + servers[0].internalServerNumber + '/redundancy',
150 'test' + servers[1].internalServerNumber + '/videos'
153 for (const directory of directories) {
154 const files = await readdir(join(root(), directory))
155 expect(files).to.have.length.at.least(4)
157 for (const resolution of [ 240, 360, 480, 720 ]) {
158 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
163 async function check0PlaylistRedundancies (videoUUID?: string) {
164 if (!videoUUID) videoUUID = video1Server2UUID
166 for (const server of servers) {
167 // With token to avoid issues with video follow constraints
168 const res = await getVideoWithToken(server.url, server.accessToken, videoUUID)
169 const video: VideoDetails = res.body
171 expect(video.streamingPlaylists).to.be.an('array')
172 expect(video.streamingPlaylists).to.have.lengthOf(1)
173 expect(video.streamingPlaylists[0].redundancies).to.have.lengthOf(0)
177 async function check1PlaylistRedundancies (videoUUID?: string) {
178 if (!videoUUID) videoUUID = video1Server2UUID
180 for (const server of servers) {
181 const res = await getVideo(server.url, videoUUID)
182 const video: VideoDetails = res.body
184 expect(video.streamingPlaylists).to.have.lengthOf(1)
185 expect(video.streamingPlaylists[0].redundancies).to.have.lengthOf(1)
187 const redundancy = video.streamingPlaylists[0].redundancies[0]
189 expect(redundancy.baseUrl).to.equal(servers[0].url + '/static/redundancy/hls/' + videoUUID)
192 const baseUrlPlaylist = servers[1].url + '/static/streaming-playlists/hls'
193 const baseUrlSegment = servers[0].url + '/static/redundancy/hls'
195 const res = await getVideo(servers[0].url, videoUUID)
196 const hlsPlaylist = (res.body as VideoDetails).streamingPlaylists[0]
198 for (const resolution of [ 240, 360, 480, 720 ]) {
199 await checkSegmentHash(baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist)
202 const directories = [
203 'test' + servers[0].internalServerNumber + '/redundancy/hls',
204 'test' + servers[1].internalServerNumber + '/streaming-playlists/hls'
207 for (const directory of directories) {
208 const files = await readdir(join(root(), directory, videoUUID))
209 expect(files).to.have.length.at.least(4)
211 for (const resolution of [ 240, 360, 480, 720 ]) {
212 const filename = `${videoUUID}-${resolution}-fragmented.mp4`
214 expect(files.find(f => f === filename)).to.not.be.undefined
219 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
220 const res = await getStats(servers[0].url)
221 const data: ServerStats = res.body
223 expect(data.videosRedundancy).to.have.lengthOf(1)
224 const stat = data.videosRedundancy[0]
226 expect(stat.strategy).to.equal(strategy)
227 expect(stat.totalSize).to.equal(409600)
228 expect(stat.totalUsed).to.be.at.least(1).and.below(409601)
229 expect(stat.totalVideoFiles).to.equal(4)
230 expect(stat.totalVideos).to.equal(1)
233 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
234 const res = await getStats(servers[0].url)
235 const data: ServerStats = res.body
237 expect(data.videosRedundancy).to.have.lengthOf(1)
239 const stat = data.videosRedundancy[0]
240 expect(stat.strategy).to.equal(strategy)
241 expect(stat.totalSize).to.equal(409600)
242 expect(stat.totalUsed).to.equal(0)
243 expect(stat.totalVideoFiles).to.equal(0)
244 expect(stat.totalVideos).to.equal(0)
247 async function enableRedundancyOnServer1 () {
248 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
250 const res = await getFollowingListPaginationAndSort({ url: servers[ 0 ].url, start: 0, count: 5, sort: '-createdAt' })
251 const follows: ActorFollow[] = res.body.data
252 const server2 = follows.find(f => f.following.host === `localhost:${servers[ 1 ].port}`)
253 const server3 = follows.find(f => f.following.host === `localhost:${servers[ 2 ].port}`)
255 expect(server3).to.not.be.undefined
256 expect(server3.following.hostRedundancyAllowed).to.be.false
258 expect(server2).to.not.be.undefined
259 expect(server2.following.hostRedundancyAllowed).to.be.true
262 async function disableRedundancyOnServer1 () {
263 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
265 const res = await getFollowingListPaginationAndSort({ url: servers[ 0 ].url, start: 0, count: 5, sort: '-createdAt' })
266 const follows: ActorFollow[] = res.body.data
267 const server2 = follows.find(f => f.following.host === `localhost:${servers[ 1 ].port}`)
268 const server3 = follows.find(f => f.following.host === `localhost:${servers[ 2 ].port}`)
270 expect(server3).to.not.be.undefined
271 expect(server3.following.hostRedundancyAllowed).to.be.false
273 expect(server2).to.not.be.undefined
274 expect(server2.following.hostRedundancyAllowed).to.be.false
277 describe('Test videos redundancy', function () {
279 describe('With most-views strategy', function () {
280 const strategy = 'most-views'
285 return flushAndRunServers(strategy)
288 it('Should have 1 webseed on the first video', async function () {
289 await check1WebSeed()
290 await check0PlaylistRedundancies()
291 await checkStatsWith1Webseed(strategy)
294 it('Should enable redundancy on server 1', function () {
295 return enableRedundancyOnServer1()
298 it('Should have 2 webseeds on the first video', async function () {
301 await waitJobs(servers)
302 await waitUntilLog(servers[0], 'Duplicated ', 5)
303 await waitJobs(servers)
305 await check2Webseeds()
306 await check1PlaylistRedundancies()
307 await checkStatsWith2Webseed(strategy)
310 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
313 await disableRedundancyOnServer1()
315 await waitJobs(servers)
318 await check1WebSeed()
319 await check0PlaylistRedundancies()
321 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].internalServerNumber, [ 'videos', join('playlists', 'hls') ])
324 after(async function () {
325 return cleanupTests(servers)
329 describe('With trending strategy', function () {
330 const strategy = 'trending'
335 return flushAndRunServers(strategy)
338 it('Should have 1 webseed on the first video', async function () {
339 await check1WebSeed()
340 await check0PlaylistRedundancies()
341 await checkStatsWith1Webseed(strategy)
344 it('Should enable redundancy on server 1', function () {
345 return enableRedundancyOnServer1()
348 it('Should have 2 webseeds on the first video', async function () {
351 await waitJobs(servers)
352 await waitUntilLog(servers[0], 'Duplicated ', 5)
353 await waitJobs(servers)
355 await check2Webseeds()
356 await check1PlaylistRedundancies()
357 await checkStatsWith2Webseed(strategy)
360 it('Should unfollow on server 1 and remove duplicated videos', async function () {
363 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
365 await waitJobs(servers)
368 await check1WebSeed()
369 await check0PlaylistRedundancies()
371 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].internalServerNumber, [ 'videos' ])
374 after(async function () {
375 await cleanupTests(servers)
379 describe('With recently added strategy', function () {
380 const strategy = 'recently-added'
385 return flushAndRunServers(strategy, { min_views: 3 })
388 it('Should have 1 webseed on the first video', async function () {
389 await check1WebSeed()
390 await check0PlaylistRedundancies()
391 await checkStatsWith1Webseed(strategy)
394 it('Should enable redundancy on server 1', function () {
395 return enableRedundancyOnServer1()
398 it('Should still have 1 webseed on the first video', async function () {
401 await waitJobs(servers)
403 await waitJobs(servers)
405 await check1WebSeed()
406 await check0PlaylistRedundancies()
407 await checkStatsWith1Webseed(strategy)
410 it('Should view 2 times the first video to have > min_views config', async function () {
413 await viewVideo(servers[ 0 ].url, video1Server2UUID)
414 await viewVideo(servers[ 2 ].url, video1Server2UUID)
417 await waitJobs(servers)
420 it('Should have 2 webseeds on the first video', async function () {
423 await waitJobs(servers)
424 await waitUntilLog(servers[0], 'Duplicated ', 5)
425 await waitJobs(servers)
427 await check2Webseeds()
428 await check1PlaylistRedundancies()
429 await checkStatsWith2Webseed(strategy)
432 it('Should remove the video and the redundancy files', async function () {
435 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID)
437 await waitJobs(servers)
439 for (const server of servers) {
440 await checkVideoFilesWereRemoved(video1Server2UUID, server.internalServerNumber)
444 after(async function () {
445 await cleanupTests(servers)
449 describe('Test expiration', function () {
450 const strategy = 'recently-added'
452 async function checkContains (servers: ServerInfo[], str: string) {
453 for (const server of servers) {
454 const res = await getVideo(server.url, video1Server2UUID)
455 const video: VideoDetails = res.body
457 for (const f of video.files) {
458 expect(f.magnetUri).to.contain(str)
463 async function checkNotContains (servers: ServerInfo[], str: string) {
464 for (const server of servers) {
465 const res = await getVideo(server.url, video1Server2UUID)
466 const video: VideoDetails = res.body
468 for (const f of video.files) {
469 expect(f.magnetUri).to.not.contain(str)
474 before(async function () {
477 await flushAndRunServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
479 await enableRedundancyOnServer1()
482 it('Should still have 2 webseeds after 10 seconds', async function () {
488 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A' + servers[0].port)
490 // Maybe a server deleted a redundancy in the scheduler
493 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A' + servers[0].port)
497 it('Should stop server 1 and expire video redundancy', async function () {
500 killallServers([ servers[0] ])
504 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A' + servers[0].port)
507 after(async function () {
508 await cleanupTests(servers)
512 describe('Test file replacement', function () {
513 let video2Server2UUID: string
514 const strategy = 'recently-added'
516 before(async function () {
519 await flushAndRunServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
521 await enableRedundancyOnServer1()
523 await waitJobs(servers)
524 await waitUntilLog(servers[0], 'Duplicated ', 5)
525 await waitJobs(servers)
527 await check2Webseeds()
528 await check1PlaylistRedundancies()
529 await checkStatsWith2Webseed(strategy)
531 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
532 video2Server2UUID = res.body.video.uuid
535 it('Should cache video 2 webseeds on the first video', async function () {
538 await waitJobs(servers)
542 while (checked === false) {
546 await check1WebSeed(video1Server2UUID)
547 await check0PlaylistRedundancies(video1Server2UUID)
548 await check2Webseeds(video2Server2UUID)
549 await check1PlaylistRedundancies(video2Server2UUID)
558 it('Should disable strategy and remove redundancies', async function () {
561 await waitJobs(servers)
563 killallServers([ servers[ 0 ] ])
564 await reRunServer(servers[ 0 ], {
567 check_interval: '1 second',
573 await waitJobs(servers)
575 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].internalServerNumber, [ join('redundancy', 'hls') ])
578 after(async function () {
579 await cleanupTests(servers)