summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/cron_parser_spec.rb
blob: dd27b4045c9a9d0f8467e49181b878751118147e (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# frozen_string_literal: true

require 'spec_helper'

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

  shared_examples_for "returns time in the past" do
    it { is_expected.to be < Time.now }
  end

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

      it_behaves_like returns_time_for_epoch

      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_for_epoch

      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_for_epoch

      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_for_epoch

      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 TZInfo format' do
      before do
        allow(Time).to receive(:zone)
          .and_return(ActiveSupport::TimeZone['UTC'])
      end

      let(:hour_in_utc) do
        ActiveSupport::TimeZone[cron_timezone]
          .now.change(hour: 0).in_time_zone('UTC').hour
      end

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

        it_behaves_like returns_time_for_epoch

        context 'when PST (Pacific Standard Time)' do
          it 'converts time in server time zone' do
            travel_to(Time.utc(2017, 1, 1)) do
              expect(subject.hour).to eq(hour_in_utc)
            end
          end
        end

        context 'when PDT (Pacific Daylight Time)' do
          it 'converts time in server time zone' do
            travel_to(Time.utc(2017, 6, 1)) do
              expect(subject.hour).to eq(hour_in_utc)
            end
          end
        end
      end
    end

    context 'when cron_timezone is ActiveSupport::TimeZone format' do
      before do
        allow(Time).to receive(:zone)
          .and_return(ActiveSupport::TimeZone['UTC'])
      end

      let(:hour_in_utc) do
        ActiveSupport::TimeZone[cron_timezone]
          .now.change(hour: 0).in_time_zone('UTC').hour
      end

      context 'when cron_timezone is Berlin' do
        let(:cron) { '* 0 * * *' }
        let(:cron_timezone) { 'Berlin' }

        it_behaves_like returns_time_for_epoch

        context 'when CET (Central European Time)' do
          it 'converts time in server time zone' do
            travel_to(Time.utc(2017, 1, 1)) do
              expect(subject.hour).to eq(hour_in_utc)
            end
          end
        end

        context 'when CEST (Central European Summer Time)' do
          it 'converts time in server time zone' do
            travel_to(Time.utc(2017, 6, 1)) do
              expect(subject.hour).to eq(hour_in_utc)
            end
          end
        end
      end
    end
  end

  shared_examples_for 'when cron_timezone is Eastern Time (US & Canada)' do |returns_time_for_epoch, year|
    let(:cron) { '* 0 * * *' }
    let(:cron_timezone) { 'Eastern Time (US & Canada)' }

    before do
      allow(Time).to receive(:zone)
        .and_return(ActiveSupport::TimeZone['UTC'])
    end

    let(:hour_in_utc) do
      ActiveSupport::TimeZone[cron_timezone]
        .now.change(hour: 0).in_time_zone('UTC').hour
    end

    it_behaves_like returns_time_for_epoch

    context 'when EST (Eastern Standard Time)' do
      it 'converts time in server time zone' do
        travel_to(Time.utc(2017, 1, 1)) do
          expect(subject.hour).to eq(hour_in_utc)
        end
      end
    end

    context 'when EDT (Eastern Daylight Time)' do
      it 'converts time in server time zone' do
        travel_to(Time.utc(2017, 6, 1)) do
          expect(subject.hour).to eq(hour_in_utc)
        end
      end
    end

    context 'when time crosses a Daylight Savings boundary' do
      let(:cron) { '* 0 1 12 *'}

      # Note this previously only failed if the time zone is set
      # to a zone that observes Daylight Savings
      # (e.g. America/Chicago) at the start of the test. Stubbing
      # TZ doesn't appear to be enough.
      it 'generates day without TZInfo::AmbiguousTime error' do
        travel_to(Time.utc(2020, 1, 1)) do
          expect(subject.year).to eq(year)
          expect(subject.month).to eq(12)
          expect(subject.day).to eq(1)
        end
      end
    end
  end

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

    it { is_expected.to be_nil }
  end

  shared_examples_for 'when cron syntax is quoted' do
    let(:cron) { "'0 * * * *'" }
    let(:cron_timezone) { 'UTC' }

    it { expect(subject).to be_nil }
  end

  shared_examples_for 'when cron syntax is rufus-scheduler syntax' do
    let(:cron) { 'every 3h' }
    let(:cron_timezone) { 'UTC' }

    it { expect(subject).to be_nil }
  end

  shared_examples_for 'when cron is scheduled to a non existent day' do
    let(:cron) { '0 12 31 2 *' }
    let(:cron_timezone) { 'UTC' }

    it { expect(subject).to be_nil }
  end

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

    it_behaves_like 'when cron and cron_timezone are valid', 'returns time in the future'

    it_behaves_like 'when cron_timezone is Eastern Time (US & Canada)', 'returns time in the future', 2020

    it_behaves_like 'when cron and cron_timezone are invalid'

    it_behaves_like 'when cron syntax is quoted'

    it_behaves_like 'when cron syntax is rufus-scheduler syntax'

    it_behaves_like 'when cron is scheduled to a non existent day'
  end

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

    it_behaves_like 'when cron and cron_timezone are valid', 'returns time in the past'

    it_behaves_like 'when cron_timezone is Eastern Time (US & Canada)', 'returns time in the past', 2019

    it_behaves_like 'when cron and cron_timezone are invalid'

    it_behaves_like 'when cron syntax is quoted'

    it_behaves_like 'when cron syntax is rufus-scheduler syntax'

    it_behaves_like 'when cron is scheduled to a non existent day'
  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

    context 'when cron syntax is quoted' do
      let(:cron) { "'0 * * * *'" }

      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

    context 'when cron_timezone is ActiveSupport::TimeZone format' do
      let(:cron_timezone) { 'Eastern Time (US & Canada)' }

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