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/webseed/' + 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 // Only servers 1 and 2 have the video
153 if (server.serverNumber !== 3) {
154 await makeGetRequest({
156 statusCodeExpected: 200,
157 path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
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)
168 for (const resolution of [ 240, 360, 480, 720 ]) {
169 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
174 async function enableRedundancyOnServer1 () {
175 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
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')
182 expect(server3).to.not.be.undefined
183 expect(server3.following.hostRedundancyAllowed).to.be.false
185 expect(server2).to.not.be.undefined
186 expect(server2.following.hostRedundancyAllowed).to.be.true
189 async function disableRedundancyOnServer1 () {
190 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
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')
197 expect(server3).to.not.be.undefined
198 expect(server3.following.hostRedundancyAllowed).to.be.false
200 expect(server2).to.not.be.undefined
201 expect(server2.following.hostRedundancyAllowed).to.be.false
204 async function cleanServers () {
205 killallServers(servers)
208 describe('Test videos redundancy', function () {
210 describe('With most-views strategy', function () {
211 const strategy = 'most-views'
216 return runServers(strategy)
219 it('Should have 1 webseed on the first video', async function () {
220 await check1WebSeed(strategy)
221 await checkStatsWith1Webseed(strategy)
224 it('Should enable redundancy on server 1', function () {
225 return enableRedundancyOnServer1()
228 it('Should have 2 webseeds on the first video', async function () {
231 await waitJobs(servers)
232 await waitUntilLog(servers[0], 'Duplicated ', 4)
233 await waitJobs(servers)
235 await check2Webseeds(strategy)
236 await checkStatsWith2Webseed(strategy)
239 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
242 await disableRedundancyOnServer1()
244 await waitJobs(servers)
247 await check1WebSeed(strategy)
249 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
253 return cleanServers()
257 describe('With trending strategy', function () {
258 const strategy = 'trending'
263 return runServers(strategy)
266 it('Should have 1 webseed on the first video', async function () {
267 await check1WebSeed(strategy)
268 await checkStatsWith1Webseed(strategy)
271 it('Should enable redundancy on server 1', function () {
272 return enableRedundancyOnServer1()
275 it('Should have 2 webseeds on the first video', async function () {
278 await waitJobs(servers)
279 await waitUntilLog(servers[0], 'Duplicated ', 4)
280 await waitJobs(servers)
282 await check2Webseeds(strategy)
283 await checkStatsWith2Webseed(strategy)
286 it('Should unfollow on server 1 and remove duplicated videos', async function () {
289 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
291 await waitJobs(servers)
294 await check1WebSeed(strategy)
296 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
300 return cleanServers()
304 describe('With recently added strategy', function () {
305 const strategy = 'recently-added'
310 return runServers(strategy, { min_views: 3 })
313 it('Should have 1 webseed on the first video', async function () {
314 await check1WebSeed(strategy)
315 await checkStatsWith1Webseed(strategy)
318 it('Should enable redundancy on server 1', function () {
319 return enableRedundancyOnServer1()
322 it('Should still have 1 webseed on the first video', async function () {
325 await waitJobs(servers)
327 await waitJobs(servers)
329 await check1WebSeed(strategy)
330 await checkStatsWith1Webseed(strategy)
333 it('Should view 2 times the first video to have > min_views config', async function () {
336 await viewVideo(servers[ 0 ].url, video1Server2UUID)
337 await viewVideo(servers[ 2 ].url, video1Server2UUID)
340 await waitJobs(servers)
343 it('Should have 2 webseeds on the first video', async function () {
346 await waitJobs(servers)
347 await waitUntilLog(servers[0], 'Duplicated ', 4)
348 await waitJobs(servers)
350 await check2Webseeds(strategy)
351 await checkStatsWith2Webseed(strategy)
354 it('Should remove the video and the redundancy files', async function () {
357 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID)
359 await waitJobs(servers)
361 for (const server of servers) {
362 await checkVideoFilesWereRemoved(video1Server2UUID, server.serverNumber)
367 return cleanServers()
371 describe('Test expiration', function () {
372 const strategy = 'recently-added'
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
379 for (const f of video.files) {
380 expect(f.magnetUri).to.contain(str)
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
390 for (const f of video.files) {
391 expect(f.magnetUri).to.not.contain(str)
396 before(async function () {
399 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
401 await enableRedundancyOnServer1()
404 it('Should still have 2 webseeds after 10 seconds', async function () {
410 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
412 // Maybe a server deleted a redundancy in the scheduler
415 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
419 it('Should stop server 1 and expire video redundancy', async function () {
422 killallServers([ servers[0] ])
426 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
430 return killallServers([ servers[1], servers[2] ])
434 describe('Test file replacement', function () {
435 let video2Server2UUID: string
436 const strategy = 'recently-added'
438 before(async function () {
441 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
443 await enableRedundancyOnServer1()
445 await waitJobs(servers)
446 await waitUntilLog(servers[0], 'Duplicated ', 4)
447 await waitJobs(servers)
449 await check2Webseeds(strategy)
450 await checkStatsWith2Webseed(strategy)
452 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
453 video2Server2UUID = res.body.video.uuid
456 it('Should cache video 2 webseeds on the first video', async function () {
459 await waitJobs(servers)
463 while (checked === false) {
467 await check1WebSeed(strategy, video1Server2UUID)
468 await check2Webseeds(strategy, video2Server2UUID)
478 return cleanServers()