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, unfollow,
20 checkVideoFilesWereRemoved, removeVideo
21 } from '../../../../shared/utils'
22 import { waitJobs } from '../../../../shared/utils/server/jobs'
23 import * as magnetUtil from 'magnet-uri'
24 import { updateRedundancy } from '../../../../shared/utils/server/redundancy'
25 import { ActorFollow } from '../../../../shared/models/actors'
26 import { readdir } from 'fs-extra'
27 import { join } from 'path'
28 import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
29 import { getStats } from '../../../../shared/utils/server/stats'
30 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
32 const expect = chai.expect
34 let servers: ServerInfo[] = []
35 let video1Server2UUID: string
37 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[], server: ServerInfo) {
38 const parsed = magnetUtil.decode(file.magnetUri)
40 for (const ws of baseWebseeds) {
41 const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
42 expect(found, `Webseed ${ws} not found in ${file.magnetUri} on server ${server.url}`).to.not.be.undefined
45 expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
48 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
52 check_interval: '5 seconds',
55 min_lifetime: '1 hour',
63 servers = await flushAndRunMultipleServers(3, config)
65 // Get the access tokens
66 await setAccessTokensToServers(servers)
69 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
70 video1Server2UUID = res.body.video.uuid
72 await viewVideo(servers[ 1 ].url, video1Server2UUID)
75 await waitJobs(servers)
77 // Server 1 and server 2 follow each other
78 await doubleFollow(servers[ 0 ], servers[ 1 ])
79 // Server 1 and server 3 follow each other
80 await doubleFollow(servers[ 0 ], servers[ 2 ])
81 // Server 2 and server 3 follow each other
82 await doubleFollow(servers[ 1 ], servers[ 2 ])
84 await waitJobs(servers)
87 async function check1WebSeed (strategy: VideoRedundancyStrategy, videoUUID?: string) {
88 if (!videoUUID) videoUUID = video1Server2UUID
91 'http://localhost:9002/static/webseed/' + videoUUID
94 for (const server of servers) {
96 const res = await getVideo(server.url, videoUUID)
98 const video: VideoDetails = res.body
99 for (const f of video.files) {
100 checkMagnetWebseeds(f, webseeds, server)
106 async function checkStatsWith2Webseed (strategy: VideoRedundancyStrategy) {
107 const res = await getStats(servers[0].url)
108 const data: ServerStats = res.body
110 expect(data.videosRedundancy).to.have.lengthOf(1)
111 const stat = data.videosRedundancy[0]
113 expect(stat.strategy).to.equal(strategy)
114 expect(stat.totalSize).to.equal(102400)
115 expect(stat.totalUsed).to.be.at.least(1).and.below(102401)
116 expect(stat.totalVideoFiles).to.equal(4)
117 expect(stat.totalVideos).to.equal(1)
120 async function checkStatsWith1Webseed (strategy: VideoRedundancyStrategy) {
121 const res = await getStats(servers[0].url)
122 const data: ServerStats = res.body
124 expect(data.videosRedundancy).to.have.lengthOf(1)
126 const stat = data.videosRedundancy[0]
127 expect(stat.strategy).to.equal(strategy)
128 expect(stat.totalSize).to.equal(102400)
129 expect(stat.totalUsed).to.equal(0)
130 expect(stat.totalVideoFiles).to.equal(0)
131 expect(stat.totalVideos).to.equal(0)
134 async function check2Webseeds (strategy: VideoRedundancyStrategy, videoUUID?: string) {
135 if (!videoUUID) videoUUID = video1Server2UUID
138 'http://localhost:9001/static/webseed/' + videoUUID,
139 'http://localhost:9002/static/webseed/' + videoUUID
142 for (const server of servers) {
143 const res = await getVideo(server.url, videoUUID)
145 const video: VideoDetails = res.body
147 for (const file of video.files) {
148 checkMagnetWebseeds(file, webseeds, server)
150 // Only servers 1 and 2 have the video
151 if (server.serverNumber !== 3) {
152 await makeGetRequest({
154 statusCodeExpected: 200,
155 path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
162 for (const directory of [ 'test1', 'test2' ]) {
163 const files = await readdir(join(root(), directory, 'videos'))
164 expect(files).to.have.length.at.least(4)
166 for (const resolution of [ 240, 360, 480, 720 ]) {
167 expect(files.find(f => f === `${videoUUID}-${resolution}.mp4`)).to.not.be.undefined
172 async function enableRedundancyOnServer1 () {
173 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
175 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
176 const follows: ActorFollow[] = res.body.data
177 const server2 = follows.find(f => f.following.host === 'localhost:9002')
178 const server3 = follows.find(f => f.following.host === 'localhost:9003')
180 expect(server3).to.not.be.undefined
181 expect(server3.following.hostRedundancyAllowed).to.be.false
183 expect(server2).to.not.be.undefined
184 expect(server2.following.hostRedundancyAllowed).to.be.true
187 async function disableRedundancyOnServer1 () {
188 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
190 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
191 const follows: ActorFollow[] = res.body.data
192 const server2 = follows.find(f => f.following.host === 'localhost:9002')
193 const server3 = follows.find(f => f.following.host === 'localhost:9003')
195 expect(server3).to.not.be.undefined
196 expect(server3.following.hostRedundancyAllowed).to.be.false
198 expect(server2).to.not.be.undefined
199 expect(server2.following.hostRedundancyAllowed).to.be.false
202 async function cleanServers () {
203 killallServers(servers)
206 describe('Test videos redundancy', function () {
208 describe('With most-views strategy', function () {
209 const strategy = 'most-views'
214 return runServers(strategy)
217 it('Should have 1 webseed on the first video', async function () {
218 await check1WebSeed(strategy)
219 await checkStatsWith1Webseed(strategy)
222 it('Should enable redundancy on server 1', function () {
223 return enableRedundancyOnServer1()
226 it('Should have 2 webseed on the first video', async function () {
229 await waitJobs(servers)
230 await waitUntilLog(servers[0], 'Duplicated ', 4)
231 await waitJobs(servers)
233 await check2Webseeds(strategy)
234 await checkStatsWith2Webseed(strategy)
237 it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
240 await disableRedundancyOnServer1()
242 await waitJobs(servers)
245 await check1WebSeed(strategy)
247 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
251 return cleanServers()
255 describe('With trending strategy', function () {
256 const strategy = 'trending'
261 return runServers(strategy)
264 it('Should have 1 webseed on the first video', async function () {
265 await check1WebSeed(strategy)
266 await checkStatsWith1Webseed(strategy)
269 it('Should enable redundancy on server 1', function () {
270 return enableRedundancyOnServer1()
273 it('Should have 2 webseed on the first video', async function () {
276 await waitJobs(servers)
277 await waitUntilLog(servers[0], 'Duplicated ', 4)
278 await waitJobs(servers)
280 await check2Webseeds(strategy)
281 await checkStatsWith2Webseed(strategy)
284 it('Should unfollow on server 1 and remove duplicated videos', async function () {
287 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
289 await waitJobs(servers)
292 await check1WebSeed(strategy)
294 await checkVideoFilesWereRemoved(video1Server2UUID, servers[0].serverNumber, [ 'videos' ])
298 return cleanServers()
302 describe('With recently added strategy', function () {
303 const strategy = 'recently-added'
308 return runServers(strategy, { min_views: 3 })
311 it('Should have 1 webseed on the first video', async function () {
312 await check1WebSeed(strategy)
313 await checkStatsWith1Webseed(strategy)
316 it('Should enable redundancy on server 1', function () {
317 return enableRedundancyOnServer1()
320 it('Should still have 1 webseed on the first video', async function () {
323 await waitJobs(servers)
325 await waitJobs(servers)
327 await check1WebSeed(strategy)
328 await checkStatsWith1Webseed(strategy)
331 it('Should view 2 times the first video to have > min_views config', async function () {
334 await viewVideo(servers[ 0 ].url, video1Server2UUID)
335 await viewVideo(servers[ 2 ].url, video1Server2UUID)
338 await waitJobs(servers)
341 it('Should have 2 webseed on the first video', async function () {
344 await waitJobs(servers)
345 await waitUntilLog(servers[0], 'Duplicated ', 4)
346 await waitJobs(servers)
348 await check2Webseeds(strategy)
349 await checkStatsWith2Webseed(strategy)
352 it('Should remove the video and the redundancy files', async function () {
355 await removeVideo(servers[1].url, servers[1].accessToken, video1Server2UUID)
357 await waitJobs(servers)
359 for (const server of servers) {
360 await checkVideoFilesWereRemoved(video1Server2UUID, server.serverNumber)
365 return cleanServers()
369 describe('Test expiration', function () {
370 const strategy = 'recently-added'
372 async function checkContains (servers: ServerInfo[], str: string) {
373 for (const server of servers) {
374 const res = await getVideo(server.url, video1Server2UUID)
375 const video: VideoDetails = res.body
377 for (const f of video.files) {
378 expect(f.magnetUri).to.contain(str)
383 async function checkNotContains (servers: ServerInfo[], str: string) {
384 for (const server of servers) {
385 const res = await getVideo(server.url, video1Server2UUID)
386 const video: VideoDetails = res.body
388 for (const f of video.files) {
389 expect(f.magnetUri).to.not.contain(str)
394 before(async function () {
397 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
399 await enableRedundancyOnServer1()
402 it('Should still have 2 webseeds after 10 seconds', async function () {
408 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
410 // Maybe a server deleted a redundancy in the scheduler
413 await checkContains(servers, 'http%3A%2F%2Flocalhost%3A9001')
417 it('Should stop server 1 and expire video redundancy', async function () {
420 killallServers([ servers[0] ])
424 await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A9001')
428 return killallServers([ servers[1], servers[2] ])
432 describe('Test file replacement', function () {
433 let video2Server2UUID: string
434 const strategy = 'recently-added'
436 before(async function () {
439 await runServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
441 await enableRedundancyOnServer1()
443 await waitJobs(servers)
444 await waitUntilLog(servers[0], 'Duplicated ', 4)
445 await waitJobs(servers)
447 await check2Webseeds(strategy)
448 await checkStatsWith2Webseed(strategy)
450 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
451 video2Server2UUID = res.body.video.uuid
454 it('Should cache video 2 webseed on the first video', async function () {
457 await waitJobs(servers)
461 while (checked === false) {
465 await check1WebSeed(strategy, video1Server2UUID)
466 await check2Webseeds(strategy, video2Server2UUID)
476 return cleanServers()