1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { VideoDetails } from '../../../../shared/models/videos'
8 flushAndRunMultipleServers,
9 getFollowingListPaginationAndSort,
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 const files = await readdir(join(root(), 'test1', 'videos'))
151 expect(files).to.have.lengthOf(4)
153 for (const resolution of [ 240, 360, 480, 720 ]) {
154 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
158 async function enableRedundancyOnServer1 () {
159 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
161 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
162 const follows: ActorFollow[] = res.body.data
163 const server2 = follows.find(f => f.following.host === 'localhost:9002')
164 const server3 = follows.find(f => f.following.host === 'localhost:9003')
166 expect(server3).to.not.be.undefined
167 expect(server3.following.hostRedundancyAllowed).to.be.false
169 expect(server2).to.not.be.undefined
170 expect(server2.following.hostRedundancyAllowed).to.be.true
173 async function cleanServers () {
174 killallServers(servers)
177 describe('Test videos redundancy', function () {
179 describe('With most-views strategy', function () {
180 const strategy = 'most-views'
185 return runServers(strategy)
188 it('Should have 1 webseed on the first video', async function () {
189 await check1WebSeed(strategy)
190 await checkStatsWith1Webseed(strategy)
193 it('Should enable redundancy on server 1', function () {
194 return enableRedundancyOnServer1()
197 it('Should have 2 webseed on the first video', async function () {
200 await waitJobs(servers)
202 await waitJobs(servers)
204 await check2Webseeds(strategy)
205 await checkStatsWith2Webseed(strategy)
209 return cleanServers()
213 describe('With trending strategy', function () {
214 const strategy = 'trending'
219 return runServers(strategy)
222 it('Should have 1 webseed on the first video', async function () {
223 await check1WebSeed(strategy)
224 await checkStatsWith1Webseed(strategy)
227 it('Should enable redundancy on server 1', function () {
228 return enableRedundancyOnServer1()
231 it('Should have 2 webseed on the first video', async function () {
234 await waitJobs(servers)
236 await waitJobs(servers)
238 await check2Webseeds(strategy)
239 await checkStatsWith2Webseed(strategy)
243 return cleanServers()
247 describe('With recently added strategy', function () {
248 const strategy = 'recently-added'
253 return runServers(strategy, { min_views: 3 })
256 it('Should have 1 webseed on the first video', async function () {
257 await check1WebSeed(strategy)
258 await checkStatsWith1Webseed(strategy)
261 it('Should enable redundancy on server 1', function () {
262 return enableRedundancyOnServer1()
265 it('Should still have 1 webseed on the first video', async function () {
268 await waitJobs(servers)
270 await waitJobs(servers)
272 await check1WebSeed(strategy)
273 await checkStatsWith1Webseed(strategy)
276 it('Should view 2 times the first video to have > min_views config', async function () {
279 await viewVideo(servers[ 0 ].url, video1Server2UUID)
280 await viewVideo(servers[ 2 ].url, video1Server2UUID)
283 await waitJobs(servers)
286 it('Should have 2 webseed on the first video', async function () {
289 await waitJobs(servers)
291 await waitJobs(servers)
293 await check2Webseeds(strategy)
294 await checkStatsWith2Webseed(strategy)
298 return cleanServers()
302 describe('Test expiration', function () {
303 const strategy = 'recently-added'
305 async function checkContains (servers: ServerInfo[], str: string) {
306 for (const server of servers) {
307 const res = await getVideo(server.url, video1Server2UUID)
308 const video: VideoDetails = res.body
310 for (const f of video.files) {
311 expect(f.magnetUri).to.contain(str)
316 async function checkNotContains (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.not.contain(str)
327 before(async function () {
330 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
332 await enableRedundancyOnServer1()
335 it('Should still have 2 webseeds after 10 seconds', async function () {
341 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
343 // Maybe a server deleted a redundancy in the scheduler
346 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
350 it('Should stop server 1 and expire video redundancy', async function () {
353 killallServers([ servers[0] ])
357 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
361 return killallServers([ servers[1], servers[2] ])
365 describe('Test file replacement', function () {
366 let video2Server2UUID: string
367 const strategy = 'recently-added'
369 before(async function () {
372 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
374 await enableRedundancyOnServer1()
376 await waitJobs(servers)
378 await waitJobs(servers)
380 await check2Webseeds(strategy)
381 await checkStatsWith2Webseed(strategy)
383 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
384 video2Server2UUID = res.body.video.uuid
387 it('Should cache video 2 webseed on the first video', async function () {
391 await waitJobs(servers)
395 await check1WebSeed(strategy, video1Server2UUID)
396 await check2Webseeds(strategy, video2Server2UUID)
400 return cleanServers()