summaryrefslogtreecommitdiff
path: root/spec/services/ci/process_build_service_spec.rb
blob: 9f47439dc4ab5c5716be6697d1d2bc045340f779 (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
# frozen_string_literal: true
require 'spec_helper'

describe Ci::ProcessBuildService, '#execute' do
  let(:user) { create(:user) }
  let(:project) { create(:project) }

  subject { described_class.new(project, user).execute(build, current_status) }

  before do
    project.add_maintainer(user)
  end

  shared_examples_for 'Enqueuing properly' do |valid_statuses_for_when|
    valid_statuses_for_when.each do |status_for_prior_stages|
      context "when status for prior stages is #{status_for_prior_stages}" do
        let(:current_status) { status_for_prior_stages }

        %w[created skipped manual scheduled].each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, when: when_option, user: user, project: project) }

            it 'enqueues the build' do
              expect { subject }.to change { build.status }.to('pending')
            end
          end
        end

        %w[pending running success failed canceled].each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, when: when_option, user: user, project: project) }

            it 'does not change the build status' do
              expect { subject }.not_to change { build.status }
            end
          end
        end
      end
    end

    (HasStatus::AVAILABLE_STATUSES - valid_statuses_for_when).each do |status_for_prior_stages|
      let(:current_status) { status_for_prior_stages }

      context "when status for prior stages is #{status_for_prior_stages}" do
        %w[created pending].each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, when: when_option, user: user, project: project) }

            it 'skips the build' do
              expect { subject }.to change { build.status }.to('skipped')
            end
          end
        end

        (HasStatus::AVAILABLE_STATUSES - %w[created pending]).each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, when: when_option, user: user, project: project) }

            it 'does not change build status' do
              expect { subject }.not_to change { build.status }
            end
          end
        end
      end
    end
  end

  shared_examples_for 'Actionizing properly' do |valid_statuses_for_when|
    valid_statuses_for_when.each do |status_for_prior_stages|
      context "when status for prior stages is #{status_for_prior_stages}" do
        let(:current_status) { status_for_prior_stages }

        %w[created].each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, :actionable, user: user, project: project) }

            it 'enqueues the build' do
              expect { subject }.to change { build.status }.to('manual')
            end
          end
        end

        %w[manual skipped pending running success failed canceled scheduled].each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, :actionable, user: user, project: project) }

            it 'does not change the build status' do
              expect { subject }.not_to change { build.status }
            end
          end
        end
      end
    end

    (HasStatus::AVAILABLE_STATUSES - valid_statuses_for_when).each do |status_for_prior_stages|
      let(:current_status) { status_for_prior_stages }

      context "when status for prior stages is #{status_for_prior_stages}" do
        %w[created pending].each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, :actionable, user: user, project: project) }

            it 'skips the build' do
              expect { subject }.to change { build.status }.to('skipped')
            end
          end
        end

        (HasStatus::AVAILABLE_STATUSES - %w[created pending]).each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, :actionable, user: user, project: project) }

            it 'does not change build status' do
              expect { subject }.not_to change { build.status }
            end
          end
        end
      end
    end
  end

  shared_examples_for 'Scheduling properly' do |valid_statuses_for_when|
    valid_statuses_for_when.each do |status_for_prior_stages|
      context "when status for prior stages is #{status_for_prior_stages}" do
        let(:current_status) { status_for_prior_stages }

        %w[created].each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, :schedulable, user: user, project: project) }

            it 'enqueues the build' do
              expect { subject }.to change { build.status }.to('scheduled')
            end
          end
        end

        %w[manual skipped pending running success failed canceled scheduled].each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, :schedulable, user: user, project: project) }

            it 'does not change the build status' do
              expect { subject }.not_to change { build.status }
            end
          end
        end
      end
    end

    (HasStatus::AVAILABLE_STATUSES - valid_statuses_for_when).each do |status_for_prior_stages|
      let(:current_status) { status_for_prior_stages }

      context "when status for prior stages is #{status_for_prior_stages}" do
        %w[created pending].each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, :schedulable, user: user, project: project) }

            it 'skips the build' do
              expect { subject }.to change { build.status }.to('skipped')
            end
          end
        end

        (HasStatus::AVAILABLE_STATUSES - %w[created pending]).each do |status|
          context "when build status is #{status}" do
            let(:build) { create(:ci_build, status.to_sym, :schedulable, user: user, project: project) }

            it 'does not change build status' do
              expect { subject }.not_to change { build.status }
            end
          end
        end
      end
    end
  end

  context 'when build has on_success option' do
    let(:when_option) { :on_success }

    it_behaves_like 'Enqueuing properly', %w[success skipped]
  end

  context 'when build has on_failure option' do
    let(:when_option) { :on_failure }

    it_behaves_like 'Enqueuing properly', %w[failed]
  end

  context 'when build has always option' do
    let(:when_option) { :always }

    it_behaves_like 'Enqueuing properly', %w[success failed skipped]
  end

  context 'when build has manual option' do
    let(:when_option) { :manual }

    it_behaves_like 'Actionizing properly', %w[success skipped]
  end

  context 'when build has delayed option' do
    let(:when_option) { :delayed }

    before do
      allow(Ci::BuildScheduleWorker).to receive(:perform_at) { }
    end

    context 'when ci_enable_scheduled_build is enabled' do
      before do
        stub_feature_flags(ci_enable_scheduled_build: true)
      end

      it_behaves_like 'Scheduling properly', %w[success skipped]
    end

    context 'when ci_enable_scheduled_build is enabled' do
      before do
        stub_feature_flags(ci_enable_scheduled_build: false)
      end

      it_behaves_like 'Actionizing properly', %w[success skipped]
    end
  end
end