]>
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' | |
7 | import { buildUUID } from '@server/helpers/uuid' | |
9fff08cf | 8 | import { HttpStatusCode } from '@shared/core-utils' |
e2600d8b | 9 | import { |
e2600d8b | 10 | cleanupTests, |
329619b3 | 11 | CLICommand, |
e2600d8b | 12 | doubleFollow, |
e2600d8b | 13 | flushAndRunMultipleServers, |
52fb1d97 | 14 | killallServers, |
a1587156 | 15 | makeGetRequest, |
e2600d8b | 16 | ServerInfo, |
a1587156 C |
17 | setAccessTokensToServers, |
18 | setDefaultVideoChannel, | |
9fff08cf C |
19 | wait, |
20 | waitJobs | |
21 | } from '@shared/extra-utils' | |
22 | import { VideoPlaylistPrivacy } from '@shared/models' | |
e2600d8b C |
23 | |
24 | const expect = chai.expect | |
25 | ||
6c5065a0 C |
26 | async function countFiles (server: ServerInfo, directory: string) { |
27 | const files = await readdir(server.serversCommand.buildDirectory(directory)) | |
e2600d8b C |
28 | |
29 | return files.length | |
30 | } | |
31 | ||
6c5065a0 C |
32 | async function assertNotExists (server: ServerInfo, directory: string, substring: string) { |
33 | const files = await readdir(server.serversCommand.buildDirectory(directory)) | |
e2600d8b C |
34 | |
35 | for (const f of files) { | |
36 | expect(f).to.not.contain(substring) | |
37 | } | |
38 | } | |
39 | ||
40 | async function assertCountAreOkay (servers: ServerInfo[]) { | |
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 | ||
59 | describe('Test prune storage scripts', function () { | |
60 | let servers: ServerInfo[] | |
a1587156 | 61 | const badNames: { [directory: string]: string[] } = {} |
e2600d8b C |
62 | |
63 | before(async function () { | |
64 | this.timeout(120000) | |
65 | ||
66 | servers = await flushAndRunMultipleServers(2, { transcoding: { enabled: true } }) | |
67 | await setAccessTokensToServers(servers) | |
68 | await setDefaultVideoChannel(servers) | |
69 | ||
70 | for (const server of servers) { | |
d23dd9fb C |
71 | await server.videosCommand.upload({ attributes: { name: 'video 1' } }) |
72 | await server.videosCommand.upload({ attributes: { name: 'video 2' } }) | |
e2600d8b | 73 | |
7926c5f9 | 74 | await server.usersCommand.updateMyAvatar({ fixture: 'avatar.png' }) |
e2600d8b | 75 | |
e6346d59 C |
76 | await server.playlistsCommand.create({ |
77 | attributes: { | |
e2600d8b C |
78 | displayName: 'playlist', |
79 | privacy: VideoPlaylistPrivacy.PUBLIC, | |
80 | videoChannelId: server.videoChannel.id, | |
81 | thumbnailfile: 'thumbnail.jpg' | |
82 | } | |
83 | }) | |
84 | } | |
85 | ||
86 | await doubleFollow(servers[0], servers[1]) | |
87 | ||
88 | // Lazy load the remote avatar | |
89 | { | |
9fff08cf | 90 | const account = await servers[0].accountsCommand.get({ accountName: 'root@localhost:' + servers[1].port }) |
66fb2aa3 | 91 | await makeGetRequest({ |
a1587156 | 92 | url: servers[0].url, |
66fb2aa3 | 93 | path: account.avatar.path, |
2d53be02 | 94 | statusCodeExpected: HttpStatusCode.OK_200 |
66fb2aa3 | 95 | }) |
e2600d8b C |
96 | } |
97 | ||
98 | { | |
9fff08cf | 99 | const account = await servers[1].accountsCommand.get({ accountName: 'root@localhost:' + servers[0].port }) |
66fb2aa3 | 100 | await makeGetRequest({ |
a1587156 | 101 | url: servers[1].url, |
66fb2aa3 | 102 | path: account.avatar.path, |
2d53be02 | 103 | statusCodeExpected: 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 | { | |
6c5065a0 | 122 | const base = servers[0].serversCommand.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 | { | |
6c5065a0 | 134 | const base = servers[0].serversCommand.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 | { | |
6c5065a0 | 146 | const base = servers[0].serversCommand.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 | { | |
6c5065a0 | 158 | const base = servers[0].serversCommand.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 | { | |
6c5065a0 | 170 | const base = servers[0].serversCommand.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 | ||
329619b3 C |
186 | const env = servers[0].cliCommand.getEnv() |
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 | }) |