]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/redundancy/manage-redundancy.ts
Shorter server command names
[github/Chocobozzz/PeerTube.git] / server / tests / api / redundancy / manage-redundancy.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import {
6 cleanupTests,
7 doubleFollow,
8 flushAndRunMultipleServers,
9 RedundancyCommand,
10 ServerInfo,
11 setAccessTokensToServers,
12 waitJobs
13 } from '@shared/extra-utils'
14 import { VideoPrivacy, VideoRedundanciesTarget } from '@shared/models'
15
16 const expect = chai.expect
17
18 describe('Test manage videos redundancy', function () {
19 const targets: VideoRedundanciesTarget[] = [ 'my-videos', 'remote-videos' ]
20
21 let servers: ServerInfo[]
22 let video1Server2UUID: string
23 let video2Server2UUID: string
24 let redundanciesToRemove: number[] = []
25
26 let commands: RedundancyCommand[]
27
28 before(async function () {
29 this.timeout(120000)
30
31 const config = {
32 transcoding: {
33 hls: {
34 enabled: true
35 }
36 },
37 redundancy: {
38 videos: {
39 check_interval: '1 second',
40 strategies: [
41 {
42 strategy: 'recently-added',
43 min_lifetime: '1 hour',
44 size: '10MB',
45 min_views: 0
46 }
47 ]
48 }
49 }
50 }
51 servers = await flushAndRunMultipleServers(3, config)
52
53 // Get the access tokens
54 await setAccessTokensToServers(servers)
55
56 commands = servers.map(s => s.redundancy)
57
58 {
59 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 1 server 2' } })
60 video1Server2UUID = uuid
61 }
62
63 {
64 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 2 server 2' } })
65 video2Server2UUID = uuid
66 }
67
68 await waitJobs(servers)
69
70 // Server 1 and server 2 follow each other
71 await doubleFollow(servers[0], servers[1])
72 await commands[0].updateRedundancy({ host: servers[1].host, redundancyAllowed: true })
73
74 await waitJobs(servers)
75 })
76
77 it('Should not have redundancies on server 3', async function () {
78 for (const target of targets) {
79 const body = await commands[2].listVideos({ target })
80
81 expect(body.total).to.equal(0)
82 expect(body.data).to.have.lengthOf(0)
83 }
84 })
85
86 it('Should not have "remote-videos" redundancies on server 2', async function () {
87 this.timeout(120000)
88
89 await waitJobs(servers)
90 await servers[0].servers.waitUntilLog('Duplicated ', 10)
91 await waitJobs(servers)
92
93 const body = await commands[1].listVideos({ target: 'remote-videos' })
94
95 expect(body.total).to.equal(0)
96 expect(body.data).to.have.lengthOf(0)
97 })
98
99 it('Should have "my-videos" redundancies on server 2', async function () {
100 this.timeout(120000)
101
102 const body = await commands[1].listVideos({ target: 'my-videos' })
103 expect(body.total).to.equal(2)
104
105 const videos = body.data
106 expect(videos).to.have.lengthOf(2)
107
108 const videos1 = videos.find(v => v.uuid === video1Server2UUID)
109 const videos2 = videos.find(v => v.uuid === video2Server2UUID)
110
111 expect(videos1.name).to.equal('video 1 server 2')
112 expect(videos2.name).to.equal('video 2 server 2')
113
114 expect(videos1.redundancies.files).to.have.lengthOf(4)
115 expect(videos1.redundancies.streamingPlaylists).to.have.lengthOf(1)
116
117 const redundancies = videos1.redundancies.files.concat(videos1.redundancies.streamingPlaylists)
118
119 for (const r of redundancies) {
120 expect(r.strategy).to.be.null
121 expect(r.fileUrl).to.exist
122 expect(r.createdAt).to.exist
123 expect(r.updatedAt).to.exist
124 expect(r.expiresOn).to.exist
125 }
126 })
127
128 it('Should not have "my-videos" redundancies on server 1', async function () {
129 const body = await commands[0].listVideos({ target: 'my-videos' })
130
131 expect(body.total).to.equal(0)
132 expect(body.data).to.have.lengthOf(0)
133 })
134
135 it('Should have "remote-videos" redundancies on server 1', async function () {
136 this.timeout(120000)
137
138 const body = await commands[0].listVideos({ target: 'remote-videos' })
139 expect(body.total).to.equal(2)
140
141 const videos = body.data
142 expect(videos).to.have.lengthOf(2)
143
144 const videos1 = videos.find(v => v.uuid === video1Server2UUID)
145 const videos2 = videos.find(v => v.uuid === video2Server2UUID)
146
147 expect(videos1.name).to.equal('video 1 server 2')
148 expect(videos2.name).to.equal('video 2 server 2')
149
150 expect(videos1.redundancies.files).to.have.lengthOf(4)
151 expect(videos1.redundancies.streamingPlaylists).to.have.lengthOf(1)
152
153 const redundancies = videos1.redundancies.files.concat(videos1.redundancies.streamingPlaylists)
154
155 for (const r of redundancies) {
156 expect(r.strategy).to.equal('recently-added')
157 expect(r.fileUrl).to.exist
158 expect(r.createdAt).to.exist
159 expect(r.updatedAt).to.exist
160 expect(r.expiresOn).to.exist
161 }
162 })
163
164 it('Should correctly paginate and sort results', async function () {
165 {
166 const body = await commands[0].listVideos({
167 target: 'remote-videos',
168 sort: 'name',
169 start: 0,
170 count: 2
171 })
172
173 const videos = body.data
174 expect(videos[0].name).to.equal('video 1 server 2')
175 expect(videos[1].name).to.equal('video 2 server 2')
176 }
177
178 {
179 const body = await commands[0].listVideos({
180 target: 'remote-videos',
181 sort: '-name',
182 start: 0,
183 count: 2
184 })
185
186 const videos = body.data
187 expect(videos[0].name).to.equal('video 2 server 2')
188 expect(videos[1].name).to.equal('video 1 server 2')
189 }
190
191 {
192 const body = await commands[0].listVideos({
193 target: 'remote-videos',
194 sort: '-name',
195 start: 1,
196 count: 1
197 })
198
199 expect(body.data[0].name).to.equal('video 1 server 2')
200 }
201 })
202
203 it('Should manually add a redundancy and list it', async function () {
204 this.timeout(120000)
205
206 const uuid = (await servers[1].videos.quickUpload({ name: 'video 3 server 2', privacy: VideoPrivacy.UNLISTED })).uuid
207 await waitJobs(servers)
208 const videoId = await servers[0].videos.getId({ uuid })
209
210 await commands[0].addVideo({ videoId })
211
212 await waitJobs(servers)
213 await servers[0].servers.waitUntilLog('Duplicated ', 15)
214 await waitJobs(servers)
215
216 {
217 const body = await commands[0].listVideos({
218 target: 'remote-videos',
219 sort: '-name',
220 start: 0,
221 count: 5
222 })
223
224 const video = body.data[0]
225
226 expect(video.name).to.equal('video 3 server 2')
227 expect(video.redundancies.files).to.have.lengthOf(4)
228 expect(video.redundancies.streamingPlaylists).to.have.lengthOf(1)
229
230 const redundancies = video.redundancies.files.concat(video.redundancies.streamingPlaylists)
231
232 for (const r of redundancies) {
233 redundanciesToRemove.push(r.id)
234
235 expect(r.strategy).to.equal('manual')
236 expect(r.fileUrl).to.exist
237 expect(r.createdAt).to.exist
238 expect(r.updatedAt).to.exist
239 expect(r.expiresOn).to.be.null
240 }
241 }
242
243 const body = await commands[1].listVideos({
244 target: 'my-videos',
245 sort: '-name',
246 start: 0,
247 count: 5
248 })
249
250 const video = body.data[0]
251 expect(video.name).to.equal('video 3 server 2')
252 expect(video.redundancies.files).to.have.lengthOf(4)
253 expect(video.redundancies.streamingPlaylists).to.have.lengthOf(1)
254
255 const redundancies = video.redundancies.files.concat(video.redundancies.streamingPlaylists)
256
257 for (const r of redundancies) {
258 expect(r.strategy).to.be.null
259 expect(r.fileUrl).to.exist
260 expect(r.createdAt).to.exist
261 expect(r.updatedAt).to.exist
262 expect(r.expiresOn).to.be.null
263 }
264 })
265
266 it('Should manually remove a redundancy and remove it from the list', async function () {
267 this.timeout(120000)
268
269 for (const redundancyId of redundanciesToRemove) {
270 await commands[0].removeVideo({ redundancyId })
271 }
272
273 {
274 const body = await commands[0].listVideos({
275 target: 'remote-videos',
276 sort: '-name',
277 start: 0,
278 count: 5
279 })
280
281 const videos = body.data
282
283 expect(videos).to.have.lengthOf(2)
284
285 const video = videos[0]
286 expect(video.name).to.equal('video 2 server 2')
287 expect(video.redundancies.files).to.have.lengthOf(4)
288 expect(video.redundancies.streamingPlaylists).to.have.lengthOf(1)
289
290 const redundancies = video.redundancies.files.concat(video.redundancies.streamingPlaylists)
291
292 redundanciesToRemove = redundancies.map(r => r.id)
293 }
294 })
295
296 it('Should remove another (auto) redundancy', async function () {
297 for (const redundancyId of redundanciesToRemove) {
298 await commands[0].removeVideo({ redundancyId })
299 }
300
301 const body = await commands[0].listVideos({
302 target: 'remote-videos',
303 sort: '-name',
304 start: 0,
305 count: 5
306 })
307
308 const videos = body.data
309 expect(videos).to.have.lengthOf(1)
310 expect(videos[0].name).to.equal('video 1 server 2')
311 })
312
313 after(async function () {
314 await cleanupTests(servers)
315 })
316 })