]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/cli/prune-storage.ts
Generate random uuid for video files
[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'
7import { buildUUID } from '@server/helpers/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
20} from '@shared/extra-utils'
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
254d3579 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
C
54 expect(avatarsCount).to.equal(2)
55 }
56}
57
58describe('Test prune storage scripts', function () {
254d3579 59 let servers: PeerTubeServer[]
a1587156 60 const badNames: { [directory: string]: string[] } = {}
e2600d8b
C
61
62 before(async function () {
63 this.timeout(120000)
64
254d3579 65 servers = await createMultipleServers(2, { transcoding: { enabled: true } })
e2600d8b
C
66 await setAccessTokensToServers(servers)
67 await setDefaultVideoChannel(servers)
68
69 for (const server of servers) {
89d241a7
C
70 await server.videos.upload({ attributes: { name: 'video 1' } })
71 await server.videos.upload({ attributes: { name: 'video 2' } })
e2600d8b 72
89d241a7 73 await server.users.updateMyAvatar({ fixture: 'avatar.png' })
e2600d8b 74
89d241a7 75 await server.playlists.create({
e6346d59 76 attributes: {
e2600d8b
C
77 displayName: 'playlist',
78 privacy: VideoPlaylistPrivacy.PUBLIC,
89d241a7 79 videoChannelId: server.store.channel.id,
e2600d8b
C
80 thumbnailfile: 'thumbnail.jpg'
81 }
82 })
83 }
84
85 await doubleFollow(servers[0], servers[1])
86
87 // Lazy load the remote avatar
88 {
89d241a7 89 const account = await servers[0].accounts.get({ accountName: 'root@localhost:' + servers[1].port })
66fb2aa3 90 await makeGetRequest({
a1587156 91 url: servers[0].url,
66fb2aa3 92 path: account.avatar.path,
c0e8b12e 93 expectedStatus: HttpStatusCode.OK_200
66fb2aa3 94 })
e2600d8b
C
95 }
96
97 {
89d241a7 98 const account = await servers[1].accounts.get({ accountName: 'root@localhost:' + servers[0].port })
66fb2aa3 99 await makeGetRequest({
a1587156 100 url: servers[1].url,
66fb2aa3 101 path: account.avatar.path,
c0e8b12e 102 expectedStatus: HttpStatusCode.OK_200
66fb2aa3 103 })
e2600d8b
C
104 }
105
106 await wait(1000)
107
108 await waitJobs(servers)
9293139f 109 await killallServers(servers)
52fb1d97
C
110
111 await wait(1000)
e2600d8b
C
112 })
113
114 it('Should have the files on the disk', async function () {
115 await assertCountAreOkay(servers)
116 })
117
118 it('Should create some dirty files', async function () {
119 for (let i = 0; i < 2; i++) {
120 {
89d241a7 121 const base = servers[0].servers.buildDirectory('videos')
e2600d8b 122
d4a8e7a6
C
123 const n1 = buildUUID() + '.mp4'
124 const n2 = buildUUID() + '.webm'
e2600d8b
C
125
126 await createFile(join(base, n1))
127 await createFile(join(base, n2))
128
129 badNames['videos'] = [ n1, n2 ]
130 }
131
132 {
89d241a7 133 const base = servers[0].servers.buildDirectory('torrents')
e2600d8b 134
d4a8e7a6
C
135 const n1 = buildUUID() + '-240.torrent'
136 const n2 = buildUUID() + '-480.torrent'
e2600d8b
C
137
138 await createFile(join(base, n1))
139 await createFile(join(base, n2))
140
141 badNames['torrents'] = [ n1, n2 ]
142 }
143
144 {
89d241a7 145 const base = servers[0].servers.buildDirectory('thumbnails')
e2600d8b 146
d4a8e7a6
C
147 const n1 = buildUUID() + '.jpg'
148 const n2 = buildUUID() + '.jpg'
e2600d8b
C
149
150 await createFile(join(base, n1))
151 await createFile(join(base, n2))
152
153 badNames['thumbnails'] = [ n1, n2 ]
154 }
155
156 {
89d241a7 157 const base = servers[0].servers.buildDirectory('previews')
e2600d8b 158
d4a8e7a6
C
159 const n1 = buildUUID() + '.jpg'
160 const n2 = buildUUID() + '.jpg'
e2600d8b
C
161
162 await createFile(join(base, n1))
163 await createFile(join(base, n2))
164
165 badNames['previews'] = [ n1, n2 ]
166 }
167
168 {
89d241a7 169 const base = servers[0].servers.buildDirectory('avatars')
e2600d8b 170
d4a8e7a6
C
171 const n1 = buildUUID() + '.png'
172 const n2 = buildUUID() + '.jpg'
e2600d8b
C
173
174 await createFile(join(base, n1))
175 await createFile(join(base, n2))
176
177 badNames['avatars'] = [ n1, n2 ]
178 }
179 }
180 })
181
182 it('Should run prune storage', async function () {
183 this.timeout(30000)
184
89d241a7 185 const env = servers[0].cli.getEnv()
329619b3 186 await CLICommand.exec(`echo y | ${env} npm run prune-storage`)
e2600d8b
C
187 })
188
189 it('Should have removed files', async function () {
190 await assertCountAreOkay(servers)
191
192 for (const directory of Object.keys(badNames)) {
193 for (const name of badNames[directory]) {
6c5065a0 194 await assertNotExists(servers[0], directory, name)
e2600d8b
C
195 }
196 }
197 })
198
199 after(async function () {
200 await cleanupTests(servers)
201 })
202})