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)
147 // Only servers 1 and 2 have the video
148 if (server.serverNumber !== 3) {
149 await makeGetRequest({
151 statusCodeExpected: 200,
152 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 cleanServers () {
186 killallServers(servers)
189 describe('Test videos redundancy', function () {
191 describe('With most-views strategy', function () {
192 const strategy = 'most-views'
197 return runServers(strategy)
200 it('Should have 1 webseed on the first video', async function () {
201 await check1WebSeed(strategy)
202 await checkStatsWith1Webseed(strategy)
205 it('Should enable redundancy on server 1', function () {
206 return enableRedundancyOnServer1()
209 it('Should have 2 webseed on the first video', async function () {
212 await waitJobs(servers)
214 await waitJobs(servers)
216 await check2Webseeds(strategy)
217 await checkStatsWith2Webseed(strategy)
221 return cleanServers()
225 describe('With trending strategy', function () {
226 const strategy = 'trending'
231 return runServers(strategy)
234 it('Should have 1 webseed on the first video', async function () {
235 await check1WebSeed(strategy)
236 await checkStatsWith1Webseed(strategy)
239 it('Should enable redundancy on server 1', function () {
240 return enableRedundancyOnServer1()
243 it('Should have 2 webseed on the first video', async function () {
246 await waitJobs(servers)
248 await waitJobs(servers)
250 await check2Webseeds(strategy)
251 await checkStatsWith2Webseed(strategy)
255 return cleanServers()
259 describe('With recently added strategy', function () {
260 const strategy = 'recently-added'
265 return runServers(strategy, { min_views: 3 })
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 still have 1 webseed on the first video', async function () {
280 await waitJobs(servers)
282 await waitJobs(servers)
284 await check1WebSeed(strategy)
285 await checkStatsWith1Webseed(strategy)
288 it('Should view 2 times the first video to have > min_views config', async function () {
291 await viewVideo(servers[ 0 ].url, video1Server2UUID)
292 await viewVideo(servers[ 2 ].url, video1Server2UUID)
295 await waitJobs(servers)
298 it('Should have 2 webseed on the first video', async function () {
301 await waitJobs(servers)
303 await waitJobs(servers)
305 await check2Webseeds(strategy)
306 await checkStatsWith2Webseed(strategy)
310 return cleanServers()
314 describe('Test expiration', function () {
315 const strategy = 'recently-added'
317 async function checkContains (servers: ServerInfo[], str: string) {
318 for (const server of servers) {
319 const res = await getVideo(server.url, video1Server2UUID)
320 const video: VideoDetails = res.body
322 for (const f of video.files) {
323 expect(f.magnetUri).to.contain(str)
328 async function checkNotContains (servers: ServerInfo[], str: string) {
329 for (const server of servers) {
330 const res = await getVideo(server.url, video1Server2UUID)
331 const video: VideoDetails = res.body
333 for (const f of video.files) {
334 expect(f.magnetUri).to.not.contain(str)
339 before(async function () {
342 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
344 await enableRedundancyOnServer1()
347 it('Should still have 2 webseeds after 10 seconds', async function () {
353 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
355 // Maybe a server deleted a redundancy in the scheduler
358 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
362 it('Should stop server 1 and expire video redundancy', async function () {
365 killallServers([ servers[0] ])
369 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
373 return killallServers([ servers[1], servers[2] ])
377 describe('Test file replacement', function () {
378 let video2Server2UUID: string
379 const strategy = 'recently-added'
381 before(async function () {
384 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
386 await enableRedundancyOnServer1()
388 await waitJobs(servers)
390 await waitJobs(servers)
392 await check2Webseeds(strategy)
393 await checkStatsWith2Webseed(strategy)
395 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
396 video2Server2UUID = res.body.video.uuid
399 it('Should cache video 2 webseed on the first video', async function () {
403 await waitJobs(servers)
407 await check1WebSeed(strategy, video1Server2UUID)
408 await check2Webseeds(strategy, video2Server2UUID)
412 return cleanServers()