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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe PipelineScheduleWorker, :sidekiq_inline, feature_category: :continuous_integration do
include ExclusiveLeaseHelpers
subject { described_class.new.perform }
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let!(:pipeline_schedule) do
create(:ci_pipeline_schedule, :nightly, project: project, owner: user)
end
let(:next_run_at) { pipeline_schedule.next_run_at }
before do
stub_application_setting(auto_devops_enabled: false)
stub_ci_pipeline_to_return_yaml_file
end
around do |example|
travel_to(next_run_at + 1.hour) do
example.run
end
end
context 'when the schedule is runnable by the user' do
before do
project.add_maintainer(user)
end
context 'when there is a scheduled pipeline within next_run_at' do
shared_examples 'successful scheduling' do
it 'creates a new pipeline' do
expect { subject }.to change { project.ci_pipelines.count }.by(1)
last_pipeline = project.ci_pipelines.last
expect(last_pipeline).to be_schedule
expect(last_pipeline.pipeline_schedule).to eq(pipeline_schedule)
end
it 'updates next_run_at' do
expect { subject }.to change { pipeline_schedule.reload.next_run_at }.by(1.day)
end
it 'does not change active status' do
expect { subject }.not_to change { pipeline_schedule.reload.active? }.from(true)
end
end
it_behaves_like 'successful scheduling'
context 'when the latest commit contains [ci skip]' do
before do
allow_any_instance_of(Ci::Pipeline)
.to receive(:git_commit_message)
.and_return('some commit [ci skip]')
end
it_behaves_like 'successful scheduling'
end
end
context 'when the schedule is deactivated' do
before do
pipeline_schedule.deactivate!
end
it 'does not creates a new pipeline' do
expect { subject }.not_to change { project.ci_pipelines.count }
end
end
context 'when gitlab-ci.yml is corrupted' do
before do
stub_ci_pipeline_yaml_file(YAML.dump(rspec: { variables: 'rspec' } ))
end
it 'creates a new pipeline' do
expect { subject }.to change { project.ci_pipelines.count }.by(1)
end
context 'with feature flag persist_failed_pipelines_from_schedules disabled' do
before do
stub_feature_flags(persist_failed_pipelines_from_schedules: false)
end
it 'does not create a new pipeline' do
expect { subject }.not_to change { project.ci_pipelines.count }
end
end
end
end
context 'when the schedule is not runnable by the user' do
it 'does not deactivate the schedule' do
subject
expect(pipeline_schedule.reload.active).to be_truthy
end
it 'does not create a pipeline' do
expect { subject }.not_to change { project.ci_pipelines.count }
end
it 'does not raise an exception' do
expect { subject }.not_to raise_error
end
end
context 'when .gitlab-ci.yml is missing in the project' do
before do
stub_ci_pipeline_yaml_file(nil)
project.add_maintainer(user)
end
it 'does not create a pipeline' do
expect { subject }.not_to change { project.ci_pipelines.count }
end
it 'does not raise an exception' do
expect { subject }.not_to raise_error
end
end
context 'when the project is missing' do
before do
project.delete
end
it 'does not raise an exception' do
expect { subject }.not_to raise_error
end
end
context 'when max retry attempts reach' do
let!(:lease) { stub_exclusive_lease_taken(described_class.name.underscore) }
it 'does not raise error' do
expect(lease).to receive(:try_obtain).exactly(described_class::LOCK_RETRY + 1).times
expect { subject }.to raise_error(Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError)
end
end
context 'with scheduling delay' do
before do
stub_const("#{described_class}::BATCH_SIZE", 1)
end
let!(:other_pipeline_schedule) do
create(:ci_pipeline_schedule, :every_minute, project: project, owner: user)
end
let(:next_run_at) do
[pipeline_schedule, other_pipeline_schedule].maximum(:next_run_at)
end
it 'calls bulk_perform_in with the arguments and delay' do
expect(RunPipelineScheduleWorker)
.to receive(:bulk_perform_in)
.with(1.second, [[pipeline_schedule.id, user.id, { scheduling: true }]])
.and_call_original
expect(RunPipelineScheduleWorker)
.to receive(:bulk_perform_in)
.with(7.seconds, [[other_pipeline_schedule.id, user.id, { scheduling: true }]])
.and_call_original
subject
end
context 'with run_pipeline_schedule_worker_with_delay disabled' do
before do
stub_feature_flags(run_pipeline_schedule_worker_with_delay: false)
end
it 'calls bulk_perform_async with the arguments and delay' do
expect(RunPipelineScheduleWorker)
.to receive(:bulk_perform_async)
.with([[pipeline_schedule.id, user.id, { scheduling: true }]])
.and_call_original
expect(RunPipelineScheduleWorker)
.to receive(:bulk_perform_async)
.with([[other_pipeline_schedule.id, user.id, { scheduling: true }]])
.and_call_original
subject
end
end
end
end
|