]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/cli/prune-storage.ts
shared/ typescript types dir server-commands
[github/Chocobozzz/PeerTube.git] / server / tests / cli / prune-storage.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
e2600d8b
C
2
3import 'mocha'
4import * as chai from 'chai'
d4a8e7a6
C
5import { createFile, readdir } from 'fs-extra'
6import { join } from 'path'
06aad801 7import { buildUUID } from '@shared/core-utils/uuid'
e2600d8b 8import {
e2600d8b 9 cleanupTests,
329619b3 10 CLICommand,
254d3579 11 createMultipleServers,
4c7e60bc 12 doubleFollow,
52fb1d97 13 killallServers,
a1587156 14 makeGetRequest,
254d3579 15 PeerTubeServer,
a1587156
C
16 setAccessTokensToServers,
17 setDefaultVideoChannel,
9fff08cf
C
18 wait,
19 waitJobs
bf54587a 20} from '@shared/server-commands'
4c7e60bc 21import { HttpStatusCode, VideoPlaylistPrivacy } from '@shared/models'
e2600d8b
C
22
23const expect = chai.expect
24
254d3579 25async function countFiles (server: PeerTubeServer, directory: string) {
89d241a7 26 const files = await readdir(server.servers.buildDirectory(directory))
e2600d8b
C
27
28 return files.length
29}
30
254d3579 31async function assertNotExists (server: PeerTubeServer, directory: string, substring: string) {
89d241a7 32 const files = await readdir(server.servers.buildDirectory(directory))
e2600d8b
C
33
34 for (const f of files) {
35 expect(f).to.not.contain(substring)
36 }
37}
38
90701ec1 39async function assertCountAreOkay (servers: PeerTubeServer[]) {
e2600d8b 40 for (const server of servers) {
6c5065a0 41 const videosCount = await countFiles(server, 'videos')
e2600d8b
C
42 expect(videosCount).to.equal(8)
43
6c5065a0 44 const torrentsCount = await countFiles(server, 'torrents')
66fb2aa3 45 expect(torrentsCount).to.equal(16)
e2600d8b 46
6c5065a0 47 const previewsCount = await countFiles(server, 'previews')
e2600d8b
C
48 expect(previewsCount).to.equal(2)
49
6c5065a0 50 const thumbnailsCount = await countFiles(server, 'thumbnails')
e2600d8b
C
51 expect(thumbnailsCount).to.equal(6)
52
6c5065a0 53 const avatarsCount = await countFiles(server, 'avatars')
e2600d8b 54 expect(avatarsCount).to.equal(2)
764b1a14 55
90701ec1
C
56 const hlsRootCount = await countFiles(server, 'streaming-playlists/hls')
57 expect(hlsRootCount).to.equal(2)
58 }
e2600d8b
C
59}
60
61describe('Test prune storage scripts', function () {
254d3579 62 let servers: PeerTubeServer[]
a1587156 63 const badNames: { [directory: string]: string[] } = {}
e2600d8b
C
64
65 before(async function () {
66 this.timeout(120000)
67
254d3579 68 servers = await createMultipleServers(2, { transcoding: { enabled: true } })
e2600d8b
C
69 await setAccessTokensToServers(servers)
70 await setDefaultVideoChannel(servers)
71
72 for (const server of servers) {
89d241a7 73 await server.videos.upload({ attributes: { name: 'video 1' } })
90701ec1 74 await server.videos.upload({ attributes: { name: 'video 2' } })
e2600d8b 75
89d241a7 76 await server.users.updateMyAvatar({ fixture: 'avatar.png' })
e2600d8b 77
89d241a7 78 await server.playlists.create({
e6346d59 79 attributes: {
e2600d8b
C
80 displayName: 'playlist',
81 privacy: VideoPlaylistPrivacy.PUBLIC,
89d241a7 82 videoChannelId: server.store.channel.id,
e2600d8b
C
83 thumbnailfile: 'thumbnail.jpg'
84 }
85 })
86 }
87
88 await doubleFollow(servers[0], servers[1])
89
90 // Lazy load the remote avatar
91 {
89d241a7 92 const account = await servers[0].accounts.get({ accountName: 'root@localhost:' + servers[1].port })
66fb2aa3 93 await makeGetRequest({
a1587156 94 url: servers[0].url,
66fb2aa3 95 path: account.avatar.path,
c0e8b12e 96 expectedStatus: HttpStatusCode.OK_200
66fb2aa3 97 })
e2600d8b
C
98 }
99
100 {
89d241a7 101 const account = await servers[1].accounts.get({ accountName: 'root@localhost:' + servers[0].port })
66fb2aa3 102 await makeGetRequest({
a1587156 103 url: servers[1].url,
66fb2aa3 104 path: account.avatar.path,
c0e8b12e 105 expectedStatus: HttpStatusCode.OK_200
66fb2aa3 106 })
e2600d8b
C
107 }
108
109 await wait(1000)
110
111 await waitJobs(servers)
9293139f 112 await killallServers(servers)
52fb1d97
C
113
114 await wait(1000)
e2600d8b
C
115 })
116
117 it('Should have the files on the disk', async function () {
90701ec1 118 await assertCountAreOkay(servers)
e2600d8b
C
119 })
120
121 it('Should create some dirty files', async function () {
122 for (let i = 0; i < 2; i++) {
123 {
89d241a7 124 const base = servers[0].servers.buildDirectory('videos')
e2600d8b 125
d4a8e7a6
C
126 const n1 = buildUUID() + '.mp4'
127 const n2 = buildUUID() + '.webm'
e2600d8b
C
128
129 await createFile(join(base, n1))
130 await createFile(join(base, n2))
131
132 badNames['videos'] = [ n1, n2 ]
133 }
134
135 {
89d241a7 136 const base = servers[0].servers.buildDirectory('torrents')
e2600d8b 137
d4a8e7a6
C
138 const n1 = buildUUID() + '-240.torrent'
139 const n2 = buildUUID() + '-480.torrent'
e2600d8b
C
140
141 await createFile(join(base, n1))
142 await createFile(join(base, n2))
143
144 badNames['torrents'] = [ n1, n2 ]
145 }
146
147 {
89d241a7 148 const base = servers[0].servers.buildDirectory('thumbnails')
e2600d8b 149
d4a8e7a6
C
150 const n1 = buildUUID() + '.jpg'
151 const n2 = buildUUID() + '.jpg'
e2600d8b
C
152
153 await createFile(join(base, n1))
154 await createFile(join(base, n2))
155
156 badNames['thumbnails'] = [ n1, n2 ]
157 }
158
159 {
89d241a7 160 const base = servers[0].servers.buildDirectory('previews')
e2600d8b 161
d4a8e7a6
C
162 const n1 = buildUUID() + '.jpg'
163 const n2 = buildUUID() + '.jpg'
e2600d8b
C
164
165 await createFile(join(base, n1))
166 await createFile(join(base, n2))
167
168 badNames['previews'] = [ n1, n2 ]
169 }
170
171 {
89d241a7 172 const base = servers[0].servers.buildDirectory('avatars')
e2600d8b 173
d4a8e7a6
C
174 const n1 = buildUUID() + '.png'
175 const n2 = buildUUID() + '.jpg'
e2600d8b
C
176
177 await createFile(join(base, n1))
178 await createFile(join(base, n2))
179
180 badNames['avatars'] = [ n1, n2 ]
181 }
764b1a14 182
90701ec1
C
183 {
184 const directory = join('streaming-playlists', 'hls')
185 const base = servers[0].servers.buildDirectory(directory)
764b1a14 186
90701ec1
C
187 const n1 = buildUUID()
188 await createFile(join(base, n1))
189 badNames[directory] = [ n1 ]
190 }
e2600d8b
C
191 }
192 })
193
194 it('Should run prune storage', async function () {
195 this.timeout(30000)
196
89d241a7 197 const env = servers[0].cli.getEnv()
329619b3 198 await CLICommand.exec(`echo y | ${env} npm run prune-storage`)
e2600d8b
C
199 })
200
201 it('Should have removed files', async function () {
90701ec1 202 await assertCountAreOkay(servers)
e2600d8b
C
203
204 for (const directory of Object.keys(badNames)) {
205 for (const name of badNames[directory]) {
6c5065a0 206 await assertNotExists(servers[0], directory, name)
e2600d8b
C
207 }
208 }
209 })
210
211 after(async function () {
212 await cleanupTests(servers)
213 })
214})