summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/cron_parser_spec.rb
blob: 0864bc7258dc5b76e1c6052296f34a8edbcee8d6 (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
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
require 'spec_helper'

describe Gitlab::Ci::CronParser do
  shared_examples_for "returns time in the future" do
    it { is_expected.to be > Time.now }
  end

  describe '#next_time_from' do
    subject { described_class.new(cron, cron_timezone).next_time_from(Time.now) }

    context 'when cron and cron_timezone are valid' do
      context 'when specific time' do
        let(:cron) { '3 4 5 6 *' }
        let(:cron_timezone) { 'UTC' }

        it_behaves_like "returns time in the future"

        it 'returns exact time' do
          expect(subject.min).to eq(3)
          expect(subject.hour).to eq(4)
          expect(subject.day).to eq(5)
          expect(subject.month).to eq(6)
        end
      end

      context 'when specific day of week' do
        let(:cron) { '* * * * 0' }
        let(:cron_timezone) { 'UTC' }

        it_behaves_like "returns time in the future"

        it 'returns exact day of week' do
          expect(subject.wday).to eq(0)
        end
      end

      context 'when slash used' do
        let(:cron) { '*/10 */6 */10 */10 *' }
        let(:cron_timezone) { 'UTC' }

        it_behaves_like "returns time in the future"

        it 'returns specific time' do
          expect(subject.min).to be_in([0, 10, 20, 30, 40, 50])
          expect(subject.hour).to be_in([0, 6, 12, 18])
          expect(subject.day).to be_in([1, 11, 21, 31])
          expect(subject.month).to be_in([1, 11])
        end
      end

      context 'when range used' do
        let(:cron) { '0,20,40 * 1-5 * *' }
        let(:cron_timezone) { 'UTC' }

        it_behaves_like "returns time in the future"

        it 'returns specific time' do
          expect(subject.min).to be_in([0, 20, 40])
          expect(subject.day).to be_in((1..5).to_a)
        end
      end

      context 'when cron_timezone is US/Pacific' do
        let(:cron) { '0 0 * * *' }
        let(:cron_timezone) { 'US/Pacific' }

        it_behaves_like "returns time in the future"

        it 'converts time in server time zone' do
          expect(subject.hour).to eq((Time.zone.now.in_time_zone(cron_timezone).utc_offset / 60 / 60).abs)
        end
      end
    end

    context 'when cron and cron_timezone are invalid' do
      let(:cron) { 'invalid_cron' }
      let(:cron_timezone) { 'invalid_cron_timezone' }

      it 'returns nil' do
        is_expected.to be_nil
      end 
    end
  end

  describe '#cron_valid?' do
    subject { described_class.new(cron, Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE).cron_valid? }

    context 'when cron is valid' do
      let(:cron) { '* * * * *' }

      it { is_expected.to eq(true) }
    end

    context 'when cron is invalid' do
      let(:cron) { '*********' }

      it { is_expected.to eq(false) }
    end
  end

  describe '#cron_timezone_valid?' do
    subject { described_class.new(Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_CRON, cron_timezone).cron_timezone_valid? }

    context 'when cron is valid' do
      let(:cron_timezone) { 'Europe/Istanbul' }

      it { is_expected.to eq(true) }
    end

    context 'when cron is invalid' do
      let(:cron_timezone) { 'Invalid-zone' }

      it { is_expected.to eq(false) }
    end
  end
end