From f89189907bbdff6c4bc6d3460ed9ef4c49515f17 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 May 2023 10:49:45 +0200 Subject: Prevent object storage mock conflicts When running tests in parallel --- .../server/object-storage-command.ts | 103 +++++++++++++-------- shared/server-commands/server/server.ts | 3 - shared/server-commands/videos/live-command.ts | 20 ++-- 3 files changed, 75 insertions(+), 51 deletions(-) (limited to 'shared/server-commands') diff --git a/shared/server-commands/server/object-storage-command.ts b/shared/server-commands/server/object-storage-command.ts index a1fe4f0f7..7d8ec93cd 100644 --- a/shared/server-commands/server/object-storage-command.ts +++ b/shared/server-commands/server/object-storage-command.ts @@ -1,34 +1,17 @@ - +import { randomInt } from 'crypto' import { HttpStatusCode } from '@shared/models' import { makePostBodyRequest } from '../requests' -import { AbstractCommand } from '../shared' - -export class ObjectStorageCommand extends AbstractCommand { - static readonly DEFAULT_PLAYLIST_MOCK_BUCKET = 'streaming-playlists' - static readonly DEFAULT_WEBTORRENT_MOCK_BUCKET = 'videos' +export class ObjectStorageCommand { static readonly DEFAULT_SCALEWAY_BUCKET = 'peertube-ci-test' - // --------------------------------------------------------------------------- - - static getDefaultMockConfig () { - return { - object_storage: { - enabled: true, - endpoint: 'http://' + this.getMockEndpointHost(), - region: this.getMockRegion(), - - credentials: this.getMockCredentialsConfig(), + private readonly bucketsCreated: string[] = [] + private readonly seed: number - streaming_playlists: { - bucket_name: this.DEFAULT_PLAYLIST_MOCK_BUCKET - }, + // --------------------------------------------------------------------------- - videos: { - bucket_name: this.DEFAULT_WEBTORRENT_MOCK_BUCKET - } - } - } + constructor () { + this.seed = randomInt(0, 10000) } static getMockCredentialsConfig () { @@ -46,35 +29,79 @@ export class ObjectStorageCommand extends AbstractCommand { return 'us-east-1' } - static getMockWebTorrentBaseUrl () { - return `http://${this.DEFAULT_WEBTORRENT_MOCK_BUCKET}.${this.getMockEndpointHost()}/` + getDefaultMockConfig () { + return { + object_storage: { + enabled: true, + endpoint: 'http://' + ObjectStorageCommand.getMockEndpointHost(), + region: ObjectStorageCommand.getMockRegion(), + + credentials: ObjectStorageCommand.getMockCredentialsConfig(), + + streaming_playlists: { + bucket_name: this.getMockStreamingPlaylistsBucketName() + }, + + videos: { + bucket_name: this.getMockWebVideosBucketName() + } + } + } } - static getMockPlaylistBaseUrl () { - return `http://${this.DEFAULT_PLAYLIST_MOCK_BUCKET}.${this.getMockEndpointHost()}/` + getMockWebVideosBaseUrl () { + return `http://${this.getMockWebVideosBucketName()}.${ObjectStorageCommand.getMockEndpointHost()}/` } - static async prepareDefaultMockBuckets () { - await this.createMockBucket(this.DEFAULT_PLAYLIST_MOCK_BUCKET) - await this.createMockBucket(this.DEFAULT_WEBTORRENT_MOCK_BUCKET) + getMockPlaylistBaseUrl () { + return `http://${this.getMockStreamingPlaylistsBucketName()}.${ObjectStorageCommand.getMockEndpointHost()}/` } - static async createMockBucket (name: string) { + async prepareDefaultMockBuckets () { + await this.createMockBucket(this.getMockStreamingPlaylistsBucketName()) + await this.createMockBucket(this.getMockWebVideosBucketName()) + } + + async createMockBucket (name: string) { + this.bucketsCreated.push(name) + + await this.deleteMockBucket(name) + await makePostBodyRequest({ - url: this.getMockEndpointHost(), - path: '/ui/' + name + '?delete', + url: ObjectStorageCommand.getMockEndpointHost(), + path: '/ui/' + name + '?create', expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307 }) await makePostBodyRequest({ - url: this.getMockEndpointHost(), - path: '/ui/' + name + '?create', + url: ObjectStorageCommand.getMockEndpointHost(), + path: '/ui/' + name + '?make-public', expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307 }) + } + async cleanupMock () { + for (const name of this.bucketsCreated) { + await this.deleteMockBucket(name) + } + } + + getMockStreamingPlaylistsBucketName (name = 'streaming-playlists') { + return this.getMockBucketName(name) + } + + getMockWebVideosBucketName (name = 'web-videos') { + return this.getMockBucketName(name) + } + + getMockBucketName (name: string) { + return `${this.seed}-${name}` + } + + private async deleteMockBucket (name: string) { await makePostBodyRequest({ - url: this.getMockEndpointHost(), - path: '/ui/' + name + '?make-public', + url: ObjectStorageCommand.getMockEndpointHost(), + path: '/ui/' + name + '?delete', expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307 }) } diff --git a/shared/server-commands/server/server.ts b/shared/server-commands/server/server.ts index 66b7ff09d..70f7a3ee2 100644 --- a/shared/server-commands/server/server.ts +++ b/shared/server-commands/server/server.ts @@ -48,7 +48,6 @@ import { DebugCommand } from './debug-command' import { FollowsCommand } from './follows-command' import { JobsCommand } from './jobs-command' import { MetricsCommand } from './metrics-command' -import { ObjectStorageCommand } from './object-storage-command' import { PluginsCommand } from './plugins-command' import { RedundancyCommand } from './redundancy-command' import { ServersCommand } from './servers-command' @@ -140,7 +139,6 @@ export class PeerTubeServer { servers?: ServersCommand login?: LoginCommand users?: UsersCommand - objectStorage?: ObjectStorageCommand videoStudio?: VideoStudioCommand videos?: VideosCommand videoStats?: VideoStatsCommand @@ -429,7 +427,6 @@ export class PeerTubeServer { this.login = new LoginCommand(this) this.users = new UsersCommand(this) this.videos = new VideosCommand(this) - this.objectStorage = new ObjectStorageCommand(this) this.videoStudio = new VideoStudioCommand(this) this.videoStats = new VideoStatsCommand(this) this.views = new ViewsCommand(this) diff --git a/shared/server-commands/videos/live-command.ts b/shared/server-commands/videos/live-command.ts index 73f4eefd3..44d625970 100644 --- a/shared/server-commands/videos/live-command.ts +++ b/shared/server-commands/videos/live-command.ts @@ -192,7 +192,7 @@ export class LiveCommand extends AbstractCommand { videoUUID: string playlistNumber: number segment: number - objectStorage: boolean + objectStorage?: ObjectStorageCommand objectStorageBaseUrl?: string }) { const { @@ -201,12 +201,12 @@ export class LiveCommand extends AbstractCommand { playlistNumber, segment, videoUUID, - objectStorageBaseUrl = ObjectStorageCommand.getMockPlaylistBaseUrl() + objectStorageBaseUrl } = options const segmentName = `${playlistNumber}-00000${segment}.ts` const baseUrl = objectStorage - ? join(objectStorageBaseUrl, 'hls') + ? join(objectStorageBaseUrl || objectStorage.getMockPlaylistBaseUrl(), 'hls') : server.url + '/static/streaming-playlists/hls' let error = true @@ -226,7 +226,7 @@ export class LiveCommand extends AbstractCommand { const hlsPlaylist = video.streamingPlaylists[0] // Check SHA generation - const shaBody = await server.streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url, withRetry: objectStorage }) + const shaBody = await server.streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url, withRetry: !!objectStorage }) if (!shaBody[segmentName]) { throw new Error('Segment SHA does not exist') } @@ -261,13 +261,13 @@ export class LiveCommand extends AbstractCommand { videoUUID: string playlistNumber: number segment: number - objectStorage?: boolean // default false + objectStorage?: ObjectStorageCommand }) { - const { playlistNumber, segment, videoUUID, objectStorage = false } = options + const { playlistNumber, segment, videoUUID, objectStorage } = options const segmentName = `${playlistNumber}-00000${segment}.ts` const baseUrl = objectStorage - ? ObjectStorageCommand.getMockPlaylistBaseUrl() + ? objectStorage.getMockPlaylistBaseUrl() : `${this.server.url}/static/streaming-playlists/hls` const url = `${baseUrl}/${videoUUID}/${segmentName}` @@ -284,12 +284,12 @@ export class LiveCommand extends AbstractCommand { getPlaylistFile (options: OverrideCommandOptions & { videoUUID: string playlistName: string - objectStorage?: boolean // default false + objectStorage?: ObjectStorageCommand }) { - const { playlistName, videoUUID, objectStorage = false } = options + const { playlistName, videoUUID, objectStorage } = options const baseUrl = objectStorage - ? ObjectStorageCommand.getMockPlaylistBaseUrl() + ? objectStorage.getMockPlaylistBaseUrl() : `${this.server.url}/static/streaming-playlists/hls` const url = `${baseUrl}/${videoUUID}/${playlistName}` -- cgit v1.2.3