pipeline_schedule_worker_spec.rb 2.86 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
require 'spec_helper'

describe PipelineScheduleWorker do
6 7
  include ExclusiveLeaseHelpers

8 9
  subject { described_class.new.perform }

10 11
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user) { create(:user) }
12 13 14 15 16 17

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

  before do
18
    stub_application_setting(auto_devops_enabled: false)
19 20
    stub_ci_pipeline_to_return_yaml_file

21 22
    pipeline_schedule.update_column(:next_run_at, 1.day.ago)
  end
23

24
  context 'when the schedule is runnable by the user' do
25
    before do
26
      project.add_maintainer(user)
27 28
    end

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

          pipeline_schedule.reload
Sean Arnold's avatar
Sean Arnold committed
36
          expect(pipeline_schedule.next_run_at).to be > Time.current
37
          expect(pipeline_schedule).to eq(project.ci_pipelines.last.pipeline_schedule)
Shinya Maeda's avatar
Shinya Maeda committed
38 39
          expect(pipeline_schedule).to be_active
        end
40
      end
41

Shinya Maeda's avatar
Shinya Maeda committed
42
      it_behaves_like 'successful scheduling'
43

Shinya Maeda's avatar
Shinya Maeda committed
44 45 46 47 48 49
      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
50

Shinya Maeda's avatar
Shinya Maeda committed
51
        it_behaves_like 'successful scheduling'
52
      end
53 54
    end

Shinya Maeda's avatar
Shinya Maeda committed
55
    context 'when the schedule is deactivated' do
56 57 58 59 60
      before do
        pipeline_schedule.deactivate!
      end

      it 'does not creates a new pipeline' do
61
        expect { subject }.not_to change { project.ci_pipelines.count }
62
      end
63
    end
64 65 66 67 68 69

    context 'when gitlab-ci.yml is corrupted' do
      before do
        stub_ci_pipeline_yaml_file(YAML.dump(rspec: { variables: 'rspec' } ))
      end

70 71
      it 'does not creates a new pipeline' do
        expect { subject }.not_to change { project.ci_pipelines.count }
72 73
      end
    end
74 75
  end

76
  context 'when the schedule is not runnable by the user' do
77 78 79 80 81 82 83
    it 'does not deactivate the schedule' do
      subject

      expect(pipeline_schedule.reload.active).to be_truthy
    end

    it 'does not create a pipeline' do
84
      expect { subject }.not_to change { project.ci_pipelines.count }
85
    end
86

87 88
    it 'does not raise an exception' do
      expect { subject }.not_to raise_error
89
    end
90 91 92 93 94 95 96 97 98
  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
99
      expect { subject }.not_to change { project.ci_pipelines.count }
100
    end
101 102 103 104

    it 'does not raise an exception' do
      expect { subject }.not_to raise_error
    end
105 106
  end
end