]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/redundancy/redundancy-constraints.ts
Fix tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / redundancy / redundancy-constraints.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 { listVideoRedundancies, updateRedundancy } from '@shared/extra-utils/server/redundancy'
6 import { VideoPrivacy } from '@shared/models'
7 import {
8 cleanupTests,
9 flushAndRunServer,
10 follow,
11 killallServers,
12 reRunServer,
13 ServerInfo,
14 setAccessTokensToServers,
15 updateVideo,
16 uploadVideo,
17 waitUntilLog
18 } from '../../../../shared/extra-utils'
19 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
20
21 const expect = chai.expect
22
23 describe('Test redundancy constraints', function () {
24 let remoteServer: ServerInfo
25 let localServer: ServerInfo
26 let servers: ServerInfo[]
27
28 const remoteServerConfig = {
29 redundancy: {
30 videos: {
31 check_interval: '1 second',
32 strategies: [
33 {
34 strategy: 'recently-added',
35 min_lifetime: '1 hour',
36 size: '100MB',
37 min_views: 0
38 }
39 ]
40 }
41 }
42 }
43
44 async function uploadWrapper (videoName: string) {
45 // Wait for transcoding
46 const res = await uploadVideo(localServer.url, localServer.accessToken, { name: 'to transcode', privacy: VideoPrivacy.PRIVATE })
47 await waitJobs([ localServer ])
48
49 // Update video to schedule a federation
50 await updateVideo(localServer.url, localServer.accessToken, res.body.video.id, { name: videoName, privacy: VideoPrivacy.PUBLIC })
51 }
52
53 async function getTotalRedundanciesLocalServer () {
54 const res = await listVideoRedundancies({
55 url: localServer.url,
56 accessToken: localServer.accessToken,
57 target: 'my-videos'
58 })
59
60 return res.body.total
61 }
62
63 async function getTotalRedundanciesRemoteServer () {
64 const res = await listVideoRedundancies({
65 url: remoteServer.url,
66 accessToken: remoteServer.accessToken,
67 target: 'remote-videos'
68 })
69
70 return res.body.total
71 }
72
73 before(async function () {
74 this.timeout(120000)
75
76 {
77 remoteServer = await flushAndRunServer(1, remoteServerConfig)
78 }
79
80 {
81 const config = {
82 remote_redundancy: {
83 videos: {
84 accept_from: 'nobody'
85 }
86 }
87 }
88 localServer = await flushAndRunServer(2, config)
89 }
90
91 servers = [ remoteServer, localServer ]
92
93 // Get the access tokens
94 await setAccessTokensToServers(servers)
95
96 await uploadVideo(localServer.url, localServer.accessToken, { name: 'video 1 server 2' })
97
98 await waitJobs(servers)
99
100 // Server 1 and server 2 follow each other
101 await follow(remoteServer.url, [ localServer.url ], remoteServer.accessToken)
102 await waitJobs(servers)
103 await updateRedundancy(remoteServer.url, remoteServer.accessToken, localServer.host, true)
104
105 await waitJobs(servers)
106 })
107
108 it('Should have redundancy on server 1 but not on server 2 with a nobody filter', async function () {
109 this.timeout(120000)
110
111 await waitJobs(servers)
112 await waitUntilLog(remoteServer, 'Duplicated ', 5)
113 await waitJobs(servers)
114
115 {
116 const total = await getTotalRedundanciesRemoteServer()
117 expect(total).to.equal(1)
118 }
119
120 {
121 const total = await getTotalRedundanciesLocalServer()
122 expect(total).to.equal(0)
123 }
124 })
125
126 it('Should have redundancy on server 1 and on server 2 with an anybody filter', async function () {
127 this.timeout(120000)
128
129 const config = {
130 remote_redundancy: {
131 videos: {
132 accept_from: 'anybody'
133 }
134 }
135 }
136 await killallServers([ localServer ])
137 await reRunServer(localServer, config)
138
139 await uploadWrapper('video 2 server 2')
140
141 await waitUntilLog(remoteServer, 'Duplicated ', 10)
142 await waitJobs(servers)
143
144 {
145 const total = await getTotalRedundanciesRemoteServer()
146 expect(total).to.equal(2)
147 }
148
149 {
150 const total = await getTotalRedundanciesLocalServer()
151 expect(total).to.equal(1)
152 }
153 })
154
155 it('Should have redundancy on server 1 but not on server 2 with a followings filter', async function () {
156 this.timeout(120000)
157
158 const config = {
159 remote_redundancy: {
160 videos: {
161 accept_from: 'followings'
162 }
163 }
164 }
165 await killallServers([ localServer ])
166 await reRunServer(localServer, config)
167
168 await uploadWrapper('video 3 server 2')
169
170 await waitUntilLog(remoteServer, 'Duplicated ', 15)
171 await waitJobs(servers)
172
173 {
174 const total = await getTotalRedundanciesRemoteServer()
175 expect(total).to.equal(3)
176 }
177
178 {
179 const total = await getTotalRedundanciesLocalServer()
180 expect(total).to.equal(1)
181 }
182 })
183
184 it('Should have redundancy on server 1 and on server 2 with followings filter now server 2 follows server 1', async function () {
185 this.timeout(120000)
186
187 await follow(localServer.url, [ remoteServer.url ], localServer.accessToken)
188 await waitJobs(servers)
189
190 await uploadWrapper('video 4 server 2')
191 await waitUntilLog(remoteServer, 'Duplicated ', 20)
192 await waitJobs(servers)
193
194 {
195 const total = await getTotalRedundanciesRemoteServer()
196 expect(total).to.equal(4)
197 }
198
199 {
200 const total = await getTotalRedundanciesLocalServer()
201 expect(total).to.equal(2)
202 }
203 })
204
205 after(async function () {
206 await cleanupTests(servers)
207 })
208 })