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
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 await makeGetRequest({
183 statusCodeExpected: 200,
184 path: `/static/redundancy/hls/${videoUUID}/360_000.ts`,
188 for (const directory of [ 'test1/redundancy/hls', 'test2/playlists/hls' ]) {
189 const files = await readdir(join(root(), directory, videoUUID))
190 expect(files).to.have.length.at.least(4)
192 for (const resolution of [ 240, 360, 480, 720 ]) {
193 expect(files.find(f => f === `${resolution}_000.ts`)).to.not.be.undefined
194 expect(files.find(f => f === `${resolution}_001.ts`)).to.not.be.undefined
199 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
200 const res = await getStats(servers[0].url)
201 const data: ServerStats = res.body
203 expect(data.videosRedundancy).to.have.lengthOf(1)
204 const stat = data.videosRedundancy[0]
206 expect(stat.strategy).to.equal(strategy)
207 expect(stat.totalSize).to.equal(204800)
208 expect(stat.totalUsed).to.be.at.least(1).and.below(204801)
209 expect(stat.totalVideoFiles).to.equal(4)
210 expect(stat.totalVideos).to.equal(1)
213 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
214 const res = await getStats(servers[0].url)
215 const data: ServerStats = res.body
217 expect(data.videosRedundancy).to.have.lengthOf(1)
219 const stat = data.videosRedundancy[0]
220 expect(stat.strategy).to.equal(strategy)
221 expect(stat.totalSize).to.equal(204800)
222 expect(stat.totalUsed).to.equal(0)
223 expect(stat.totalVideoFiles).to.equal(0)
224 expect(stat.totalVideos).to.equal(0)
227 async function enableRedundancyOnServer1 () {
228 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
230 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
231 const follows: ActorFollow[] = res.body.data
232 const server2 = follows.find(f => f.following.host === 'localhost:9002')
233 const server3 = follows.find(f => f.following.host === 'localhost:9003')
235 expect(server3).to.not.be.undefined
236 expect(server3.following.hostRedundancyAllowed).to.be.false
238 expect(server2).to.not.be.undefined
239 expect(server2.following.hostRedundancyAllowed).to.be.true
242 async function disableRedundancyOnServer1 () {
243 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
245 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
246 const follows: ActorFollow[] = res.body.data
247 const server2 = follows.find(f => f.following.host === 'localhost:9002')
248 const server3 = follows.find(f => f.following.host === 'localhost:9003')
250 expect(server3).to.not.be.undefined
251 expect(server3.following.hostRedundancyAllowed).to.be.false
253 expect(server2).to.not.be.undefined
254 expect(server2.following.hostRedundancyAllowed).to.be.false
257 async function cleanServers () {
258 killallServers(servers)
261 describe('Test videos redundancy', function () {
263 describe('With most-views strategy', function () {
264 const strategy = 'most-views'
269 return runServers(strategy)
272 it('Should have 1 webseed on the first video', async function () {
273 await check1WebSeed()
274 await check0PlaylistRedundancies()
275 await checkStatsWith1Webseed(strategy)
278 it('Should enable redundancy on server 1', function () {
279 return enableRedundancyOnServer1()
282 it('Should have 2 webseeds on the first video', async function () {
285 await waitJobs(servers)
286 await waitUntilLog(servers[0], 'Duplicated ', 5)
287 await waitJobs(servers)
289 await check2Webseeds()
290 await check1PlaylistRedundancies()
291 await checkStatsWith2Webseed(strategy)
294 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
297 await disableRedundancyOnServer1()
299 await waitJobs(servers)
302 await check1WebSeed()
303 await check0PlaylistRedundancies()
305 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos', join('playlists', 'hls') ])
309 return cleanServers()
313 describe('With trending strategy', function () {
314 const strategy = 'trending'
319 return runServers(strategy)
322 it('Should have 1 webseed on the first video', async function () {
323 await check1WebSeed()
324 await check0PlaylistRedundancies()
325 await checkStatsWith1Webseed(strategy)
328 it('Should enable redundancy on server 1', function () {
329 return enableRedundancyOnServer1()
332 it('Should have 2 webseeds on the first video', async function () {
335 await waitJobs(servers)
336 await waitUntilLog(servers[0], 'Duplicated ', 5)
337 await waitJobs(servers)
339 await check2Webseeds()
340 await check1PlaylistRedundancies()
341 await checkStatsWith2Webseed(strategy)
344 it('Should unfollow on server 1 and remove duplicated videos', async function () {
347 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
349 await waitJobs(servers)
352 await check1WebSeed()
353 await check0PlaylistRedundancies()
355 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
359 return cleanServers()
363 describe('With recently added strategy', function () {
364 const strategy = 'recently-added'
369 return runServers(strategy, { min_views: 3 })
372 it('Should have 1 webseed on the first video', async function () {
373 await check1WebSeed()
374 await check0PlaylistRedundancies()
375 await checkStatsWith1Webseed(strategy)
378 it('Should enable redundancy on server 1', function () {
379 return enableRedundancyOnServer1()
382 it('Should still have 1 webseed on the first video', async function () {
385 await waitJobs(servers)
387 await waitJobs(servers)
389 await check1WebSeed()
390 await check0PlaylistRedundancies()
391 await checkStatsWith1Webseed(strategy)
394 it('Should view 2 times the first video to have > min_views config', async function () {
397 await viewVideo(servers[ 0 ].url, video1Server2UUID)
398 await viewVideo(servers[ 2 ].url, video1Server2UUID)
401 await waitJobs(servers)
404 it('Should have 2 webseeds on the first video', async function () {
407 await waitJobs(servers)
408 await waitUntilLog(servers[0], 'Duplicated ', 5)
409 await waitJobs(servers)
411 await check2Webseeds()
412 await check1PlaylistRedundancies()
413 await checkStatsWith2Webseed(strategy)
416 it('Should remove the video and the redundancy files', async function () {
419 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID)
421 await waitJobs(servers)
423 for (const server of servers) {
424 await checkVideoFilesWereRemoved(video1Server2UUID, server.serverNumber)
429 return cleanServers()
433 describe('Test expiration', function () {
434 const strategy = 'recently-added'
436 async function checkContains (servers: ServerInfo[], str: string) {
437 for (const server of servers) {
438 const res = await getVideo(server.url, video1Server2UUID)
439 const video: VideoDetails = res.body
441 for (const f of video.files) {
442 expect(f.magnetUri).to.contain(str)
447 async function checkNotContains (servers: ServerInfo[], str: string) {
448 for (const server of servers) {
449 const res = await getVideo(server.url, video1Server2UUID)
450 const video: VideoDetails = res.body
452 for (const f of video.files) {
453 expect(f.magnetUri).to.not.contain(str)
458 before(async function () {
461 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
463 await enableRedundancyOnServer1()
466 it('Should still have 2 webseeds after 10 seconds', async function () {
472 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
474 // Maybe a server deleted a redundancy in the scheduler
477 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
481 it('Should stop server 1 and expire video redundancy', async function () {
484 killallServers([ servers[0] ])
488 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
492 return killallServers([ servers[1], servers[2] ])
496 describe('Test file replacement', function () {
497 let video2Server2UUID: string
498 const strategy = 'recently-added'
500 before(async function () {
503 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
505 await enableRedundancyOnServer1()
507 await waitJobs(servers)
508 await waitUntilLog(servers[0], 'Duplicated ', 5)
509 await waitJobs(servers)
511 await check2Webseeds()
512 await check1PlaylistRedundancies()
513 await checkStatsWith2Webseed(strategy)
515 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
516 video2Server2UUID = res.body.video.uuid
519 it('Should cache video 2 webseeds on the first video', async function () {
522 await waitJobs(servers)
526 while (checked === false) {
530 await check1WebSeed(video1Server2UUID)
531 await check0PlaylistRedundancies(video1Server2UUID)
532 await check2Webseeds(video2Server2UUID)
533 await check1PlaylistRedundancies(video2Server2UUID)
542 it('Should disable strategy and remove redundancies', async function () {
545 await waitJobs(servers)
547 killallServers([ servers[ 0 ] ])
548 await reRunServer(servers[ 0 ], {
551 check_interval: '1 second',
557 await waitJobs(servers)
559 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ join('redundancy', 'hls') ])
563 return cleanServers()