]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/redundancy/redundancy.ts
Add signup approval API REST doc
[github/Chocobozzz/PeerTube.git] / server / tests / api / redundancy / redundancy.ts
index e1a12f5f8c521be2884dac9ec1f8b4e2c4a2386e..57939775e30448d8c8c1e29eabe74d9f6c0e09b9 100644 (file)
@@ -1,35 +1,29 @@
 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
-import 'mocha'
-import * as chai from 'chai'
+import { expect } from 'chai'
 import { readdir } from 'fs-extra'
-import * as magnetUtil from 'magnet-uri'
+import magnetUtil from 'magnet-uri'
 import { basename, join } from 'path'
+import { checkSegmentHash, checkVideoFilesWereRemoved, saveVideoInServers } from '@server/tests/shared'
+import { wait } from '@shared/core-utils'
+import {
+  HttpStatusCode,
+  VideoDetails,
+  VideoFile,
+  VideoPrivacy,
+  VideoRedundancyStrategy,
+  VideoRedundancyStrategyWithManual
+} from '@shared/models'
 import {
-  checkSegmentHash,
-  checkVideoFilesWereRemoved,
   cleanupTests,
   createMultipleServers,
   doubleFollow,
   killallServers,
   makeRawRequest,
   PeerTubeServer,
-  root,
-  saveVideoInServers,
   setAccessTokensToServers,
-  wait,
   waitJobs
-} from '@shared/extra-utils'
-import {
-  HttpStatusCode,
-  VideoDetails,
-  VideoFile,
-  VideoPrivacy,
-  VideoRedundancyStrategy,
-  VideoRedundancyStrategyWithManual
-} from '@shared/models'
-
-const expect = chai.expect
+} from '@shared/server-commands'
 
 let servers: PeerTubeServer[] = []
 let video1Server2: VideoDetails
@@ -45,18 +39,18 @@ async function checkMagnetWebseeds (file: VideoFile, baseWebseeds: string[], ser
   expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
 
   for (const url of parsed.urlList) {
-    await makeRawRequest(url, HttpStatusCode.OK_200)
+    await makeRawRequest({ url, expectedStatus: HttpStatusCode.OK_200 })
   }
 }
 
