]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/redundancy.ts
Add trending videos strategy
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / redundancy.ts
CommitLineData
c48e82b5
C
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import { VideoDetails } from '../../../../shared/models/videos'
6import {
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'
19import { waitJobs } from '../../utils/server/jobs'
20import * as magnetUtil from 'magnet-uri'
21import { updateRedundancy } from '../../utils/server/redundancy'
22import { ActorFollow } from '../../../../shared/models/actors'
23import { readdir } from 'fs-extra'
24import { join } from 'path'
b36f41ca 25import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
c48e82b5
C
26
27const expect = chai.expect
28
b36f41ca
C
29let servers: ServerInfo[] = []
30let video1Server2UUID: string
31let video2Server2UUID: string
32
c48e82b5
C
33function 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
b36f41ca
C
42async function runServers (strategy: VideoRedundancyStrategy) {
43 const config = {
44 redundancy: {
45 videos: [
46 {
47 strategy: strategy,
48 size: '100KB'
49 }
50 ]
51 }
52 }
53 servers = await flushAndRunMultipleServers(3, config)
c48e82b5 54
b36f41ca
C
55 // Get the access tokens
56 await setAccessTokensToServers(servers)
c48e82b5 57
b36f41ca
C
58 {
59 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
60 video1Server2UUID = res.body.video.uuid
c48e82b5 61
b36f41ca
C
62 await viewVideo(servers[ 1 ].url, video1Server2UUID)
63 }
c48e82b5 64
b36f41ca
C
65 {
66 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
67 video2Server2UUID = res.body.video.uuid
68 }
c48e82b5 69
b36f41ca 70 await waitJobs(servers)
c48e82b5 71
b36f41ca
C
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}
c48e82b5 81
b36f41ca
C
82async function check1WebSeed () {
83 const webseeds = [
84 'http://localhost:9002/static/webseed/' + video1Server2UUID
85 ]
c48e82b5 86
b36f41ca
C
87 for (const server of servers) {
88 const res = await getVideo(server.url, video1Server2UUID)
c48e82b5 89
b36f41ca
C
90 const video: VideoDetails = res.body
91 video.files.forEach(f => checkMagnetWebseeds(f, webseeds))
92 }
93}
94
95async 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}
c48e82b5 109
b36f41ca
C
110async function check2Webseeds () {
111 await waitJobs(servers)
112 await wait(15000)
113 await waitJobs(servers)
c48e82b5 114
b36f41ca
C
115 const webseeds = [
116 'http://localhost:9001/static/webseed/' + video1Server2UUID,
117 'http://localhost:9002/static/webseed/' + video1Server2UUID
118 ]
c48e82b5 119
b36f41ca
C
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)
c48e82b5 127 }
b36f41ca 128 }
c48e82b5 129
b36f41ca
C
130 const files = await readdir(join(root(), 'test1', 'videos'))
131 expect(files).to.have.lengthOf(4)
c48e82b5 132
b36f41ca
C
133 for (const resolution of [ 240, 360, 480, 720 ]) {
134 expect(files.find(f => f === `${video1Server2UUID}-${resolution}.mp4`)).to.not.be.undefined
135 }
136}
c48e82b5 137
b36f41ca
C
138async function cleanServers () {
139 killallServers(servers)
140}
c48e82b5 141
b36f41ca 142describe('Test videos redundancy', function () {
c48e82b5 143
b36f41ca 144 describe('With most-views strategy', function () {
c48e82b5 145
b36f41ca
C
146 before(function () {
147 this.timeout(120000)
c48e82b5 148
b36f41ca
C
149 return runServers('most-views')
150 })
c48e82b5 151
b36f41ca
C
152 it('Should have 1 webseed on the first video', function () {
153 return check1WebSeed()
154 })
c48e82b5 155
b36f41ca
C
156 it('Should enable redundancy on server 1', async function () {
157 return enableRedundancy()
158 })
c48e82b5 159
b36f41ca
C
160 it('Should have 2 webseed on the first video', async function () {
161 this.timeout(40000)
c48e82b5 162
b36f41ca
C
163 return check2Webseeds()
164 })
c48e82b5 165
b36f41ca
C
166 after(function () {
167 return cleanServers()
168 })
c48e82b5
C
169 })
170
b36f41ca 171 describe('With trending strategy', function () {
c48e82b5 172
b36f41ca
C
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', async function () {
184 return enableRedundancy()
185 })
186
187 it('Should have 2 webseed on the first video', async function () {
188 this.timeout(40000)
189
190 return check2Webseeds()
191 })
192
193 after(function () {
194 return cleanServers()
195 })
c48e82b5
C
196 })
197})