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