PeerTube/server/tests/cli/prune-storage.ts

213 lines
5.9 KiB
TypeScript
Raw Normal View History

2020-01-31 09:56:52 -06:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2019-08-09 08:04:36 -05:00
import 'mocha'
import * as chai from 'chai'
import { createFile, readdir } from 'fs-extra'
import { join } from 'path'
import { buildUUID } from '@server/helpers/uuid'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
2019-08-09 08:04:36 -05:00
import {
buildServerDirectory,
cleanupTests,
2021-07-05 09:37:50 -05:00
CLICommand,
2019-08-09 08:04:36 -05:00
createVideoPlaylist,
doubleFollow,
flushAndRunMultipleServers,
getAccount,
2021-06-17 04:09:54 -05:00
killallServers,
2020-01-31 09:56:52 -06:00
makeGetRequest,
2019-08-09 08:04:36 -05:00
ServerInfo,
2020-01-31 09:56:52 -06:00
setAccessTokensToServers,
setDefaultVideoChannel,
2019-08-09 08:04:36 -05:00
updateMyAvatar,
uploadVideo,
wait
} from '../../../shared/extra-utils'
import { waitJobs } from '../../../shared/extra-utils/server/jobs'
2019-08-09 08:04:36 -05:00
import { Account, VideoPlaylistPrivacy } from '../../../shared/models'
const expect = chai.expect
async function countFiles (internalServerNumber: number, directory: string) {
2020-11-24 08:22:56 -06:00
const files = await readdir(buildServerDirectory({ internalServerNumber }, directory))
2019-08-09 08:04:36 -05:00
return files.length
}
async function assertNotExists (internalServerNumber: number, directory: string, substring: string) {
2020-11-24 08:22:56 -06:00
const files = await readdir(buildServerDirectory({ internalServerNumber }, directory))
2019-08-09 08:04:36 -05: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 05:16:27 -06:00
expect(torrentsCount).to.equal(16)
2019-08-09 08:04:36 -05: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 09:56:52 -06:00
const badNames: { [directory: string]: string[] } = {}
2019-08-09 08:04:36 -05: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' })
await createVideoPlaylist({
url: server.url,
token: server.accessToken,
playlistAttrs: {
displayName: 'playlist',
privacy: VideoPlaylistPrivacy.PUBLIC,
videoChannelId: server.videoChannel.id,
thumbnailfile: 'thumbnail.jpg'
}
})
}
await doubleFollow(servers[0], servers[1])
// Lazy load the remote avatar
{
2020-01-31 09:56:52 -06:00
const res = await getAccount(servers[0].url, 'root@localhost:' + servers[1].port)
2019-08-09 08:04:36 -05:00
const account: Account = res.body
2019-11-21 05:16:27 -06:00
await makeGetRequest({
2020-01-31 09:56:52 -06:00
url: servers[0].url,
2019-11-21 05:16:27 -06:00
path: account.avatar.path,
statusCodeExpected: HttpStatusCode.OK_200
2019-11-21 05:16:27 -06:00
})
2019-08-09 08:04:36 -05:00
}
{
2020-01-31 09:56:52 -06:00
const res = await getAccount(servers[1].url, 'root@localhost:' + servers[0].port)
2019-08-09 08:04:36 -05:00
const account: Account = res.body
2019-11-21 05:16:27 -06:00
await makeGetRequest({
2020-01-31 09:56:52 -06:00
url: servers[1].url,
2019-11-21 05:16:27 -06:00
path: account.avatar.path,
statusCodeExpected: HttpStatusCode.OK_200
2019-11-21 05:16:27 -06:00
})
2019-08-09 08:04:36 -05:00
}
await wait(1000)
await waitJobs(servers)
2021-06-17 04:09:54 -05:00
killallServers(servers)
await wait(1000)
2019-08-09 08:04:36 -05: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 08:22:56 -06:00
const base = buildServerDirectory(servers[0], 'videos')
2019-08-09 08:04:36 -05:00
const n1 = buildUUID() + '.mp4'
const n2 = buildUUID() + '.webm'
2019-08-09 08:04:36 -05:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['videos'] = [ n1, n2 ]
}
{
2020-11-24 08:22:56 -06:00
const base = buildServerDirectory(servers[0], 'torrents')
2019-08-09 08:04:36 -05:00
const n1 = buildUUID() + '-240.torrent'
const n2 = buildUUID() + '-480.torrent'
2019-08-09 08:04:36 -05:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['torrents'] = [ n1, n2 ]
}
{
2020-11-24 08:22:56 -06:00
const base = buildServerDirectory(servers[0], 'thumbnails')
2019-08-09 08:04:36 -05:00
const n1 = buildUUID() + '.jpg'
const n2 = buildUUID() + '.jpg'
2019-08-09 08:04:36 -05:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['thumbnails'] = [ n1, n2 ]
}
{
2020-11-24 08:22:56 -06:00
const base = buildServerDirectory(servers[0], 'previews')
2019-08-09 08:04:36 -05:00
const n1 = buildUUID() + '.jpg'
const n2 = buildUUID() + '.jpg'
2019-08-09 08:04:36 -05:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['previews'] = [ n1, n2 ]
}
{
2020-11-24 08:22:56 -06:00
const base = buildServerDirectory(servers[0], 'avatars')
2019-08-09 08:04:36 -05:00
const n1 = buildUUID() + '.png'
const n2 = buildUUID() + '.jpg'
2019-08-09 08:04:36 -05: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 09:37:50 -05:00
const env = servers[0].cliCommand.getEnv()
await CLICommand.exec(`echo y | ${env} npm run prune-storage`)
2019-08-09 08:04:36 -05: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)
})
})