]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/redundancy.ts
Basic video redundancy implementation
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / redundancy.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoDetails } from '../../../../shared/models/videos'
6 import {
7 doubleFollow,
8 flushAndRunMultipleServers,
9 flushTests,
10 getFollowingListPaginationAndSort,
11 getVideo,
12 killallServers,
13 ServerInfo,
14 setAccessTokensToServers,
15 uploadVideo,
16 wait,
17 root, viewVideo
18 } from '../../utils'
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
26 const expect = chai.expect
27
28 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[]) {
29 const parsed = magnetUtil.decode(file.magnetUri)
30
31 for (const ws of baseWebseeds) {
32 const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
33 expect(found, `Webseed ${ws} not found in ${file.magnetUri}`).to.not.be.undefined
34 }
35 }
36
37 describe('Test videos redundancy', function () {
38 let servers: ServerInfo[] = []
39 let video1Server2UUID: string
40 let video2Server2UUID: string
41
42 before(async function () {
43 this.timeout(120000)
44
45 servers = await flushAndRunMultipleServers(3)
46
47 // Get the access tokens
48 await setAccessTokensToServers(servers)
49
50 {
51 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
52 video1Server2UUID = res.body.video.uuid
53
54 await viewVideo(servers[1].url, video1Server2UUID)
55 }
56
57 {
58 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
59 video2Server2UUID = res.body.video.uuid
60 }
61
62 await waitJobs(servers)
63
64 // Server 1 and server 2 follow each other
65 await doubleFollow(servers[0], servers[1])
66 // Server 1 and server 3 follow each other
67 await doubleFollow(servers[0], servers[2])
68 // Server 2 and server 3 follow each other
69 await doubleFollow(servers[1], servers[2])
70
71 await waitJobs(servers)
72 })
73
74 it('Should have 1 webseed on the first video', async function () {
75 const webseeds = [
76 'http://localhost:9002/static/webseed/' + video1Server2UUID
77 ]
78
79 for (const server of servers) {
80 const res = await getVideo(server.url, video1Server2UUID)
81
82 const video: VideoDetails = res.body
83 video.files.forEach(f => checkMagnetWebseeds(f, webseeds))
84 }
85 })
86
87 it('Should enable redundancy on server 1', async function () {
88 await updateRedundancy(servers[0].url, servers[0].accessToken, servers[1].host, true)
89
90 const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 5, '-createdAt')
91 const follows: ActorFollow[] = res.body.data
92 const server2 = follows.find(f => f.following.host === 'localhost:9002')
93 const server3 = follows.find(f => f.following.host === 'localhost:9003')
94
95 expect(server3).to.not.be.undefined
96 expect(server3.following.hostRedundancyAllowed).to.be.false
97
98 expect(server2).to.not.be.undefined
99 expect(server2.following.hostRedundancyAllowed).to.be.true
100 })
101
102 it('Should have 2 webseed on the first video', async function () {
103 this.timeout(40000)
104
105 await waitJobs(servers)
106 await wait(15000)
107 await waitJobs(servers)
108
109 const webseeds = [
110 'http://localhost:9001/static/webseed/' + video1Server2UUID,
111 'http://localhost:9002/static/webseed/' + video1Server2UUID
112 ]
113
114 for (const server of servers) {
115 const res = await getVideo(server.url, video1Server2UUID)
116
117 const video: VideoDetails = res.body
118
119 for (const file of video.files) {
120 checkMagnetWebseeds(file, webseeds)
121 }
122 }
123
124 const files = await readdir(join(root(), 'test1', 'videos'))
125 expect(files).to.have.lengthOf(4)
126
127 for (const resolution of [ 240, 360, 480, 720 ]) {
128 expect(files.find(f => f === `${video1Server2UUID}-${resolution}.mp4`)).to.not.be.undefined
129 }
130 })
131
132 after(async function () {
133 killallServers(servers)
134
135 // Keep the logs if the test failed
136 if (this['ok']) {
137 await flushTests()
138 }
139 })
140 })