diff options
author | Zeger-Jan van de Weg <zegerjan@gitlab.com> | 2017-05-08 01:35:56 +0300 |
---|---|---|
committer | Kamil TrzciĆski <ayufan@ayufan.eu> | 2017-05-08 01:35:56 +0300 |
commit | 8df3997a92bffa2d29f3c559933a336b837cdb93 (patch) | |
tree | 5ee50876b35b6c5fd40607665f72468cfcee51fe /spec/models/ci | |
parent | 8a0cde81feb3c8f3af26eefa5cef7b72eda2d266 (diff) |
Add Pipeline Schedules that supersedes experimental Trigger Schedule
Diffstat (limited to 'spec/models/ci')
-rw-r--r-- | spec/models/ci/pipeline_schedule_spec.rb | 112 | ||||
-rw-r--r-- | spec/models/ci/pipeline_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/ci/trigger_schedule_spec.rb | 108 | ||||
-rw-r--r-- | spec/models/ci/trigger_spec.rb | 1 |
4 files changed, 113 insertions, 109 deletions
diff --git a/spec/models/ci/pipeline_schedule_spec.rb b/spec/models/ci/pipeline_schedule_spec.rb new file mode 100644 index 00000000000..822b98c5f6c --- /dev/null +++ b/spec/models/ci/pipeline_schedule_spec.rb @@ -0,0 +1,112 @@ +require 'spec_helper' + +describe Ci::PipelineSchedule, models: true do + it { is_expected.to belong_to(:project) } + it { is_expected.to belong_to(:owner) } + + it { is_expected.to have_many(:pipelines) } + + it { is_expected.to respond_to(:ref) } + it { is_expected.to respond_to(:cron) } + it { is_expected.to respond_to(:cron_timezone) } + it { is_expected.to respond_to(:description) } + it { is_expected.to respond_to(:next_run_at) } + it { is_expected.to respond_to(:deleted_at) } + + describe 'validations' do + it 'does not allow invalid cron patters' do + pipeline_schedule = build(:ci_pipeline_schedule, cron: '0 0 0 * *') + + expect(pipeline_schedule).not_to be_valid + end + + it 'does not allow invalid cron patters' do + pipeline_schedule = build(:ci_pipeline_schedule, cron_timezone: 'invalid') + + expect(pipeline_schedule).not_to be_valid + end + end + + describe '#set_next_run_at' do + let!(:pipeline_schedule) { create(:ci_pipeline_schedule, :nightly) } + + context 'when creates new pipeline schedule' do + let(:expected_next_run_at) do + Gitlab::Ci::CronParser.new(pipeline_schedule.cron, pipeline_schedule.cron_timezone). + next_time_from(Time.now) + end + + it 'updates next_run_at automatically' do + expect(Ci::PipelineSchedule.last.next_run_at).to eq(expected_next_run_at) + end + end + + context 'when updates cron of exsisted pipeline schedule' do + let(:new_cron) { '0 0 1 1 *' } + + let(:expected_next_run_at) do + Gitlab::Ci::CronParser.new(new_cron, pipeline_schedule.cron_timezone). + next_time_from(Time.now) + end + + it 'updates next_run_at automatically' do + pipeline_schedule.update!(cron: new_cron) + + expect(Ci::PipelineSchedule.last.next_run_at).to eq(expected_next_run_at) + end + end + end + + describe '#schedule_next_run!' do + let!(:pipeline_schedule) { create(:ci_pipeline_schedule, :nightly) } + + context 'when reschedules after 10 days from now' do + let(:future_time) { 10.days.from_now } + + let(:expected_next_run_at) do + Gitlab::Ci::CronParser.new(pipeline_schedule.cron, pipeline_schedule.cron_timezone). + next_time_from(future_time) + end + + it 'points to proper next_run_at' do + Timecop.freeze(future_time) do + pipeline_schedule.schedule_next_run! + + expect(pipeline_schedule.next_run_at).to eq(expected_next_run_at) + end + end + end + end + + describe '#real_next_run' do + subject do + described_class.last.real_next_run(worker_cron: worker_cron, + worker_time_zone: worker_time_zone) + end + + context 'when GitLab time_zone is UTC' do + before do + allow(Time).to receive(:zone) + .and_return(ActiveSupport::TimeZone[worker_time_zone]) + end + + let(:worker_time_zone) { 'UTC' } + + context 'when cron_timezone is Eastern Time (US & Canada)' do + before do + create(:ci_pipeline_schedule, :nightly, + cron_timezone: 'Eastern Time (US & Canada)') + end + + let(:worker_cron) { '0 1 2 3 *' } + + it 'returns the next time worker executes' do + expect(subject.min).to eq(0) + expect(subject.hour).to eq(1) + expect(subject.day).to eq(2) + expect(subject.month).to eq(3) + end + end + end + end +end diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 3b222ea1c3d..208c8cb1c3d 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -13,6 +13,7 @@ describe Ci::Pipeline, models: true do it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:auto_canceled_by) } + it { is_expected.to belong_to(:pipeline_schedule) } it { is_expected.to have_many(:statuses) } it { is_expected.to have_many(:trigger_requests) } diff --git a/spec/models/ci/trigger_schedule_spec.rb b/spec/models/ci/trigger_schedule_spec.rb deleted file mode 100644 index 92447564d7c..00000000000 --- a/spec/models/ci/trigger_schedule_spec.rb +++ /dev/null @@ -1,108 +0,0 @@ -require 'spec_helper' - -describe Ci::TriggerSchedule, models: true do - it { is_expected.to belong_to(:project) } - it { is_expected.to belong_to(:trigger) } - it { is_expected.to respond_to(:ref) } - - describe '#set_next_run_at' do - context 'when creates new TriggerSchedule' do - before do - trigger_schedule = create(:ci_trigger_schedule, :nightly) - @expected_next_run_at = Gitlab::Ci::CronParser.new(trigger_schedule.cron, trigger_schedule.cron_timezone) - .next_time_from(Time.now) - end - - it 'updates next_run_at automatically' do - expect(Ci::TriggerSchedule.last.next_run_at).to eq(@expected_next_run_at) - end - end - - context 'when updates cron of exsisted TriggerSchedule' do - before do - trigger_schedule = create(:ci_trigger_schedule, :nightly) - new_cron = '0 0 1 1 *' - trigger_schedule.update!(cron: new_cron) # Subject - @expected_next_run_at = Gitlab::Ci::CronParser.new(new_cron, trigger_schedule.cron_timezone) - .next_time_from(Time.now) - end - - it 'updates next_run_at automatically' do - expect(Ci::TriggerSchedule.last.next_run_at).to eq(@expected_next_run_at) - end - end - end - - describe '#schedule_next_run!' do - context 'when reschedules after 10 days from now' do - before do - trigger_schedule = create(:ci_trigger_schedule, :nightly) - time_future = Time.now + 10.days - allow(Time).to receive(:now).and_return(time_future) - trigger_schedule.schedule_next_run! # Subject - @expected_next_run_at = Gitlab::Ci::CronParser.new(trigger_schedule.cron, trigger_schedule.cron_timezone) - .next_time_from(time_future) - end - - it 'points to proper next_run_at' do - expect(Ci::TriggerSchedule.last.next_run_at).to eq(@expected_next_run_at) - end - end - - context 'when cron is invalid' do - before do - trigger_schedule = create(:ci_trigger_schedule, :nightly) - trigger_schedule.cron = 'Invalid-cron' - trigger_schedule.schedule_next_run! # Subject - end - - it 'sets nil to next_run_at' do - expect(Ci::TriggerSchedule.last.next_run_at).to be_nil - end - end - - context 'when cron_timezone is invalid' do - before do - trigger_schedule = create(:ci_trigger_schedule, :nightly) - trigger_schedule.cron_timezone = 'Invalid-cron_timezone' - trigger_schedule.schedule_next_run! # Subject - end - - it 'sets nil to next_run_at' do - expect(Ci::TriggerSchedule.last.next_run_at).to be_nil - end - end - end - - describe '#real_next_run' do - subject do - Ci::TriggerSchedule.last.real_next_run(worker_cron: worker_cron, - worker_time_zone: worker_time_zone) - end - - context 'when GitLab time_zone is UTC' do - before do - allow(Time).to receive(:zone) - .and_return(ActiveSupport::TimeZone[worker_time_zone]) - end - - let(:worker_time_zone) { 'UTC' } - - context 'when cron_timezone is Eastern Time (US & Canada)' do - before do - create(:ci_trigger_schedule, :nightly, - cron_timezone: 'Eastern Time (US & Canada)') - end - - let(:worker_cron) { '0 1 2 3 *' } - - it 'returns the next time worker executes' do - expect(subject.min).to eq(0) - expect(subject.hour).to eq(1) - expect(subject.day).to eq(2) - expect(subject.month).to eq(3) - end - end - end - end -end diff --git a/spec/models/ci/trigger_spec.rb b/spec/models/ci/trigger_spec.rb index d26121018ce..92c15c13c18 100644 --- a/spec/models/ci/trigger_spec.rb +++ b/spec/models/ci/trigger_spec.rb @@ -7,7 +7,6 @@ describe Ci::Trigger, models: true do it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:owner) } it { is_expected.to have_many(:trigger_requests) } - it { is_expected.to have_one(:trigger_schedule) } end describe 'before_validation' do |