aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/cli/prune-storage.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/cli/prune-storage.ts')
-rw-r--r--server/tests/cli/prune-storage.ts124
1 files changed, 73 insertions, 51 deletions
diff --git a/server/tests/cli/prune-storage.ts b/server/tests/cli/prune-storage.ts
index a0af09de8..2d4c02da7 100644
--- a/server/tests/cli/prune-storage.ts
+++ b/server/tests/cli/prune-storage.ts
@@ -5,87 +5,89 @@ import * as chai from 'chai'
5import { createFile, readdir } from 'fs-extra' 5import { createFile, readdir } from 'fs-extra'
6import { join } from 'path' 6import { join } from 'path'
7import { buildUUID } from '@server/helpers/uuid' 7import { buildUUID } from '@server/helpers/uuid'
8import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
9import { 8import {
10 buildServerDirectory,
11 cleanupTests, 9 cleanupTests,
12 createVideoPlaylist, 10 CLICommand,
11 createMultipleServers,
13 doubleFollow, 12 doubleFollow,
14 execCLI,
15 flushAndRunMultipleServers,
16 getAccount,
17 getEnvCli,
18 killallServers, 13 killallServers,
19 makeGetRequest, 14 makeGetRequest,
20 ServerInfo, 15 PeerTubeServer,
21 setAccessTokensToServers, 16 setAccessTokensToServers,
22 setDefaultVideoChannel, 17 setDefaultVideoChannel,
23 updateMyAvatar, 18 wait,
24 uploadVideo, 19 waitJobs
25 wait 20} from '@shared/extra-utils'
26} from '../../../shared/extra-utils' 21import { HttpStatusCode, VideoPlaylistPrivacy } from '@shared/models'
27import { waitJobs } from '../../../shared/extra-utils/server/jobs'
28import { Account, VideoPlaylistPrivacy } from '../../../shared/models'
29 22
30const expect = chai.expect 23const expect = chai.expect
31 24
32async function countFiles (internalServerNumber: number, directory: string) { 25async function countFiles (server: PeerTubeServer, directory: string) {
33 const files = await readdir(buildServerDirectory({ internalServerNumber }, directory)) 26 const files = await readdir(server.servers.buildDirectory(directory))
34 27
35 return files.length 28 return files.length
36} 29}
37 30
38async function assertNotExists (internalServerNumber: number, directory: string, substring: string) { 31async function assertNotExists (server: PeerTubeServer, directory: string, substring: string) {
39 const files = await readdir(buildServerDirectory({ internalServerNumber }, directory)) 32 const files = await readdir(server.servers.buildDirectory(directory))
40 33
41 for (const f of files) { 34 for (const f of files) {
42 expect(f).to.not.contain(substring) 35 expect(f).to.not.contain(substring)
43 } 36 }
44} 37}
45 38
46async function assertCountAreOkay (servers: ServerInfo[]) { 39async function assertCountAreOkay (servers: PeerTubeServer[], videoServer2UUID: string) {
47 for (const server of servers) { 40 for (const server of servers) {
48 const videosCount = await countFiles(server.internalServerNumber, 'videos') 41 const videosCount = await countFiles(server, 'videos')
49 expect(videosCount).to.equal(8) 42 expect(videosCount).to.equal(8)
50 43
51 const torrentsCount = await countFiles(server.internalServerNumber, 'torrents') 44 const torrentsCount = await countFiles(server, 'torrents')
52 expect(torrentsCount).to.equal(16) 45 expect(torrentsCount).to.equal(16)
53 46
54 const previewsCount = await countFiles(server.internalServerNumber, 'previews') 47 const previewsCount = await countFiles(server, 'previews')
55 expect(previewsCount).to.equal(2) 48 expect(previewsCount).to.equal(2)
56 49
57 const thumbnailsCount = await countFiles(server.internalServerNumber, 'thumbnails') 50 const thumbnailsCount = await countFiles(server, 'thumbnails')
58 expect(thumbnailsCount).to.equal(6) 51 expect(thumbnailsCount).to.equal(6)
59 52
60 const avatarsCount = await countFiles(server.internalServerNumber, 'avatars') 53 const avatarsCount = await countFiles(server, 'avatars')
61 expect(avatarsCount).to.equal(2) 54 expect(avatarsCount).to.equal(2)
62 } 55 }
56
57 // When we'll prune HLS directories too
58 // const hlsRootCount = await countFiles(servers[1], 'streaming-playlists/hls/')
59 // expect(hlsRootCount).to.equal(2)
60
61 // const hlsCount = await countFiles(servers[1], 'streaming-playlists/hls/' + videoServer2UUID)
62 // expect(hlsCount).to.equal(10)
63} 63}
64 64
65describe('Test prune storage scripts', function () { 65describe('Test prune storage scripts', function () {
66 let servers: ServerInfo[] 66 let servers: PeerTubeServer[]
67 const badNames: { [directory: string]: string[] } = {} 67 const badNames: { [directory: string]: string[] } = {}
68 68
69 let videoServer2UUID: string
70
69 before(async function () { 71 before(async function () {
70 this.timeout(120000) 72 this.timeout(120000)
71 73
72 servers = await flushAndRunMultipleServers(2, { transcoding: { enabled: true } }) 74 servers = await createMultipleServers(2, { transcoding: { enabled: true } })
73 await setAccessTokensToServers(servers) 75 await setAccessTokensToServers(servers)
74 await setDefaultVideoChannel(servers) 76 await setDefaultVideoChannel(servers)
75 77
76 for (const server of servers) { 78 for (const server of servers) {
77 await uploadVideo(server.url, server.accessToken, { name: 'video 1' }) 79 await server.videos.upload({ attributes: { name: 'video 1' } })
78 await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
79 80
80 await updateMyAvatar({ url: server.url, accessToken: server.accessToken, fixture: 'avatar.png' }) 81 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } })
82 if (server.serverNumber === 2) videoServer2UUID = uuid
81 83
82 await createVideoPlaylist({ 84 await server.users.updateMyAvatar({ fixture: 'avatar.png' })
83 url: server.url, 85
84 token: server.accessToken, 86 await server.playlists.create({
85 playlistAttrs: { 87 attributes: {
86 displayName: 'playlist', 88 displayName: 'playlist',
87 privacy: VideoPlaylistPrivacy.PUBLIC, 89 privacy: VideoPlaylistPrivacy.PUBLIC,
88 videoChannelId: server.videoChannel.id, 90 videoChannelId: server.store.channel.id,
89 thumbnailfile: 'thumbnail.jpg' 91 thumbnailfile: 'thumbnail.jpg'
90 } 92 }
91 }) 93 })
@@ -95,41 +97,39 @@ describe('Test prune storage scripts', function () {
95 97
96 // Lazy load the remote avatar 98 // Lazy load the remote avatar
97 { 99 {
98 const res = await getAccount(servers[0].url, 'root@localhost:' + servers[1].port) 100 const account = await servers[0].accounts.get({ accountName: 'root@localhost:' + servers[1].port })
99 const account: Account = res.body
100 await makeGetRequest({ 101 await makeGetRequest({
101 url: servers[0].url, 102 url: servers[0].url,
102 path: account.avatar.path, 103 path: account.avatar.path,
103 statusCodeExpected: HttpStatusCode.OK_200 104 expectedStatus: HttpStatusCode.OK_200
104 }) 105 })
105 } 106 }
106 107
107 { 108 {
108 const res = await getAccount(servers[1].url, 'root@localhost:' + servers[0].port) 109 const account = await servers[1].accounts.get({ accountName: 'root@localhost:' + servers[0].port })
109 const account: Account = res.body
110 await makeGetRequest({ 110 await makeGetRequest({
111 url: servers[1].url, 111 url: servers[1].url,
112 path: account.avatar.path, 112 path: account.avatar.path,
113 statusCodeExpected: HttpStatusCode.OK_200 113 expectedStatus: HttpStatusCode.OK_200
114 }) 114 })
115 } 115 }
116 116
117 await wait(1000) 117 await wait(1000)
118 118
119 await waitJobs(servers) 119 await waitJobs(servers)
120 killallServers(servers) 120 await killallServers(servers)
121 121
122 await wait(1000) 122 await wait(1000)
123 }) 123 })
124 124
125 it('Should have the files on the disk', async function () { 125 it('Should have the files on the disk', async function () {
126 await assertCountAreOkay(servers) 126 await assertCountAreOkay(servers, videoServer2UUID)
127 }) 127 })
128 128
129 it('Should create some dirty files', async function () { 129 it('Should create some dirty files', async function () {
130 for (let i = 0; i < 2; i++) { 130 for (let i = 0; i < 2; i++) {
131 { 131 {
132 const base = buildServerDirectory(servers[0], 'videos') 132 const base = servers[0].servers.buildDirectory('videos')
133 133
134 const n1 = buildUUID() + '.mp4' 134 const n1 = buildUUID() + '.mp4'
135 const n2 = buildUUID() + '.webm' 135 const n2 = buildUUID() + '.webm'
@@ -141,7 +141,7 @@ describe('Test prune storage scripts', function () {
141 } 141 }
142 142
143 { 143 {
144 const base = buildServerDirectory(servers[0], 'torrents') 144 const base = servers[0].servers.buildDirectory('torrents')
145 145
146 const n1 = buildUUID() + '-240.torrent' 146 const n1 = buildUUID() + '-240.torrent'
147 const n2 = buildUUID() + '-480.torrent' 147 const n2 = buildUUID() + '-480.torrent'
@@ -153,7 +153,7 @@ describe('Test prune storage scripts', function () {
153 } 153 }
154 154
155 { 155 {
156 const base = buildServerDirectory(servers[0], 'thumbnails') 156 const base = servers[0].servers.buildDirectory('thumbnails')
157 157
158 const n1 = buildUUID() + '.jpg' 158 const n1 = buildUUID() + '.jpg'
159 const n2 = buildUUID() + '.jpg' 159 const n2 = buildUUID() + '.jpg'
@@ -165,7 +165,7 @@ describe('Test prune storage scripts', function () {
165 } 165 }
166 166
167 { 167 {
168 const base = buildServerDirectory(servers[0], 'previews') 168 const base = servers[0].servers.buildDirectory('previews')
169 169
170 const n1 = buildUUID() + '.jpg' 170 const n1 = buildUUID() + '.jpg'
171 const n2 = buildUUID() + '.jpg' 171 const n2 = buildUUID() + '.jpg'
@@ -177,7 +177,7 @@ describe('Test prune storage scripts', function () {
177 } 177 }
178 178
179 { 179 {
180 const base = buildServerDirectory(servers[0], 'avatars') 180 const base = servers[0].servers.buildDirectory('avatars')
181 181
182 const n1 = buildUUID() + '.png' 182 const n1 = buildUUID() + '.png'
183 const n2 = buildUUID() + '.jpg' 183 const n2 = buildUUID() + '.jpg'
@@ -187,22 +187,44 @@ describe('Test prune storage scripts', function () {
187 187
188 badNames['avatars'] = [ n1, n2 ] 188 badNames['avatars'] = [ n1, n2 ]
189 } 189 }
190
191 // When we'll prune HLS directories too
192 // {
193 // const directory = join('streaming-playlists', 'hls')
194 // const base = servers[1].servers.buildDirectory(directory)
195
196 // const n1 = buildUUID()
197 // await createFile(join(base, n1))
198 // badNames[directory] = [ n1 ]
199 // }
200
201 // {
202 // const directory = join('streaming-playlists', 'hls', videoServer2UUID)
203 // const base = servers[1].servers.buildDirectory(directory)
204 // const n1 = buildUUID() + '-240-fragmented-.mp4'
205 // const n2 = buildUUID() + '-master.m3u8'
206
207 // await createFile(join(base, n1))
208 // await createFile(join(base, n2))
209
210 // badNames[directory] = [ n1, n2 ]
211 // }
190 } 212 }
191 }) 213 })
192 214
193 it('Should run prune storage', async function () { 215 it('Should run prune storage', async function () {
194 this.timeout(30000) 216 this.timeout(30000)
195 217
196 const env = getEnvCli(servers[0]) 218 const env = servers[0].cli.getEnv()
197 await execCLI(`echo y | ${env} npm run prune-storage`) 219 await CLICommand.exec(`echo y | ${env} npm run prune-storage`)
198 }) 220 })
199 221
200 it('Should have removed files', async function () { 222 it('Should have removed files', async function () {
201 await assertCountAreOkay(servers) 223 await assertCountAreOkay(servers, videoServer2UUID)
202 224
203 for (const directory of Object.keys(badNames)) { 225 for (const directory of Object.keys(badNames)) {
204 for (const name of badNames[directory]) { 226 for (const name of badNames[directory]) {
205 await assertNotExists(servers[0].internalServerNumber, directory, name) 227 await assertNotExists(servers[0], directory, name)
206 } 228 }
207 } 229 }
208 }) 230 })