diff options
Diffstat (limited to 'server/tests/cli')
-rw-r--r-- | server/tests/cli/index.ts | 1 | ||||
-rw-r--r-- | server/tests/cli/prune-storage.ts | 199 |
2 files changed, 200 insertions, 0 deletions
diff --git a/server/tests/cli/index.ts b/server/tests/cli/index.ts index 5af286fe2..029cd5196 100644 --- a/server/tests/cli/index.ts +++ b/server/tests/cli/index.ts | |||
@@ -4,5 +4,6 @@ import './create-transcoding-job' | |||
4 | import './optimize-old-videos' | 4 | import './optimize-old-videos' |
5 | import './peertube' | 5 | import './peertube' |
6 | import './plugins' | 6 | import './plugins' |
7 | import './prune-storage' | ||
7 | import './reset-password' | 8 | import './reset-password' |
8 | import './update-host' | 9 | import './update-host' |
diff --git a/server/tests/cli/prune-storage.ts b/server/tests/cli/prune-storage.ts new file mode 100644 index 000000000..67a5c564e --- /dev/null +++ b/server/tests/cli/prune-storage.ts | |||
@@ -0,0 +1,199 @@ | |||
1 | /* tslint:disable:no-unused-expression */ | ||
2 | |||
3 | import 'mocha' | ||
4 | import * as chai from 'chai' | ||
5 | import { waitJobs } from '../../../shared/extra-utils/server/jobs' | ||
6 | import { | ||
7 | buildServerDirectory, | ||
8 | cleanupTests, | ||
9 | createVideoPlaylist, | ||
10 | doubleFollow, | ||
11 | execCLI, | ||
12 | flushAndRunMultipleServers, | ||
13 | getAccount, | ||
14 | getEnvCli, | ||
15 | ServerInfo, | ||
16 | setAccessTokensToServers, setDefaultVideoChannel, | ||
17 | updateMyAvatar, | ||
18 | uploadVideo, | ||
19 | wait | ||
20 | } from '../../../shared/extra-utils' | ||
21 | import { Account, VideoPlaylistPrivacy } from '../../../shared/models' | ||
22 | import { createFile, readdir } from 'fs-extra' | ||
23 | import * as uuidv4 from 'uuid/v4' | ||
24 | import { join } from 'path' | ||
25 | import * as request from 'supertest' | ||
26 | |||
27 | const expect = chai.expect | ||
28 | |||
29 | async function countFiles (internalServerNumber: number, directory: string) { | ||
30 | const files = await readdir(buildServerDirectory(internalServerNumber, directory)) | ||
31 | |||
32 | return files.length | ||
33 | } | ||
34 | |||
35 | async function assertNotExists (internalServerNumber: number, directory: string, substring: string) { | ||
36 | const files = await readdir(buildServerDirectory(internalServerNumber, directory)) | ||
37 | |||
38 | for (const f of files) { | ||
39 | expect(f).to.not.contain(substring) | ||
40 | } | ||
41 | } | ||
42 | |||
43 | async function assertCountAreOkay (servers: ServerInfo[]) { | ||
44 | for (const server of servers) { | ||
45 | const videosCount = await countFiles(server.internalServerNumber, 'videos') | ||
46 | expect(videosCount).to.equal(8) | ||
47 | |||
48 | const torrentsCount = await countFiles(server.internalServerNumber, 'torrents') | ||
49 | expect(torrentsCount).to.equal(8) | ||
50 | |||
51 | const previewsCount = await countFiles(server.internalServerNumber, 'previews') | ||
52 | expect(previewsCount).to.equal(2) | ||
53 | |||
54 | const thumbnailsCount = await countFiles(server.internalServerNumber, 'thumbnails') | ||
55 | expect(thumbnailsCount).to.equal(6) | ||
56 | |||
57 | const avatarsCount = await countFiles(server.internalServerNumber, 'avatars') | ||
58 | expect(avatarsCount).to.equal(2) | ||
59 | } | ||
60 | } | ||
61 | |||
62 | describe('Test prune storage scripts', function () { | ||
63 | let servers: ServerInfo[] | ||
64 | const badNames: { [ directory: string ]: string[] } = {} | ||
65 | |||
66 | before(async function () { | ||
67 | this.timeout(120000) | ||
68 | |||
69 | servers = await flushAndRunMultipleServers(2, { transcoding: { enabled: true } }) | ||
70 | await setAccessTokensToServers(servers) | ||
71 | await setDefaultVideoChannel(servers) | ||
72 | |||
73 | for (const server of servers) { | ||
74 | await uploadVideo(server.url, server.accessToken, { name: 'video 1' }) | ||
75 | await uploadVideo(server.url, server.accessToken, { name: 'video 2' }) | ||
76 | |||
77 | await updateMyAvatar({ url: server.url, accessToken: server.accessToken, fixture: 'avatar.png' }) | ||
78 | |||
79 | await createVideoPlaylist({ | ||
80 | url: server.url, | ||
81 | token: server.accessToken, | ||
82 | playlistAttrs: { | ||
83 | displayName: 'playlist', | ||
84 | privacy: VideoPlaylistPrivacy.PUBLIC, | ||
85 | videoChannelId: server.videoChannel.id, | ||
86 | thumbnailfile: 'thumbnail.jpg' | ||
87 | } | ||
88 | }) | ||
89 | } | ||
90 | |||
91 | await doubleFollow(servers[0], servers[1]) | ||
92 | |||
93 | // Lazy load the remote avatar | ||
94 | { | ||
95 | const res = await getAccount(servers[ 0 ].url, 'root@localhost:' + servers[ 1 ].port) | ||
96 | const account: Account = res.body | ||
97 | await request('http://localhost:' + servers[ 0 ].port).get(account.avatar.path).expect(200) | ||
98 | } | ||
99 | |||
100 | { | ||
101 | const res = await getAccount(servers[ 1 ].url, 'root@localhost:' + servers[ 0 ].port) | ||
102 | const account: Account = res.body | ||
103 | await request('http://localhost:' + servers[ 1 ].port).get(account.avatar.path).expect(200) | ||
104 | } | ||
105 | |||
106 | await wait(1000) | ||
107 | |||
108 | await waitJobs(servers) | ||
109 | }) | ||
110 | |||
111 | it('Should have the files on the disk', async function () { | ||
112 | await assertCountAreOkay(servers) | ||
113 | }) | ||
114 | |||
115 | it('Should create some dirty files', async function () { | ||
116 | for (let i = 0; i < 2; i++) { | ||
117 | { | ||
118 | const base = buildServerDirectory(servers[0].internalServerNumber, 'videos') | ||
119 | |||
120 | const n1 = uuidv4() + '.mp4' | ||
121 | const n2 = uuidv4() + '.webm' | ||
122 | |||
123 | await createFile(join(base, n1)) | ||
124 | await createFile(join(base, n2)) | ||
125 | |||
126 | badNames['videos'] = [ n1, n2 ] | ||
127 | } | ||
128 | |||
129 | { | ||
130 | const base = buildServerDirectory(servers[0].internalServerNumber, 'torrents') | ||
131 | |||
132 | const n1 = uuidv4() + '-240.torrent' | ||
133 | const n2 = uuidv4() + '-480.torrent' | ||
134 | |||
135 | await createFile(join(base, n1)) | ||
136 | await createFile(join(base, n2)) | ||
137 | |||
138 | badNames['torrents'] = [ n1, n2 ] | ||
139 | } | ||
140 | |||
141 | { | ||
142 | const base = buildServerDirectory(servers[0].internalServerNumber, 'thumbnails') | ||
143 | |||
144 | const n1 = uuidv4() + '.jpg' | ||
145 | const n2 = uuidv4() + '.jpg' | ||
146 | |||
147 | await createFile(join(base, n1)) | ||
148 | await createFile(join(base, n2)) | ||
149 | |||
150 | badNames['thumbnails'] = [ n1, n2 ] | ||
151 | } | ||
152 | |||
153 | { | ||
154 | const base = buildServerDirectory(servers[0].internalServerNumber, 'previews') | ||
155 | |||
156 | const n1 = uuidv4() + '.jpg' | ||
157 | const n2 = uuidv4() + '.jpg' | ||
158 | |||
159 | await createFile(join(base, n1)) | ||
160 | await createFile(join(base, n2)) | ||
161 | |||
162 | badNames['previews'] = [ n1, n2 ] | ||
163 | } | ||
164 | |||
165 | { | ||
166 | const base = buildServerDirectory(servers[0].internalServerNumber, 'avatars') | ||
167 | |||
168 | const n1 = uuidv4() + '.png' | ||
169 | const n2 = uuidv4() + '.jpg' | ||
170 | |||
171 | await createFile(join(base, n1)) | ||
172 | await createFile(join(base, n2)) | ||
173 | |||
174 | badNames['avatars'] = [ n1, n2 ] | ||
175 | } | ||
176 | } | ||
177 | }) | ||
178 | |||
179 | it('Should run prune storage', async function () { | ||
180 | this.timeout(30000) | ||
181 | |||
182 | const env = getEnvCli(servers[0]) | ||
183 | await execCLI(`echo y | ${env} npm run prune-storage`) | ||
184 | }) | ||
185 | |||
186 | it('Should have removed files', async function () { | ||
187 | await assertCountAreOkay(servers) | ||
188 | |||
189 | for (const directory of Object.keys(badNames)) { | ||
190 | for (const name of badNames[directory]) { | ||
191 | await assertNotExists(servers[0].internalServerNumber, directory, name) | ||
192 | } | ||
193 | } | ||
194 | }) | ||
195 | |||
196 | after(async function () { | ||
197 | await cleanupTests(servers) | ||
198 | }) | ||
199 | }) | ||