1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { VideoDetails } from '../../../../shared/models/videos'
8 flushAndRunMultipleServers,
10 getFollowingListPaginationAndSort,
14 setAccessTokensToServers,
17 root, viewVideo, immutableAssign
19 import { waitJobs } from '../../utils/server/jobs'
20 import * as magnetUtil from 'magnet-uri'
21 import { updateRedundancy } from '../../utils/server/redundancy'
22 import { ActorFollow } from '../../../../shared/models/actors'
23 import { readdir } from 'fs-extra'
24 import { join } from 'path'
25 import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
26 import { getStats } from '../../utils/server/stats'
27 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
29 const expect = chai.expect
31 let servers: ServerInfo[] = []
32 let video1Server2UUID: string
33 let video2Server2UUID: string
35 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[]) {
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}`).to.not.be.undefined
44 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
55 servers = await flushAndRunMultipleServers(3, config)
57 // Get the access tokens
58 await setAccessTokensToServers(servers)
61 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
62 video1Server2UUID = res.body.video.uuid
64 await viewVideo(servers[ 1 ].url, video1Server2UUID)
68 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
69 video2Server2UUID = res.body.video.uuid
72 await waitJobs(servers)
74 // Server 1 and server 2 follow each other
75 await doubleFollow(servers[ 0 ], servers[ 1 ])
76 // Server 1 and server 3 follow each other
77 await doubleFollow(servers[ 0 ], servers[ 2 ])
78 // Server 2 and server 3 follow each other
79 await doubleFollow(servers[ 1 ], servers[ 2 ])
81 await waitJobs(servers)
84 async function check1WebSeed (strategy: VideoRedundancyStrategy) {
86 'http://localhost:9002/static/webseed/' + video1Server2UUID
89 for (const server of servers) {
91 const res = await getVideo(server.url, video1Server2UUID)
93 const video: VideoDetails = res.body
94 video.files.forEach(f => checkMagnetWebseeds(f, webseeds))
98 const res = await getStats(server.url)
99 const data: ServerStats = res.body
101 expect(data.videosRedundancy).to.have.lengthOf(1)
103 const stat = data.videosRedundancy[0]
104 expect(stat.strategy).to.equal(strategy)
105 expect(stat.totalSize).to.equal(102400)
106 expect(stat.totalUsed).to.equal(0)
107 expect(stat.totalVideoFiles).to.equal(0)
108 expect(stat.totalVideos).to.equal(0)
113 async function enableRedundancy () {
114 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
116 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
117 const follows: ActorFollow[] = res.body.data
118 const server2 = follows.find(f => f.following.host === 'localhost:9002')
119 const server3 = follows.find(f => f.following.host === 'localhost:9003')
121 expect(server3).to.not.be.undefined
122 expect(server3.following.hostRedundancyAllowed).to.be.false
124 expect(server2).to.not.be.undefined
125 expect(server2.following.hostRedundancyAllowed).to.be.true
128 async function check2Webseeds (strategy: VideoRedundancyStrategy) {
129 await waitJobs(servers)
131 await waitJobs(servers)
134 'http://localhost:9001/static/webseed/' + video1Server2UUID,
135 'http://localhost:9002/static/webseed/' + video1Server2UUID
138 for (const server of servers) {
140 const res = await getVideo(server.url, video1Server2UUID)
142 const video: VideoDetails = res.body
144 for (const file of video.files) {
145 checkMagnetWebseeds(file, webseeds)
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 === `${video1Server2UUID}-${resolution}.mp4`)).to.not.be.undefined
158 const res = await getStats(servers[0].url)
159 const data: ServerStats = res.body
161 expect(data.videosRedundancy).to.have.lengthOf(1)
162 const stat = data.videosRedundancy[0]
164 expect(stat.strategy).to.equal(strategy)
165 expect(stat.totalSize).to.equal(102400)
166 expect(stat.totalUsed).to.be.at.least(1).and.below(102401)
167 expect(stat.totalVideoFiles).to.equal(4)
168 expect(stat.totalVideos).to.equal(1)
172 async function cleanServers () {
173 killallServers(servers)
176 describe('Test videos redundancy', function () {
178 describe('With most-views strategy', function () {
179 const strategy = 'most-views'
184 return runServers(strategy)
187 it('Should have 1 webseed on the first video', function () {
188 return check1WebSeed(strategy)
191 it('Should enable redundancy on server 1', function () {
192 return enableRedundancy()
195 it('Should have 2 webseed on the first video', function () {
198 return check2Webseeds(strategy)
202 return cleanServers()
206 describe('With trending strategy', function () {
207 const strategy = 'trending'
212 return runServers(strategy)
215 it('Should have 1 webseed on the first video', function () {
216 return check1WebSeed(strategy)
219 it('Should enable redundancy on server 1', function () {
220 return enableRedundancy()
223 it('Should have 2 webseed on the first video', function () {
226 return check2Webseeds(strategy)
230 return cleanServers()
234 describe('With recently added strategy', function () {
235 const strategy = 'recently-added'
240 return runServers(strategy, { minViews: 3 })
243 it('Should have 1 webseed on the first video', function () {
244 return check1WebSeed(strategy)
247 it('Should enable redundancy on server 1', function () {
248 return enableRedundancy()
251 it('Should still have 1 webseed on the first video', async function () {
254 await waitJobs(servers)
256 await waitJobs(servers)
258 return check1WebSeed(strategy)
261 it('Should view 2 times the first video', async function () {
264 await viewVideo(servers[ 0 ].url, video1Server2UUID)
265 await viewVideo(servers[ 2 ].url, video1Server2UUID)
268 await waitJobs(servers)
271 it('Should have 2 webseed on the first video', function () {
274 return check2Webseeds(strategy)
278 return cleanServers()