diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
commit | 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c (patch) | |
tree | 4dcc884cf6d81db44adae4aa99f8ec1233a41f55 /spec/models/ci/build_trace_metadata_spec.rb | |
parent | 744144d28e3e7fddc117924fef88de5d9674fe4c (diff) |
Add latest changes from gitlab-org/gitlab@14-3-stable-eev14.3.0-rc42
Diffstat (limited to 'spec/models/ci/build_trace_metadata_spec.rb')
-rw-r--r-- | spec/models/ci/build_trace_metadata_spec.rb | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/spec/models/ci/build_trace_metadata_spec.rb b/spec/models/ci/build_trace_metadata_spec.rb index 42b9d5d34b6..5e4645c5dc4 100644 --- a/spec/models/ci/build_trace_metadata_spec.rb +++ b/spec/models/ci/build_trace_metadata_spec.rb @@ -7,4 +7,128 @@ RSpec.describe Ci::BuildTraceMetadata do it { is_expected.to belong_to(:trace_artifact) } it { is_expected.to validate_presence_of(:build) } + it { is_expected.to validate_presence_of(:archival_attempts) } + + describe '#can_attempt_archival_now?' do + let(:metadata) do + build(:ci_build_trace_metadata, + archival_attempts: archival_attempts, + last_archival_attempt_at: last_archival_attempt_at) + end + + subject { metadata.can_attempt_archival_now? } + + context 'when archival_attempts is over the limit' do + let(:archival_attempts) { described_class::MAX_ATTEMPTS + 1 } + let(:last_archival_attempt_at) {} + + it { is_expected.to be_falsey } + end + + context 'when last_archival_attempt_at is not set' do + let(:archival_attempts) { described_class::MAX_ATTEMPTS } + let(:last_archival_attempt_at) {} + + it { is_expected.to be_truthy } + end + + context 'when last_archival_attempt_at is set' do + let(:archival_attempts) { described_class::MAX_ATTEMPTS } + let(:last_archival_attempt_at) { 6.days.ago } + + it { is_expected.to be_truthy } + end + + context 'when last_archival_attempt_at is too close' do + let(:archival_attempts) { described_class::MAX_ATTEMPTS } + let(:last_archival_attempt_at) { 1.hour.ago } + + it { is_expected.to be_falsey } + end + end + + describe '#archival_attempts_available?' do + let(:metadata) do + build(:ci_build_trace_metadata, archival_attempts: archival_attempts) + end + + subject { metadata.archival_attempts_available? } + + context 'when archival_attempts is over the limit' do + let(:archival_attempts) { described_class::MAX_ATTEMPTS + 1 } + + it { is_expected.to be_falsey } + end + + context 'when archival_attempts is at the limit' do + let(:archival_attempts) { described_class::MAX_ATTEMPTS } + + it { is_expected.to be_truthy } + end + end + + describe '#increment_archival_attempts!' do + let_it_be(:metadata) do + create(:ci_build_trace_metadata, + archival_attempts: 2, + last_archival_attempt_at: 1.day.ago) + end + + it 'increments the attempts' do + expect { metadata.increment_archival_attempts! } + .to change { metadata.reload.archival_attempts } + end + + it 'updates the last_archival_attempt_at timestamp' do + expect { metadata.increment_archival_attempts! } + .to change { metadata.reload.last_archival_attempt_at } + end + end + + describe '#track_archival!' do + let(:trace_artifact) { create(:ci_job_artifact) } + let(:metadata) { create(:ci_build_trace_metadata) } + + it 'stores the artifact id and timestamp' do + expect(metadata.trace_artifact_id).to be_nil + + metadata.track_archival!(trace_artifact.id) + metadata.reload + + expect(metadata.trace_artifact_id).to eq(trace_artifact.id) + expect(metadata.archived_at).to be_like_time(Time.current) + end + end + + describe '.find_or_upsert_for!' do + let_it_be(:build) { create(:ci_build) } + + subject(:execute) do + described_class.find_or_upsert_for!(build.id) + end + + it 'creates a new record' do + metadata = execute + + expect(metadata).to be_a(described_class) + expect(metadata.id).to eq(build.id) + expect(metadata.archival_attempts).to eq(0) + end + + context 'with existing records' do + before do + create(:ci_build_trace_metadata, + build: build, + archival_attempts: described_class::MAX_ATTEMPTS) + end + + it 'returns the existing record' do + metadata = execute + + expect(metadata).to be_a(described_class) + expect(metadata.id).to eq(build.id) + expect(metadata.archival_attempts).to eq(described_class::MAX_ATTEMPTS) + end + end + end end |