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,
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
44 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
48 check_interval: '5 seconds',
51 min_lifetime: '1 hour',
59 servers = await flushAndRunMultipleServers(3, config)
61 // Get the access tokens
62 await setAccessTokensToServers(servers)
65 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
66 video1Server2UUID = res.body.video.uuid
68 await viewVideo(servers[ 1 ].url, video1Server2UUID)
71 await waitJobs(servers)
73 // Server 1 and server 2 follow each other
74 await doubleFollow(servers[ 0 ], servers[ 1 ])
75 // Server 1 and server 3 follow each other
76 await doubleFollow(servers[ 0 ], servers[ 2 ])
77 // Server 2 and server 3 follow each other
78 await doubleFollow(servers[ 1 ], servers[ 2 ])
80 await waitJobs(servers)
83 async function check1WebSeed (strategy: VideoRedundancyStrategy, videoUUID?: string) {
84 if (!videoUUID) videoUUID = video1Server2UUID
87 'http://localhost:9002/static/webseed/' + videoUUID
90 for (const server of servers) {
92 const res = await getVideo(server.url, videoUUID)
94 const video: VideoDetails = res.body
95 for (const f of video.files) {
96 checkMagnetWebseeds(f, webseeds, server)
102 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
103 const res = await getStats(servers[0].url)
104 const data: ServerStats = res.body
106 expect(data.videosRedundancy).to.have.lengthOf(1)
107 const stat = data.videosRedundancy[0]
109 expect(stat.strategy).to.equal(strategy)
110 expect(stat.totalSize).to.equal(102400)
111 expect(stat.totalUsed).to.be.at.least(1).and.below(102401)
112 expect(stat.totalVideoFiles).to.equal(4)
113 expect(stat.totalVideos).to.equal(1)
116 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
117 const res = await getStats(servers[0].url)
118 const data: ServerStats = res.body
120 expect(data.videosRedundancy).to.have.lengthOf(1)
122 const stat = data.videosRedundancy[0]
123 expect(stat.strategy).to.equal(strategy)
124 expect(stat.totalSize).to.equal(102400)
125 expect(stat.totalUsed).to.equal(0)
126 expect(stat.totalVideoFiles).to.equal(0)
127 expect(stat.totalVideos).to.equal(0)
130 async function check2Webseeds (strategy: VideoRedundancyStrategy, videoUUID?: string) {
131 if (!videoUUID) videoUUID = video1Server2UUID
134 'http://localhost:9001/static/webseed/' + videoUUID,
135 'http://localhost:9002/static/webseed/' + videoUUID
138 for (const server of servers) {
140 const res = await getVideo(server.url, videoUUID)
142 const video: VideoDetails = res.body
144 for (const file of video.files) {
145 checkMagnetWebseeds(file, webseeds, server)
150 for (const url of [ 'http://localhost:9001', 'http://localhost:9002' ]) {
151 await makeGetRequest({
153 statusCodeExpected: 200,
154 path: '/static/webseed/' + videoUUID,
159 for (const directory of [ 'test1', 'test2' ]) {
160 const files = await readdir(join(root(), directory, 'videos'))
161 expect(files).to.have.length.at.least(4)
163 for (const resolution of [ 240, 360, 480, 720 ]) {
164 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
169 async function enableRedundancyOnServer1 () {
170 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
172 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
173 const follows: ActorFollow[] = res.body.data
174 const server2 = follows.find(f => f.following.host === 'localhost:9002')
175 const server3 = follows.find(f => f.following.host === 'localhost:9003')
177 expect(server3).to.not.be.undefined
178 expect(server3.following.hostRedundancyAllowed).to.be.false
180 expect(server2).to.not.be.undefined
181 expect(server2.following.hostRedundancyAllowed).to.be.true
184 async function cleanServers () {
185 killallServers(servers)
188 describe('Test videos redundancy', function () {
190 describe('With most-views strategy', function () {
191 const strategy = 'most-views'
196 return runServers(strategy)
199 it('Should have 1 webseed on the first video', async function () {
200 await check1WebSeed(strategy)
201 await checkStatsWith1Webseed(strategy)
204 it('Should enable redundancy on server 1', function () {
205 return enableRedundancyOnServer1()
208 it('Should have 2 webseed on the first video', async function () {
211 await waitJobs(servers)
213 await waitJobs(servers)
215 await check2Webseeds(strategy)
216 await checkStatsWith2Webseed(strategy)
220 return cleanServers()
224 describe('With trending strategy', function () {
225 const strategy = 'trending'
230 return runServers(strategy)
233 it('Should have 1 webseed on the first video', async function () {
234 await check1WebSeed(strategy)
235 await checkStatsWith1Webseed(strategy)
238 it('Should enable redundancy on server 1', function () {
239 return enableRedundancyOnServer1()
242 it('Should have 2 webseed on the first video', async function () {
245 await waitJobs(servers)
247 await waitJobs(servers)
249 await check2Webseeds(strategy)
250 await checkStatsWith2Webseed(strategy)
254 return cleanServers()
258 describe('With recently added strategy', function () {
259 const strategy = 'recently-added'
264 return runServers(strategy, { min_views: 3 })
267 it('Should have 1 webseed on the first video', async function () {
268 await check1WebSeed(strategy)
269 await checkStatsWith1Webseed(strategy)
272 it('Should enable redundancy on server 1', function () {
273 return enableRedundancyOnServer1()
276 it('Should still have 1 webseed on the first video', async function () {
279 await waitJobs(servers)
281 await waitJobs(servers)
283 await check1WebSeed(strategy)
284 await checkStatsWith1Webseed(strategy)
287 it('Should view 2 times the first video to have > min_views config', async function () {
290 await viewVideo(servers[ 0 ].url, video1Server2UUID)
291 await viewVideo(servers[ 2 ].url, video1Server2UUID)
294 await waitJobs(servers)
297 it('Should have 2 webseed on the first video', async function () {
300 await waitJobs(servers)
302 await waitJobs(servers)
304 await check2Webseeds(strategy)
305 await checkStatsWith2Webseed(strategy)
309 return cleanServers()
313 describe('Test expiration', function () {
314 const strategy = 'recently-added'
316 async function checkContains (servers: ServerInfo[], str: string) {
317 for (const server of servers) {
318 const res = await getVideo(server.url, video1Server2UUID)
319 const video: VideoDetails = res.body
321 for (const f of video.files) {
322 expect(f.magnetUri).to.contain(str)
327 async function checkNotContains (servers: ServerInfo[], str: string) {
328 for (const server of servers) {
329 const res = await getVideo(server.url, video1Server2UUID)
330 const video: VideoDetails = res.body
332 for (const f of video.files) {
333 expect(f.magnetUri).to.not.contain(str)
338 before(async function () {
341 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
343 await enableRedundancyOnServer1()
346 it('Should still have 2 webseeds after 10 seconds', async function () {
352 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
354 // Maybe a server deleted a redundancy in the scheduler
357 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
361 it('Should stop server 1 and expire video redundancy', async function () {
364 killallServers([ servers[0] ])
368 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
372 return killallServers([ servers[1], servers[2] ])
376 describe('Test file replacement', function () {
377 let video2Server2UUID: string
378 const strategy = 'recently-added'
380 before(async function () {
383 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
385 await enableRedundancyOnServer1()
387 await waitJobs(servers)
389 await waitJobs(servers)
391 await check2Webseeds(strategy)
392 await checkStatsWith2Webseed(strategy)
394 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
395 video2Server2UUID = res.body.video.uuid
398 it('Should cache video 2 webseed on the first video', async function () {
402 await waitJobs(servers)
406 await check1WebSeed(strategy, video1Server2UUID)
407 await check2Webseeds(strategy, video2Server2UUID)
411 return cleanServers()