aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/redundancy/redundancy-constraints.ts
blob: 17c6b25a59d94545940266ce96aeb41f0bc09f79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import { expect } from 'chai'
import { VideoPrivacy } from '@shared/models'
import {
  cleanupTests,
  createSingleServer,
  killallServers,
  PeerTubeServer,
  setAccessTokensToServers,
  waitJobs
} from '@shared/server-commands'

describe('Test redundancy constraints', function () {
  let remoteServer: PeerTubeServer
  let localServer: PeerTubeServer
  let servers: PeerTubeServer[]

  const remoteServerConfig = {
    redundancy: {
      videos: {
        check_interval: '1 second',
        strategies: [
          {
            strategy: 'recently-added',
            min_lifetime: '1 hour',
            size: '100MB',
            min_views: 0
          }
        ]
      }
    }
  }

  async function uploadWrapper (videoName: string) {
    // Wait for transcoding
    const { id } = await localServer.videos.upload({ attributes: { name: 'to transcode', privacy: VideoPrivacy.PRIVATE } })
    await waitJobs([ localServer ])

    // Update video to schedule a federation
    await localServer.videos.update({ id, attributes: { name: videoName, privacy: VideoPrivacy.PUBLIC } })
  }

  async function getTotalRedundanciesLocalServer () {
    const body = await localServer.redundancy.listVideos({ target: 'my-videos' })

    return body.total
  }

  async function getTotalRedundanciesRemoteServer () {
    const body = await remoteServer.redundancy.listVideos({ target: 'remote-videos' })

    return body.total
  }

  before(async function () {
    this.timeout(120000)

    {
      remoteServer = await createSingleServer(1, remoteServerConfig)
    }

    {
      const config = {
        remote_redundancy: {
          videos: {
            accept_from: 'nobody'
          }
        }
      }
      localServer = await createSingleServer(2, config)
    }

    servers = [ remoteServer, localServer ]

    // Get the access tokens
    await setAccessTokensToServers(servers)

    await localServer.videos.upload({ attributes: { name: 'video 1 server 2' } })

    await waitJobs(servers)

    // Server 1 and server 2 follow each other
    await remoteServer.follows.follow({ hosts: [ localServer.url ] })
    await waitJobs(servers)
    await remoteServer.redundancy.updateRedundancy({ host: localServer.host, redundancyAllowed: true })

    await waitJobs(servers)
  })

  it('Should have redundancy on server 1 but not on server 2 with a nobody filter', async function () {
    this.timeout(120000)

    await waitJobs(servers)
    await remoteServer.servers.waitUntilLog('Duplicated ', 5)
    await waitJobs(servers)

    {
      const total = await getTotalRedundanciesRemoteServer()
      expect(total).to.equal(1)
    }

    {
      const total = await getTotalRedundanciesLocalServer()
      expect(total).to.equal(0)
    }
  })

  it('Should have redundancy on server 1 and on server 2 with an anybody filter', async function () {
    this.timeout(120000)

    const config = {
      remote_redundancy: {
        videos: {
          accept_from: 'anybody'
        }
      }
    }
    await killallServers([ localServer ])
    await localServer.run(config)

    await uploadWrapper('video 2 server 2')

    await remoteServer.servers.waitUntilLog('Duplicated ', 10)
    await waitJobs(servers)

    {
      const total = await getTotalRedundanciesRemoteServer()
      expect(total).to.equal(2)
    }

    {
      const total = await getTotalRedundanciesLocalServer()
      expect(total).to.equal(1)
    }
  })

  it('Should have redundancy on server 1 but not on server 2 with a followings filter', async function () {
    this.timeout(120000)

    const config = {
      remote_redundancy: {
        videos: {
          accept_from: 'followings'
        }
      }
    }
    await killallServers([ localServer ])
    await localServer.run(config)

    await uploadWrapper('video 3 server 2')

    await remoteServer.servers.waitUntilLog('Duplicated ', 15)
    await waitJobs(servers)

    {
      const total = await getTotalRedundanciesRemoteServer()
      expect(total).to.equal(3)
    }

    {
      const total = await getTotalRedundanciesLocalServer()
      expect(total).to.equal(1)
    }
  })

  it('Should have redundancy on server 1 and on server 2 with followings filter now server 2 follows server 1', async function () {
    this.timeout(120000)

    await localServer.follows.follow({ hosts: [ remoteServer.url ] })
    await waitJobs(servers)

    await uploadWrapper('video 4 server 2')
    await remoteServer.servers.waitUntilLog('Duplicated ', 20)
    await waitJobs(servers)

    {
      const total = await getTotalRedundanciesRemoteServer()
      expect(total).to.equal(4)
    }

    {
      const total = await getTotalRedundanciesLocalServer()
      expect(total).to.equal(2)
    }
  })

  after(async function () {
    await cleanupTests(servers)
  })
})