summaryrefslogtreecommitdiff
path: root/spec/models/ci/build_trace_metadata_spec.rb
blob: 5e4645c5dc4963209a9b10a2f55d59d4c1cf5f0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::BuildTraceMetadata do
  it { is_expected.to belong_to(:build) }
  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