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

225 lines
6.7 KiB
TypeScript
Raw Normal View History

2020-01-31 15:56:52 +00:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2019-08-09 13:04:36 +00:00
2022-08-17 13:44:32 +00:00
import { expect } from 'chai'
import { createFile } from 'fs-extra/esm'
import { readdir } from 'fs/promises'
import { join } from 'path'
import { wait } from '@peertube/peertube-core-utils'
import { buildUUID } from '@peertube/peertube-node-utils'
import { HttpStatusCode, VideoPlaylistPrivacy, VideoPrivacy } from '@peertube/peertube-models'
2019-08-09 13:04:36 +00:00
import {
cleanupTests,
2021-07-05 14:37:50 +00:00
CLICommand,
2021-07-16 07:47:51 +00:00
createMultipleServers,
2021-07-16 12:27:30 +00:00
doubleFollow,
2021-06-17 09:09:54 +00:00
killallServers,
2020-01-31 15:56:52 +00:00
makeGetRequest,
2021-07-16 07:47:51 +00:00
PeerTubeServer,
2020-01-31 15:56:52 +00:00
setAccessTokensToServers,
setDefaultVideoChannel,
2021-07-07 11:38:26 +00:00
waitJobs
} from '@peertube/peertube-server-commands'
2019-08-09 13:04:36 +00:00
2021-07-16 07:47:51 +00:00
async function assertNotExists (server: PeerTubeServer, directory: string, substring: string) {
2021-07-16 07:04:35 +00:00
const files = await readdir(server.servers.buildDirectory(directory))
2019-08-09 13:04:36 +00:00
for (const f of files) {
expect(f).to.not.contain(substring)
}
}
2021-12-10 09:28:46 +00:00
async function assertCountAreOkay (servers: PeerTubeServer[]) {
2019-08-09 13:04:36 +00:00
for (const server of servers) {
2023-07-19 14:02:49 +00:00
const videosCount = await server.servers.countFiles('web-videos')
expect(videosCount).to.equal(9) // 2 videos with 4 resolutions + private directory
2023-07-19 14:02:49 +00:00
const privateVideosCount = await server.servers.countFiles('web-videos/private')
expect(privateVideosCount).to.equal(4)
2019-08-09 13:04:36 +00:00
2023-07-19 14:02:49 +00:00
const torrentsCount = await server.servers.countFiles('torrents')
expect(torrentsCount).to.equal(24)
2019-08-09 13:04:36 +00:00
2023-07-19 14:02:49 +00:00
const previewsCount = await server.servers.countFiles('previews')
expect(previewsCount).to.equal(3)
2019-08-09 13:04:36 +00:00
2023-07-19 14:02:49 +00:00
const thumbnailsCount = await server.servers.countFiles('thumbnails')
2023-06-19 14:21:03 +00:00
expect(thumbnailsCount).to.equal(5) // 3 local videos, 1 local playlist, 2 remotes videos (lazy downloaded) and 1 remote playlist
2019-08-09 13:04:36 +00:00
2023-07-19 14:02:49 +00:00
const avatarsCount = await server.servers.countFiles('avatars')
expect(avatarsCount).to.equal(4)
2021-07-23 09:20:00 +00:00
2023-07-19 14:02:49 +00:00
const hlsRootCount = await server.servers.countFiles(join('streaming-playlists', 'hls'))
expect(hlsRootCount).to.equal(3) // 2 videos + private directory
2023-07-19 14:02:49 +00:00
const hlsPrivateRootCount = await server.servers.countFiles(join('streaming-playlists', 'hls', 'private'))
expect(hlsPrivateRootCount).to.equal(1)
2021-12-10 09:28:46 +00:00
}
2019-08-09 13:04:36 +00:00
}
describe('Test prune storage scripts', function () {
2021-07-16 07:47:51 +00:00
let servers: PeerTubeServer[]
2020-01-31 15:56:52 +00:00
const badNames: { [directory: string]: string[] } = {}
2019-08-09 13:04:36 +00:00
before(async function () {
this.timeout(120000)
2021-07-16 07:47:51 +00:00
servers = await createMultipleServers(2, { transcoding: { enabled: true } })
2019-08-09 13:04:36 +00:00
await setAccessTokensToServers(servers)
await setDefaultVideoChannel(servers)
for (const server of servers) {
await server.videos.upload({ attributes: { name: 'video 1', privacy: VideoPrivacy.PUBLIC } })
await server.videos.upload({ attributes: { name: 'video 2', privacy: VideoPrivacy.PUBLIC } })
await server.videos.upload({ attributes: { name: 'video 3', privacy: VideoPrivacy.PRIVATE } })
2019-08-09 13:04:36 +00:00
2021-07-16 07:04:35 +00:00
await server.users.updateMyAvatar({ fixture: 'avatar.png' })
2019-08-09 13:04:36 +00:00
2021-07-16 07:04:35 +00:00
await server.playlists.create({
2021-07-08 13:54:39 +00:00
attributes: {
2019-08-09 13:04:36 +00:00
displayName: 'playlist',
privacy: VideoPlaylistPrivacy.PUBLIC,
2021-07-16 07:04:35 +00:00
videoChannelId: server.store.channel.id,
2023-06-06 09:14:13 +00:00
thumbnailfile: 'custom-thumbnail.jpg'
2019-08-09 13:04:36 +00:00
}
})
}
await doubleFollow(servers[0], servers[1])
// Lazy load the remote avatars
2019-08-09 13:04:36 +00:00
{
2022-12-09 10:14:47 +00:00
const account = await servers[0].accounts.get({ accountName: 'root@' + servers[1].host })
for (const avatar of account.avatars) {
await makeGetRequest({
url: servers[0].url,
path: avatar.path,
expectedStatus: HttpStatusCode.OK_200
})
}
2019-08-09 13:04:36 +00:00
}
{
2022-12-09 10:14:47 +00:00
const account = await servers[1].accounts.get({ accountName: 'root@' + servers[0].host })
for (const avatar of account.avatars) {
await makeGetRequest({
url: servers[1].url,
path: avatar.path,
expectedStatus: HttpStatusCode.OK_200
})
}
2019-08-09 13:04:36 +00:00
}
await wait(1000)
await waitJobs(servers)
2021-07-09 13:37:43 +00:00
await killallServers(servers)
2021-06-17 09:09:54 +00:00
await wait(1000)
2019-08-09 13:04:36 +00:00
})
it('Should have the files on the disk', async function () {
2021-12-10 09:28:46 +00:00
await assertCountAreOkay(servers)
2019-08-09 13:04:36 +00:00
})
it('Should create some dirty files', async function () {
for (let i = 0; i < 2; i++) {
{
2023-07-11 09:23:51 +00:00
const basePublic = servers[0].servers.buildDirectory('web-videos')
const basePrivate = servers[0].servers.buildDirectory(join('web-videos', 'private'))
2019-08-09 13:04:36 +00:00
const n1 = buildUUID() + '.mp4'
const n2 = buildUUID() + '.webm'
2019-08-09 13:04:36 +00:00
await createFile(join(basePublic, n1))
await createFile(join(basePublic, n2))
await createFile(join(basePrivate, n1))
await createFile(join(basePrivate, n2))
2019-08-09 13:04:36 +00:00
2023-07-12 09:32:12 +00:00
badNames['web-videos'] = [ n1, n2 ]
2019-08-09 13:04:36 +00:00
}
{
2021-07-16 07:04:35 +00:00
const base = servers[0].servers.buildDirectory('torrents')
2019-08-09 13:04:36 +00:00
const n1 = buildUUID() + '-240.torrent'
const n2 = buildUUID() + '-480.torrent'
2019-08-09 13:04:36 +00:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['torrents'] = [ n1, n2 ]
}
{
2021-07-16 07:04:35 +00:00
const base = servers[0].servers.buildDirectory('thumbnails')
2019-08-09 13:04:36 +00:00
const n1 = buildUUID() + '.jpg'
const n2 = buildUUID() + '.jpg'
2019-08-09 13:04:36 +00:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['thumbnails'] = [ n1, n2 ]
}
{
2021-07-16 07:04:35 +00:00
const base = servers[0].servers.buildDirectory('previews')
2019-08-09 13:04:36 +00:00
const n1 = buildUUID() + '.jpg'
const n2 = buildUUID() + '.jpg'
2019-08-09 13:04:36 +00:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['previews'] = [ n1, n2 ]
}
{
2021-07-16 07:04:35 +00:00
const base = servers[0].servers.buildDirectory('avatars')
2019-08-09 13:04:36 +00:00
const n1 = buildUUID() + '.png'
const n2 = buildUUID() + '.jpg'
2019-08-09 13:04:36 +00:00
await createFile(join(base, n1))
await createFile(join(base, n2))
badNames['avatars'] = [ n1, n2 ]
}
2021-07-23 09:20:00 +00:00
2021-12-10 09:28:46 +00:00
{
const directory = join('streaming-playlists', 'hls')
const basePublic = servers[0].servers.buildDirectory(directory)
const basePrivate = servers[0].servers.buildDirectory(join(directory, 'private'))
2021-07-23 09:20:00 +00:00
2021-12-10 09:28:46 +00:00
const n1 = buildUUID()
await createFile(join(basePublic, n1))
await createFile(join(basePrivate, n1))
2021-12-10 09:28:46 +00:00
badNames[directory] = [ n1 ]
}
2019-08-09 13:04:36 +00:00
}
})
it('Should run prune storage', async function () {
this.timeout(30000)
2021-07-16 07:04:35 +00:00
const env = servers[0].cli.getEnv()
2021-07-05 14:37:50 +00:00
await CLICommand.exec(`echo y | ${env} npm run prune-storage`)
2019-08-09 13:04:36 +00:00
})
it('Should have removed files', async function () {
2021-12-10 09:28:46 +00:00
await assertCountAreOkay(servers)
2019-08-09 13:04:36 +00:00
for (const directory of Object.keys(badNames)) {
for (const name of badNames[directory]) {
2021-07-13 07:43:59 +00:00
await assertNotExists(servers[0], directory, name)
2019-08-09 13:04:36 +00:00
}
}
})
after(async function () {
await cleanupTests(servers)
})
})