summaryrefslogtreecommitdiff
path: root/spec/models/ci/stage_spec.rb
blob: 372b662fab2eb2f0b17cbf9768a70d94bfe220e6 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
require 'spec_helper'

describe Ci::Stage, models: true do
  let(:stage) { build(:ci_stage) }
  let(:pipeline) { stage.pipeline }
  let(:stage_name) { stage.name }

  describe '#expectations' do
    subject { stage }

    it { is_expected.to include_module(StaticModel) }

    it { is_expected.to respond_to(:pipeline) }
    it { is_expected.to respond_to(:name) }

    it { is_expected.to delegate_method(:project).to(:pipeline) }
  end

  describe '#statuses' do
    let!(:stage_build) { create_job(:ci_build) }
    let!(:commit_status) { create_job(:commit_status) }
    let!(:other_build) { create_job(:ci_build, stage: 'other stage') }

    subject { stage.statuses }

    it "returns only matching statuses" do
      is_expected.to contain_exactly(stage_build, commit_status)
    end
  end

  describe '#groups' do
    before do
      create_job(:ci_build, name: 'rspec 0 2')
      create_job(:ci_build, name: 'rspec 0 1')
      create_job(:ci_build, name: 'spinach 0 1')
      create_job(:commit_status, name: 'aaaaa')
    end

    it 'returns an array of three groups' do
      expect(stage.groups).to be_a Array
      expect(stage.groups).to all(be_a Ci::Group)
      expect(stage.groups.size).to eq 3
    end

    it 'returns groups with correctly ordered statuses' do
      expect(stage.groups.first.jobs.map(&:name))
        .to eq ['aaaaa']
      expect(stage.groups.second.jobs.map(&:name))
        .to eq ['rspec 0 1', 'rspec 0 2']
      expect(stage.groups.third.jobs.map(&:name))
        .to eq ['spinach 0 1']
    end

    it 'returns groups with correct names' do
      expect(stage.groups.map(&:name))
        .to eq %w[aaaaa rspec spinach]
    end
  end

  describe '#statuses_count' do
    before do
      create_job(:ci_build)
      create_job(:ci_build, stage: 'other stage')
    end

    subject { stage.statuses_count }

    it "counts statuses only from current stage" do
      is_expected.to eq(1)
    end
  end

  describe '#builds' do
    let!(:stage_build) { create_job(:ci_build) }
    let!(:commit_status) { create_job(:commit_status) }

    subject { stage.builds }

    it "returns only builds" do
      is_expected.to contain_exactly(stage_build)
    end
  end

  describe '#status' do
    subject { stage.status }

    context 'if status is already defined' do
      let(:stage) { build(:ci_stage, status: 'success') }

      it "returns defined status" do
        is_expected.to eq('success')
      end
    end

    context 'if status has to be calculated' do
      let!(:stage_build) { create_job(:ci_build, status: :failed) }

      it "returns status of a build" do
        is_expected.to eq('failed')
      end

      context 'and builds are retried' do
        let!(:new_build) { create_job(:ci_build, status: :success) }

        it "returns status of latest build" do
          is_expected.to eq('success')
        end
      end
    end
  end

  describe '#detailed_status' do
    let(:user) { create(:user) }

    subject { stage.detailed_status(user) }

    context 'when build is created' do
      let!(:stage_build) { create_job(:ci_build, status: :created) }

      it 'returns detailed status for created stage' do
        expect(subject.text).to eq 'created'
      end
    end

    context 'when build is pending' do
      let!(:stage_build) { create_job(:ci_build, status: :pending) }

      it 'returns detailed status for pending stage' do
        expect(subject.text).to eq 'pending'
      end
    end

    context 'when build is running' do
      let!(:stage_build) { create_job(:ci_build, status: :running) }

      it 'returns detailed status for running stage' do
        expect(subject.text).to eq 'running'
      end
    end

    context 'when build is successful' do
      let!(:stage_build) { create_job(:ci_build, status: :success) }

      it 'returns detailed status for successful stage' do
        expect(subject.text).to eq 'passed'
      end
    end

    context 'when build is failed' do
      let!(:stage_build) { create_job(:ci_build, status: :failed) }

      it 'returns detailed status for failed stage' do
        expect(subject.text).to eq 'failed'
      end
    end

    context 'when build is canceled' do
      let!(:stage_build) { create_job(:ci_build, status: :canceled) }

      it 'returns detailed status for canceled stage' do
        expect(subject.text).to eq 'canceled'
      end
    end

    context 'when build is skipped' do
      let!(:stage_build) { create_job(:ci_build, status: :skipped) }

      it 'returns detailed status for skipped stage' do
        expect(subject.text).to eq 'skipped'
      end
    end
  end

  describe '#success?' do
    context 'when stage is successful' do
      before do
        create_job(:ci_build, status: :success)
        create_job(:generic_commit_status, status: :success)
      end

      it 'is successful' do
        expect(stage).to be_success
      end
    end

    context 'when stage is not successful' do
      before do
        create_job(:ci_build, status: :failed)
        create_job(:generic_commit_status, status: :success)
      end

      it 'is not successful' do
        expect(stage).not_to be_success
      end
    end
  end

  describe '#has_warnings?' do
    context 'when stage has warnings' do
      context 'when using memoized warnings flag' do
        context 'when there are warnings' do
          let(:stage) { build(:ci_stage, warnings: 2) }

          it 'returns true using memoized value' do
            expect(stage).not_to receive(:statuses)
            expect(stage).to have_warnings
          end
        end

        context 'when there are no warnings' do
          let(:stage) { build(:ci_stage, warnings: 0) }

          it 'returns false using memoized value' do
            expect(stage).not_to receive(:statuses)
            expect(stage).not_to have_warnings
          end
        end

        context 'when number of warnings is not a valid value' do
          let(:stage) { build(:ci_stage, warnings: true) }

          it 'calculates statuses using database queries' do
            expect(stage).to receive(:statuses).and_call_original
            expect(stage).not_to have_warnings
          end
        end
      end

      context 'when calculating warnings from statuses' do
        before do
          create(:ci_build, :failed, :allowed_to_fail,
                 stage: stage_name, pipeline: pipeline)
        end

        it 'has warnings calculated from statuses' do
          expect(stage).to receive(:statuses).and_call_original
          expect(stage).to have_warnings
        end
      end
    end

    context 'when stage does not have warnings' do
      before do
        create(:ci_build, :success, stage: stage_name,
                                    pipeline: pipeline)
      end

      it 'does not have warnings calculated from statuses' do
        expect(stage).to receive(:statuses).and_call_original
        expect(stage).not_to have_warnings
      end
    end
  end

  def create_job(type, status: 'success', stage: stage_name, **opts)
    create(type, pipeline: pipeline, stage: stage, status: status, **opts)
  end
end