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