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
34 let video2Server2UUID: string
36 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[]) {
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}`).to.not.be.undefined
45 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
49 check_interval: '5 seconds',
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)
72 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
73 video2Server2UUID = res.body.video.uuid
76 await waitJobs(servers)
78 // Server 1 and server 2 follow each other
79 await doubleFollow(servers[ 0 ], servers[ 1 ])
80 // Server 1 and server 3 follow each other
81 await doubleFollow(servers[ 0 ], servers[ 2 ])
82 // Server 2 and server 3 follow each other
83 await doubleFollow(servers[ 1 ], servers[ 2 ])
85 await waitJobs(servers)
88 async function check1WebSeed (strategy: VideoRedundancyStrategy) {
90 'http://localhost:9002/static/webseed/' + video1Server2UUID
93 for (const server of servers) {
95 const res = await getVideo(server.url, video1Server2UUID)
97 const video: VideoDetails = res.body
98 video.files.forEach(f => checkMagnetWebseeds(f, webseeds))
102 const res = await getStats(server.url)
103 const data: ServerStats = res.body
105 expect(data.videosRedundancy).to.have.lengthOf(1)
107 const stat = data.videosRedundancy[0]
108 expect(stat.strategy).to.equal(strategy)
109 expect(stat.totalSize).to.equal(102400)
110 expect(stat.totalUsed).to.equal(0)
111 expect(stat.totalVideoFiles).to.equal(0)
112 expect(stat.totalVideos).to.equal(0)
117 async function enableRedundancy () {
118 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
120 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
121 const follows: ActorFollow[] = res.body.data
122 const server2 = follows.find(f => f.following.host === 'localhost:9002')
123 const server3 = follows.find(f => f.following.host === 'localhost:9003')
125 expect(server3).to.not.be.undefined
126 expect(server3.following.hostRedundancyAllowed).to.be.false
128 expect(server2).to.not.be.undefined
129 expect(server2.following.hostRedundancyAllowed).to.be.true
132 async function check2Webseeds (strategy: VideoRedundancyStrategy) {
133 await waitJobs(servers)
135 await waitJobs(servers)
138 'http://localhost:9001/static/webseed/' + video1Server2UUID,
139 'http://localhost:9002/static/webseed/' + video1Server2UUID
142 for (const server of servers) {
144 const res = await getVideo(server.url, video1Server2UUID)
146 const video: VideoDetails = res.body
148 for (const file of video.files) {
149 checkMagnetWebseeds(file, webseeds)
154 const files = await readdir(join(root(), 'test1', 'videos'))
155 expect(files).to.have.lengthOf(4)
157 for (const resolution of [ 240, 360, 480, 720 ]) {
158 expect(files.find(f => f === `${video1Server2UUID}-${resolution}.mp4`)).to.not.be.undefined
162 const res = await getStats(servers[0].url)
163 const data: ServerStats = res.body
165 expect(data.videosRedundancy).to.have.lengthOf(1)
166 const stat = data.videosRedundancy[0]
168 expect(stat.strategy).to.equal(strategy)
169 expect(stat.totalSize).to.equal(102400)
170 expect(stat.totalUsed).to.be.at.least(1).and.below(102401)
171 expect(stat.totalVideoFiles).to.equal(4)
172 expect(stat.totalVideos).to.equal(1)
176 async function cleanServers () {
177 killallServers(servers)
180 describe('Test videos redundancy', function () {
182 describe('With most-views strategy', function () {
183 const strategy = 'most-views'
188 return runServers(strategy)
191 it('Should have 1 webseed on the first video', function () {
192 return check1WebSeed(strategy)
195 it('Should enable redundancy on server 1', function () {
196 return enableRedundancy()
199 it('Should have 2 webseed on the first video', function () {
202 return check2Webseeds(strategy)
206 return cleanServers()
210 describe('With trending strategy', function () {
211 const strategy = 'trending'
216 return runServers(strategy)
219 it('Should have 1 webseed on the first video', function () {
220 return check1WebSeed(strategy)
223 it('Should enable redundancy on server 1', function () {
224 return enableRedundancy()
227 it('Should have 2 webseed on the first video', function () {
230 return check2Webseeds(strategy)
234 return cleanServers()
238 describe('With recently added strategy', function () {
239 const strategy = 'recently-added'
244 return runServers(strategy, { minViews: 3 })
247 it('Should have 1 webseed on the first video', function () {
248 return check1WebSeed(strategy)
251 it('Should enable redundancy on server 1', function () {
252 return enableRedundancy()
255 it('Should still have 1 webseed on the first video', async function () {
258 await waitJobs(servers)
260 await waitJobs(servers)
262 return check1WebSeed(strategy)
265 it('Should view 2 times the first video', async function () {
268 await viewVideo(servers[ 0 ].url, video1Server2UUID)
269 await viewVideo(servers[ 2 ].url, video1Server2UUID)
272 await waitJobs(servers)
275 it('Should have 2 webseed on the first video', function () {
278 return check2Webseeds(strategy)
282 return cleanServers()