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