summaryrefslogtreecommitdiff
path: root/spec/workers/trigger_schedule_worker_spec.rb
blob: 861bed4442ec5e93ed5a69dac8d9aef1bad32cf3 (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
require 'spec_helper'

describe TriggerScheduleWorker do
  let(:worker) { described_class.new }

  before do
    stub_ci_pipeline_to_return_yaml_file
  end

  context 'when there is a scheduled trigger within next_run_at' do
    let(:next_run_at) { 2.days.ago }

    let!(:trigger_schedule) do
      create(:ci_trigger_schedule, :nightly)
    end

    before do
      trigger_schedule.update_column(:next_run_at, next_run_at)
    end

    it 'creates a new trigger request' do
      expect { worker.perform }.to change { Ci::TriggerRequest.count }
    end

    it 'creates a new pipeline' do
      expect { worker.perform }.to change { Ci::Pipeline.count }
      expect(Ci::Pipeline.last).to be_pending
    end

    it 'updates next_run_at' do
      worker.perform

      expect(trigger_schedule.reload.next_run_at).not_to eq(next_run_at)
    end

    context 'inactive schedule' do
      before do
        trigger_schedule.update(active: false)
      end

      it 'does not create a new trigger' do
        expect { worker.perform }.not_to change { Ci::TriggerRequest.count }
      end
    end
  end

  context 'when there are no scheduled triggers within next_run_at' do
    before { create(:ci_trigger_schedule, :nightly) }

    it 'does not create a new pipeline' do
      expect { worker.perform }.not_to change { Ci::Pipeline.count }
    end

    it 'does not update next_run_at' do
      expect { worker.perform }.not_to change { Ci::TriggerSchedule.last.next_run_at }
    end
  end

  context 'when next_run_at is nil' do
    before do
      schedule = create(:ci_trigger_schedule, :nightly)
      schedule.update_column(:next_run_at, nil)
    end

    it 'does not create a new pipeline' do
      expect { worker.perform }.not_to change { Ci::Pipeline.count }
    end

    it 'does not update next_run_at' do
      expect { worker.perform }.not_to change { Ci::TriggerSchedule.last.next_run_at }
    end
  end
end