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 import { waitJobs } from '../../utils/server/jobs'
21 import * as magnetUtil from 'magnet-uri'
22 import { updateRedundancy } from '../../utils/server/redundancy'
23 import { ActorFollow } from '../../../../shared/models/actors'
24 import { readdir } from 'fs-extra'
25 import { join } from 'path'
26 import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
27 import { getStats } from '../../utils/server/stats'
28 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
30 const expect = chai.expect
32 let servers: ServerInfo[] = []
33 let video1Server2UUID: string
35 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[], server: ServerInfo) {
36 const parsed = magnetUtil.decode(file.magnetUri)
38 for (const ws of baseWebseeds) {
39 const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
40 expect(found, `Webseed ${ws} not found in ${file.magnetUri} on server ${server.url}`).to.not.be.undefined
43 expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
46 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
50 check_interval: '5 seconds',
53 min_lifetime: '1 hour',
61 servers = await flushAndRunMultipleServers(3, config)
63 // Get the access tokens
64 await setAccessTokensToServers(servers)
67 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
68 video1Server2UUID = res.body.video.uuid
70 await viewVideo(servers[ 1 ].url, video1Server2UUID)
73 await waitJobs(servers)
75 // Server 1 and server 2 follow each other
76 await doubleFollow(servers[ 0 ], servers[ 1 ])
77 // Server 1 and server 3 follow each other
78 await doubleFollow(servers[ 0 ], servers[ 2 ])
79 // Server 2 and server 3 follow each other
80 await doubleFollow(servers[ 1 ], servers[ 2 ])
82 await waitJobs(servers)
85 async function check1WebSeed (strategy: VideoRedundancyStrategy, videoUUID?: string) {
86 if (!videoUUID) videoUUID = video1Server2UUID
89 'http://localhost:9002/static/webseed/' + videoUUID
92 for (const server of servers) {
94 const res = await getVideo(server.url, videoUUID)
96 const video: VideoDetails = res.body
97 for (const f of video.files) {
98 checkMagnetWebseeds(f, webseeds, server)
104 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
105 const res = await getStats(servers[0].url)
106 const data: ServerStats = res.body
108 expect(data.videosRedundancy).to.have.lengthOf(1)
109 const stat = data.videosRedundancy[0]
111 expect(stat.strategy).to.equal(strategy)
112 expect(stat.totalSize).to.equal(102400)
113 expect(stat.totalUsed).to.be.at.least(1).and.below(102401)
114 expect(stat.totalVideoFiles).to.equal(4)
115 expect(stat.totalVideos).to.equal(1)
118 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
119 const res = await getStats(servers[0].url)
120 const data: ServerStats = res.body
122 expect(data.videosRedundancy).to.have.lengthOf(1)
124 const stat = data.videosRedundancy[0]
125 expect(stat.strategy).to.equal(strategy)
126 expect(stat.totalSize).to.equal(102400)
127 expect(stat.totalUsed).to.equal(0)
128 expect(stat.totalVideoFiles).to.equal(0)
129 expect(stat.totalVideos).to.equal(0)
132 async function check2Webseeds (strategy: VideoRedundancyStrategy, videoUUID?: string) {
133 if (!videoUUID) videoUUID = video1Server2UUID
136 'http://localhost:9001/static/webseed/' + videoUUID,
137 'http://localhost:9002/static/webseed/' + videoUUID
140 for (const server of servers) {
141 const res = await getVideo(server.url, videoUUID)
143 const video: VideoDetails = res.body
145 for (const file of video.files) {
146 checkMagnetWebseeds(file, webseeds, server)
148 // Only servers 1 and 2 have the video
149 if (server.serverNumber !== 3) {
150 await makeGetRequest({
152 statusCodeExpected: 200,
153 path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
160 for (const directory of [ 'test1', 'test2' ]) {
161 const files = await readdir(join(root(), directory, 'videos'))
162 expect(files).to.have.length.at.least(4)
164 for (const resolution of [ 240, 360, 480, 720 ]) {
165 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
170 async function enableRedundancyOnServer1 () {
171 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
173 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
174 const follows: ActorFollow[] = res.body.data
175 const server2 = follows.find(f => f.following.host === 'localhost:9002')
176 const server3 = follows.find(f => f.following.host === 'localhost:9003')
178 expect(server3).to.not.be.undefined
179 expect(server3.following.hostRedundancyAllowed).to.be.false
181 expect(server2).to.not.be.undefined
182 expect(server2.following.hostRedundancyAllowed).to.be.true
185 async function disableRedundancyOnServer1 () {
186 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
188 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
189 const follows: ActorFollow[] = res.body.data
190 const server2 = follows.find(f => f.following.host === 'localhost:9002')
191 const server3 = follows.find(f => f.following.host === 'localhost:9003')
193 expect(server3).to.not.be.undefined
194 expect(server3.following.hostRedundancyAllowed).to.be.false
196 expect(server2).to.not.be.undefined
197 expect(server2.following.hostRedundancyAllowed).to.be.false
200 async function cleanServers () {
201 killallServers(servers)
204 describe('Test videos redundancy', function () {
206 describe('With most-views strategy', function () {
207 const strategy = 'most-views'
212 return runServers(strategy)
215 it('Should have 1 webseed on the first video', async function () {
216 await check1WebSeed(strategy)
217 await checkStatsWith1Webseed(strategy)
220 it('Should enable redundancy on server 1', function () {
221 return enableRedundancyOnServer1()
224 it('Should have 2 webseed on the first video', async function () {
227 await waitJobs(servers)
229 await waitJobs(servers)
231 await check2Webseeds(strategy)
232 await checkStatsWith2Webseed(strategy)
235 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
238 await disableRedundancyOnServer1()
240 await waitJobs(servers)
243 await check1WebSeed(strategy)
247 return cleanServers()
251 describe('With trending strategy', function () {
252 const strategy = 'trending'
257 return runServers(strategy)
260 it('Should have 1 webseed on the first video', async function () {
261 await check1WebSeed(strategy)
262 await checkStatsWith1Webseed(strategy)
265 it('Should enable redundancy on server 1', function () {
266 return enableRedundancyOnServer1()
269 it('Should have 2 webseed on the first video', async function () {
272 await waitJobs(servers)
274 await waitJobs(servers)
276 await check2Webseeds(strategy)
277 await checkStatsWith2Webseed(strategy)
280 it('Should unfollow on server 1 and remove duplicated videos', async function () {
283 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
285 await waitJobs(servers)
288 await check1WebSeed(strategy)
292 return cleanServers()
296 describe('With recently added strategy', function () {
297 const strategy = 'recently-added'
302 return runServers(strategy, { min_views: 3 })
305 it('Should have 1 webseed on the first video', async function () {
306 await check1WebSeed(strategy)
307 await checkStatsWith1Webseed(strategy)
310 it('Should enable redundancy on server 1', function () {
311 return enableRedundancyOnServer1()
314 it('Should still have 1 webseed on the first video', async function () {
317 await waitJobs(servers)
319 await waitJobs(servers)
321 await check1WebSeed(strategy)
322 await checkStatsWith1Webseed(strategy)
325 it('Should view 2 times the first video to have > min_views config', async function () {
328 await viewVideo(servers[ 0 ].url, video1Server2UUID)
329 await viewVideo(servers[ 2 ].url, video1Server2UUID)
332 await waitJobs(servers)
335 it('Should have 2 webseed on the first video', async function () {
338 await waitJobs(servers)
340 await waitJobs(servers)
342 await check2Webseeds(strategy)
343 await checkStatsWith2Webseed(strategy)
347 return cleanServers()
351 describe('Test expiration', function () {
352 const strategy = 'recently-added'
354 async function checkContains (servers: ServerInfo[], str: string) {
355 for (const server of servers) {
356 const res = await getVideo(server.url, video1Server2UUID)
357 const video: VideoDetails = res.body
359 for (const f of video.files) {
360 expect(f.magnetUri).to.contain(str)
365 async function checkNotContains (servers: ServerInfo[], str: string) {
366 for (const server of servers) {
367 const res = await getVideo(server.url, video1Server2UUID)
368 const video: VideoDetails = res.body
370 for (const f of video.files) {
371 expect(f.magnetUri).to.not.contain(str)
376 before(async function () {
379 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
381 await enableRedundancyOnServer1()
384 it('Should still have 2 webseeds after 10 seconds', async function () {
390 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
392 // Maybe a server deleted a redundancy in the scheduler
395 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
399 it('Should stop server 1 and expire video redundancy', async function () {
402 killallServers([ servers[0] ])
406 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
410 return killallServers([ servers[1], servers[2] ])
414 describe('Test file replacement', function () {
415 let video2Server2UUID: string
416 const strategy = 'recently-added'
418 before(async function () {
421 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
423 await enableRedundancyOnServer1()
425 await waitJobs(servers)
427 await waitJobs(servers)
429 await check2Webseeds(strategy)
430 await checkStatsWith2Webseed(strategy)
432 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
433 video2Server2UUID = res.body.video.uuid
436 it('Should cache video 2 webseed on the first video', async function () {
440 await waitJobs(servers)
444 await check1WebSeed(strategy, video1Server2UUID)
445 await check2Webseeds(strategy, video2Server2UUID)
449 return cleanServers()