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,
21 import { waitJobs } from '../../utils/server/jobs'
22 import * as magnetUtil from 'magnet-uri'
23 import { updateRedundancy } from '../../utils/server/redundancy'
24 import { ActorFollow } from '../../../../shared/models/actors'
25 import { readdir } from 'fs-extra'
26 import { join } from 'path'
27 import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
28 import { getStats } from '../../utils/server/stats'
29 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
31 const expect = chai.expect
33 let servers: ServerInfo[] = []
34 let video1Server2UUID: string
36 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[], server: ServerInfo) {
37 const parsed = magnetUtil.decode(file.magnetUri)
39 for (const ws of baseWebseeds) {
40 const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
41 expect(found, `Webseed ${ws} not found in ${file.magnetUri} on server ${server.url}`).to.not.be.undefined
44 expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
47 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
51 check_interval: '5 seconds',
54 min_lifetime: '1 hour',
62 servers = await flushAndRunMultipleServers(3, config)
64 // Get the access tokens
65 await setAccessTokensToServers(servers)
68 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
69 video1Server2UUID = res.body.video.uuid
71 await viewVideo(servers[ 1 ].url, video1Server2UUID)
74 await waitJobs(servers)
76 // Server 1 and server 2 follow each other
77 await doubleFollow(servers[ 0 ], servers[ 1 ])
78 // Server 1 and server 3 follow each other
79 await doubleFollow(servers[ 0 ], servers[ 2 ])
80 // Server 2 and server 3 follow each other
81 await doubleFollow(servers[ 1 ], servers[ 2 ])
83 await waitJobs(servers)
86 async function check1WebSeed (strategy: VideoRedundancyStrategy, videoUUID?: string) {
87 if (!videoUUID) videoUUID = video1Server2UUID
90 'http://localhost:9002/static/webseed/' + videoUUID
93 for (const server of servers) {
95 const res = await getVideo(server.url, videoUUID)
97 const video: VideoDetails = res.body
98 for (const f of video.files) {
99 checkMagnetWebseeds(f, webseeds, server)
105 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
106 const res = await getStats(servers[0].url)
107 const data: ServerStats = res.body
109 expect(data.videosRedundancy).to.have.lengthOf(1)
110 const stat = data.videosRedundancy[0]
112 expect(stat.strategy).to.equal(strategy)
113 expect(stat.totalSize).to.equal(102400)
114 expect(stat.totalUsed).to.be.at.least(1).and.below(102401)
115 expect(stat.totalVideoFiles).to.equal(4)
116 expect(stat.totalVideos).to.equal(1)
119 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
120 const res = await getStats(servers[0].url)
121 const data: ServerStats = res.body
123 expect(data.videosRedundancy).to.have.lengthOf(1)
125 const stat = data.videosRedundancy[0]
126 expect(stat.strategy).to.equal(strategy)
127 expect(stat.totalSize).to.equal(102400)
128 expect(stat.totalUsed).to.equal(0)
129 expect(stat.totalVideoFiles).to.equal(0)
130 expect(stat.totalVideos).to.equal(0)
133 async function check2Webseeds (strategy: VideoRedundancyStrategy, videoUUID?: string) {
134 if (!videoUUID) videoUUID = video1Server2UUID
137 'http://localhost:9001/static/webseed/' + videoUUID,
138 'http://localhost:9002/static/webseed/' + videoUUID
141 for (const server of servers) {
142 const res = await getVideo(server.url, videoUUID)
144 const video: VideoDetails = res.body
146 for (const file of video.files) {
147 checkMagnetWebseeds(file, webseeds, server)
149 // Only servers 1 and 2 have the video
150 if (server.serverNumber !== 3) {
151 await makeGetRequest({
153 statusCodeExpected: 200,
154 path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
161 for (const directory of [ 'test1', 'test2' ]) {
162 const files = await readdir(join(root(), directory, 'videos'))
163 expect(files).to.have.length.at.least(4)
165 for (const resolution of [ 240, 360, 480, 720 ]) {
166 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
171 async function enableRedundancyOnServer1 () {
172 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
174 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
175 const follows: ActorFollow[] = res.body.data
176 const server2 = follows.find(f => f.following.host === 'localhost:9002')
177 const server3 = follows.find(f => f.following.host === 'localhost:9003')
179 expect(server3).to.not.be.undefined
180 expect(server3.following.hostRedundancyAllowed).to.be.false
182 expect(server2).to.not.be.undefined
183 expect(server2.following.hostRedundancyAllowed).to.be.true
186 async function disableRedundancyOnServer1 () {
187 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
189 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
190 const follows: ActorFollow[] = res.body.data
191 const server2 = follows.find(f => f.following.host === 'localhost:9002')
192 const server3 = follows.find(f => f.following.host === 'localhost:9003')
194 expect(server3).to.not.be.undefined
195 expect(server3.following.hostRedundancyAllowed).to.be.false
197 expect(server2).to.not.be.undefined
198 expect(server2.following.hostRedundancyAllowed).to.be.false
201 async function cleanServers () {
202 killallServers(servers)
205 describe('Test videos redundancy', function () {
207 describe('With most-views strategy', function () {
208 const strategy = 'most-views'
213 return runServers(strategy)
216 it('Should have 1 webseed on the first video', async function () {
217 await check1WebSeed(strategy)
218 await checkStatsWith1Webseed(strategy)
221 it('Should enable redundancy on server 1', function () {
222 return enableRedundancyOnServer1()
225 it('Should have 2 webseed on the first video', async function () {
228 await waitJobs(servers)
229 await waitUntilLog(servers[0], 'Duplicated ', 4)
230 await waitJobs(servers)
232 await check2Webseeds(strategy)
233 await checkStatsWith2Webseed(strategy)
236 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
239 await disableRedundancyOnServer1()
241 await waitJobs(servers)
244 await check1WebSeed(strategy)
248 return cleanServers()
252 describe('With trending strategy', function () {
253 const strategy = 'trending'
258 return runServers(strategy)
261 it('Should have 1 webseed on the first video', async function () {
262 await check1WebSeed(strategy)
263 await checkStatsWith1Webseed(strategy)
266 it('Should enable redundancy on server 1', function () {
267 return enableRedundancyOnServer1()
270 it('Should have 2 webseed on the first video', async function () {
273 await waitJobs(servers)
274 await waitUntilLog(servers[0], 'Duplicated ', 4)
275 await waitJobs(servers)
277 await check2Webseeds(strategy)
278 await checkStatsWith2Webseed(strategy)
281 it('Should unfollow on server 1 and remove duplicated videos', async function () {
284 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
286 await waitJobs(servers)
289 await check1WebSeed(strategy)
293 return cleanServers()
297 describe('With recently added strategy', function () {
298 const strategy = 'recently-added'
303 return runServers(strategy, { min_views: 3 })
306 it('Should have 1 webseed on the first video', async function () {
307 await check1WebSeed(strategy)
308 await checkStatsWith1Webseed(strategy)
311 it('Should enable redundancy on server 1', function () {
312 return enableRedundancyOnServer1()
315 it('Should still have 1 webseed on the first video', async function () {
318 await waitJobs(servers)
320 await waitJobs(servers)
322 await check1WebSeed(strategy)
323 await checkStatsWith1Webseed(strategy)
326 it('Should view 2 times the first video to have > min_views config', async function () {
329 await viewVideo(servers[ 0 ].url, video1Server2UUID)
330 await viewVideo(servers[ 2 ].url, video1Server2UUID)
333 await waitJobs(servers)
336 it('Should have 2 webseed on the first video', async function () {
339 await waitJobs(servers)
340 await waitUntilLog(servers[0], 'Duplicated ', 4)
341 await waitJobs(servers)
343 await check2Webseeds(strategy)
344 await checkStatsWith2Webseed(strategy)
348 return cleanServers()
352 describe('Test expiration', function () {
353 const strategy = 'recently-added'
355 async function checkContains (servers: ServerInfo[], str: string) {
356 for (const server of servers) {
357 const res = await getVideo(server.url, video1Server2UUID)
358 const video: VideoDetails = res.body
360 for (const f of video.files) {
361 expect(f.magnetUri).to.contain(str)
366 async function checkNotContains (servers: ServerInfo[], str: string) {
367 for (const server of servers) {
368 const res = await getVideo(server.url, video1Server2UUID)
369 const video: VideoDetails = res.body
371 for (const f of video.files) {
372 expect(f.magnetUri).to.not.contain(str)
377 before(async function () {
380 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
382 await enableRedundancyOnServer1()
385 it('Should still have 2 webseeds after 10 seconds', async function () {
391 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
393 // Maybe a server deleted a redundancy in the scheduler
396 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
400 it('Should stop server 1 and expire video redundancy', async function () {
403 killallServers([ servers[0] ])
407 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
411 return killallServers([ servers[1], servers[2] ])
415 describe('Test file replacement', function () {
416 let video2Server2UUID: string
417 const strategy = 'recently-added'
419 before(async function () {
422 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
424 await enableRedundancyOnServer1()
426 await waitJobs(servers)
427 await waitUntilLog(servers[0], 'Duplicated ', 4)
428 await waitJobs(servers)
430 await check2Webseeds(strategy)
431 await checkStatsWith2Webseed(strategy)
433 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
434 video2Server2UUID = res.body.video.uuid
437 it('Should cache video 2 webseed on the first video', async function () {
440 await waitJobs(servers)
445 await check1WebSeed(strategy, video1Server2UUID)
446 await check2Webseeds(strategy, video2Server2UUID)
451 await check1WebSeed(strategy, video1Server2UUID)
452 await check2Webseeds(strategy, video2Server2UUID)
456 await check1WebSeed(strategy, video1Server2UUID)
457 await check2Webseeds(strategy, video2Server2UUID)
463 return cleanServers()