PeerTube/packages/tests/src/api/server/handle-down.ts

336 lines
10 KiB
TypeScript
Raw Normal View History

2020-01-31 16:56:52 +01:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2018-01-10 17:18:12 +01:00
2022-08-17 15:44:32 +02:00
import { expect } from 'chai'
import { wait } from '@peertube/peertube-core-utils'
import { HttpStatusCode, JobState, VideoCreateResult, VideoPrivacy } from '@peertube/peertube-models'
2018-01-10 17:18:12 +01:00
import {
2019-04-24 15:10:37 +02:00
cleanupTests,
2021-07-09 14:15:11 +02:00
CommentsCommand,
2021-07-16 09:47:51 +02:00
createMultipleServers,
2018-07-10 17:02:20 +02:00
killallServers,
2021-07-16 09:47:51 +02:00
PeerTubeServer,
2018-07-10 17:02:20 +02:00
setAccessTokensToServers,
2021-07-07 09:16:40 +02:00
waitJobs
} from '@peertube/peertube-server-commands'
import { SQLCommand } from '@tests/shared/sql-command.js'
import { completeVideoCheck } from '@tests/shared/videos.js'
2018-01-10 17:18:12 +01:00
describe('Test handle downs', function () {
2021-07-16 09:47:51 +02:00
let servers: PeerTubeServer[] = []
2023-05-10 16:23:55 +02:00
let sqlCommands: SQLCommand[] = []
2023-04-21 15:00:01 +02:00
2018-01-11 11:40:18 +01:00
let threadIdServer1: number
let threadIdServer2: number
let commentIdServer1: number
let commentIdServer2: number
2021-07-15 10:02:54 +02:00
let missedVideo1: VideoCreateResult
let missedVideo2: VideoCreateResult
let unlistedVideo: VideoCreateResult
2018-01-10 17:18:12 +01:00
const videoIdsServer1: string[] = []
2019-08-06 17:19:53 +02:00
2018-01-10 17:18:12 +01:00
const videoAttributes = {
name: 'my super name for server 1',
category: 5,
licence: 4,
2018-04-23 14:39:52 +02:00
language: 'ja',
2018-01-10 17:18:12 +01:00
nsfw: true,
2018-01-11 11:40:18 +01:00
privacy: VideoPrivacy.PUBLIC,
2018-01-10 17:18:12 +01:00
description: 'my super description for server 1',
support: 'my super support text for server 1',
2018-01-10 17:18:12 +01:00
tags: [ 'tag1p1', 'tag2p1' ],
fixture: 'video_short1.webm'
}
2021-07-13 09:43:59 +02:00
const unlistedVideoAttributes = { ...videoAttributes, privacy: VideoPrivacy.UNLISTED }
2018-01-11 11:40:18 +01:00
const checkAttributes = {
name: 'my super name for server 1',
category: 5,
licence: 4,
language: 'ja',
nsfw: true,
description: 'my super description for server 1',
support: 'my super support text for server 1',
account: {
name: 'root',
host: ''
},
duration: 10,
tags: [ 'tag1p1', 'tag2p1' ],
privacy: VideoPrivacy.PUBLIC,
commentsEnabled: true,
downloadEnabled: true,
channel: {
name: 'root_channel',
displayName: 'Main root channel',
description: ''
},
fixture: 'video_short1.webm',
files: [
{
resolution: 720,
size: 572456
}
]
}
const unlistedCheckAttributes = { ...checkAttributes, privacy: VideoPrivacy.UNLISTED }
2018-01-11 11:40:18 +01:00
2021-07-09 14:15:11 +02:00
let commentCommands: CommentsCommand[]
2018-01-10 17:18:12 +01:00
before(async function () {
2021-12-28 11:41:41 +01:00
this.timeout(120000)
2018-01-10 17:18:12 +01:00
2021-07-16 09:47:51 +02:00
servers = await createMultipleServers(3)
2021-07-16 09:04:35 +02:00
commentCommands = servers.map(s => s.comments)
2018-01-10 17:18:12 +01:00
checkAttributes.account.host = servers[0].host
unlistedCheckAttributes.account.host = servers[0].host
2019-04-26 08:50:52 +02:00
2018-01-10 17:18:12 +01:00
// Get the access tokens
await setAccessTokensToServers(servers)
2023-04-21 15:00:01 +02:00
sqlCommands = servers.map(s => new SQLCommand(s))
2018-01-10 17:18:12 +01:00
})
it('Should remove followers that are often down', async function () {
2019-07-29 11:59:29 +02:00
this.timeout(240000)
2018-01-10 17:18:12 +01:00
2018-07-31 12:21:04 +02:00
// Server 2 and 3 follow server 1
await servers[1].follows.follow({ hosts: [ servers[0].url ] })
await servers[2].follows.follow({ hosts: [ servers[0].url ] })
2018-01-10 17:18:12 +01:00
await waitJobs(servers)
2018-01-10 17:18:12 +01:00
2018-07-31 12:21:04 +02:00
// Upload a video to server 1
2021-07-16 09:04:35 +02:00
await servers[0].videos.upload({ attributes: videoAttributes })
2018-01-10 17:18:12 +01:00
await waitJobs(servers)
2018-01-10 17:18:12 +01:00
2018-07-31 12:21:04 +02:00
// And check all servers have this video
2018-01-10 17:18:12 +01:00
for (const server of servers) {
2021-07-16 09:04:35 +02:00
const { data } = await server.videos.list()
2021-07-15 10:02:54 +02:00
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(1)
2018-01-10 17:18:12 +01:00
}
2018-07-31 12:21:04 +02:00
// Kill server 2
2021-07-09 15:37:43 +02:00
await killallServers([ servers[1] ])
2018-01-10 17:18:12 +01:00
// Remove server 2 follower
for (let i = 0; i < 10; i++) {
2021-07-16 09:04:35 +02:00
await servers[0].videos.upload({ attributes: videoAttributes })
2018-01-11 11:40:18 +01:00
}
2021-05-10 16:28:26 +02:00
await waitJobs([ servers[0], servers[2] ])
2018-07-31 12:21:04 +02:00
// Kill server 3
2021-07-09 15:37:43 +02:00
await killallServers([ servers[2] ])
2018-07-31 12:21:04 +02:00
2021-07-16 09:04:35 +02:00
missedVideo1 = await servers[0].videos.upload({ attributes: videoAttributes })
2018-07-31 12:21:04 +02:00
2021-07-16 09:04:35 +02:00
missedVideo2 = await servers[0].videos.upload({ attributes: videoAttributes })
2018-07-31 12:21:04 +02:00
// Unlisted video
2021-07-16 09:04:35 +02:00
unlistedVideo = await servers[0].videos.upload({ attributes: unlistedVideoAttributes })
2018-01-11 11:40:18 +01:00
// Add comments to video 2
{
const text = 'thread 1'
2021-07-09 14:15:11 +02:00
let comment = await commentCommands[0].createThread({ videoId: missedVideo2.uuid, text })
2018-01-11 11:40:18 +01:00
threadIdServer1 = comment.id
2021-07-09 14:15:11 +02:00
comment = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-1' })
2018-01-11 11:40:18 +01:00
2021-07-09 14:15:11 +02:00
const created = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-2' })
commentIdServer1 = created.id
2018-01-10 17:18:12 +01:00
}
await waitJobs(servers[0])
// Wait scheduler
await wait(11000)
2018-01-10 17:18:12 +01:00
2018-07-31 12:21:04 +02:00
// Only server 3 is still a follower of server 1
2021-07-16 09:04:35 +02:00
const body = await servers[0].follows.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
2021-07-07 09:16:40 +02:00
expect(body.data).to.be.an('array')
expect(body.data).to.have.lengthOf(1)
2022-12-09 11:14:47 +01:00
expect(body.data[0].follower.host).to.equal(servers[2].host)
2018-01-10 17:18:12 +01:00
})
it('Should not have pending/processing jobs anymore', async function () {
2018-07-10 17:02:20 +02:00
const states: JobState[] = [ 'waiting', 'active' ]
2018-01-10 17:18:12 +01:00
for (const state of states) {
2021-08-25 15:13:41 +02:00
const body = await servers[0].jobs.list({
2022-07-13 11:58:01 +02:00
state,
2019-12-04 14:49:59 +01:00
start: 0,
count: 50,
sort: '-createdAt'
})
2021-07-07 09:34:56 +02:00
expect(body.data).to.have.length(0)
2018-01-10 17:18:12 +01:00
}
})
2018-07-31 12:21:04 +02:00
it('Should re-follow server 1', async function () {
2021-10-14 08:30:17 +02:00
this.timeout(70000)
2018-01-11 11:40:18 +01:00
2021-07-16 09:47:51 +02:00
await servers[1].run()
await servers[2].run()
2018-07-31 12:21:04 +02:00
2021-07-16 09:04:35 +02:00
await servers[1].follows.unfollow({ target: servers[0] })
2018-07-31 12:21:04 +02:00
await waitJobs(servers)
2018-01-10 17:18:12 +01:00
await servers[1].follows.follow({ hosts: [ servers[0].url ] })
2018-01-10 17:18:12 +01:00
await waitJobs(servers)
2018-01-10 17:18:12 +01:00
2021-07-16 09:04:35 +02:00
const body = await servers[0].follows.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
2021-07-07 09:16:40 +02:00
expect(body.data).to.be.an('array')
expect(body.data).to.have.lengthOf(2)
2018-01-10 17:18:12 +01:00
})
it('Should send an update to server 3, and automatically fetch the video', async function () {
2018-01-11 11:40:18 +01:00
this.timeout(15000)
2018-01-10 17:18:12 +01:00
2021-07-15 10:02:54 +02:00
{
2021-07-16 09:04:35 +02:00
const { data } = await servers[2].videos.list()
2021-07-15 10:02:54 +02:00
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(11)
}
2018-07-31 12:21:04 +02:00
2021-07-16 09:04:35 +02:00
await servers[0].videos.update({ id: missedVideo1.uuid })
await servers[0].videos.update({ id: unlistedVideo.uuid })
2018-01-10 17:18:12 +01:00
await waitJobs(servers)
2018-01-10 17:18:12 +01:00
2021-07-15 10:02:54 +02:00
{
2021-07-16 09:04:35 +02:00
const { data } = await servers[2].videos.list()
2021-07-15 10:02:54 +02:00
expect(data).to.be.an('array')
// 1 video is unlisted
expect(data).to.have.lengthOf(12)
}
2018-01-11 11:40:18 +01:00
2018-07-31 12:21:04 +02:00
// Check unlisted video
2021-07-16 09:04:35 +02:00
const video = await servers[2].videos.get({ id: unlistedVideo.uuid })
2023-04-21 15:00:01 +02:00
await completeVideoCheck({ server: servers[2], originServer: servers[0], videoUUID: video.uuid, attributes: unlistedCheckAttributes })
2018-01-11 11:40:18 +01:00
})
2018-07-31 12:21:04 +02:00
it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
2018-01-11 11:40:18 +01:00
this.timeout(25000)
2018-01-10 17:18:12 +01:00
2021-07-09 14:15:11 +02:00
await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer1, text: 'comment 1-3' })
2018-01-11 11:40:18 +01:00
await waitJobs(servers)
2018-01-11 11:40:18 +01:00
2021-07-16 09:04:35 +02:00
await servers[2].videos.get({ id: missedVideo2.uuid })
2018-01-11 11:40:18 +01:00
{
2021-07-16 09:04:35 +02:00
const { data } = await servers[2].comments.listThreads({ videoId: missedVideo2.uuid })
2021-07-09 14:15:11 +02:00
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(1)
2018-01-11 11:40:18 +01:00
2021-07-09 14:15:11 +02:00
threadIdServer2 = data[0].id
2018-01-11 11:40:18 +01:00
2021-07-16 09:04:35 +02:00
const tree = await servers[2].comments.getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer2 })
2018-01-11 11:40:18 +01:00
expect(tree.comment.text).equal('thread 1')
expect(tree.children).to.have.lengthOf(1)
const firstChild = tree.children[0]
expect(firstChild.comment.text).to.equal('comment 1-1')
expect(firstChild.children).to.have.lengthOf(1)
const childOfFirstChild = firstChild.children[0]
expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
expect(childOfFirstChild.children).to.have.lengthOf(1)
const childOfChildFirstChild = childOfFirstChild.children[0]
expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
expect(childOfChildFirstChild.children).to.have.lengthOf(0)
commentIdServer2 = childOfChildFirstChild.comment.id
}
})
it('Should correctly reply to the comment', async function () {
2021-07-16 09:04:35 +02:00
await servers[2].comments.addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer2, text: 'comment 1-4' })
2018-01-11 11:40:18 +01:00
await waitJobs(servers)
2018-01-11 11:40:18 +01:00
2021-07-09 14:15:11 +02:00
const tree = await commentCommands[0].getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer1 })
2018-01-11 11:40:18 +01:00
2021-07-09 14:15:11 +02:00
expect(tree.comment.text).equal('thread 1')
expect(tree.children).to.have.lengthOf(1)
2018-01-11 11:40:18 +01:00
2021-07-09 14:15:11 +02:00
const firstChild = tree.children[0]
expect(firstChild.comment.text).to.equal('comment 1-1')
expect(firstChild.children).to.have.lengthOf(1)
2018-01-11 11:40:18 +01:00
2021-07-09 14:15:11 +02:00
const childOfFirstChild = firstChild.children[0]
expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
expect(childOfFirstChild.children).to.have.lengthOf(1)
2018-01-11 11:40:18 +01:00
2021-07-09 14:15:11 +02:00
const childOfChildFirstChild = childOfFirstChild.children[0]
expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
expect(childOfChildFirstChild.children).to.have.lengthOf(1)
2018-01-11 11:40:18 +01:00
2021-07-09 14:15:11 +02:00
const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
2018-01-10 17:18:12 +01:00
})
2019-08-06 17:19:53 +02:00
it('Should upload many videos on server 1', async function () {
this.timeout(240000)
2019-08-06 17:19:53 +02:00
for (let i = 0; i < 10; i++) {
2021-07-16 09:04:35 +02:00
const uuid = (await servers[0].videos.quickUpload({ name: 'video ' + i })).uuid
2019-08-06 17:19:53 +02:00
videoIdsServer1.push(uuid)
}
await waitJobs(servers)
for (const id of videoIdsServer1) {
2021-07-16 09:04:35 +02:00
await servers[1].videos.get({ id })
2019-08-06 17:19:53 +02:00
}
await waitJobs(servers)
2023-04-21 15:00:01 +02:00
await sqlCommands[1].setActorFollowScores(20)
2019-08-06 17:19:53 +02:00
// Wait video expiration
await wait(11000)
// Refresh video -> score + 10 = 30
2021-07-16 09:04:35 +02:00
await servers[1].videos.get({ id: videoIdsServer1[0] })
2019-08-06 17:19:53 +02:00
await waitJobs(servers)
})
it('Should remove followings that are down', async function () {
this.timeout(120000)
2021-07-09 15:37:43 +02:00
await killallServers([ servers[0] ])
2019-08-06 17:19:53 +02:00
// Wait video expiration
await wait(11000)
2021-05-11 14:23:49 +02:00
for (let i = 0; i < 5; i++) {
try {
2021-07-16 09:04:35 +02:00
await servers[1].videos.get({ id: videoIdsServer1[i] })
2021-05-11 14:23:49 +02:00
await waitJobs([ servers[1] ])
await wait(1500)
} catch {}
2019-08-06 17:19:53 +02:00
}
for (const id of videoIdsServer1) {
2021-07-16 09:04:35 +02:00
await servers[1].videos.get({ id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
2019-08-06 17:19:53 +02:00
}
})
2019-04-24 15:10:37 +02:00
after(async function () {
2023-04-21 15:00:01 +02:00
for (const sqlCommand of sqlCommands) {
await sqlCommand.cleanup()
}
2019-04-24 15:10:37 +02:00
await cleanupTests(servers)
2018-01-10 17:18:12 +01:00
})
})