1
0
Fork 0
peertube/server/tests/cli/prune-storage.ts

207 lines
5.6 KiB
TypeScript
Raw Normal View History

2020-01-31 10:56:52 -05:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2019-08-09 09:04:36 -04:00
import 'mocha'
import * as chai from 'chai'
import { createFile, readdir } from 'fs-extra'
import { join } from 'path'
import { buildUUID } from '@server/helpers/uuid'
2021-07-07 07:38:26 -04:00
import { HttpStatusCode } from '@shared/core-utils'
2019-08-09 09:04:36 -04:00
import {
buildServerDirectory,
cleanupTests,
2021-07-05 10:37:50 -04:00
CLICommand,
2019-08-09 09:04:36 -04:00
doubleFollow,
flushAndRunMultipleServers,
2021-06-17 05:09:54 -04:00
killallServers,
2020-01-31 10:56:52 -05:00
makeGetRequest,
2019-08-09 09:04:36 -04:00
ServerInfo,
2020-01-31 10:56:52 -05:00
setAccessTokensToServers,
setDefaultVideoChannel,
2019-08-09 09:04:36 -04:00
updateMyAvatar,
uploadVideo,
2021-07-07 07:38:26 -04:00
wait,
waitJobs
} from '@shared/extra-utils'
import { VideoPlaylistPrivacy } from '@shared/models'
2019-08-09 09:04:36 -04:00
const expect = chai.expect
async function countFiles (internalServerNumber: number, directory: string) {
2020-11-24 09:22:56 -05:00
const files = await readdir(buildServerDirectory({ internalServerNumber }, directory))
2019-08-09 09:04:36 -04:00
return files.length
}
async function assertNotExists (internalServerNumber: number, directory: string, substring: string) {
2020-11-24 09:22:56 -05:00
const files = await readdir(buildServerDirectory({ internalServerNumber }, directory))
2019-08-09 09:04:36 -04:00
for (const f of files) {
expect(f).to.not.contain(substring)
}
}
async function assertCountAreOkay (servers: ServerInfo[]) {
for (const server of servers) {
const videosCount = await countFiles(server.internalServerNumber, 'videos')
expect(videosCount).to.equal(8)
const torrentsCount = await countFiles(server.internalServerNumber, 'torrents')
2019-11-21 06:16:27 -05:00
expect(torrentsCount).to.equal(16)
2019-08-09 09:04:36 -04:00
const previewsCount = await countFiles(server.internalServerNumber, 'previews')
expect(previewsCount).to.equal(2)
const thumbnailsCount = await countFiles(server.internalServerNumber, 'thumbnails')
expect(thumbnailsCount).to.equal(6)
const avatarsCount = await countFiles(server.internalServerNumber, 'avatars')
expect(avatarsCount).to.equal(2)
}
}
describe('Test prune storage scripts', function () {
let servers: ServerInfo[]
2020-01-31 10:56:52 -05:00
const badNames: { [directory: string]: string[] } = {}
2019-08-09 09:04:36 -04:00
before(async function () {
this.timeout(120000)
servers = await flushAndRunMultipleServers(2, { transcoding: { enabled: true } })
await setAccessTokensToServers(servers)
await setDefaultVideoChannel(servers)
for (const server of servers) {
await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
await updateMyAvatar({ url: server.url, accessToken: server.accessToken, fixture: 'avatar.png' })
2021-07-08 09:54:39 -04:00
await server.playlistsCommand.create({
attributes: {
2019-08-09 09:04:36 -04:00
displayName: 'playlist',
privacy: VideoPlaylistPrivacy.PUBLIC,
videoChannelId: server.videoChannel.id,
thumbnailfile: 'thumbnail.jpg'
}
})
}
await doubleFollow(servers[0], servers[1])
// Lazy load the remote avatar
{
2021-07-07 07:38:26 -04:00
const account = await servers[0].accountsCommand.get({ accountName: 'root@localhost:' + servers[1].port })
2019-11-21 06:16:27 -05:00
await makeGetRequest({
2020-01-31 10:56:52 -05:00
url: servers[0].url,
2019-11-21 06:16:27 -05:00
path: account.avatar.path,
statusCodeExpected: HttpStatusCode.OK_200
2019-11-21 06:16:27 -05:00
})
2019-08-09 09:04:36 -04:00
}
{
2021-07-07 07:38:26 -04:00
const account = await servers[1].accountsCommand.get({ accountName: 'root@localhost:' + servers[0].port })
2019-11-21 06:16:27 -05:00
await makeGetRequest({
2020-01-31 10:56:52 -05:00
url: servers[1].url,
2019-11-21 06:16:27 -05:00
path: account.avatar.path,
statusCodeExpected: HttpStatusCode.OK_200
2019-11-21 06:16:27 -05:00
})
2019-08-09 09:04:36 -04:00
}
await wait(1000)
await waitJobs(servers)
2021-07-09 09:37:43 -04:00
await killallServers(servers)
2021-06-17 05:09:54 -04:00
await wait(1000)
2019-08-09 09:04:36 -04:00
})
it('Should have the files on the disk', async function () {
await assertCountAreOkay(servers)
})
it('Should create some dirty files', async function () {
for (let i = 0; i < 2; i++) {
{
2020-11-24 09:22:56 -05:00
const base = buildServerDirectory(servers[0], 'videos')
2019-08-09 09:04:36 -04:00
const n1 = buildUUID() + '.mp4'
const n2 = buildUUID() + '.webm'
2019-08-09 09:04:36 -04:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['videos'] = [ n1, n2 ]
}
{
2020-11-24 09:22:56 -05:00
const base = buildServerDirectory(servers[0], 'torrents')
2019-08-09 09:04:36 -04:00
const n1 = buildUUID() + '-240.torrent'
const n2 = buildUUID() + '-480.torrent'
2019-08-09 09:04:36 -04:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['torrents'] = [ n1, n2 ]
}
{
2020-11-24 09:22:56 -05:00
const base = buildServerDirectory(servers[0], 'thumbnails')
2019-08-09 09:04:36 -04:00
const n1 = buildUUID() + '.jpg'
const n2 = buildUUID() + '.jpg'
2019-08-09 09:04:36 -04:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['thumbnails'] = [ n1, n2 ]
}
{
2020-11-24 09:22:56 -05:00
const base = buildServerDirectory(servers[0], 'previews')
2019-08-09 09:04:36 -04:00
const n1 = buildUUID() + '.jpg'
const n2 = buildUUID() + '.jpg'
2019-08-09 09:04:36 -04:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['previews'] = [ n1, n2 ]
}
{
2020-11-24 09:22:56 -05:00
const base = buildServerDirectory(servers[0], 'avatars')
2019-08-09 09:04:36 -04:00
const n1 = buildUUID() + '.png'
const n2 = buildUUID() + '.jpg'
2019-08-09 09:04:36 -04:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['avatars'] = [ n1, n2 ]
}
}
})
it('Should run prune storage', async function () {
this.timeout(30000)
2021-07-05 10:37:50 -04:00
const env = servers[0].cliCommand.getEnv()
await CLICommand.exec(`echo y | ${env} npm run prune-storage`)
2019-08-09 09:04:36 -04:00
})
it('Should have removed files', async function () {
await assertCountAreOkay(servers)
for (const directory of Object.keys(badNames)) {
for (const name of badNames[directory]) {
await assertNotExists(servers[0].internalServerNumber, directory, name)
}
}
})
after(async function () {
await cleanupTests(servers)
})
})