]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/redundancy.ts
6574e8ea9dbb732b7451b5fa72cc6cae38c6935c
[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, immutableAssign
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 import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
26
27 const expect = chai.expect
28
29 let servers: ServerInfo[] = []
30 let video1Server2UUID: string
31 let video2Server2UUID: string
32
33 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[]) {
34 const parsed = magnetUtil.decode(file.magnetUri)
35
36 for (const ws of baseWebseeds) {
37 const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
38 expect(found, `Webseed ${ws} not found in ${file.magnetUri}`).to.not.be.undefined
39 }
40 }
41
42 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
43 const config = {
44 redundancy: {
45 videos: [
46 immutableAssign({
47 strategy: strategy,
48 size: '100KB'
49 }, additionalParams)
50 ]
51 }
52 }
53 servers = await flushAndRunMultipleServers(3, config)
54
55 // Get the access tokens
56 await setAccessTokensToServers(servers)
57
58 {
59 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
60 video1Server2UUID = res.body.video.uuid
61
62 await viewVideo(servers[ 1 ].url, video1Server2UUID)
63 }
64
65 {
66 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
67 video2Server2UUID = res.body.video.uuid
68 }
69
70 await waitJobs(servers)
71
72 // Server 1 and server 2 follow each other
73 await doubleFollow(servers[ 0 ], servers[ 1 ])
74 // Server 1 and server 3 follow each other
75 await doubleFollow(servers[ 0 ], servers[ 2 ])
76 // Server 2 and server 3 follow each other
77 await doubleFollow(servers[ 1 ], servers[ 2 ])
78
79 await waitJobs(servers)
80 }
81
82 async function check1WebSeed () {
83 const webseeds = [
84 'http://localhost:9002/static/webseed/' + video1Server2UUID
85 ]
86
87 for (const server of servers) {
88 const res = await getVideo(server.url, video1Server2UUID)
89
90 const video: VideoDetails = res.body
91 video.files.forEach(f => checkMagnetWebseeds(f, webseeds))
92 }
93 }
94
95 async function enableRedundancy () {
96 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
97
98 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
99 const follows: ActorFollow[] = res.body.data
100 const server2 = follows.find(f => f.following.host === 'localhost:9002')
101 const server3 = follows.find(f => f.following.host === 'localhost:9003')
102
103 expect(server3).to.not.be.undefined
104 expect(server3.following.hostRedundancyAllowed).to.be.false
105
106 expect(server2).to.not.be.undefined
107 expect(server2.following.hostRedundancyAllowed).to.be.true
108 }
109
110 async function check2Webseeds () {
111 await waitJobs(servers)
112 await wait(15000)
113 await waitJobs(servers)
114
115 const webseeds = [
116 'http://localhost:9001/static/webseed/' + video1Server2UUID,
117 'http://localhost:9002/static/webseed/' + video1Server2UUID
118 ]
119
120 for (const server of servers) {
121 const res = await getVideo(server.url, video1Server2UUID)
122
123 const video: VideoDetails = res.body
124
125 for (const file of video.files) {
126 checkMagnetWebseeds(file, webseeds)
127 }
128 }
129
130 const files = await readdir(join(root(), 'test1', 'videos'))
131 expect(files).to.have.lengthOf(4)
132
133 for (const resolution of [ 240, 360, 480, 720 ]) {
134 expect(files.find(f => f === `${video1Server2UUID}-${resolution}.mp4`)).to.not.be.undefined
135 }
136 }
137
138 async function cleanServers () {
139 killallServers(servers)
140 }
141
142 describe('Test videos redundancy', function () {
143
144 describe('With most-views strategy', function () {
145
146 before(function () {
147 this.timeout(120000)
148
149 return runServers('most-views')
150 })
151
152 it('Should have 1 webseed on the first video', function () {
153 return check1WebSeed()
154 })
155
156 it('Should enable redundancy on server 1', function () {
157 return enableRedundancy()
158 })
159
160 it('Should have 2 webseed on the first video', function () {
161 this.timeout(40000)
162
163 return check2Webseeds()
164 })
165
166 after(function () {
167 return cleanServers()
168 })
169 })
170
171 describe('With trending strategy', function () {
172
173 before(function () {
174 this.timeout(120000)
175
176 return runServers('trending')
177 })
178
179 it('Should have 1 webseed on the first video', function () {
180 return check1WebSeed()
181 })
182
183 it('Should enable redundancy on server 1', function () {
184 return enableRedundancy()
185 })
186
187 it('Should have 2 webseed on the first video', function () {
188 this.timeout(40000)
189
190 return check2Webseeds()
191 })
192
193 after(function () {
194 return cleanServers()
195 })
196 })
197
198 describe('With recently added strategy', function () {
199
200 before(function () {
201 this.timeout(120000)
202
203 return runServers('recently-added', { minViews: 3 })
204 })
205
206 it('Should have 1 webseed on the first video', function () {
207 return check1WebSeed()
208 })
209
210 it('Should enable redundancy on server 1', function () {
211 return enableRedundancy()
212 })
213
214 it('Should still have 1 webseed on the first video', async function () {
215 this.timeout(40000)
216
217 await waitJobs(servers)
218 await wait(15000)
219 await waitJobs(servers)
220
221 return check1WebSeed()
222 })
223
224 it('Should view 2 times the first video', async function () {
225 this.timeout(40000)
226
227 await viewVideo(servers[ 0 ].url, video1Server2UUID)
228 await viewVideo(servers[ 2 ].url, video1Server2UUID)
229
230 await wait(10000)
231 await waitJobs(servers)
232 })
233
234 it('Should have 2 webseed on the first video', function () {
235 this.timeout(40000)
236
237 return check2Webseeds()
238 })
239
240 after(function () {
241 return cleanServers()
242 })
243 })
244 })