diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-20 13:43:29 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-20 13:43:29 +0300 |
commit | 3b1af5cc7ed2666ff18b718ce5d30fa5a2756674 (patch) | |
tree | 3bc4a40e0ee51ec27eabf917c537033c0c5b14d4 /spec/lib/object_storage | |
parent | 9bba14be3f2c211bf79e15769cd9b77bc73a13bc (diff) |
Add latest changes from gitlab-org/gitlab@16-1-stable-eev16.1.0-rc42
Diffstat (limited to 'spec/lib/object_storage')
-rw-r--r-- | spec/lib/object_storage/direct_upload_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/object_storage/fog_helpers_spec.rb | 49 | ||||
-rw-r--r-- | spec/lib/object_storage/pending_direct_upload_spec.rb | 111 |
3 files changed, 156 insertions, 6 deletions
diff --git a/spec/lib/object_storage/direct_upload_spec.rb b/spec/lib/object_storage/direct_upload_spec.rb index 4fcc0e3f306..3a42e6ebd09 100644 --- a/spec/lib/object_storage/direct_upload_spec.rb +++ b/spec/lib/object_storage/direct_upload_spec.rb @@ -377,7 +377,7 @@ RSpec.describe ObjectStorage::DirectUpload, feature_category: :shared do end end - context 'when maximum upload size is < 5 MB' do + context 'when maximum upload size is < 5 MiB' do let(:maximum_size) { 1024 } it 'returns only 1 part' do diff --git a/spec/lib/object_storage/fog_helpers_spec.rb b/spec/lib/object_storage/fog_helpers_spec.rb new file mode 100644 index 00000000000..2ad1ac22359 --- /dev/null +++ b/spec/lib/object_storage/fog_helpers_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'spec_helper' + +module Dummy + class Implementation + include ObjectStorage::FogHelpers + + def storage_location_identifier + :artifacts + end + end + + class WrongImplementation + include ObjectStorage::FogHelpers + end +end + +RSpec.describe ObjectStorage::FogHelpers, feature_category: :shared do + let(:implementation_class) { Dummy::Implementation } + + subject { implementation_class.new.available? } + + before do + stub_artifacts_object_storage(enabled: true) + end + + describe '#available?' do + context 'when object storage is enabled' do + it { is_expected.to eq(true) } + end + + context 'when object storage is disabled' do + before do + stub_artifacts_object_storage(enabled: false) + end + + it { is_expected.to eq(false) } + end + + context 'when implementing class did not define storage_location_identifier' do + let(:implementation_class) { Dummy::WrongImplementation } + + it 'raises an error' do + expect { subject }.to raise_error(NotImplementedError) + end + end + end +end diff --git a/spec/lib/object_storage/pending_direct_upload_spec.rb b/spec/lib/object_storage/pending_direct_upload_spec.rb index af08b9c8188..7acd599ed9f 100644 --- a/spec/lib/object_storage/pending_direct_upload_spec.rb +++ b/spec/lib/object_storage/pending_direct_upload_spec.rb @@ -2,23 +2,38 @@ require 'spec_helper' -RSpec.describe ObjectStorage::PendingDirectUpload, :clean_gitlab_redis_shared_state, feature_category: :shared do +RSpec.describe ObjectStorage::PendingDirectUpload, :direct_uploads, :clean_gitlab_redis_shared_state, feature_category: :shared do let(:location_identifier) { :artifacts } let(:path) { 'some/path/123' } describe '.prepare' do it 'creates a redis entry for the given location identifier and path' do + redis_key = described_class.redis_key(location_identifier, path) + + expect_to_log(:prepared, redis_key) + freeze_time do described_class.prepare(location_identifier, path) ::Gitlab::Redis::SharedState.with do |redis| - key = described_class.key(location_identifier, path) - expect(redis.hget('pending_direct_uploads', key)).to eq(Time.current.utc.to_i.to_s) + expect(redis.hget('pending_direct_uploads', redis_key)).to eq(Time.current.utc.to_i.to_s) end end end end + describe '.count' do + subject { described_class.count } + + before do + described_class.prepare(:artifacts, 'some/path') + described_class.prepare(:uploads, 'some/other/path') + described_class.prepare(:artifacts, 'some/new/path') + end + + it { is_expected.to eq(3) } + end + describe '.exists?' do let(:path) { 'some/path/123' } @@ -56,15 +71,101 @@ RSpec.describe ObjectStorage::PendingDirectUpload, :clean_gitlab_redis_shared_st expect(described_class.exists?(location_identifier, path)).to eq(true) + redis_key = described_class.redis_key(location_identifier, path) + + expect_to_log(:completed, redis_key) + described_class.complete(location_identifier, path) expect(described_class.exists?(location_identifier, path)).to eq(false) end end - describe '.key' do - subject { described_class.key(location_identifier, path) } + describe '.redis_key' do + subject { described_class.redis_key(location_identifier, path) } it { is_expected.to eq("#{location_identifier}:#{path}") } end + + describe '.each' do + before do + described_class.prepare(:artifacts, 'some/path') + described_class.prepare(:uploads, 'some/other/path') + described_class.prepare(:artifacts, 'some/new/path') + end + + it 'yields each pending direct upload object' do + expect { |b| described_class.each(&b) }.to yield_control.exactly(3).times + end + end + + describe '#stale?' do + let(:pending_direct_upload) do + described_class.new( + redis_key: 'artifacts:some/path', + storage_location_identifier: 'artifacts', + object_storage_path: 'some/path', + timestamp: timestamp + ) + end + + subject { pending_direct_upload.stale? } + + context 'when timestamp is older than 3 hours ago' do + let(:timestamp) { 4.hours.ago.utc.to_i } + + it { is_expected.to eq(true) } + end + + context 'when timestamp is not older than 3 hours ago' do + let(:timestamp) { 2.hours.ago.utc.to_i } + + it { is_expected.to eq(false) } + end + end + + describe '#delete' do + let(:object_storage_path) { 'some/path' } + let(:pending_direct_upload) do + described_class.new( + redis_key: 'artifacts:some/path', + storage_location_identifier: location_identifier, + object_storage_path: object_storage_path, + timestamp: 4.hours.ago + ) + end + + let(:location_identifier) { JobArtifactUploader.storage_location_identifier } + let(:fog_connection) { stub_artifacts_object_storage(JobArtifactUploader, direct_upload: true) } + + before do + fog_connection.directories + .new(key: location_identifier.to_s) + .files + .create( # rubocop:disable Rails/SaveBang + key: object_storage_path, + body: 'something' + ) + + prepare_pending_direct_upload(object_storage_path, 4.hours.ago) + end + + it 'deletes the object from storage and also the redis entry' do + redis_key = described_class.redis_key(location_identifier, object_storage_path) + + expect_to_log(:deleted, redis_key) + + expect { pending_direct_upload.delete }.to change { total_pending_direct_uploads }.by(-1) + + expect_not_to_have_pending_direct_upload(object_storage_path) + expect_pending_uploaded_object_not_to_exist(object_storage_path) + end + end + + def expect_to_log(event, redis_key) + expect(Gitlab::AppLogger).to receive(:info).with( + message: "Pending direct upload #{event}", + redis_key: redis_key + ) + end end |