pipeline_schedule_worker_spec.rb 1.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'spec_helper'

describe PipelineScheduleWorker do
  subject { described_class.new.perform }

  set(:project) { create(:project, :repository) }
  set(:user) { create(:user) }

  let!(:pipeline_schedule) do
    create(:ci_pipeline_schedule, :nightly, project: project, owner: user)
  end

  before do
    stub_ci_pipeline_to_return_yaml_file

16
17
    pipeline_schedule.update_column(:next_run_at, 1.day.ago)
  end
18

19
  context 'when the schedule is runnable by the user' do
20
    before do
21
      project.add_maintainer(user)
22
23
    end

24
    context 'when there is a scheduled pipeline within next_run_at' do
Shinya Maeda's avatar
Shinya Maeda committed
25
26
27
      shared_examples 'successful scheduling' do
        it 'creates a new pipeline' do
          expect { subject }.to change { project.pipelines.count }.by(1)
Shinya Maeda's avatar
Shinya Maeda committed
28
          expect(Ci::Pipeline.last).to be_schedule
Shinya Maeda's avatar
Shinya Maeda committed
29
30
31
32
33
34

          pipeline_schedule.reload
          expect(pipeline_schedule.next_run_at).to be > Time.now
          expect(pipeline_schedule).to eq(project.pipelines.last.pipeline_schedule)
          expect(pipeline_schedule).to be_active
        end
35
      end
36

Shinya Maeda's avatar
Shinya Maeda committed
37
      it_behaves_like 'successful scheduling'
38

Shinya Maeda's avatar
Shinya Maeda committed
39
40
41
42
43
44
      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
45

Shinya Maeda's avatar
Shinya Maeda committed
46
        it_behaves_like 'successful scheduling'
47
      end
48
49
    end

Shinya Maeda's avatar
Shinya Maeda committed
50
    context 'when the schedule is deactivated' do
51
52
53
54
55
56
57
      before do
        pipeline_schedule.deactivate!
      end

      it 'does not creates a new pipeline' do
        expect { subject }.not_to change { project.pipelines.count }
      end
58
59
60
    end
  end

61
62
63
64
65
  context 'when the schedule is not runnable by the user' do
    it 'deactivates the schedule' do
      subject

      expect(pipeline_schedule.reload.active).to be_falsy
66
67
    end

68
    it 'does not schedule a pipeline' do
69
70
71
72
      expect { subject }.not_to change { project.pipelines.count }
    end
  end
end