]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/handle-down.ts
Shorter server command names
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / handle-down.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2ccaeeb3 2
2ccaeeb3 3import 'mocha'
c3d29f69
C
4import * as chai from 'chai'
5import { HttpStatusCode } from '@shared/core-utils'
2ccaeeb3 6import {
7c3b7976 7 cleanupTests,
12edc149 8 CommentsCommand,
9639bd17 9 completeVideoCheck,
94831479 10 flushAndRunMultipleServers,
94831479 11 killallServers,
a4101923 12 reRunServer,
94831479
C
13 ServerInfo,
14 setAccessTokensToServers,
c3d29f69
C
15 wait,
16 waitJobs
17} from '@shared/extra-utils'
d23dd9fb 18import { JobState, VideoCreateResult, VideoPrivacy } from '@shared/models'
2ccaeeb3
C
19
20const expect = chai.expect
21
22describe('Test handle downs', function () {
23 let servers: ServerInfo[] = []
7bc29171
C
24 let threadIdServer1: number
25 let threadIdServer2: number
26 let commentIdServer1: number
27 let commentIdServer2: number
d23dd9fb
C
28 let missedVideo1: VideoCreateResult
29 let missedVideo2: VideoCreateResult
30 let unlistedVideo: VideoCreateResult
2ccaeeb3 31
d4a8e7a6 32 const videoIdsServer1: string[] = []
6b9c966f 33
2ccaeeb3
C
34 const videoAttributes = {
35 name: 'my super name for server 1',
36 category: 5,
37 licence: 4,
9d3ef9fe 38 language: 'ja',
2ccaeeb3 39 nsfw: true,
7bc29171 40 privacy: VideoPrivacy.PUBLIC,
2ccaeeb3 41 description: 'my super description for server 1',
2422c46b 42 support: 'my super support text for server 1',
2ccaeeb3
C
43 tags: [ 'tag1p1', 'tag2p1' ],
44 fixture: 'video_short1.webm'
45 }
46
6c5065a0 47 const unlistedVideoAttributes = { ...videoAttributes, privacy: VideoPrivacy.UNLISTED }
7bc29171 48
48f07b4a
C
49 let checkAttributes: any
50 let unlistedCheckAttributes: any
7bc29171 51
12edc149
C
52 let commentCommands: CommentsCommand[]
53
2ccaeeb3 54 before(async function () {
e212f887 55 this.timeout(30000)
2ccaeeb3 56
cc6373e6 57 servers = await flushAndRunMultipleServers(3)
89d241a7 58 commentCommands = servers.map(s => s.comments)
2ccaeeb3 59
48f07b4a
C
60 checkAttributes = {
61 name: 'my super name for server 1',
62 category: 5,
63 licence: 4,
64 language: 'ja',
65 nsfw: true,
66 description: 'my super description for server 1',
67 support: 'my super support text for server 1',
68 account: {
69 name: 'root',
70 host: 'localhost:' + servers[0].port
71 },
72 isLocal: false,
73 duration: 10,
74 tags: [ 'tag1p1', 'tag2p1' ],
75 privacy: VideoPrivacy.PUBLIC,
76 commentsEnabled: true,
77 downloadEnabled: true,
78 channel: {
79 name: 'root_channel',
80 displayName: 'Main root channel',
81 description: '',
82 isLocal: false
83 },
84 fixture: 'video_short1.webm',
85 files: [
86 {
87 resolution: 720,
88 size: 572456
89 }
90 ]
91 }
6c5065a0 92 unlistedCheckAttributes = { ...checkAttributes, privacy: VideoPrivacy.UNLISTED }
48f07b4a 93
2ccaeeb3
C
94 // Get the access tokens
95 await setAccessTokensToServers(servers)
96 })
97
98 it('Should remove followers that are often down', async function () {
2284f202 99 this.timeout(240000)
2ccaeeb3 100
cc6373e6 101 // Server 2 and 3 follow server 1
89d241a7
C
102 await servers[1].follows.follow({ targets: [ servers[0].url ] })
103 await servers[2].follows.follow({ targets: [ servers[0].url ] })
2ccaeeb3 104
3cd0734f 105 await waitJobs(servers)
2ccaeeb3 106
cc6373e6 107 // Upload a video to server 1
89d241a7 108 await servers[0].videos.upload({ attributes: videoAttributes })
2ccaeeb3 109
3cd0734f 110 await waitJobs(servers)
2ccaeeb3 111
cc6373e6 112 // And check all servers have this video
2ccaeeb3 113 for (const server of servers) {
89d241a7 114 const { data } = await server.videos.list()
d23dd9fb
C
115 expect(data).to.be.an('array')
116 expect(data).to.have.lengthOf(1)
2ccaeeb3
C
117 }
118
cc6373e6 119 // Kill server 2
9293139f 120 await killallServers([ servers[1] ])
2ccaeeb3
C
121
122 // Remove server 2 follower
123 for (let i = 0; i < 10; i++) {
89d241a7 124 await servers[0].videos.upload({ attributes: videoAttributes })
7bc29171
C
125 }
126
f67d1dca 127 await waitJobs([ servers[0], servers[2] ])
6502c3d4 128
cc6373e6 129 // Kill server 3
9293139f 130 await killallServers([ servers[2] ])
cc6373e6 131
89d241a7 132 missedVideo1 = await servers[0].videos.upload({ attributes: videoAttributes })
cc6373e6 133
89d241a7 134 missedVideo2 = await servers[0].videos.upload({ attributes: videoAttributes })
cc6373e6
C
135
136 // Unlisted video
89d241a7 137 unlistedVideo = await servers[0].videos.upload({ attributes: unlistedVideoAttributes })
6502c3d4 138
7bc29171
C
139 // Add comments to video 2
140 {
141 const text = 'thread 1'
12edc149 142 let comment = await commentCommands[0].createThread({ videoId: missedVideo2.uuid, text })
7bc29171
C
143 threadIdServer1 = comment.id
144
12edc149 145 comment = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-1' })
7bc29171 146
12edc149
C
147 const created = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-2' })
148 commentIdServer1 = created.id
2ccaeeb3
C
149 }
150
3cd0734f
C
151 await waitJobs(servers[0])
152 // Wait scheduler
9a4a9b6c 153 await wait(11000)
2ccaeeb3 154
cc6373e6 155 // Only server 3 is still a follower of server 1
89d241a7 156 const body = await servers[0].follows.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
c3d29f69
C
157 expect(body.data).to.be.an('array')
158 expect(body.data).to.have.lengthOf(1)
159 expect(body.data[0].follower.host).to.equal('localhost:' + servers[2].port)
2ccaeeb3
C
160 })
161
162 it('Should not have pending/processing jobs anymore', async function () {
94831479 163 const states: JobState[] = [ 'waiting', 'active' ]
2ccaeeb3 164
94a5ff8a 165 for (const state of states) {
89d241a7 166 const body = await servers[0].jobs.getJobsList({
1061c73f
C
167 state: state,
168 start: 0,
169 count: 50,
170 sort: '-createdAt'
171 })
9c6327f8 172 expect(body.data).to.have.length(0)
2ccaeeb3
C
173 }
174 })
175
cc6373e6 176 it('Should re-follow server 1', async function () {
cf7a61b5 177 this.timeout(35000)
7bc29171
C
178
179 await reRunServer(servers[1])
cc6373e6
C
180 await reRunServer(servers[2])
181
89d241a7 182 await servers[1].follows.unfollow({ target: servers[0] })
cc6373e6 183 await waitJobs(servers)
2ccaeeb3 184
89d241a7 185 await servers[1].follows.follow({ targets: [ servers[0].url ] })
2ccaeeb3 186
3cd0734f 187 await waitJobs(servers)
2ccaeeb3 188
89d241a7 189 const body = await servers[0].follows.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
c3d29f69
C
190 expect(body.data).to.be.an('array')
191 expect(body.data).to.have.lengthOf(2)
2ccaeeb3
C
192 })
193
8cf99873 194 it('Should send an update to server 3, and automatically fetch the video', async function () {
7bc29171 195 this.timeout(15000)
2ccaeeb3 196
d23dd9fb 197 {
89d241a7 198 const { data } = await servers[2].videos.list()
d23dd9fb
C
199 expect(data).to.be.an('array')
200 expect(data).to.have.lengthOf(11)
201 }
cc6373e6 202
89d241a7
C
203 await servers[0].videos.update({ id: missedVideo1.uuid })
204 await servers[0].videos.update({ id: unlistedVideo.uuid })
2ccaeeb3 205
3cd0734f 206 await waitJobs(servers)
2ccaeeb3 207
d23dd9fb 208 {
89d241a7 209 const { data } = await servers[2].videos.list()
d23dd9fb
C
210 expect(data).to.be.an('array')
211 // 1 video is unlisted
212 expect(data).to.have.lengthOf(12)
213 }
7bc29171 214
cc6373e6 215 // Check unlisted video
89d241a7 216 const video = await servers[2].videos.get({ id: unlistedVideo.uuid })
d23dd9fb 217 await completeVideoCheck(servers[2], video, unlistedCheckAttributes)
7bc29171
C
218 })
219
cc6373e6 220 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
7bc29171 221 this.timeout(25000)
2ccaeeb3 222
12edc149 223 await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer1, text: 'comment 1-3' })
7bc29171 224
3cd0734f 225 await waitJobs(servers)
7bc29171 226
89d241a7 227 await servers[2].videos.get({ id: missedVideo2.uuid })
7bc29171 228
7bc29171 229 {
89d241a7 230 const { data } = await servers[2].comments.listThreads({ videoId: missedVideo2.uuid })
12edc149
C
231 expect(data).to.be.an('array')
232 expect(data).to.have.lengthOf(1)
7bc29171 233
12edc149 234 threadIdServer2 = data[0].id
7bc29171 235
89d241a7 236 const tree = await servers[2].comments.getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer2 })
7bc29171
C
237 expect(tree.comment.text).equal('thread 1')
238 expect(tree.children).to.have.lengthOf(1)
239
240 const firstChild = tree.children[0]
241 expect(firstChild.comment.text).to.equal('comment 1-1')
242 expect(firstChild.children).to.have.lengthOf(1)
243
244 const childOfFirstChild = firstChild.children[0]
245 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
246 expect(childOfFirstChild.children).to.have.lengthOf(1)
247
248 const childOfChildFirstChild = childOfFirstChild.children[0]
249 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
250 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
251
252 commentIdServer2 = childOfChildFirstChild.comment.id
253 }
254 })
255
256 it('Should correctly reply to the comment', async function () {
257 this.timeout(15000)
258
89d241a7 259 await servers[2].comments.addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer2, text: 'comment 1-4' })
7bc29171 260
3cd0734f 261 await waitJobs(servers)
7bc29171 262
12edc149 263 const tree = await commentCommands[0].getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer1 })
7bc29171 264
12edc149
C
265 expect(tree.comment.text).equal('thread 1')
266 expect(tree.children).to.have.lengthOf(1)
7bc29171 267
12edc149
C
268 const firstChild = tree.children[0]
269 expect(firstChild.comment.text).to.equal('comment 1-1')
270 expect(firstChild.children).to.have.lengthOf(1)
7bc29171 271
12edc149
C
272 const childOfFirstChild = firstChild.children[0]
273 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
274 expect(childOfFirstChild.children).to.have.lengthOf(1)
7bc29171 275
12edc149
C
276 const childOfChildFirstChild = childOfFirstChild.children[0]
277 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
278 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
7bc29171 279
12edc149
C
280 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
281 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
282 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
2ccaeeb3
C
283 })
284
6b9c966f
C
285 it('Should upload many videos on server 1', async function () {
286 this.timeout(120000)
287
288 for (let i = 0; i < 10; i++) {
89d241a7 289 const uuid = (await servers[0].videos.quickUpload({ name: 'video ' + i })).uuid
6b9c966f
C
290 videoIdsServer1.push(uuid)
291 }
292
293 await waitJobs(servers)
294
295 for (const id of videoIdsServer1) {
89d241a7 296 await servers[1].videos.get({ id })
6b9c966f
C
297 }
298
299 await waitJobs(servers)
89d241a7 300 await servers[1].sql.setActorFollowScores(20)
6b9c966f
C
301
302 // Wait video expiration
303 await wait(11000)
304
305 // Refresh video -> score + 10 = 30
89d241a7 306 await servers[1].videos.get({ id: videoIdsServer1[0] })
6b9c966f
C
307
308 await waitJobs(servers)
309 })
310
311 it('Should remove followings that are down', async function () {
312 this.timeout(120000)
313
9293139f 314 await killallServers([ servers[0] ])
6b9c966f
C
315
316 // Wait video expiration
317 await wait(11000)
318
a3c997b3
C
319 for (let i = 0; i < 5; i++) {
320 try {
89d241a7 321 await servers[1].videos.get({ id: videoIdsServer1[i] })
a3c997b3
C
322 await waitJobs([ servers[1] ])
323 await wait(1500)
324 } catch {}
6b9c966f
C
325 }
326
327 for (const id of videoIdsServer1) {
89d241a7 328 await servers[1].videos.get({ id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
6b9c966f
C
329 }
330 })
331
7c3b7976
C
332 after(async function () {
333 await cleanupTests(servers)
2ccaeeb3
C
334 })
335})