From 0305db28c98fd6cf43a3c50ba92c76215e99d512 Mon Sep 17 00:00:00 2001 From: Jelle Besseling Date: Tue, 17 Aug 2021 08:26:20 +0200 Subject: Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz --- server/tests/api/index.ts | 1 + server/tests/api/live/live-save-replay.ts | 30 +- server/tests/api/object-storage/index.ts | 3 + server/tests/api/object-storage/live.ts | 136 ++++++++ server/tests/api/object-storage/video-imports.ts | 112 +++++++ server/tests/api/object-storage/videos.ts | 391 +++++++++++++++++++++++ server/tests/api/redundancy/redundancy.ts | 6 +- server/tests/api/videos/video-hls.ts | 73 ++++- server/tests/cli/create-import-video-file-job.ts | 56 +++- server/tests/cli/create-transcoding-job.ts | 95 ++++-- server/tests/helpers/request.ts | 8 +- 11 files changed, 832 insertions(+), 79 deletions(-) create mode 100644 server/tests/api/object-storage/index.ts create mode 100644 server/tests/api/object-storage/live.ts create mode 100644 server/tests/api/object-storage/video-imports.ts create mode 100644 server/tests/api/object-storage/videos.ts (limited to 'server/tests') diff --git a/server/tests/api/index.ts b/server/tests/api/index.ts index b62e2f5f7..19301c0b9 100644 --- a/server/tests/api/index.ts +++ b/server/tests/api/index.ts @@ -2,6 +2,7 @@ import './activitypub' import './check-params' import './moderation' +import './object-storage' import './notifications' import './redundancy' import './search' diff --git a/server/tests/api/live/live-save-replay.ts b/server/tests/api/live/live-save-replay.ts index 8f1fb78a5..6c4ea90ca 100644 --- a/server/tests/api/live/live-save-replay.ts +++ b/server/tests/api/live/live-save-replay.ts @@ -15,7 +15,9 @@ import { stopFfmpeg, testFfmpegStreamError, wait, - waitJobs + waitJobs, + waitUntilLivePublishedOnAllServers, + waitUntilLiveSavedOnAllServers } from '@shared/extra-utils' import { HttpStatusCode, LiveVideoCreate, VideoPrivacy, VideoState } from '@shared/models' @@ -66,18 +68,6 @@ describe('Save replay setting', function () { } } - async function waitUntilLivePublishedOnAllServers (videoId: string) { - for (const server of servers) { - await server.live.waitUntilPublished({ videoId }) - } - } - - async function waitUntilLiveSavedOnAllServers (videoId: string) { - for (const server of servers) { - await server.live.waitUntilSaved({ videoId }) - } - } - before(async function () { this.timeout(120000) @@ -127,7 +117,7 @@ describe('Save replay setting', function () { ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) - await waitUntilLivePublishedOnAllServers(liveVideoUUID) + await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) await waitJobs(servers) @@ -160,7 +150,7 @@ describe('Save replay setting', function () { ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) - await waitUntilLivePublishedOnAllServers(liveVideoUUID) + await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) await waitJobs(servers) await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200) @@ -189,7 +179,7 @@ describe('Save replay setting', function () { ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) - await waitUntilLivePublishedOnAllServers(liveVideoUUID) + await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) await waitJobs(servers) await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200) @@ -224,7 +214,7 @@ describe('Save replay setting', function () { this.timeout(20000) ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) - await waitUntilLivePublishedOnAllServers(liveVideoUUID) + await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) await waitJobs(servers) @@ -237,7 +227,7 @@ describe('Save replay setting', function () { await stopFfmpeg(ffmpegCommand) - await waitUntilLiveSavedOnAllServers(liveVideoUUID) + await waitUntilLiveSavedOnAllServers(servers, liveVideoUUID) await waitJobs(servers) // Live has been transcoded @@ -268,7 +258,7 @@ describe('Save replay setting', function () { liveVideoUUID = await createLiveWrapper(true) ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) - await waitUntilLivePublishedOnAllServers(liveVideoUUID) + await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) await waitJobs(servers) await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200) @@ -296,7 +286,7 @@ describe('Save replay setting', function () { liveVideoUUID = await createLiveWrapper(true) ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) - await waitUntilLivePublishedOnAllServers(liveVideoUUID) + await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) await waitJobs(servers) await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200) diff --git a/server/tests/api/object-storage/index.ts b/server/tests/api/object-storage/index.ts new file mode 100644 index 000000000..f319d6ef5 --- /dev/null +++ b/server/tests/api/object-storage/index.ts @@ -0,0 +1,3 @@ +export * from './live' +export * from './video-imports' +export * from './videos' diff --git a/server/tests/api/object-storage/live.ts b/server/tests/api/object-storage/live.ts new file mode 100644 index 000000000..d3e6777f2 --- /dev/null +++ b/server/tests/api/object-storage/live.ts @@ -0,0 +1,136 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' +import * as chai from 'chai' +import { FfmpegCommand } from 'fluent-ffmpeg' +import { + areObjectStorageTestsDisabled, + createMultipleServers, + doubleFollow, + expectStartWith, + killallServers, + makeRawRequest, + ObjectStorageCommand, + PeerTubeServer, + setAccessTokensToServers, + setDefaultVideoChannel, + stopFfmpeg, + waitJobs, + waitUntilLivePublishedOnAllServers, + waitUntilLiveSavedOnAllServers +} from '@shared/extra-utils' +import { HttpStatusCode, LiveVideoCreate, VideoFile, VideoPrivacy } from '@shared/models' + +const expect = chai.expect + +async function createLive (server: PeerTubeServer) { + const attributes: LiveVideoCreate = { + channelId: server.store.channel.id, + privacy: VideoPrivacy.PUBLIC, + name: 'my super live', + saveReplay: true + } + + const { uuid } = await server.live.create({ fields: attributes }) + + return uuid +} + +async function checkFiles (files: VideoFile[]) { + for (const file of files) { + expectStartWith(file.fileUrl, ObjectStorageCommand.getPlaylistBaseUrl()) + + await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) + } +} + +describe('Object storage for lives', function () { + if (areObjectStorageTestsDisabled()) return + + let ffmpegCommand: FfmpegCommand + let servers: PeerTubeServer[] + let videoUUID: string + + before(async function () { + this.timeout(120000) + + await ObjectStorageCommand.prepareDefaultBuckets() + + servers = await createMultipleServers(2, ObjectStorageCommand.getDefaultConfig()) + + await setAccessTokensToServers(servers) + await setDefaultVideoChannel(servers) + await doubleFollow(servers[0], servers[1]) + + await servers[0].config.enableTranscoding() + }) + + describe('Without live transcoding', async function () { + + before(async function () { + await servers[0].config.enableLive({ transcoding: false }) + + videoUUID = await createLive(servers[0]) + }) + + it('Should create a live and save the replay on object storage', async function () { + this.timeout(220000) + + ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID }) + await waitUntilLivePublishedOnAllServers(servers, videoUUID) + + await stopFfmpeg(ffmpegCommand) + + await waitUntilLiveSavedOnAllServers(servers, videoUUID) + await waitJobs(servers) + + for (const server of servers) { + const video = await server.videos.get({ id: videoUUID }) + + expect(video.files).to.have.lengthOf(0) + expect(video.streamingPlaylists).to.have.lengthOf(1) + + const files = video.streamingPlaylists[0].files + + await checkFiles(files) + } + }) + }) + + describe('With live transcoding', async function () { + + before(async function () { + await servers[0].config.enableLive({ transcoding: true }) + + videoUUID = await createLive(servers[0]) + }) + + it('Should import a video and have sent it to object storage', async function () { + this.timeout(240000) + + ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID }) + await waitUntilLivePublishedOnAllServers(servers, videoUUID) + + await stopFfmpeg(ffmpegCommand) + + await waitUntilLiveSavedOnAllServers(servers, videoUUID) + await waitJobs(servers) + + for (const server of servers) { + const video = await server.videos.get({ id: videoUUID }) + + expect(video.files).to.have.lengthOf(0) + expect(video.streamingPlaylists).to.have.lengthOf(1) + + const files = video.streamingPlaylists[0].files + expect(files).to.have.lengthOf(4) + + await checkFiles(files) + } + }) + }) + + after(async function () { + await killallServers(servers) + }) +}) diff --git a/server/tests/api/object-storage/video-imports.ts b/server/tests/api/object-storage/video-imports.ts new file mode 100644 index 000000000..efc01f550 --- /dev/null +++ b/server/tests/api/object-storage/video-imports.ts @@ -0,0 +1,112 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' +import * as chai from 'chai' +import { + areObjectStorageTestsDisabled, + createSingleServer, + expectStartWith, + FIXTURE_URLS, + killallServers, + makeRawRequest, + ObjectStorageCommand, + PeerTubeServer, + setAccessTokensToServers, + setDefaultVideoChannel, + waitJobs +} from '@shared/extra-utils' +import { HttpStatusCode, VideoPrivacy } from '@shared/models' + +const expect = chai.expect + +async function importVideo (server: PeerTubeServer) { + const attributes = { + name: 'import 2', + privacy: VideoPrivacy.PUBLIC, + channelId: server.store.channel.id, + targetUrl: FIXTURE_URLS.goodVideo720 + } + + const { video: { uuid } } = await server.imports.importVideo({ attributes }) + + return uuid +} + +describe('Object storage for video import', function () { + if (areObjectStorageTestsDisabled()) return + + let server: PeerTubeServer + + before(async function () { + this.timeout(120000) + + await ObjectStorageCommand.prepareDefaultBuckets() + + server = await createSingleServer(1, ObjectStorageCommand.getDefaultConfig()) + + await setAccessTokensToServers([ server ]) + await setDefaultVideoChannel([ server ]) + + await server.config.enableImports() + }) + + describe('Without transcoding', async function () { + + before(async function () { + await server.config.disableTranscoding() + }) + + it('Should import a video and have sent it to object storage', async function () { + this.timeout(120000) + + const uuid = await importVideo(server) + await waitJobs(server) + + const video = await server.videos.get({ id: uuid }) + + expect(video.files).to.have.lengthOf(1) + expect(video.streamingPlaylists).to.have.lengthOf(0) + + const fileUrl = video.files[0].fileUrl + expectStartWith(fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl()) + + await makeRawRequest(fileUrl, HttpStatusCode.OK_200) + }) + }) + + describe('With transcoding', async function () { + + before(async function () { + await server.config.enableTranscoding() + }) + + it('Should import a video and have sent it to object storage', async function () { + this.timeout(120000) + + const uuid = await importVideo(server) + await waitJobs(server) + + const video = await server.videos.get({ id: uuid }) + + expect(video.files).to.have.lengthOf(4) + expect(video.streamingPlaylists).to.have.lengthOf(1) + expect(video.streamingPlaylists[0].files).to.have.lengthOf(4) + + for (const file of video.files) { + expectStartWith(file.fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl()) + + await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) + } + + for (const file of video.streamingPlaylists[0].files) { + expectStartWith(file.fileUrl, ObjectStorageCommand.getPlaylistBaseUrl()) + + await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) + } + }) + }) + + after(async function () { + await killallServers([ server ]) + }) +}) diff --git a/server/tests/api/object-storage/videos.ts b/server/tests/api/object-storage/videos.ts new file mode 100644 index 000000000..3958bd3d7 --- /dev/null +++ b/server/tests/api/object-storage/videos.ts @@ -0,0 +1,391 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' +import * as chai from 'chai' +import { merge } from 'lodash' +import { + areObjectStorageTestsDisabled, + checkTmpIsEmpty, + cleanupTests, + createMultipleServers, + createSingleServer, + doubleFollow, + expectStartWith, + killallServers, + makeRawRequest, + MockObjectStorage, + ObjectStorageCommand, + PeerTubeServer, + setAccessTokensToServers, + waitJobs, + webtorrentAdd +} from '@shared/extra-utils' +import { HttpStatusCode, VideoDetails } from '@shared/models' + +const expect = chai.expect + +async function checkFiles (options: { + video: VideoDetails + + baseMockUrl?: string + + playlistBucket: string + playlistPrefix?: string + + webtorrentBucket: string + webtorrentPrefix?: string +}) { + const { + video, + playlistBucket, + webtorrentBucket, + baseMockUrl, + playlistPrefix, + webtorrentPrefix + } = options + + let allFiles = video.files + + for (const file of video.files) { + const baseUrl = baseMockUrl + ? `${baseMockUrl}/${webtorrentBucket}/` + : `http://${webtorrentBucket}.${ObjectStorageCommand.getEndpointHost()}/` + + const prefix = webtorrentPrefix || '' + const start = baseUrl + prefix + + expectStartWith(file.fileUrl, start) + + const res = await makeRawRequest(file.fileDownloadUrl, HttpStatusCode.FOUND_302) + const location = res.headers['location'] + expectStartWith(location, start) + + await makeRawRequest(location, HttpStatusCode.OK_200) + } + + const hls = video.streamingPlaylists[0] + + if (hls) { + allFiles = allFiles.concat(hls.files) + + const baseUrl = baseMockUrl + ? `${baseMockUrl}/${playlistBucket}/` + : `http://${playlistBucket}.${ObjectStorageCommand.getEndpointHost()}/` + + const prefix = playlistPrefix || '' + const start = baseUrl + prefix + + expectStartWith(hls.playlistUrl, start) + expectStartWith(hls.segmentsSha256Url, start) + + await makeRawRequest(hls.playlistUrl, HttpStatusCode.OK_200) + + const resSha = await makeRawRequest(hls.segmentsSha256Url, HttpStatusCode.OK_200) + expect(JSON.stringify(resSha.body)).to.not.throw + + for (const file of hls.files) { + expectStartWith(file.fileUrl, start) + + const res = await makeRawRequest(file.fileDownloadUrl, HttpStatusCode.FOUND_302) + const location = res.headers['location'] + expectStartWith(location, start) + + await makeRawRequest(location, HttpStatusCode.OK_200) + } + } + + for (const file of allFiles) { + const torrent = await webtorrentAdd(file.magnetUri, true) + + expect(torrent.files).to.be.an('array') + expect(torrent.files.length).to.equal(1) + expect(torrent.files[0].path).to.exist.and.to.not.equal('') + + const res = await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) + expect(res.body).to.have.length.above(100) + } + + return allFiles.map(f => f.fileUrl) +} + +function runTestSuite (options: { + playlistBucket: string + playlistPrefix?: string + + webtorrentBucket: string + webtorrentPrefix?: string + + useMockBaseUrl?: boolean + + maxUploadPart?: string +}) { + const mockObjectStorage = new MockObjectStorage() + let baseMockUrl: string + + let servers: PeerTubeServer[] + + let keptUrls: string[] = [] + + const uuidsToDelete: string[] = [] + let deletedUrls: string[] = [] + + before(async function () { + this.timeout(120000) + + const port = await mockObjectStorage.initialize() + baseMockUrl = options.useMockBaseUrl ? `http://localhost:${port}` : undefined + + await ObjectStorageCommand.createBucket(options.playlistBucket) + await ObjectStorageCommand.createBucket(options.webtorrentBucket) + + const config = { + object_storage: { + enabled: true, + endpoint: 'http://' + ObjectStorageCommand.getEndpointHost(), + region: ObjectStorageCommand.getRegion(), + + credentials: ObjectStorageCommand.getCredentialsConfig(), + + max_upload_part: options.maxUploadPart || '2MB', + + streaming_playlists: { + bucket_name: options.playlistBucket, + prefix: options.playlistPrefix, + base_url: baseMockUrl + ? `${baseMockUrl}/${options.playlistBucket}` + : undefined + }, + + videos: { + bucket_name: options.webtorrentBucket, + prefix: options.webtorrentPrefix, + base_url: baseMockUrl + ? `${baseMockUrl}/${options.webtorrentBucket}` + : undefined + } + } + } + + servers = await createMultipleServers(2, config) + + await setAccessTokensToServers(servers) + await doubleFollow(servers[0], servers[1]) + + for (const server of servers) { + const { uuid } = await server.videos.quickUpload({ name: 'video to keep' }) + await waitJobs(servers) + + const files = await server.videos.listFiles({ id: uuid }) + keptUrls = keptUrls.concat(files.map(f => f.fileUrl)) + } + }) + + it('Should upload a video and move it to the object storage without transcoding', async function () { + this.timeout(20000) + + const { uuid } = await servers[0].videos.quickUpload({ name: 'video 1' }) + uuidsToDelete.push(uuid) + + await waitJobs(servers) + + for (const server of servers) { + const video = await server.videos.get({ id: uuid }) + const files = await checkFiles({ ...options, video, baseMockUrl }) + + deletedUrls = deletedUrls.concat(files) + } + }) + + it('Should upload a video and move it to the object storage with transcoding', async function () { + this.timeout(40000) + + const { uuid } = await servers[1].videos.quickUpload({ name: 'video 2' }) + uuidsToDelete.push(uuid) + + await waitJobs(servers) + + for (const server of servers) { + const video = await server.videos.get({ id: uuid }) + const files = await checkFiles({ ...options, video, baseMockUrl }) + + deletedUrls = deletedUrls.concat(files) + } + }) + + it('Should correctly delete the files', async function () { + await servers[0].videos.remove({ id: uuidsToDelete[0] }) + await servers[1].videos.remove({ id: uuidsToDelete[1] }) + + await waitJobs(servers) + + for (const url of deletedUrls) { + await makeRawRequest(url, HttpStatusCode.NOT_FOUND_404) + } + }) + + it('Should have kept other files', async function () { + for (const url of keptUrls) { + await makeRawRequest(url, HttpStatusCode.OK_200) + } + }) + + it('Should have an empty tmp directory', async function () { + for (const server of servers) { + await checkTmpIsEmpty(server) + } + }) + + after(async function () { + mockObjectStorage.terminate() + + await cleanupTests(servers) + }) +} + +describe('Object storage for videos', function () { + if (areObjectStorageTestsDisabled()) return + + describe('Test config', function () { + let server: PeerTubeServer + + const baseConfig = { + object_storage: { + enabled: true, + endpoint: 'http://' + ObjectStorageCommand.getEndpointHost(), + region: ObjectStorageCommand.getRegion(), + + credentials: ObjectStorageCommand.getCredentialsConfig(), + + streaming_playlists: { + bucket_name: ObjectStorageCommand.DEFAULT_PLAYLIST_BUCKET + }, + + videos: { + bucket_name: ObjectStorageCommand.DEFAULT_WEBTORRENT_BUCKET + } + } + } + + const badCredentials = { + access_key_id: 'AKIAIOSFODNN7EXAMPLE', + secret_access_key: 'aJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY' + } + + it('Should fail with same bucket names without prefix', function (done) { + const config = merge({}, baseConfig, { + object_storage: { + streaming_playlists: { + bucket_name: 'aaa' + }, + + videos: { + bucket_name: 'aaa' + } + } + }) + + createSingleServer(1, config) + .then(() => done(new Error('Did not throw'))) + .catch(() => done()) + }) + + it('Should fail with bad credentials', async function () { + this.timeout(60000) + + await ObjectStorageCommand.prepareDefaultBuckets() + + const config = merge({}, baseConfig, { + object_storage: { + credentials: badCredentials + } + }) + + server = await createSingleServer(1, config) + await setAccessTokensToServers([ server ]) + + const { uuid } = await server.videos.quickUpload({ name: 'video' }) + + await waitJobs([ server ], true) + const video = await server.videos.get({ id: uuid }) + + expectStartWith(video.files[0].fileUrl, server.url) + + await killallServers([ server ]) + }) + + it('Should succeed with credentials from env', async function () { + this.timeout(60000) + + await ObjectStorageCommand.prepareDefaultBuckets() + + const config = merge({}, baseConfig, { + object_storage: { + credentials: { + access_key_id: '', + secret_access_key: '' + } + } + }) + + const goodCredentials = ObjectStorageCommand.getCredentialsConfig() + + server = await createSingleServer(1, config, { + env: { + AWS_ACCESS_KEY_ID: goodCredentials.access_key_id, + AWS_SECRET_ACCESS_KEY: goodCredentials.secret_access_key + } + }) + + await setAccessTokensToServers([ server ]) + + const { uuid } = await server.videos.quickUpload({ name: 'video' }) + + await waitJobs([ server ], true) + const video = await server.videos.get({ id: uuid }) + + expectStartWith(video.files[0].fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl()) + }) + + after(async function () { + await killallServers([ server ]) + }) + }) + + describe('Test simple object storage', function () { + runTestSuite({ + playlistBucket: 'streaming-playlists', + webtorrentBucket: 'videos' + }) + }) + + describe('Test object storage with prefix', function () { + runTestSuite({ + playlistBucket: 'mybucket', + webtorrentBucket: 'mybucket', + + playlistPrefix: 'streaming-playlists_', + webtorrentPrefix: 'webtorrent_' + }) + }) + + describe('Test object storage with prefix and base URL', function () { + runTestSuite({ + playlistBucket: 'mybucket', + webtorrentBucket: 'mybucket', + + playlistPrefix: 'streaming-playlists_', + webtorrentPrefix: 'webtorrent_', + + useMockBaseUrl: true + }) + }) + + describe('Test object storage with small upload part', function () { + runTestSuite({ + playlistBucket: 'streaming-playlists', + webtorrentBucket: 'videos', + + maxUploadPart: '5KB' + }) + }) +}) diff --git a/server/tests/api/redundancy/redundancy.ts b/server/tests/api/redundancy/redundancy.ts index e1a12f5f8..3400b1d9a 100644 --- a/server/tests/api/redundancy/redundancy.ts +++ b/server/tests/api/redundancy/redundancy.ts @@ -207,14 +207,14 @@ async function check1PlaylistRedundancies (videoUUID?: string) { expect(redundancy.baseUrl).to.equal(servers[0].url + '/static/redundancy/hls/' + videoUUID) } - const baseUrlPlaylist = servers[1].url + '/static/streaming-playlists/hls' - const baseUrlSegment = servers[0].url + '/static/redundancy/hls' + const baseUrlPlaylist = servers[1].url + '/static/streaming-playlists/hls/' + videoUUID + const baseUrlSegment = servers[0].url + '/static/redundancy/hls/' + videoUUID const video = await servers[0].videos.get({ id: videoUUID }) const hlsPlaylist = video.streamingPlaylists[0] for (const resolution of [ 240, 360, 480, 720 ]) { - await checkSegmentHash({ server: servers[1], baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist }) + await checkSegmentHash({ server: servers[1], baseUrlPlaylist, baseUrlSegment, resolution, hlsPlaylist }) } const { hlsFilenames } = await ensureSameFilenames(videoUUID) diff --git a/server/tests/api/videos/video-hls.ts b/server/tests/api/videos/video-hls.ts index 961f0e617..2c829f532 100644 --- a/server/tests/api/videos/video-hls.ts +++ b/server/tests/api/videos/video-hls.ts @@ -5,6 +5,7 @@ import * as chai from 'chai' import { basename, join } from 'path' import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils' import { + areObjectStorageTestsDisabled, checkDirectoryIsEmpty, checkResolutionsInMasterPlaylist, checkSegmentHash, @@ -12,7 +13,9 @@ import { cleanupTests, createMultipleServers, doubleFollow, + expectStartWith, makeRawRequest, + ObjectStorageCommand, PeerTubeServer, setAccessTokensToServers, waitJobs, @@ -23,8 +26,19 @@ import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants' const expect = chai.expect -async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) { - for (const server of servers) { +async function checkHlsPlaylist (options: { + servers: PeerTubeServer[] + videoUUID: string + hlsOnly: boolean + + resolutions?: number[] + objectStorageBaseUrl: string +}) { + const { videoUUID, hlsOnly, objectStorageBaseUrl } = options + + const resolutions = options.resolutions ?? [ 240, 360, 480, 720 ] + + for (const server of options.servers) { const videoDetails = await server.videos.get({ id: videoUUID }) const baseUrl = `http://${videoDetails.account.host}` @@ -48,9 +62,15 @@ async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, h expect(file.torrentUrl).to.match( new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`) ) - expect(file.fileUrl).to.match( - new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`) - ) + + if (objectStorageBaseUrl) { + expectStartWith(file.fileUrl, objectStorageBaseUrl) + } else { + expect(file.fileUrl).to.match( + new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`) + ) + } + expect(file.resolution.label).to.equal(resolution + 'p') await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200) @@ -80,9 +100,11 @@ async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, h const file = hlsFiles.find(f => f.resolution.id === resolution) const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8' - const subPlaylist = await server.streamingPlaylists.get({ - url: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}` - }) + const url = objectStorageBaseUrl + ? `${objectStorageBaseUrl}hls_${videoUUID}/${playlistName}` + : `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}` + + const subPlaylist = await server.streamingPlaylists.get({ url }) expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`)) expect(subPlaylist).to.contain(basename(file.fileUrl)) @@ -90,14 +112,15 @@ async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, h } { - const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls' + const baseUrlAndPath = objectStorageBaseUrl + ? objectStorageBaseUrl + 'hls_' + videoUUID + : baseUrl + '/static/streaming-playlists/hls/' + videoUUID for (const resolution of resolutions) { await checkSegmentHash({ server, baseUrlPlaylist: baseUrlAndPath, baseUrlSegment: baseUrlAndPath, - videoUUID, resolution, hlsPlaylist }) @@ -111,7 +134,7 @@ describe('Test HLS videos', function () { let videoUUID = '' let videoAudioUUID = '' - function runTestSuite (hlsOnly: boolean) { + function runTestSuite (hlsOnly: boolean, objectStorageBaseUrl?: string) { it('Should upload a video and transcode it to HLS', async function () { this.timeout(120000) @@ -121,7 +144,7 @@ describe('Test HLS videos', function () { await waitJobs(servers) - await checkHlsPlaylist(servers, videoUUID, hlsOnly) + await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl }) }) it('Should upload an audio file and transcode it to HLS', async function () { @@ -132,7 +155,13 @@ describe('Test HLS videos', function () { await waitJobs(servers) - await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ]) + await checkHlsPlaylist({ + servers, + videoUUID: videoAudioUUID, + hlsOnly, + resolutions: [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ], + objectStorageBaseUrl + }) }) it('Should update the video', async function () { @@ -142,7 +171,7 @@ describe('Test HLS videos', function () { await waitJobs(servers) - await checkHlsPlaylist(servers, videoUUID, hlsOnly) + await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl }) }) it('Should delete videos', async function () { @@ -229,6 +258,22 @@ describe('Test HLS videos', function () { runTestSuite(true) }) + describe('With object storage enabled', function () { + if (areObjectStorageTestsDisabled()) return + + before(async function () { + this.timeout(120000) + + const configOverride = ObjectStorageCommand.getDefaultConfig() + await ObjectStorageCommand.prepareDefaultBuckets() + + await servers[0].kill() + await servers[0].run(configOverride) + }) + + runTestSuite(true, ObjectStorageCommand.getPlaylistBaseUrl()) + }) + after(async function () { await cleanupTests(servers) }) diff --git a/server/tests/cli/create-import-video-file-job.ts b/server/tests/cli/create-import-video-file-job.ts index bddcff5e7..9f1b57a2e 100644 --- a/server/tests/cli/create-import-video-file-job.ts +++ b/server/tests/cli/create-import-video-file-job.ts @@ -2,8 +2,19 @@ import 'mocha' import * as chai from 'chai' -import { cleanupTests, createMultipleServers, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils' -import { VideoFile } from '@shared/models' +import { + areObjectStorageTestsDisabled, + cleanupTests, + createMultipleServers, + doubleFollow, + expectStartWith, + makeRawRequest, + ObjectStorageCommand, + PeerTubeServer, + setAccessTokensToServers, + waitJobs +} from '@shared/extra-utils' +import { HttpStatusCode, VideoDetails, VideoFile } from '@shared/models' const expect = chai.expect @@ -17,22 +28,35 @@ function assertVideoProperties (video: VideoFile, resolution: number, extname: s if (size) expect(video.size).to.equal(size) } -describe('Test create import video jobs', function () { - this.timeout(60000) +async function checkFiles (video: VideoDetails, objectStorage: boolean) { + for (const file of video.files) { + if (objectStorage) expectStartWith(file.fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl()) - let servers: PeerTubeServer[] = [] + await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) + } +} + +function runTests (objectStorage: boolean) { let video1UUID: string let video2UUID: string + let servers: PeerTubeServer[] = [] + before(async function () { this.timeout(90000) + const config = objectStorage + ? ObjectStorageCommand.getDefaultConfig() + : {} + // Run server 2 to have transcoding enabled - servers = await createMultipleServers(2) + servers = await createMultipleServers(2, config) await setAccessTokensToServers(servers) await doubleFollow(servers[0], servers[1]) + if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets() + // Upload two videos for our needs { const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video1' } }) @@ -44,7 +68,6 @@ describe('Test create import video jobs', function () { video2UUID = uuid } - // Transcoding await waitJobs(servers) }) @@ -65,6 +88,8 @@ describe('Test create import video jobs', function () { const [ originalVideo, transcodedVideo ] = videoDetails.files assertVideoProperties(originalVideo, 720, 'webm', 218910) assertVideoProperties(transcodedVideo, 480, 'webm', 69217) + + await checkFiles(videoDetails, objectStorage) } }) @@ -87,6 +112,8 @@ describe('Test create import video jobs', function () { assertVideoProperties(transcodedVideo420, 480, 'mp4') assertVideoProperties(transcodedVideo320, 360, 'mp4') assertVideoProperties(transcodedVideo240, 240, 'mp4') + + await checkFiles(videoDetails, objectStorage) } }) @@ -107,10 +134,25 @@ describe('Test create import video jobs', function () { const [ video720, video480 ] = videoDetails.files assertVideoProperties(video720, 720, 'webm', 942961) assertVideoProperties(video480, 480, 'webm', 69217) + + await checkFiles(videoDetails, objectStorage) } }) after(async function () { await cleanupTests(servers) }) +} + +describe('Test create import video jobs', function () { + + describe('On filesystem', function () { + runTests(false) + }) + + describe('On object storage', function () { + if (areObjectStorageTestsDisabled()) return + + runTests(true) + }) }) diff --git a/server/tests/cli/create-transcoding-job.ts b/server/tests/cli/create-transcoding-job.ts index df787ccdc..3313a492f 100644 --- a/server/tests/cli/create-transcoding-job.ts +++ b/server/tests/cli/create-transcoding-job.ts @@ -2,10 +2,15 @@ import 'mocha' import * as chai from 'chai' +import { HttpStatusCode, VideoFile } from '@shared/models' import { + areObjectStorageTestsDisabled, cleanupTests, createMultipleServers, doubleFollow, + expectStartWith, + makeRawRequest, + ObjectStorageCommand, PeerTubeServer, setAccessTokensToServers, waitJobs @@ -13,39 +18,39 @@ import { const expect = chai.expect -describe('Test create transcoding jobs', function () { - let servers: PeerTubeServer[] = [] - const videosUUID: string[] = [] +async function checkFilesInObjectStorage (files: VideoFile[], type: 'webtorrent' | 'playlist') { + for (const file of files) { + const shouldStartWith = type === 'webtorrent' + ? ObjectStorageCommand.getWebTorrentBaseUrl() + : ObjectStorageCommand.getPlaylistBaseUrl() - const config = { - transcoding: { - enabled: false, - resolutions: { - '240p': true, - '360p': true, - '480p': true, - '720p': true, - '1080p': true, - '1440p': true, - '2160p': true - }, - hls: { - enabled: false - } - } + expectStartWith(file.fileUrl, shouldStartWith) + + await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) } +} + +function runTests (objectStorage: boolean) { + let servers: PeerTubeServer[] = [] + const videosUUID: string[] = [] before(async function () { this.timeout(60000) + const config = objectStorage + ? ObjectStorageCommand.getDefaultConfig() + : {} + // Run server 2 to have transcoding enabled - servers = await createMultipleServers(2) + servers = await createMultipleServers(2, config) await setAccessTokensToServers(servers) - await servers[0].config.updateCustomSubConfig({ newConfig: config }) + await servers[0].config.disableTranscoding() await doubleFollow(servers[0], servers[1]) + if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets() + for (let i = 1; i <= 5; i++) { const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' + i } }) videosUUID.push(uuid) @@ -81,27 +86,29 @@ describe('Test create transcoding jobs', function () { let infoHashes: { [id: number]: string } for (const video of data) { - const videoDetail = await server.videos.get({ id: video.uuid }) + const videoDetails = await server.videos.get({ id: video.uuid }) if (video.uuid === videosUUID[1]) { - expect(videoDetail.files).to.have.lengthOf(4) - expect(videoDetail.streamingPlaylists).to.have.lengthOf(0) + expect(videoDetails.files).to.have.lengthOf(4) + expect(videoDetails.streamingPlaylists).to.have.lengthOf(0) + + if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent') if (!infoHashes) { infoHashes = {} - for (const file of videoDetail.files) { + for (const file of videoDetails.files) { infoHashes[file.resolution.id.toString()] = file.magnetUri } } else { for (const resolution of Object.keys(infoHashes)) { - const file = videoDetail.files.find(f => f.resolution.id.toString() === resolution) + const file = videoDetails.files.find(f => f.resolution.id.toString() === resolution) expect(file.magnetUri).to.equal(infoHashes[resolution]) } } } else { - expect(videoDetail.files).to.have.lengthOf(1) - expect(videoDetail.streamingPlaylists).to.have.lengthOf(0) + expect(videoDetails.files).to.have.lengthOf(1) + expect(videoDetails.streamingPlaylists).to.have.lengthOf(0) } } } @@ -125,6 +132,8 @@ describe('Test create transcoding jobs', function () { expect(videoDetails.files[1].resolution.id).to.equal(480) expect(videoDetails.streamingPlaylists).to.have.lengthOf(0) + + if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent') } }) @@ -139,11 +148,15 @@ describe('Test create transcoding jobs', function () { const videoDetails = await server.videos.get({ id: videosUUID[2] }) expect(videoDetails.files).to.have.lengthOf(1) + if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent') + expect(videoDetails.streamingPlaylists).to.have.lengthOf(1) const files = videoDetails.streamingPlaylists[0].files expect(files).to.have.lengthOf(1) expect(files[0].resolution.id).to.equal(480) + + if (objectStorage) await checkFilesInObjectStorage(files, 'playlist') } }) @@ -160,6 +173,8 @@ describe('Test create transcoding jobs', function () { const files = videoDetails.streamingPlaylists[0].files expect(files).to.have.lengthOf(1) expect(files[0].resolution.id).to.equal(480) + + if (objectStorage) await checkFilesInObjectStorage(files, 'playlist') } }) @@ -178,15 +193,15 @@ describe('Test create transcoding jobs', function () { const files = videoDetails.streamingPlaylists[0].files expect(files).to.have.lengthOf(4) + + if (objectStorage) await checkFilesInObjectStorage(files, 'playlist') } }) it('Should optimize the video file and generate HLS videos if enabled in config', async function () { this.timeout(120000) - config.transcoding.hls.enabled = true - await servers[0].config.updateCustomSubConfig({ newConfig: config }) - + await servers[0].config.enableTranscoding() await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`) await waitJobs(servers) @@ -197,10 +212,28 @@ describe('Test create transcoding jobs', function () { expect(videoDetails.files).to.have.lengthOf(4) expect(videoDetails.streamingPlaylists).to.have.lengthOf(1) expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(4) + + if (objectStorage) { + await checkFilesInObjectStorage(videoDetails.files, 'webtorrent') + await checkFilesInObjectStorage(videoDetails.streamingPlaylists[0].files, 'playlist') + } } }) after(async function () { await cleanupTests(servers) }) +} + +describe('Test create transcoding jobs', function () { + + describe('On filesystem', function () { + runTests(false) + }) + + describe('On object storage', function () { + if (areObjectStorageTestsDisabled()) return + + runTests(true) + }) }) diff --git a/server/tests/helpers/request.ts b/server/tests/helpers/request.ts index 7f7873df3..c9a2eb831 100644 --- a/server/tests/helpers/request.ts +++ b/server/tests/helpers/request.ts @@ -13,7 +13,7 @@ describe('Request helpers', function () { it('Should throw an error when the bytes limit is exceeded for request', async function () { try { - await doRequest(FIXTURE_URLS.video4K, { bodyKBLimit: 3 }) + await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 3 }) } catch { return } @@ -23,7 +23,7 @@ describe('Request helpers', function () { it('Should throw an error when the bytes limit is exceeded for request and save file', async function () { try { - await doRequestAndSaveToFile(FIXTURE_URLS.video4K, destPath1, { bodyKBLimit: 3 }) + await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath1, { bodyKBLimit: 3 }) } catch { await wait(500) @@ -35,8 +35,8 @@ describe('Request helpers', function () { }) it('Should succeed if the file is below the limit', async function () { - await doRequest(FIXTURE_URLS.video4K, { bodyKBLimit: 5 }) - await doRequestAndSaveToFile(FIXTURE_URLS.video4K, destPath2, { bodyKBLimit: 5 }) + await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 5 }) + await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath2, { bodyKBLimit: 5 }) expect(await pathExists(destPath2)).to.be.true }) -- cgit v1.2.3