X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fserver%2Fhandle-down.ts;h=b3255a72135e692f8bc8753ba87e084ea2a566bb;hb=851675c5591dcab1070183f0ed1b1a788de07d2c;hp=f5ff3e9e52075fd406be4bcdf70c983740f9ef49;hpb=b64c950a1cb37da880fc14e8535f438c78b5b7f7;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/server/handle-down.ts b/server/tests/api/server/handle-down.ts index f5ff3e9e5..b3255a721 100644 --- a/server/tests/api/server/handle-down.ts +++ b/server/tests/api/server/handle-down.ts @@ -1,38 +1,39 @@ -/* tslint:disable:no-unused-expression */ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import * as chai from 'chai' import 'mocha' -import { JobState } from '../../../../shared/models' -import { VideoPrivacy } from '../../../../shared/models/videos' -import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model' -import { completeVideoCheck, getVideo, immutableAssign, reRunServer, viewVideo } from '../../utils' - -import { - flushAndRunMultipleServers, flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo, - wait -} from '../../utils/index' -import { follow, getFollowersListPaginationAndSort } from '../../utils/server/follows' -import { getJobsListPaginationAndSort } from '../../utils/server/jobs' +import * as chai from 'chai' import { - addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads, - getVideoThreadComments -} from '../../utils/videos/video-comments' + cleanupTests, + CommentsCommand, + completeVideoCheck, + createMultipleServers, + killallServers, + PeerTubeServer, + setAccessTokensToServers, + wait, + waitJobs +} from '@shared/extra-utils' +import { HttpStatusCode, JobState, VideoCreateResult, VideoPrivacy } from '@shared/models' const expect = chai.expect describe('Test handle downs', function () { - let servers: ServerInfo[] = [] - const videos = [] + let servers: PeerTubeServer[] = [] let threadIdServer1: number let threadIdServer2: number let commentIdServer1: number let commentIdServer2: number + let missedVideo1: VideoCreateResult + let missedVideo2: VideoCreateResult + let unlistedVideo: VideoCreateResult + + const videoIdsServer1: string[] = [] const videoAttributes = { name: 'my super name for server 1', category: 5, licence: 4, - language: 9, + language: 'ja', nsfw: true, privacy: VideoPrivacy.PUBLIC, description: 'my super description for server 1', @@ -41,171 +42,196 @@ describe('Test handle downs', function () { fixture: 'video_short1.webm' } - const unlistedVideoAttributes = immutableAssign(videoAttributes, { - privacy: VideoPrivacy.UNLISTED - }) + const unlistedVideoAttributes = { ...videoAttributes, privacy: VideoPrivacy.UNLISTED } - const checkAttributes = { - name: 'my super name for server 1', - category: 5, - licence: 4, - language: 9, - nsfw: true, - description: 'my super description for server 1', - support: 'my super support text for server 1', - account: { - name: 'root', - host: 'localhost:9001' - }, - isLocal: false, - duration: 10, - tags: [ 'tag1p1', 'tag2p1' ], - privacy: VideoPrivacy.PUBLIC, - commentsEnabled: true, - channel: { - name: 'Default root channel', - description: '', - isLocal: false - }, - fixture: 'video_short1.webm', - files: [ - { - resolution: 720, - size: 572456 - } - ] - } + let checkAttributes: any + let unlistedCheckAttributes: any - const unlistedCheckAttributes = immutableAssign(checkAttributes, { - privacy: VideoPrivacy.UNLISTED - }) + let commentCommands: CommentsCommand[] before(async function () { this.timeout(30000) - servers = await flushAndRunMultipleServers(2) + servers = await createMultipleServers(3) + commentCommands = servers.map(s => s.comments) + + 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: 'localhost:' + servers[0].port + }, + isLocal: false, + duration: 10, + tags: [ 'tag1p1', 'tag2p1' ], + privacy: VideoPrivacy.PUBLIC, + commentsEnabled: true, + downloadEnabled: true, + channel: { + name: 'root_channel', + displayName: 'Main root channel', + description: '', + isLocal: false + }, + fixture: 'video_short1.webm', + files: [ + { + resolution: 720, + size: 572456 + } + ] + } + unlistedCheckAttributes = { ...checkAttributes, privacy: VideoPrivacy.UNLISTED } // Get the access tokens await setAccessTokensToServers(servers) }) it('Should remove followers that are often down', async function () { - this.timeout(60000) + this.timeout(240000) - await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken) + // 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 ] }) - await wait(5000) + await waitJobs(servers) - await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes) + // Upload a video to server 1 + await servers[0].videos.upload({ attributes: videoAttributes }) - await wait(5000) + await waitJobs(servers) + // And check all servers have this video for (const server of servers) { - const res = await getVideosList(server.url) - expect(res.body.data).to.be.an('array') - expect(res.body.data).to.have.lengthOf(1) + const { data } = await server.videos.list() + expect(data).to.be.an('array') + expect(data).to.have.lengthOf(1) } - // Kill server 1 - killallServers([ servers[1] ]) - - let resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, unlistedVideoAttributes) - videos.push(resVideo.body.video) + // Kill server 2 + await killallServers([ servers[1] ]) // Remove server 2 follower for (let i = 0; i < 10; i++) { - resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes) - videos.push(resVideo.body.video) + await servers[0].videos.upload({ attributes: videoAttributes }) } - await wait(2000) + await waitJobs([ servers[0], servers[2] ]) + + // Kill server 3 + await killallServers([ servers[2] ]) - await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes) + missedVideo1 = await servers[0].videos.upload({ attributes: videoAttributes }) + + missedVideo2 = await servers[0].videos.upload({ attributes: videoAttributes }) + + // Unlisted video + unlistedVideo = await servers[0].videos.upload({ attributes: unlistedVideoAttributes }) // Add comments to video 2 { const text = 'thread 1' - let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videos[1].uuid, text) - let comment = resComment.body.comment + let comment = await commentCommands[0].createThread({ videoId: missedVideo2.uuid, text }) threadIdServer1 = comment.id - resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, comment.id, 'comment 1-1') - comment = resComment.body.comment + comment = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-1' }) - resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, comment.id, 'comment 1-2') - commentIdServer1 = resComment.body.comment.id + const created = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-2' }) + commentIdServer1 = created.id } - await wait(10000) + await waitJobs(servers[0]) + // Wait scheduler + await wait(11000) - const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 1, 'createdAt') - expect(res.body.data).to.be.an('array') - expect(res.body.data).to.have.lengthOf(0) + // Only server 3 is still a follower of server 1 + const body = await servers[0].follows.getFollowers({ start: 0, count: 2, sort: 'createdAt' }) + expect(body.data).to.be.an('array') + expect(body.data).to.have.lengthOf(1) + expect(body.data[0].follower.host).to.equal('localhost:' + servers[2].port) }) it('Should not have pending/processing jobs anymore', async function () { - const states: JobState[] = [ 'inactive', 'active' ] + const states: JobState[] = [ 'waiting', 'active' ] for (const state of states) { - const res = await getJobsListPaginationAndSort(servers[ 0 ].url, servers[ 0 ].accessToken, state,0, 50, '-createdAt') - expect(res.body.data).to.have.length(0) + const body = await servers[0].jobs.list({ + state: state, + start: 0, + count: 50, + sort: '-createdAt' + }) + expect(body.data).to.have.length(0) } }) - it('Should follow server 1', async function () { - this.timeout(15000) + it('Should re-follow server 1', async function () { + this.timeout(35000) + + await servers[1].run() + await servers[2].run() - await reRunServer(servers[1]) + await servers[1].follows.unfollow({ target: servers[0] }) + await waitJobs(servers) - await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken) + await servers[1].follows.follow({ hosts: [ servers[0].url ] }) - await wait(5000) + await waitJobs(servers) - const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 1, 'createdAt') - expect(res.body.data).to.be.an('array') - expect(res.body.data).to.have.lengthOf(1) + const body = await servers[0].follows.getFollowers({ start: 0, count: 2, sort: 'createdAt' }) + expect(body.data).to.be.an('array') + expect(body.data).to.have.lengthOf(2) }) - it('Should send a view to server 2, and automatically fetch the video', async function () { + it('Should send an update to server 3, and automatically fetch the video', async function () { this.timeout(15000) - await viewVideo(servers[0].url, videos[0].uuid) + { + const { data } = await servers[2].videos.list() + expect(data).to.be.an('array') + expect(data).to.have.lengthOf(11) + } - await wait(5000) + await servers[0].videos.update({ id: missedVideo1.uuid }) + await servers[0].videos.update({ id: unlistedVideo.uuid }) - const res = await getVideosList(servers[1].url) - expect(res.body.data).to.be.an('array') - // Video is unlisted - expect(res.body.data).to.have.lengthOf(1) + await waitJobs(servers) - const resVideo = await getVideo(servers[1].url, videos[0].uuid) - expect(resVideo.body).not.to.be.undefined + { + const { data } = await servers[2].videos.list() + expect(data).to.be.an('array') + // 1 video is unlisted + expect(data).to.have.lengthOf(12) + } - await completeVideoCheck(servers[1].url, resVideo.body, unlistedCheckAttributes) + // Check unlisted video + const video = await servers[2].videos.get({ id: unlistedVideo.uuid }) + await completeVideoCheck(servers[2], video, unlistedCheckAttributes) }) - it('Should send comments on a video to server 2, and automatically fetch the video', async function () { + it('Should send comments on a video to server 3, and automatically fetch the video', async function () { this.timeout(25000) - await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, commentIdServer1, 'comment 1-3') + await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer1, text: 'comment 1-3' }) - await wait(5000) + await waitJobs(servers) - const resVideo = await getVideo(servers[1].url, videos[0].uuid) - expect(resVideo.body).not.to.be.undefined - - await completeVideoCheck(servers[1].url, resVideo.body, unlistedCheckAttributes) + await servers[2].videos.get({ id: missedVideo2.uuid }) { - let resComment = await getVideoCommentThreads(servers[1].url, videos[1].uuid, 0, 5) - expect(resComment.body.data).to.be.an('array') - expect(resComment.body.data).to.have.lengthOf(1) - - threadIdServer2 = resComment.body.data[0].id + const { data } = await servers[2].comments.listThreads({ videoId: missedVideo2.uuid }) + expect(data).to.be.an('array') + expect(data).to.have.lengthOf(1) - resComment = await getVideoThreadComments(servers[1].url, videos[1].uuid, threadIdServer2) + threadIdServer2 = data[0].id - const tree: VideoCommentThreadTree = resComment.body + const tree = await servers[2].comments.getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer2 }) expect(tree.comment.text).equal('thread 1') expect(tree.children).to.have.lengthOf(1) @@ -228,41 +254,80 @@ describe('Test handle downs', function () { it('Should correctly reply to the comment', async function () { this.timeout(15000) - await addVideoCommentReply(servers[1].url, servers[1].accessToken, videos[1].uuid, commentIdServer2, 'comment 1-4') + await servers[2].comments.addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer2, text: 'comment 1-4' }) - await wait(5000) + await waitJobs(servers) - { - const resComment = await getVideoThreadComments(servers[0].url, videos[1].uuid, threadIdServer1) + const tree = await commentCommands[0].getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer1 }) - const tree: VideoCommentThreadTree = resComment.body - expect(tree.comment.text).equal('thread 1') - expect(tree.children).to.have.lengthOf(1) + 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 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 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(1) + const childOfChildFirstChild = childOfFirstChild.children[0] + expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3') + expect(childOfChildFirstChild.children).to.have.lengthOf(1) + + const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0] + expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4') + expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0) + }) - const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0] - expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4') - expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0) + it('Should upload many videos on server 1', async function () { + this.timeout(120000) + + for (let i = 0; i < 10; i++) { + const uuid = (await servers[0].videos.quickUpload({ name: 'video ' + i })).uuid + videoIdsServer1.push(uuid) } + + await waitJobs(servers) + + for (const id of videoIdsServer1) { + await servers[1].videos.get({ id }) + } + + await waitJobs(servers) + await servers[1].sql.setActorFollowScores(20) + + // Wait video expiration + await wait(11000) + + // Refresh video -> score + 10 = 30 + await servers[1].videos.get({ id: videoIdsServer1[0] }) + + await waitJobs(servers) }) - after(async function () { - killallServers(servers) + it('Should remove followings that are down', async function () { + this.timeout(120000) - // Keep the logs if the test failed - if (this['ok']) { - await flushTests() + await killallServers([ servers[0] ]) + + // Wait video expiration + await wait(11000) + + for (let i = 0; i < 5; i++) { + try { + await servers[1].videos.get({ id: videoIdsServer1[i] }) + await waitJobs([ servers[1] ]) + await wait(1500) + } catch {} + } + + for (const id of videoIdsServer1) { + await servers[1].videos.get({ id, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) } }) + + after(async function () { + await cleanupTests(servers) + }) })