2022-10-11 01:57:18 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
RSpec.describe ObjectStorage::CDN, feature_category: :build_artifacts do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:uploader_class) do
|
|
|
|
Class.new(GitlabUploader) do
|
|
|
|
include ObjectStorage::Concern
|
|
|
|
include ObjectStorage::CDN::Concern
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# user/:id
|
|
|
|
def dynamic_segment
|
|
|
|
File.join(model.class.underscore, model.id.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:object) { build_stubbed(:user) }
|
2022-11-25 23:54:43 +05:30
|
|
|
let(:public_ip) { '18.245.0.1' }
|
2023-04-23 21:23:45 +05:30
|
|
|
let(:query_params) { { foo: :bar } }
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
let_it_be(:project) { build(:project) }
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
subject { uploader_class.new(object, :file) }
|
|
|
|
|
|
|
|
context 'with CDN config' do
|
2023-05-27 22:25:52 +05:30
|
|
|
let(:cdn_options) do
|
|
|
|
{
|
|
|
|
'object_store' => {
|
|
|
|
'cdn' => {
|
|
|
|
'provider' => cdn_provider,
|
|
|
|
'url' => 'https://gitlab.example.com',
|
|
|
|
'key_name' => 'test-key',
|
|
|
|
'key' => Base64.urlsafe_encode64('12345')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.freeze
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
before do
|
2022-11-25 23:54:43 +05:30
|
|
|
stub_artifacts_object_storage(enabled: true)
|
2023-07-09 08:55:56 +05:30
|
|
|
options = Gitlab.config.uploads.deep_merge(cdn_options)
|
2023-05-27 22:25:52 +05:30
|
|
|
allow(uploader_class).to receive(:options).and_return(options)
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
context 'with a known CDN provider' do
|
|
|
|
let(:cdn_provider) { 'google' }
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe '#cdn_enabled_url' do
|
|
|
|
it 'calls #cdn_signed_url' do
|
|
|
|
expect(subject).not_to receive(:url)
|
|
|
|
expect(subject).to receive(:cdn_signed_url).with(query_params).and_call_original
|
|
|
|
|
|
|
|
result = subject.cdn_enabled_url(public_ip, query_params)
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
expect(result.used_cdn).to be true
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe '#use_cdn?' do
|
|
|
|
it 'returns true' do
|
|
|
|
expect(subject.use_cdn?(public_ip)).to be true
|
|
|
|
end
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe '#cdn_signed_url' do
|
|
|
|
it 'returns a URL' do
|
|
|
|
expect_next_instance_of(ObjectStorage::CDN::GoogleCDN) do |cdn|
|
|
|
|
expect(cdn).to receive(:signed_url).and_return("https://cdn.example.com/path")
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(subject.cdn_signed_url).to eq("https://cdn.example.com/path")
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an unknown CDN provider' do
|
|
|
|
let(:cdn_provider) { 'amazon' }
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject.use_cdn?(public_ip) }.to raise_error("Unknown CDN provider: amazon")
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without CDN config' do
|
2023-04-23 21:23:45 +05:30
|
|
|
describe '#cdn_enabled_url' do
|
|
|
|
it 'calls #url' do
|
|
|
|
expect(subject).not_to receive(:cdn_signed_url)
|
|
|
|
expect(subject).to receive(:url).with(query: query_params).and_call_original
|
|
|
|
|
|
|
|
result = subject.cdn_enabled_url(public_ip, query_params)
|
|
|
|
|
|
|
|
expect(result.used_cdn).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
describe '#use_cdn?' do
|
|
|
|
it 'returns false' do
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(subject.use_cdn?(public_ip)).to be false
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|