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

# After Feature one_dimensional_matrix is removed, this can be changed back to fast_spec_helper
require 'spec_helper'
require_dependency 'active_model'

RSpec.describe Gitlab::Ci::Config::Entry::Product::Variables do
  let(:entry) { described_class.new(config) }

  describe 'validations' do
    context 'when entry config value is correct' do
      let(:config) do
        {
          'VARIABLE_1' => 1,
          'VARIABLE_2' => 'value 2',
          'VARIABLE_3' => :value_3,
          :VARIABLE_4 => 'value 4',
          5 => ['value 5'],
          'VARIABLE_6' => ['value 6']
        }
      end

      describe '#value' do
        it 'returns hash with key value strings' do
          expect(entry.value).to match({
            'VARIABLE_1' => ['1'],
            'VARIABLE_2' => ['value 2'],
            'VARIABLE_3' => ['value_3'],
            'VARIABLE_4' => ['value 4'],
            '5' => ['value 5'],
            'VARIABLE_6' => ['value 6']
          })
        end
      end

      describe '#errors' do
        it 'does not append errors' do
          expect(entry.errors).to be_empty
        end
      end

      describe '#valid?' do
        it 'is valid' do
          expect(entry).to be_valid
        end
      end
    end

    context 'with one_dimensional_matrix feature flag enabled' do
      context 'with only one variable' do
        before do
          stub_feature_flags(one_dimensional_matrix: true)
        end
        let(:config) { { VAR: 'test' } }

        describe '#valid?' do
          it 'is valid' do
            expect(entry).to be_valid
          end
        end

        describe '#errors' do
          it 'does not append errors' do
            expect(entry.errors).to be_empty
          end
        end
      end
    end

    context 'with one_dimensional_matrix feature flag disabled' do
      context 'when entry value is not correct' do
        before do
          stub_feature_flags(one_dimensional_matrix: false)
        end
        shared_examples 'invalid variables' do |message|
          describe '#errors' do
            it 'saves errors' do
              expect(entry.errors).to include(message)
            end
          end

          describe '#valid?' do
            it 'is not valid' do
              expect(entry).not_to be_valid
            end
          end
        end

        context 'with array' do
          let(:config) { [:VAR, 'test'] }

          it_behaves_like 'invalid variables', /should be a hash of key value pairs/
        end

        context 'with empty array' do
          let(:config) { { VAR: 'test', VAR2: [] } }

          it_behaves_like 'invalid variables', /should be a hash of key value pairs/
        end

        context 'with nested array' do
          let(:config) { { VAR: 'test', VAR2: [1, [2]] } }

          it_behaves_like 'invalid variables', /should be a hash of key value pairs/
        end

        context 'with one_dimensional_matrix feature flag disabled' do
          context 'with only one variable' do
            let(:config) { { VAR: 'test' } }

            it_behaves_like 'invalid variables', /variables config requires at least 2 items/
          end
        end
      end
    end
  end
end