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