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

require 'fast_spec_helper'
require_dependency 'active_model'

RSpec.describe ::Gitlab::Ci::Config::Entry::Product::Matrix do
  subject(:matrix) { described_class.new(config) }

  describe 'validations' do
    before do
      matrix.compose!
    end

    context 'when entry config value is correct' do
      let(:config) do
        [
          { 'VAR_1' => [1, 2, 3], 'VAR_2' => [4, 5, 6] },
          { 'VAR_3' => %w[a b], 'VAR_4' => %w[c d] }
        ]
      end

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

    context 'when entry config generates too many jobs' do
      let(:config) do
        [
          {
            'VAR_1' => (1..10).to_a,
            'VAR_2' => (11..20).to_a
          }
        ]
      end

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

      describe '#errors' do
        it 'returns error about too many jobs' do
          expect(matrix.errors)
            .to include('matrix config generates too many jobs (maximum is 50)')
        end
      end
    end

    context 'when entry config has only one variable with multiple values' do
      let(:config) do
        [
          {
            'VAR_1' => %w[build test]
          }
        ]
      end

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

      describe '#errors' do
        it 'returns no errors' do
          expect(matrix.errors)
            .to be_empty
        end
      end

      describe '#value' do
        before do
          matrix.compose!
        end

        it 'returns the value without raising an error' do
          expect(matrix.value).to eq([{ 'VAR_1' => %w[build test] }])
        end
      end

      context 'when entry config has only one variable with one value' do
        let(:config) do
          [
            {
              'VAR_1' => %w[test]
            }
          ]
        end

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

        describe '#errors' do
          it 'returns no errors' do
            expect(matrix.errors)
              .to be_empty
          end
        end

        describe '#value' do
          before do
            matrix.compose!
          end

          it 'returns the value without raising an error' do
            expect(matrix.value).to eq([{ 'VAR_1' => %w[test] }])
          end
        end
      end
    end

    context 'when config value has wrong type' do
      let(:config) { {} }

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

      describe '#errors' do
        it 'returns error about incorrect type' do
          expect(matrix.errors)
            .to include('matrix config should be an array of hashes')
        end
      end
    end
  end

  describe '.compose!' do
    context 'when valid job entries composed' do
      let(:config) do
        [
          { PROVIDER: 'aws', STACK: %w[monitoring app1 app2] },
          { STACK: %w[monitoring backup app], PROVIDER: 'ovh' },
          { PROVIDER: 'gcp', STACK: %w[data processing], ARGS: 'normal' },
          { PROVIDER: 'vultr', STACK: 'data', ARGS: 'store' }
        ]
      end

      before do
        matrix.compose!
      end

      describe '#value' do
        it 'returns key value' do
          expect(matrix.value).to match(
            [
              { 'PROVIDER' => %w[aws], 'STACK' => %w[monitoring app1 app2] },
              { 'PROVIDER' => %w[ovh], 'STACK' => %w[monitoring backup app] },
              { 'ARGS' => %w[normal], 'PROVIDER' => %w[gcp], 'STACK' => %w[data processing] },
              { 'ARGS' => %w[store], 'PROVIDER' => %w[vultr], 'STACK' => %w[data] }
            ]
          )
        end
      end

      describe '#descendants' do
        it 'creates valid descendant nodes' do
          expect(matrix.descendants.count).to eq(config.size)
          expect(matrix.descendants)
            .to all(be_an_instance_of(::Gitlab::Ci::Config::Entry::Product::Variables))
        end
      end
    end

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

      before do
        matrix.compose!
      end

      describe '#value' do
        it 'returns empty value' do
          expect(matrix.value).to eq([])
        end
      end
    end
  end

  describe '#number_of_generated_jobs' do
    before do
      matrix.compose!
    end

    subject { matrix.number_of_generated_jobs }

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

      it { is_expected.to be_zero }
    end

    context 'with only one variable' do
      let(:config) do
        [{ 'VAR_1' => (1..10).to_a }]
      end

      it { is_expected.to eq(10) }
    end

    context 'with two variables' do
      let(:config) do
        [{ 'VAR_1' => (1..10).to_a, 'VAR_2' => (1..5).to_a }]
      end

      it { is_expected.to eq(50) }
    end

    context 'with two sets of variables' do
      let(:config) do
        [
          { 'VAR_1' => (1..10).to_a, 'VAR_2' => (1..5).to_a },
          { 'VAR_3' => (1..2).to_a, 'VAR_4' => (1..3).to_a }
        ]
      end

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