Welcome to mirror list, hosted at ThFree Co, Russian Federation.

context_selector_spec.rb « helpers « specs « spec « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a320cde71f9bbc2c8fb1acac0905a736c83c139 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# frozen_string_literal: true

require 'rspec/core/sandbox'

RSpec.describe QA::Specs::Helpers::ContextSelector do
  include QA::Support::Helpers::StubEnv
  include QA::Specs::Helpers::RSpec

  around do |ex|
    QA::Runtime::Scenario.define(:gitlab_address, 'https://staging.gitlab.com')

    RSpec::Core::Sandbox.sandboxed do |config|
      config.formatter = QA::Support::Formatters::ContextFormatter

      # If there is an example-within-an-example, we want to make sure the inner example
      # does not get a reference to the outer example (the real spec) if it calls
      # something like `pending`
      config.before(:context) { RSpec.current_example = nil }
      config.color_mode = :off

      ex.run
    end
  end

  describe '.context_matches?' do
    it 'returns true when url has .com' do
      QA::Runtime::Scenario.define(:gitlab_address, "https://staging.gitlab.com")

      expect(described_class.dot_com?).to be_truthy
    end

    it 'returns false when url does not have .com' do
      QA::Runtime::Scenario.define(:gitlab_address, "https://gitlab.test")

      expect(described_class.dot_com?).to be_falsey
    end

    context 'with arguments' do
      it 'returns true when :subdomain is set' do
        QA::Runtime::Scenario.define(:gitlab_address, "https://staging.gitlab.com")

        expect(described_class.dot_com?(subdomain: :staging)).to be_truthy
      end

      it 'matches multiple subdomains' do
        QA::Runtime::Scenario.define(:gitlab_address, "https://staging.gitlab.com")

        aggregate_failures do
          expect(described_class.context_matches?(subdomain: [:release, :staging])).to be_truthy
          expect(described_class.context_matches?(:production, subdomain: [:release, :staging])).to be_truthy
        end
      end

      it 'matches :production' do
        QA::Runtime::Scenario.define(:gitlab_address, "https://gitlab.com/")

        expect(described_class.context_matches?(:production)).to be_truthy
      end

      it 'doesnt match with mismatching switches' do
        QA::Runtime::Scenario.define(:gitlab_address, 'https://gitlab.test')

        aggregate_failures do
          expect(described_class.context_matches?(tld: '.net')).to be_falsey
          expect(described_class.context_matches?(:production)).to be_falsey
          expect(described_class.context_matches?(subdomain: [:staging])).to be_falsey
          expect(described_class.context_matches?(domain: 'example')).to be_falsey
        end
      end
    end

    it 'returns false for mismatching' do
      QA::Runtime::Scenario.define(:gitlab_address, "https://staging.gitlab.com")

      expect(described_class.context_matches?(:production)).to be_falsey
    end
  end

  describe 'description and context blocks' do
    context 'with environment set' do
      it 'can apply to contexts or descriptions' do
        group = describe_successfully 'Runs in staging', only: { subdomain: :staging } do
          it('runs in staging') {}
        end

        expect(group.examples[0].execution_result.status).to eq(:passed)
      end

      context 'when excluding contexts' do
        it 'can apply to contexts or descriptions' do
          group = describe_successfully 'skips staging', except: { subdomain: :staging } do
            it('skips staging') {}
          end

          expect(group.examples[0].execution_result.status).to eq(:pending)
        end
      end
    end

    context 'with different environment set' do
      before do
        QA::Runtime::Scenario.define(:gitlab_address, 'https://gitlab.com')
      end

      it 'does not run against production' do
        group = describe_successfully 'Runs in staging', :something, only: { subdomain: :staging } do
          it('runs in staging') {}
        end

        expect(group.examples[0].execution_result.status).to eq(:pending)
      end

      context 'when excluding contexts' do
        it 'runs against production' do
          group = describe_successfully 'Runs in staging', :something, except: { subdomain: :staging } do
            it('runs in staging') {}
          end

          expect(group.examples[0].execution_result.status).to eq(:passed)
        end
      end
    end
  end

  it 'runs only in staging' do
    group = describe_successfully do
      it('runs in staging', only: { subdomain: :staging }) {}
      it('doesnt run in staging', only: :production) {}
      it('runs in staging also', only: { subdomain: %i[release staging] }) {}
      it('runs in any env') {}
    end

    aggregate_failures do
      expect(group.examples[0].execution_result.status).to eq(:passed)
      expect(group.examples[1].execution_result.status).to eq(:pending)
      expect(group.examples[2].execution_result.status).to eq(:passed)
      expect(group.examples[3].execution_result.status).to eq(:passed)
    end
  end

  context 'when excluding contexts' do
    it 'skips staging' do
      group = describe_successfully do
        it('skips staging', except: { subdomain: :staging }) {}
        it('runs in staging', except: :production) {}
        it('skips staging also', except: { subdomain: %i[release staging] }) {}
      end

      aggregate_failures do
        expect(group.examples[0].execution_result.status).to eq(:pending)
        expect(group.examples[1].execution_result.status).to eq(:passed)
        expect(group.examples[2].execution_result.status).to eq(:pending)
      end
    end
  end

  context 'custom env' do
    before do
      QA::Runtime::Scenario.define(:gitlab_address, 'https://release.gitlab.net')
    end

    it 'runs on a custom environment' do
      group = describe_successfully do
        it('runs on release gitlab net', only: { tld: '.net', subdomain: :release, domain: 'gitlab' }) {}
        it('does not run on release', only: :production) {}
      end

      aggregate_failures do
        expect(group.examples.first.execution_result.status).to eq(:passed)
        expect(group.examples.last.execution_result.status).to eq(:pending)
      end
    end

    context 'when excluding contexts' do
      it 'skips a custom environment' do
        group = describe_successfully do
          it('skips release gitlab net', except: { tld: '.net', subdomain: :release, domain: 'gitlab' }) {}
          it('runs on release', except: :production) {}
        end

        aggregate_failures do
          expect(group.examples.first.execution_result.status).to eq(:pending)
          expect(group.examples.last.execution_result.status).to eq(:passed)
        end
      end
    end
  end

  context 'staging-ref' do
    before do
      QA::Runtime::Scenario.define(:gitlab_address, 'https://staging-ref.gitlab.com/')
    end

    it 'runs on staging-ref' do
      group = describe_successfully do
        it('does not run in staging', only: { subdomain: :staging }) {}
        it('runs in staging-ref', only: { subdomain: /^staging-ref./ }) {}
      end

      aggregate_failures do
        expect(group.examples[0].execution_result.status).to eq(:pending)
        expect(group.examples[1].execution_result.status).to eq(:passed)
      end
    end
  end

  context 'production' do
    before do
      QA::Runtime::Scenario.define(:gitlab_address, 'https://gitlab.com/')
    end

    it 'runs on production' do
      group = describe_successfully do
        it('runs on prod', only: :production) {}
        it('does not run in prod', only: { subdomain: :staging }) {}
        it('runs in prod and staging', only: { subdomain: /(staging.)?/, domain: 'gitlab' }) {}
      end

      aggregate_failures do
        expect(group.examples[0].execution_result.status).to eq(:passed)
        expect(group.examples[1].execution_result.status).to eq(:pending)
        expect(group.examples[2].execution_result.status).to eq(:passed)
      end
    end

    context 'when excluding contexts' do
      it 'skips production' do
        group = describe_successfully do
          it('skips prod', except: :production) {}
          it('runs on prod', except: { subdomain: :staging }) {}
          it('skips prod and staging', except: { subdomain: /(staging.)?/, domain: 'gitlab' }) {}
        end

        aggregate_failures do
          expect(group.examples[0].execution_result.status).to eq(:pending)
          expect(group.examples[1].execution_result.status).to eq(:passed)
          expect(group.examples[2].execution_result.status).to eq(:pending)
        end
      end
    end
  end

  it 'outputs a message for invalid environments' do
    group = describe_successfully do
      it('will skip', only: :production) {}
    end

    expect(group.examples.first.execution_result.pending_message).to match(/[Tt]est.*not compatible.*environment/)
  end

  context 'with pipeline constraints' do
    context 'without CI_PROJECT_NAME set' do
      before do
        stub_env('CI_PROJECT_NAME', nil)
      end

      it 'runs on any pipeline' do
        group = describe_successfully do
          it('runs given a single named pipeline', only: { pipeline: :nightly }) {}
          it('runs given an array of pipelines', only: { pipeline: [:canary, :not_nightly] }) {}
        end

        aggregate_failures do
          expect(group.examples[0].execution_result.status).to eq(:passed)
          expect(group.examples[1].execution_result.status).to eq(:passed)
        end
      end

      context 'when excluding contexts' do
        it 'runs in any pipeline' do
          group = describe_successfully do
            it('runs given a single named pipeline', except: { pipeline: :nightly }) {}
            it('runs given an array of pipelines', except: { pipeline: [:canary, :not_nightly] }) {}
          end

          aggregate_failures do
            group.examples.each do |example|
              expect(example.execution_result.status).to eq(:passed)
            end
          end
        end
      end
    end

    context 'when a pipeline triggered from the default branch runs in gitlab-qa' do
      before do
        stub_env('CI_PROJECT_NAME', 'gitlab-qa')
      end

      it 'runs on default branch pipelines' do
        group = describe_successfully do
          it('runs on main pipeline given a single pipeline', only: { pipeline: :main }) {}
          it('runs in main given an array of pipelines', only: { pipeline: [:canary, :main] }) {}
          it('does not run in non-default pipelines', only: { pipeline: [:nightly, :not_nightly, :not_main] }) {}
        end

        aggregate_failures do
          expect(group.examples[0].execution_result.status).to eq(:passed)
          expect(group.examples[1].execution_result.status).to eq(:passed)
          expect(group.examples[2].execution_result.status).to eq(:pending)
        end
      end

      context 'when excluding contexts' do
        it 'skips default branch pipelines' do
          group = describe_successfully do
            it('skips main pipeline given a single pipeline', except: { pipeline: :main }) {}
            it('skips main given an array of pipelines', except: { pipeline: [:canary, :main] }) {}
            it('runs non-default pipelines', except: { pipeline: [:nightly, :not_nightly, :not_main] }) {}
          end

          aggregate_failures do
            expect(group.examples[0].execution_result.status).to eq(:pending)
            expect(group.examples[1].execution_result.status).to eq(:pending)
            expect(group.examples[2].execution_result.status).to eq(:passed)
          end
        end
      end
    end

    context 'with CI_PROJECT_NAME set' do
      before do
        stub_env('CI_PROJECT_NAME', 'NIGHTLY')
      end

      it 'runs on designated pipeline' do
        group = describe_successfully do
          it('runs on nightly', only: { pipeline: :nightly }) {}
          it('does not run in not_nightly', only: { pipeline: :not_nightly }) {}
          it('runs on nightly given an array', only: { pipeline: [:canary, :nightly] }) {}
          it('does not run in not_nightly given an array', only: { pipeline: [:not_nightly, :canary] }) {}
        end

        aggregate_failures do
          expect(group.examples[0].execution_result.status).to eq(:passed)
          expect(group.examples[1].execution_result.status).to eq(:pending)
          expect(group.examples[2].execution_result.status).to eq(:passed)
          expect(group.examples[3].execution_result.status).to eq(:pending)
        end
      end

      context 'when excluding contexts' do
        it 'skips designated pipeline' do
          group = describe_successfully do
            it('skips nightly', except: { pipeline: :nightly }) {}
            it('runs in not_nightly', except: { pipeline: :not_nightly }) {}
            it('skips on nightly given an array', except: { pipeline: [:canary, :nightly] }) {}
            it('runs in not_nightly given an array', except: { pipeline: [:not_nightly, :canary] }) {}
          end

          aggregate_failures do
            expect(group.examples[0].execution_result.status).to eq(:pending)
            expect(group.examples[1].execution_result.status).to eq(:passed)
            expect(group.examples[2].execution_result.status).to eq(:pending)
            expect(group.examples[3].execution_result.status).to eq(:passed)
          end
        end
      end
    end
  end

  context 'with job constraints' do
    context 'without CI_JOB_NAME set' do
      before do
        stub_env('CI_JOB_NAME', nil)
      end

      context 'when excluding contexts' do
        it 'runs in any job' do
          group = describe_successfully do
            it('runs given a single named job', except: { job: 'ee:instance-image' }) {}
            it('runs given a single regex pattern', except: { job: '.*:instance-image' }) {}
            it('runs given an array of jobs', except: { job: %w[ee:instance-image qa-schedules-browser_ui-3_create] }) {}
            it('runs given an array of regex patterns', except: { job: %w[ee:.* qa-schedules-browser_ui.*] }) {}
            it('runs given a mix of strings and regex patterns', except: { job: %w[ee:instance-image qa-schedules-browser_ui.*] }) {}
          end

          aggregate_failures do
            group.examples.each do |example|
              expect(example.execution_result.status).to eq(:passed)
            end
          end
        end
      end

      context 'when including only specific contexts' do
        it 'runs in any job' do
          group = describe_successfully do
            it('runs given a single named job', only: { job: 'ee:instance-image' }) {}
            it('runs given a single regex pattern', only: { job: '.*:instance-image' }) {}
            it('runs given an array of jobs', only: { job: %w[ee:instance-image qa-schedules-browser_ui-3_create] }) {}
            it('runs given an array of regex patterns', only: { job: %w[ee:.* qa-schedules-browser_ui.*] }) {}
            it('runs given a mix of strings and regex patterns', only: { job: %w[ee:instance-image qa-schedules-browser_ui.*] }) {}
          end

          aggregate_failures do
            group.examples.each do |example|
              expect(example.execution_result.status).to eq(:passed)
            end
          end
        end
      end
    end

    context 'with CI_JOB_NAME set' do
      before do
        stub_env('CI_JOB_NAME', 'ee:instance-image')
      end

      context 'when excluding contexts' do
        it 'does not run in the specified job' do
          group = describe_successfully do
            it('skips given a single named job', except: { job: 'ee:instance-image' }) {}
            it('skips given a single regex pattern', except: { job: '.*:instance-image' }) {}
            it('skips given an array of jobs', except: { job: %w[ee:instance-image qa-schedules-browser_ui-3_create] }) {}
            it('skips given an array of regex patterns', except: { job: %w[ee:.* qa-schedules-browser_ui.*] }) {}
            it('skips given a mix of strings and regex patterns', except: { job: %w[ee:instance-image qa-schedules-browser_ui.*] }) {}
          end

          aggregate_failures do
            group.examples.each do |example|
              expect(example.execution_result.status).to eq(:pending)
            end
          end
        end

        it 'runs in jobs that do not match' do
          group = describe_successfully do
            it('runs given a single named job', except: { job: 'ce:instance-image' }) {}
            it('runs given a single regex pattern', except: { job: '.*:instance-image-quarantine' }) {}
            it('runs given an array of jobs', except: { job: %w[ce:instance-image qa-schedules-browser_ui-3_create] }) {}
            it('runs given an array of regex patterns', except: { job: %w[ce:.* qa-schedules-browser_ui.*] }) {}
            it('runs given a mix of strings and regex patterns', except: { job: %w[ce:instance-image qa-schedules-browser_ui.*] }) {}
          end

          aggregate_failures do
            group.examples.each do |example|
              expect(example.execution_result.status).to eq(:passed)
            end
          end
        end
      end

      context 'when including only specific contexts' do
        it 'runs only in the specified jobs' do
          group = describe_successfully do
            it('runs given a single named job', only: { job: 'ee:instance-image' }) {}
            it('runs given a single regex pattern', only: { job: '.*:instance-image' }) {}
            it('runs given an array of jobs', only: { job: %w[ee:instance-image qa-schedules-browser_ui-3_create] }) {}
            it('runs given an array of regex patterns', only: { job: %w[ee:.* qa-schedules-browser_ui.*] }) {}
            it('runs given a mix of strings and regex patterns', only: { job: %w[ee:instance-image qa-schedules-browser_ui.*] }) {}
          end

          aggregate_failures do
            group.examples.each do |example|
              expect(example.execution_result.status).to eq(:passed)
            end
          end
        end

        it 'does not run in jobs that do not match' do
          group = describe_successfully do
            it('skips given a single named job', only: { job: 'ce:instance-image' }) {}
            it('skips given a single regex pattern', only: { job: '.*:instance-image-quarantine' }) {}
            it('skips given an array of jobs', only: { job: %w[ce:instance-image qa-schedules-browser_ui-3_create] }) {}
            it('skips given an array of regex patterns', only: { job: %w[ce:.* qa-schedules-browser_ui.*] }) {}
            it('skips given a mix of strings and regex patterns', only: { job: %w[ce:instance-image qa-schedules-browser_ui.*] }) {}
          end

          aggregate_failures do
            group.examples.each do |example|
              expect(example.execution_result.status).to eq(:pending)
            end
          end
        end
      end
    end
  end
end