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