-async function createSingleServers (strategy: VideoRedundancyStrategy | null, additionalParams: any = {}, withWebtorrent = true) {
+async function createServers (strategy: VideoRedundancyStrategy | null, additionalParams: any = {}, withWebtorrent = true) {
   const strategies: any[] = []
 
   if (strategy !== null) {
     strategies.push(
       {
         min_lifetime: '1 hour',
-        strategy: strategy,
+        strategy,
         size: '400KB',
 
         ...additionalParams
@@ -90,7 +84,7 @@ async function createSingleServers (strategy: VideoRedundancyStrategy | null, ad
     const { id } = await servers[1].videos.upload({ attributes: { name: 'video 1 server 2' } })
     video1Server2 = await servers[1].videos.get({ id })
 
-    await servers[1].videos.view({ id })
+    await servers[1].views.simulateView({ id })
   }
 
   await waitJobs(servers)
@@ -131,7 +125,7 @@ async function check1WebSeed (videoUUID?: string) {
   if (!videoUUID) videoUUID = video1Server2.uuid
 
   const webseeds = [
-    `http://localhost:${servers[1].port}/static/webseed/`
+    `${servers[1].url}/static/webseed/`
   ]
 
   for (const server of servers) {
@@ -150,8 +144,8 @@ async function check2Webseeds (videoUUID?: string) {
   if (!videoUUID) videoUUID = video1Server2.uuid
 
   const webseeds = [
-    `http://localhost:${servers[0].port}/static/redundancy/`,
-    `http://localhost:${servers[1].port}/static/webseed/`
+    `${servers[0].url}/static/redundancy/`,
+    `${servers[1].url}/static/webseed/`
   ]
 
   for (const server of servers) {
@@ -165,12 +159,12 @@ async function check2Webseeds (videoUUID?: string) {
   const { webtorrentFilenames } = await ensureSameFilenames(videoUUID)
 
   const directories = [
-    'test' + servers[0].internalServerNumber + '/redundancy',
-    'test' + servers[1].internalServerNumber + '/videos'
+    servers[0].getDirectoryPath('redundancy'),
+    servers[1].getDirectoryPath('videos')
   ]
 
   for (const directory of directories) {
-    const files = await readdir(join(root(), directory))
+    const files = await readdir(directory)
     expect(files).to.have.length.at.least(4)
 
     // Ensure we files exist on disk
@@ -207,25 +201,25 @@ async function check1PlaylistRedundancies (videoUUID?: string) {
     expect(redundancy.baseUrl).to.equal(servers[0].url + '/static/redundancy/hls/' + videoUUID)
   }
 
-  const baseUrlPlaylist = servers[1].url + '/static/streaming-playlists/hls'
-  const baseUrlSegment = servers[0].url + '/static/redundancy/hls'
+  const baseUrlPlaylist = servers[1].url + '/static/streaming-playlists/hls/' + videoUUID
+  const baseUrlSegment = servers[0].url + '/static/redundancy/hls/' + videoUUID
 
   const video = await servers[0].videos.get({ id: videoUUID })
   const hlsPlaylist = video.streamingPlaylists[0]
 
   for (const resolution of [ 240, 360, 480, 720 ]) {
-    await checkSegmentHash({ server: servers[1], baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist })
+    await checkSegmentHash({ server: servers[1], baseUrlPlaylist, baseUrlSegment, resolution, hlsPlaylist })
   }
 
   const { hlsFilenames } = await ensureSameFilenames(videoUUID)
 
   const directories = [
-    'test' + servers[0].internalServerNumber + '/redundancy/hls',
-    'test' + servers[1].internalServerNumber + '/streaming-playlists/hls'
+    servers[0].getDirectoryPath('redundancy/hls'),
+    servers[1].getDirectoryPath('streaming-playlists/hls')
   ]
 
   for (const directory of directories) {
-    const files = await readdir(join(root(), directory, videoUUID))
+    const files = await readdir(join(directory, videoUUID))
     expect(files).to.have.length.at.least(4)
 
     // Ensure we files exist on disk
@@ -271,8 +265,8 @@ async function checkStatsWithoutRedundancy (strategy: VideoRedundancyStrategyWit
 async function findServerFollows () {
   const body = await servers[0].follows.getFollowings({ start: 0, count: 5, sort: '-createdAt' })
   const follows = body.data
-  const server2 = follows.find(f => f.following.host === `localhost:${servers[1].port}`)
-  const server3 = follows.find(f => f.following.host === `localhost:${servers[2].port}`)
+  const server2 = follows.find(f => f.following.host === `${servers[1].host}`)
+  const server3 = follows.find(f => f.following.host === `${servers[2].host}`)
 
   return { server2, server3 }
 }
@@ -307,9 +301,9 @@ describe('Test videos redundancy', function () {
     const strategy = 'most-views'
 
     before(function () {
-      this.timeout(120000)
+      this.timeout(240000)
 
-      return createSingleServers(strategy)
+      return createServers(strategy)
     })
 
     it('Should have 1 webseed on the first video', async function () {
@@ -357,9 +351,9 @@ describe('Test videos redundancy', function () {
     const strategy = 'trending'
 
     before(function () {
-      this.timeout(120000)
+      this.timeout(240000)
 
-      return createSingleServers(strategy)
+      return createServers(strategy)
     })
 
     it('Should have 1 webseed on the first video', async function () {
@@ -384,7 +378,20 @@ describe('Test videos redundancy', function () {
       await checkStatsWith1Redundancy(strategy)
     })
 
-    it('Should unfollow on server 1 and remove duplicated videos', async function () {
+    it('Should unfollow server 3 and keep duplicated videos', async function () {
+      this.timeout(80000)
+
+      await servers[0].follows.unfollow({ target: servers[2] })
+
+      await waitJobs(servers)
+      await wait(5000)
+
+      await check2Webseeds()
+      await check1PlaylistRedundancies()
+      await checkStatsWith1Redundancy(strategy)
+    })
+
+    it('Should unfollow server 2 and remove duplicated videos', async function () {
       this.timeout(80000)
 
       await servers[0].follows.unfollow({ target: servers[1] })
@@ -407,9 +414,9 @@ describe('Test videos redundancy', function () {
     const strategy = 'recently-added'
 
     before(function () {
-      this.timeout(120000)
+      this.timeout(240000)
 
-      return createSingleServers(strategy, { min_views: 3 })
+      return createServers(strategy, { min_views: 3 })
     })
 
     it('Should have 1 webseed on the first video', async function () {
@@ -437,8 +444,8 @@ describe('Test videos redundancy', function () {
     it('Should view 2 times the first video to have > min_views config', async function () {
       this.timeout(80000)
 
-      await servers[0].videos.view({ id: video1Server2.uuid })
-      await servers[2].videos.view({ id: video1Server2.uuid })
+      await servers[0].views.simulateView({ id: video1Server2.uuid })
+      await servers[2].views.simulateView({ id: video1Server2.uuid })
 
       await wait(10000)
       await waitJobs(servers)
@@ -478,9 +485,9 @@ describe('Test videos redundancy', function () {
     const strategy = 'recently-added'
 
     before(async function () {
-      this.timeout(120000)
+      this.timeout(240000)
 
-      await createSingleServers(strategy, { min_views: 3 }, false)
+      await createServers(strategy, { min_views: 3 }, false)
     })
 
     it('Should have 0 playlist redundancy on the first video', async function () {
@@ -506,8 +513,8 @@ describe('Test videos redundancy', function () {
     it('Should have 1 redundancy on the first video', async function () {
       this.timeout(160000)
 
-      await servers[0].videos.view({ id: video1Server2.uuid })
-      await servers[2].videos.view({ id: video1Server2.uuid })
+      await servers[0].views.simulateView({ id: video1Server2.uuid })
+      await servers[2].views.simulateView({ id: video1Server2.uuid })
 
       await wait(10000)
       await waitJobs(servers)
@@ -540,9 +547,9 @@ describe('Test videos redundancy', function () {
 
   describe('With manual strategy', function () {
     before(function () {
-      this.timeout(120000)
+      this.timeout(240000)
 
-      return createSingleServers(null)
+      return createServers(null)
     })
 
     it('Should have 1 webseed on the first video', async function () {
@@ -619,9 +626,9 @@ describe('Test videos redundancy', function () {
     }
 
     before(async function () {
-      this.timeout(120000)
+      this.timeout(240000)
 
-      await createSingleServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
+      await createServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
 
       await enableRedundancyOnServer1()
     })
@@ -632,12 +639,12 @@ describe('Test videos redundancy', function () {
       await wait(10000)
 
       try {
-        await checkContains(servers, 'http%3A%2F%2Flocalhost%3A' + servers[0].port)
+        await checkContains(servers, 'http%3A%2F%2F' + servers[0].hostname + '%3A' + servers[0].port)
       } catch {
         // Maybe a server deleted a redundancy in the scheduler
         await wait(2000)
 
-        await checkContains(servers, 'http%3A%2F%2Flocalhost%3A' + servers[0].port)
+        await checkContains(servers, 'http%3A%2F%2F' + servers[0].hostname + '%3A' + servers[0].port)
       }
     })
 
@@ -648,7 +655,7 @@ describe('Test videos redundancy', function () {
 
       await wait(15000)
 
-      await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2Flocalhost%3A' + servers[0].port)
+      await checkNotContains([ servers[1], servers[2] ], 'http%3A%2F%2F' + servers[0].port + '%3A' + servers[0].port)
     })
 
     after(async function () {
@@ -661,9 +668,9 @@ describe('Test videos redundancy', function () {
     const strategy = 'recently-added'
 
     before(async function () {
-      this.timeout(120000)
+      this.timeout(240000)
 
-      await createSingleServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
+      await createServers(strategy, { min_lifetime: '7 seconds', min_views: 0 })
 
       await enableRedundancyOnServer1()
 
@@ -685,7 +692,7 @@ describe('Test videos redundancy', function () {
     })
 
     it('Should cache video 2 webseeds on the first video', async function () {
-      this.timeout(120000)
+      this.timeout(240000)
 
       await waitJobs(servers)