summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config/entry/product/parallel_spec.rb
blob: a16f1cf9e4375e48daef65531fac2b15fc46f6f0 (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
# frozen_string_literal: true

require 'fast_spec_helper'
require_dependency 'active_model'

RSpec.describe ::Gitlab::Ci::Config::Entry::Product::Parallel do
  let(:metadata) { {} }

  subject(:parallel) { described_class.new(config, **metadata) }

  shared_examples 'invalid config' do |error_message|
    describe '#valid?' do
      it { is_expected.not_to be_valid }
    end

    describe '#errors' do
      it 'returns error about invalid type' do
        expect(parallel.errors).to match(a_collection_including(error_message))
      end
    end
  end

  context 'with invalid config' do
    context 'when it is not a numeric value' do
      let(:config) { true }

      it_behaves_like 'invalid config', /should be an integer or a hash/
    end

    context 'when it is lower than two' do
      let(:config) { 1 }

      it_behaves_like 'invalid config', /must be greater than or equal to 2/
    end

    context 'when it is bigger than 50' do
      let(:config) { 51 }

      it_behaves_like 'invalid config', /must be less than or equal to 50/
    end

    context 'when it is not an integer' do
      let(:config) { 1.5 }

      it_behaves_like 'invalid config', /must be an integer/
    end

    context 'with empty hash config' do
      let(:config) { {} }

      it_behaves_like 'invalid config', /matrix builds config missing required keys: matrix/
    end
  end

  context 'with numeric config' do
    context 'when job is specified' do
      let(:config) { 2 }

      describe '#valid?' do
        it { is_expected.to be_valid }
      end

      describe '#value' do
        it 'returns job needs configuration' do
          expect(parallel.value).to match(number: config)
        end
      end

      context 'when :numeric is not allowed' do
        let(:metadata) { { allowed_strategies: [:matrix] } }

        it_behaves_like 'invalid config', /cannot use "parallel: <number>"/
      end
    end
  end

  context 'with matrix builds config' do
    context 'when matrix is specified' do
      let(:config) do
        {
          matrix: [
            { PROVIDER: 'aws', STACK: %w[monitoring app1 app2] },
            { PROVIDER: 'gcp', STACK: %w[data processing] }
          ]
        }
      end

      describe '#valid?' do
        it { is_expected.to be_valid }
      end

      describe '#value' do
        it 'returns job needs configuration' do
          expect(parallel.value).to match(matrix:
            [
              { PROVIDER: 'aws', STACK: %w[monitoring app1 app2] },
              { PROVIDER: 'gcp', STACK: %w[data processing] }
            ])
        end
      end

      context 'when :matrix is not allowed' do
        let(:metadata) { { allowed_strategies: [:numeric] } }

        it_behaves_like 'invalid config', /cannot use "parallel: matrix"/
      end
    end
  end
end