aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/cli/prune-storage.ts
blob: 2d4c02da74482aee2148e8a6f10f9591d00ccc2b (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import * as chai from 'chai'
import { createFile, readdir } from 'fs-extra'
import { join } from 'path'
import { buildUUID } from '@server/helpers/uuid'
import {
  cleanupTests,
  CLICommand,
  createMultipleServers,
  doubleFollow,
  killallServers,
  makeGetRequest,
  PeerTubeServer,
  setAccessTokensToServers,
  setDefaultVideoChannel,
  wait,
  waitJobs
} from '@shared/extra-utils'
import { HttpStatusCode, VideoPlaylistPrivacy } from '@shared/models'

const expect = chai.expect

async function countFiles (server: PeerTubeServer, directory: string) {
  const files = await readdir(server.servers.buildDirectory(directory))

  return files.length
}

async function assertNotExists (server: PeerTubeServer, directory: string, substring: string) {
  const files = await readdir(server.servers.buildDirectory(directory))

  for (const f of files) {
    expect(f).to.not.contain(substring)
  }
}

async function assertCountAreOkay (servers: PeerTubeServer[], videoServer2UUID: string) {
  for (const server of servers) {
    const videosCount = await countFiles(server, 'videos')
    expect(videosCount).to.equal(8)

    const torrentsCount = await countFiles(server, 'torrents')
    expect(torrentsCount).to.equal(16)

    const previewsCount = await countFiles(server, 'previews')
    expect(previewsCount).to.equal(2)

    const thumbnailsCount = await countFiles(server, 'thumbnails')
    expect(thumbnailsCount).to.equal(6)

    const avatarsCount = await countFiles(server, 'avatars')
    expect(avatarsCount).to.equal(2)
  }

  // When we'll prune HLS directories too
  // const hlsRootCount = await countFiles(servers[1], 'streaming-playlists/hls/')
  // expect(hlsRootCount).to.equal(2)

  // const hlsCount = await countFiles(servers[1], 'streaming-playlists/hls/' + videoServer2UUID)
  // expect(hlsCount).to.equal(10)
}

describe('Test prune storage scripts', function () {
  let servers: PeerTubeServer[]
  const badNames: { [directory: string]: string[] } = {}

  let videoServer2UUID: string

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

    servers = await createMultipleServers(2, { transcoding: { enabled: true } })
    await setAccessTokensToServers(servers)
    await setDefaultVideoChannel(servers)

    for (const server of servers) {
      await server.videos.upload({ attributes: { name: 'video 1' } })

      const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } })
      if (server.serverNumber === 2) videoServer2UUID = uuid

      await server.users.updateMyAvatar({ fixture: 'avatar.png' })

      await server.playlists.create({
        attributes: {
          displayName: 'playlist',
          privacy: VideoPlaylistPrivacy.PUBLIC,
          videoChannelId: server.store.channel.id,
          thumbnailfile: 'thumbnail.jpg'
        }
      })
    }

    await doubleFollow(servers[0], servers[1])

    // Lazy load the remote avatar
    {
      const account = await servers[0].accounts.get({ accountName: 'root@localhost:' + servers[1].port })
      await makeGetRequest({
        url: servers[0].url,
        path: account.avatar.path,
        expectedStatus: HttpStatusCode.OK_200
      })
    }

    {
      const account = await servers[1].accounts.get({ accountName: 'root@localhost:' + servers[0].port })
      await makeGetRequest({
        url: servers[1].url,
        path: account.avatar.path,
        expectedStatus: HttpStatusCode.OK_200
      })
    }

    await wait(1000)

    await waitJobs(servers)
    await killallServers(servers)

    await wait(1000)
  })

  it('Should have the files on the disk', async function () {
    await assertCountAreOkay(servers, videoServer2UUID)
  })

  it('Should create some dirty files', async function () {
    for (let i = 0; i < 2; i++) {
      {
        const base = servers[0].servers.buildDirectory('videos')

        const n1 = buildUUID() + '.mp4'
        const n2 = buildUUID() + '.webm'

        await createFile(join(base, n1))
        await createFile(join(base, n2))

        badNames['videos'] = [ n1, n2 ]
      }

      {
        const base = servers[0].servers.buildDirectory('torrents')

        const n1 = buildUUID() + '-240.torrent'
        const n2 = buildUUID() + '-480.torrent'

        await createFile(join(base, n1))
        await createFile(join(base, n2))

        badNames['torrents'] = [ n1, n2 ]
      }

      {
        const base = servers[0].servers.buildDirectory('thumbnails')

        const n1 = buildUUID() + '.jpg'
        const n2 = buildUUID() + '.jpg'

        await createFile(join(base, n1))
        await createFile(join(base, n2))

        badNames['thumbnails'] = [ n1, n2 ]
      }

      {
        const base = servers[0].servers.buildDirectory('previews')

        const n1 = buildUUID() + '.jpg'
        const n2 = buildUUID() + '.jpg'

        await createFile(join(base, n1))
        await createFile(join(base, n2))

        badNames['previews'] = [ n1, n2 ]
      }

      {
        const base = servers[0].servers.buildDirectory('avatars')

        const n1 = buildUUID() + '.png'
        const n2 = buildUUID() + '.jpg'

        await createFile(join(base, n1))
        await createFile(join(base, n2))

        badNames['avatars'] = [ n1, n2 ]
      }

      // When we'll prune HLS directories too
      // {
      //   const directory = join('streaming-playlists', 'hls')
      //   const base = servers[1].servers.buildDirectory(directory)

      //   const n1 = buildUUID()
      //   await createFile(join(base, n1))
      //   badNames[directory] = [ n1 ]
      // }

      // {
      //   const directory = join('streaming-playlists', 'hls', videoServer2UUID)
      //   const base = servers[1].servers.buildDirectory(directory)
      //   const n1 = buildUUID() + '-240-fragmented-.mp4'
      //   const n2 = buildUUID() + '-master.m3u8'

      //   await createFile(join(base, n1))
      //   await createFile(join(base, n2))

      //   badNames[directory] = [ n1, n2 ]
      // }
    }
  })

  it('Should run prune storage', async function () {
    this.timeout(30000)

    const env = servers[0].cli.getEnv()
    await CLICommand.exec(`echo y | ${env} npm run prune-storage`)
  })

  it('Should have removed files', async function () {
    await assertCountAreOkay(servers, videoServer2UUID)

    for (const directory of Object.keys(badNames)) {
      for (const name of badNames[directory]) {
        await assertNotExists(servers[0], directory, name)
      }
    }
  })

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