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
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 = {}) {
53 check_interval: '5 seconds',
56 min_lifetime: '1 hour',
64 servers = await flushAndRunMultipleServers(3, config)
66 // Get the access tokens
67 await setAccessTokensToServers(servers)
70 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
71 video1Server2UUID = res.body.video.uuid
73 await viewVideo(servers[ 1 ].url, video1Server2UUID)
76 await waitJobs(servers)
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 ])
85 await waitJobs(servers)
88 async function check1WebSeed (strategy: VideoRedundancyStrategy, videoUUID?: string) {
89 if (!videoUUID) videoUUID = video1Server2UUID
92 'http://localhost:9002/static/webseed/' + videoUUID
95 for (const server of servers) {
97 // With token to avoid issues with video follow constraints
98 const res = await getVideoWithToken(server.url, server.accessToken, videoUUID)
100 const video: VideoDetails = res.body
101 for (const f of video.files) {
102 checkMagnetWebseeds(f, webseeds, server)
108 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
109 const res = await getStats(servers[0].url)
110 const data: ServerStats = res.body
112 expect(data.videosRedundancy).to.have.lengthOf(1)
113 const stat = data.videosRedundancy[0]
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)
122 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
123 const res = await getStats(servers[0].url)
124 const data: ServerStats = res.body
126 expect(data.videosRedundancy).to.have.lengthOf(1)
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)
136 async function check2Webseeds (strategy: VideoRedundancyStrategy, videoUUID?: string) {
137 if (!videoUUID) videoUUID = video1Server2UUID
140 'http://localhost:9001/static/redundancy/' + videoUUID,
141 'http://localhost:9002/static/webseed/' + videoUUID
144 for (const server of servers) {
145 const res = await getVideo(server.url, videoUUID)
147 const video: VideoDetails = res.body
149 for (const file of video.files) {
150 checkMagnetWebseeds(file, webseeds, server)
152 await makeGetRequest({
154 statusCodeExpected: 200,
155 path: '/static/redundancy/' + `${videoUUID}-${file.resolution.id}.mp4`,
158 await makeGetRequest({
160 statusCodeExpected: 200,
161 path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
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)
171 for (const resolution of [ 240, 360, 480, 720 ]) {
172 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
177 async function enableRedundancyOnServer1 () {
178 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
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')
185 expect(server3).to.not.be.undefined
186 expect(server3.following.hostRedundancyAllowed).to.be.false
188 expect(server2).to.not.be.undefined
189 expect(server2.following.hostRedundancyAllowed).to.be.true
192 async function disableRedundancyOnServer1 () {
193 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
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')
200 expect(server3).to.not.be.undefined
201 expect(server3.following.hostRedundancyAllowed).to.be.false
203 expect(server2).to.not.be.undefined
204 expect(server2.following.hostRedundancyAllowed).to.be.false
207 async function cleanServers () {
208 killallServers(servers)
211 describe('Test videos redundancy', function () {
213 describe('With most-views strategy', function () {
214 const strategy = 'most-views'
219 return runServers(strategy)
222 it('Should have 1 webseed on the first video', async function () {
223 await check1WebSeed(strategy)
224 await checkStatsWith1Webseed(strategy)
227 it('Should enable redundancy on server 1', function () {
228 return enableRedundancyOnServer1()
231 it('Should have 2 webseeds on the first video', async function () {
234 await waitJobs(servers)
235 await waitUntilLog(servers[0], 'Duplicated ', 4)
236 await waitJobs(servers)
238 await check2Webseeds(strategy)
239 await checkStatsWith2Webseed(strategy)
242 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
245 await disableRedundancyOnServer1()
247 await waitJobs(servers)
250 await check1WebSeed(strategy)
252 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
256 return cleanServers()
260 describe('With trending strategy', function () {
261 const strategy = 'trending'
266 return runServers(strategy)
269 it('Should have 1 webseed on the first video', async function () {
270 await check1WebSeed(strategy)
271 await checkStatsWith1Webseed(strategy)
274 it('Should enable redundancy on server 1', function () {
275 return enableRedundancyOnServer1()
278 it('Should have 2 webseeds on the first video', async function () {
281 await waitJobs(servers)
282 await waitUntilLog(servers[0], 'Duplicated ', 4)
283 await waitJobs(servers)
285 await check2Webseeds(strategy)
286 await checkStatsWith2Webseed(strategy)
289 it('Should unfollow on server 1 and remove duplicated videos', async function () {
292 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
294 await waitJobs(servers)
297 await check1WebSeed(strategy)
299 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
303 return cleanServers()
307 describe('With recently added strategy', function () {
308 const strategy = 'recently-added'
313 return runServers(strategy, { min_views: 3 })
316 it('Should have 1 webseed on the first video', async function () {
317 await check1WebSeed(strategy)
318 await checkStatsWith1Webseed(strategy)
321 it('Should enable redundancy on server 1', function () {
322 return enableRedundancyOnServer1()
325 it('Should still have 1 webseed on the first video', async function () {
328 await waitJobs(servers)
330 await waitJobs(servers)
332 await check1WebSeed(strategy)
333 await checkStatsWith1Webseed(strategy)
336 it('Should view 2 times the first video to have > min_views config', async function () {
339 await viewVideo(servers[ 0 ].url, video1Server2UUID)
340 await viewVideo(servers[ 2 ].url, video1Server2UUID)
343 await waitJobs(servers)
346 it('Should have 2 webseeds on the first video', async function () {
349 await waitJobs(servers)
350 await waitUntilLog(servers[0], 'Duplicated ', 4)
351 await waitJobs(servers)
353 await check2Webseeds(strategy)
354 await checkStatsWith2Webseed(strategy)
357 it('Should remove the video and the redundancy files', async function () {
360 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID)
362 await waitJobs(servers)
364 for (const server of servers) {
365 await checkVideoFilesWereRemoved(video1Server2UUID, server.serverNumber)
370 return cleanServers()
374 describe('Test expiration', function () {
375 const strategy = 'recently-added'
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
382 for (const f of video.files) {
383 expect(f.magnetUri).to.contain(str)
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
393 for (const f of video.files) {
394 expect(f.magnetUri).to.not.contain(str)
399 before(async function () {
402 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
404 await enableRedundancyOnServer1()
407 it('Should still have 2 webseeds after 10 seconds', async function () {
413 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
415 // Maybe a server deleted a redundancy in the scheduler
418 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
422 it('Should stop server 1 and expire video redundancy', async function () {
425 killallServers([ servers[0] ])
429 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
433 return killallServers([ servers[1], servers[2] ])
437 describe('Test file replacement', function () {
438 let video2Server2UUID: string
439 const strategy = 'recently-added'
441 before(async function () {
444 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
446 await enableRedundancyOnServer1()
448 await waitJobs(servers)
449 await waitUntilLog(servers[0], 'Duplicated ', 4)
450 await waitJobs(servers)
452 await check2Webseeds(strategy)
453 await checkStatsWith2Webseed(strategy)
455 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
456 video2Server2UUID = res.body.video.uuid
459 it('Should cache video 2 webseeds on the first video', async function () {
462 await waitJobs(servers)
466 while (checked === false) {
470 await check1WebSeed(strategy, video1Server2UUID)
471 await check2Webseeds(strategy, video2Server2UUID)
481 return cleanServers()