diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-14 21:12:48 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-14 21:12:48 +0300 |
commit | 35f525b5e4c2246f2411f627d4c119c5b2c5ae81 (patch) | |
tree | faa8eb9ded77bae862b881e9128a919e74ad1c5e /spec/uploaders | |
parent | c014b6b4e5c33180dd5cbff1dd1bb4623d1eca80 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/uploaders')
-rw-r--r-- | spec/uploaders/object_storage/cdn/google_cdn_spec.rb | 48 | ||||
-rw-r--r-- | spec/uploaders/object_storage/cdn/google_ip_cache_spec.rb | 84 |
2 files changed, 87 insertions, 45 deletions
diff --git a/spec/uploaders/object_storage/cdn/google_cdn_spec.rb b/spec/uploaders/object_storage/cdn/google_cdn_spec.rb index 6e57995f59f..b72f6d66d69 100644 --- a/spec/uploaders/object_storage/cdn/google_cdn_spec.rb +++ b/spec/uploaders/object_storage/cdn/google_cdn_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe ObjectStorage::CDN::GoogleCDN, :use_clean_rails_memory_store_caching do +RSpec.describe ObjectStorage::CDN::GoogleCDN, + :use_clean_rails_memory_store_caching, :use_clean_rails_redis_caching, :sidekiq_inline do include StubRequests let(:key) { SecureRandom.hex } @@ -15,7 +16,7 @@ RSpec.describe ObjectStorage::CDN::GoogleCDN, :use_clean_rails_memory_store_cach subject { described_class.new(options) } before do - WebMock.stub_request(:get, described_class::GOOGLE_IP_RANGES_URL) + WebMock.stub_request(:get, GoogleCloud::FetchGoogleIpListService::GOOGLE_IP_RANGES_URL) .to_return(status: 200, body: google_cloud_ips, headers: headers) end @@ -35,12 +36,6 @@ RSpec.describe ObjectStorage::CDN::GoogleCDN, :use_clean_rails_memory_store_cach it { expect(subject.use_cdn?(ip_address)).to eq(expected) } end - it 'caches the value' do - expect(subject.use_cdn?(public_ip)).to be true - expect(Rails.cache.fetch(described_class::GOOGLE_CDN_LIST_KEY)).to be_present - expect(Gitlab::ProcessMemoryCache.cache_backend.fetch(described_class::GOOGLE_CDN_LIST_KEY)).to be_present - end - context 'when the key name is missing' do let(:options) { { url: 'https://cdn.gitlab.example.com', key: Base64.urlsafe_encode64(SecureRandom.hex) } } @@ -73,43 +68,6 @@ RSpec.describe ObjectStorage::CDN::GoogleCDN, :use_clean_rails_memory_store_cach expect(subject.use_cdn?(public_ip)).to be false end end - - shared_examples 'IP range retrieval failure' do - it 'does not cache the result and logs an error' do - expect(Gitlab::ErrorTracking).to receive(:log_exception).and_call_original - expect(subject.use_cdn?(public_ip)).to be false - expect(Rails.cache.fetch(described_class::GOOGLE_CDN_LIST_KEY)).to be_nil - expect(Gitlab::ProcessMemoryCache.cache_backend.fetch(described_class::GOOGLE_CDN_LIST_KEY)).to be_nil - end - end - - context 'when the URL returns a 404' do - before do - WebMock.stub_request(:get, described_class::GOOGLE_IP_RANGES_URL).to_return(status: 404) - end - - it_behaves_like 'IP range retrieval failure' - end - - context 'when the URL returns too large of a payload' do - before do - stub_const("#{described_class}::RESPONSE_BODY_LIMIT", 300) - end - - it_behaves_like 'IP range retrieval failure' - end - - context 'when the URL returns HTML' do - let(:headers) { { 'Content-Type' => 'text/html' } } - - it_behaves_like 'IP range retrieval failure' - end - - context 'when the URL returns empty results' do - let(:google_cloud_ips) { '{}' } - - it_behaves_like 'IP range retrieval failure' - end end describe '#signed_url' do diff --git a/spec/uploaders/object_storage/cdn/google_ip_cache_spec.rb b/spec/uploaders/object_storage/cdn/google_ip_cache_spec.rb new file mode 100644 index 00000000000..d6568636bc0 --- /dev/null +++ b/spec/uploaders/object_storage/cdn/google_ip_cache_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ObjectStorage::CDN::GoogleIpCache, + :use_clean_rails_memory_store_caching, :use_clean_rails_redis_caching do + include StubRequests + + let(:subnets) { [IPAddr.new("34.80.0.0/15"), IPAddr.new("2600:1900:4180::/44")] } + let(:public_ip) { '18.245.0.42' } + + describe '.update!' do + it 'caches to both L1 and L2 caches' do + expect(Gitlab::ProcessMemoryCache.cache_backend.exist?(described_class::GOOGLE_CDN_LIST_KEY)).to be false + expect(Rails.cache.exist?(described_class::GOOGLE_CDN_LIST_KEY)).to be false + + described_class.update!(subnets) + + expect(Gitlab::ProcessMemoryCache.cache_backend.fetch(described_class::GOOGLE_CDN_LIST_KEY)).to eq(subnets) + expect(Rails.cache.fetch(described_class::GOOGLE_CDN_LIST_KEY)).to eq(subnets) + end + end + + describe '.ready?' do + it 'returns false' do + expect(described_class.ready?).to be false + end + + it 'returns true' do + described_class.update!(subnets) + + expect(described_class.ready?).to be true + end + end + + describe '.google_ip?' do + using RSpec::Parameterized::TableSyntax + + where(:ip_address, :expected) do + '34.80.0.1' | true + '18.245.0.42' | false + '2500:1900:4180:0000:0000:0000:0000:0000' | false + '2600:1900:4180:0000:0000:0000:0000:0000' | true + '10.10.1.5' | false + 'fc00:0000:0000:0000:0000:0000:0000:0000' | false + end + + before do + described_class.update!(subnets) + end + + with_them do + it { expect(described_class.google_ip?(ip_address)).to eq(expected) } + end + + it 'uses the L2 cache and updates the L1 cache when L1 is missing' do + Gitlab::ProcessMemoryCache.cache_backend.delete(described_class::GOOGLE_CDN_LIST_KEY) + expect(Rails.cache.fetch(described_class::GOOGLE_CDN_LIST_KEY)).to eq(subnets) + + expect(described_class.google_ip?(public_ip)).to be false + + expect(Gitlab::ProcessMemoryCache.cache_backend.fetch(described_class::GOOGLE_CDN_LIST_KEY)).to eq(subnets) + expect(Rails.cache.fetch(described_class::GOOGLE_CDN_LIST_KEY)).to eq(subnets) + end + + it 'avoids populating L1 cache if L2 is missing' do + Gitlab::ProcessMemoryCache.cache_backend.delete(described_class::GOOGLE_CDN_LIST_KEY) + Rails.cache.delete(described_class::GOOGLE_CDN_LIST_KEY) + + expect(described_class.google_ip?(public_ip)).to be false + + expect(Gitlab::ProcessMemoryCache.cache_backend.exist?(described_class::GOOGLE_CDN_LIST_KEY)).to be false + expect(Rails.cache.exist?(described_class::GOOGLE_CDN_LIST_KEY)).to be false + end + end + + describe '.async_refresh' do + it 'schedules the worker' do + expect(::GoogleCloud::FetchGoogleIpListWorker).to receive(:perform_async) + + described_class.async_refresh + end + end +end |