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