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
22 import { waitJobs } from '../../utils/server/jobs'
23 import * as magnetUtil from 'magnet-uri'
24 import { updateRedundancy } from '../../utils/server/redundancy'
25 import { ActorFollow } from '../../../../shared/models/actors'
26 import { readdir } from 'fs-extra'
27 import { join } from 'path'
28 import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
29 import { getStats } from '../../utils/server/stats'
30 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
32 const expect = chai.expect
34 let servers: ServerInfo[] = []
35 let video1Server2UUID: string
37 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[], server: ServerInfo) {
38 const parsed = magnetUtil.decode(file.magnetUri)
40 for (const ws of baseWebseeds) {
41 const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
42 expect(found, `Webseed ${ws} not found in ${file.magnetUri} on server ${server.url}`).to.not.be.undefined
45 expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
48 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
52 check_interval: '5 seconds',
55 min_lifetime: '1 hour',
63 servers = await flushAndRunMultipleServers(3, config)
65 // Get the access tokens
66 await setAccessTokensToServers(servers)
69 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
70 video1Server2UUID = res.body.video.uuid
72 await viewVideo(servers[ 1 ].url, video1Server2UUID)
75 await waitJobs(servers)
77 // Server 1 and server 2 follow each other
78 await doubleFollow(servers[ 0 ], servers[ 1 ])
79 // Server 1 and server 3 follow each other
80 await doubleFollow(servers[ 0 ], servers[ 2 ])
81 // Server 2 and server 3 follow each other
82 await doubleFollow(servers[ 1 ], servers[ 2 ])
84 await waitJobs(servers)
87 async function check1WebSeed (strategy: VideoRedundancyStrategy, videoUUID?: string) {
88 if (!videoUUID) videoUUID = video1Server2UUID
91 'http://localhost:9002/static/webseed/' + videoUUID
94 for (const server of servers) {
96 // With token to avoid issues with video follow constraints
97 const res = await getVideoWithToken(server.url, server.accessToken, videoUUID)
99 const video: VideoDetails = res.body
100 for (const f of video.files) {
101 checkMagnetWebseeds(f, webseeds, server)
107 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
108 const res = await getStats(servers[0].url)
109 const data: ServerStats = res.body
111 expect(data.videosRedundancy).to.have.lengthOf(1)
112 const stat = data.videosRedundancy[0]
114 expect(stat.strategy).to.equal(strategy)
115 expect(stat.totalSize).to.equal(204800)
116 expect(stat.totalUsed).to.be.at.least(1).and.below(204801)
117 expect(stat.totalVideoFiles).to.equal(4)
118 expect(stat.totalVideos).to.equal(1)
121 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
122 const res = await getStats(servers[0].url)
123 const data: ServerStats = res.body
125 expect(data.videosRedundancy).to.have.lengthOf(1)
127 const stat = data.videosRedundancy[0]
128 expect(stat.strategy).to.equal(strategy)
129 expect(stat.totalSize).to.equal(204800)
130 expect(stat.totalUsed).to.equal(0)
131 expect(stat.totalVideoFiles).to.equal(0)
132 expect(stat.totalVideos).to.equal(0)
135 async function check2Webseeds (strategy: VideoRedundancyStrategy, videoUUID?: string) {
136 if (!videoUUID) videoUUID = video1Server2UUID
139 'http://localhost:9001/static/redundancy/' + videoUUID,
140 'http://localhost:9002/static/webseed/' + videoUUID
143 for (const server of servers) {
144 const res = await getVideo(server.url, videoUUID)
146 const video: VideoDetails = res.body
148 for (const file of video.files) {
149 checkMagnetWebseeds(file, webseeds, server)
151 await makeGetRequest({
153 statusCodeExpected: 200,
154 path: '/static/redundancy/' + `${videoUUID}-${file.resolution.id}.mp4`,
157 await makeGetRequest({
159 statusCodeExpected: 200,
160 path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
166 for (const directory of [ 'test1/redundancy', 'test2/videos' ]) {
167 const files = await readdir(join(root(), directory))
168 expect(files).to.have.length.at.least(4)
170 for (const resolution of [ 240, 360, 480, 720 ]) {
171 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
176 async function enableRedundancyOnServer1 () {
177 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
179 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
180 const follows: ActorFollow[] = res.body.data
181 const server2 = follows.find(f => f.following.host === 'localhost:9002')
182 const server3 = follows.find(f => f.following.host === 'localhost:9003')
184 expect(server3).to.not.be.undefined
185 expect(server3.following.hostRedundancyAllowed).to.be.false
187 expect(server2).to.not.be.undefined
188 expect(server2.following.hostRedundancyAllowed).to.be.true
191 async function disableRedundancyOnServer1 () {
192 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
194 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
195 const follows: ActorFollow[] = res.body.data
196 const server2 = follows.find(f => f.following.host === 'localhost:9002')
197 const server3 = follows.find(f => f.following.host === 'localhost:9003')
199 expect(server3).to.not.be.undefined
200 expect(server3.following.hostRedundancyAllowed).to.be.false
202 expect(server2).to.not.be.undefined
203 expect(server2.following.hostRedundancyAllowed).to.be.false
206 async function cleanServers () {
207 killallServers(servers)
210 describe('Test videos redundancy', function () {
212 describe('With most-views strategy', function () {
213 const strategy = 'most-views'
218 return runServers(strategy)
221 it('Should have 1 webseed on the first video', async function () {
222 await check1WebSeed(strategy)
223 await checkStatsWith1Webseed(strategy)
226 it('Should enable redundancy on server 1', function () {
227 return enableRedundancyOnServer1()
230 it('Should have 2 webseeds on the first video', async function () {
233 await waitJobs(servers)
234 await waitUntilLog(servers[0], 'Duplicated ', 4)
235 await waitJobs(servers)
237 await check2Webseeds(strategy)
238 await checkStatsWith2Webseed(strategy)
241 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
244 await disableRedundancyOnServer1()
246 await waitJobs(servers)
249 await check1WebSeed(strategy)
251 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
255 return cleanServers()
259 describe('With trending strategy', function () {
260 const strategy = 'trending'
265 return runServers(strategy)
268 it('Should have 1 webseed on the first video', async function () {
269 await check1WebSeed(strategy)
270 await checkStatsWith1Webseed(strategy)
273 it('Should enable redundancy on server 1', function () {
274 return enableRedundancyOnServer1()
277 it('Should have 2 webseeds on the first video', async function () {
280 await waitJobs(servers)
281 await waitUntilLog(servers[0], 'Duplicated ', 4)
282 await waitJobs(servers)
284 await check2Webseeds(strategy)
285 await checkStatsWith2Webseed(strategy)
288 it('Should unfollow on server 1 and remove duplicated videos', async function () {
291 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
293 await waitJobs(servers)
296 await check1WebSeed(strategy)
298 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
302 return cleanServers()
306 describe('With recently added strategy', function () {
307 const strategy = 'recently-added'
312 return runServers(strategy, { min_views: 3 })
315 it('Should have 1 webseed on the first video', async function () {
316 await check1WebSeed(strategy)
317 await checkStatsWith1Webseed(strategy)
320 it('Should enable redundancy on server 1', function () {
321 return enableRedundancyOnServer1()
324 it('Should still have 1 webseed on the first video', async function () {
327 await waitJobs(servers)
329 await waitJobs(servers)
331 await check1WebSeed(strategy)
332 await checkStatsWith1Webseed(strategy)
335 it('Should view 2 times the first video to have > min_views config', async function () {
338 await viewVideo(servers[ 0 ].url, video1Server2UUID)
339 await viewVideo(servers[ 2 ].url, video1Server2UUID)
342 await waitJobs(servers)
345 it('Should have 2 webseeds on the first video', async function () {
348 await waitJobs(servers)
349 await waitUntilLog(servers[0], 'Duplicated ', 4)
350 await waitJobs(servers)
352 await check2Webseeds(strategy)
353 await checkStatsWith2Webseed(strategy)
356 it('Should remove the video and the redundancy files', async function () {
359 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID)
361 await waitJobs(servers)
363 for (const server of servers) {
364 await checkVideoFilesWereRemoved(video1Server2UUID, server.serverNumber)
369 return cleanServers()
373 describe('Test expiration', function () {
374 const strategy = 'recently-added'
376 async function checkContains (servers: ServerInfo[], str: string) {
377 for (const server of servers) {
378 const res = await getVideo(server.url, video1Server2UUID)
379 const video: VideoDetails = res.body
381 for (const f of video.files) {
382 expect(f.magnetUri).to.contain(str)
387 async function checkNotContains (servers: ServerInfo[], str: string) {
388 for (const server of servers) {
389 const res = await getVideo(server.url, video1Server2UUID)
390 const video: VideoDetails = res.body
392 for (const f of video.files) {
393 expect(f.magnetUri).to.not.contain(str)
398 before(async function () {
401 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
403 await enableRedundancyOnServer1()
406 it('Should still have 2 webseeds after 10 seconds', async function () {
412 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
414 // Maybe a server deleted a redundancy in the scheduler
417 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
421 it('Should stop server 1 and expire video redundancy', async function () {
424 killallServers([ servers[0] ])
428 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
432 return killallServers([ servers[1], servers[2] ])
436 describe('Test file replacement', function () {
437 let video2Server2UUID: string
438 const strategy = 'recently-added'
440 before(async function () {
443 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
445 await enableRedundancyOnServer1()
447 await waitJobs(servers)
448 await waitUntilLog(servers[0], 'Duplicated ', 4)
449 await waitJobs(servers)
451 await check2Webseeds(strategy)
452 await checkStatsWith2Webseed(strategy)
454 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
455 video2Server2UUID = res.body.video.uuid
458 it('Should cache video 2 webseeds on the first video', async function () {
461 await waitJobs(servers)
465 while (checked === false) {
469 await check1WebSeed(strategy, video1Server2UUID)
470 await check2Webseeds(strategy, video2Server2UUID)
480 return cleanServers()