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

variables_spec.rb « entry « config « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 085f304094e4f1553b84ca43a458a4626768056e (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Config::Entry::Variables do
  let(:config) { {} }
  let(:metadata) { {} }

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

  before do
    entry.compose!
  end

  shared_examples 'valid config' do
    describe '#value' do
      it 'returns hash with key value strings' do
        expect(entry.value).to eq result
      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

  shared_examples 'invalid config' do |error_message|
    describe '#valid?' do
      it 'is not valid' do
        expect(entry).not_to be_valid
      end
    end

    describe '#errors' do
      it 'saves errors' do
        expect(entry.errors)
          .to include(error_message)
      end
    end
  end

  context 'when entry config value has key-value pairs' do
    let(:config) do
      { 'VARIABLE_1' => 'value 1', 'VARIABLE_2' => 'value 2' }
    end

    let(:result) do
      { 'VARIABLE_1' => 'value 1', 'VARIABLE_2' => 'value 2' }
    end

    it_behaves_like 'valid config'

    describe '#value_with_data' do
      it 'returns variable with data' do
        expect(entry.value_with_data).to eq(
          'VARIABLE_1' => { value: 'value 1' },
          'VARIABLE_2' => { value: 'value 2' }
        )
      end
    end
  end

  context 'with numeric keys and values in the config' do
    let(:config) { { 10 => 20 } }
    let(:result) do
      { '10' => '20' }
    end

    it_behaves_like 'valid config'
  end

  context 'when key is an array' do
    let(:config) { { ['VAR1'] => 'val1' } }

    it_behaves_like 'invalid config', /must be an alphanumeric string/
  end

  context 'when value is a symbol' do
    let(:config) { { 'VAR1' => :val1 } }
    let(:result) do
      { 'VAR1' => 'val1' }
    end

    it_behaves_like 'valid config'
  end

  context 'when value is a boolean' do
    let(:config) { { 'VAR1' => true } }

    it_behaves_like 'invalid config', /must be either a string or a hash/
  end

  context 'when entry config value has unallowed value key-value pair and value is a string' do
    let(:config) do
      { 'VARIABLE_1' => { value: 'value', description: 'variable 1' } }
    end

    context 'when there is no allowed_value_data metadata' do
      it_behaves_like 'invalid config', /variable_1 config must be a string/
    end

    context 'when metadata has allow_array_value and allowed_value_data' do
      let(:metadata) { { allowed_value_data: %i[value description], allow_array_value: true } }

      let(:result) do
        { 'VARIABLE_1' => 'value' }
      end

      it_behaves_like 'valid config'

      describe '#value_with_data' do
        it 'returns variable with data' do
          expect(entry.value_with_data).to eq(
            'VARIABLE_1' => { value: 'value', description: 'variable 1' }
          )
        end
      end
    end
  end

  context 'when entry config value has key-value pair and value is an array' do
    let(:config) do
      { 'VARIABLE_1' => { value: %w[value1 value2], description: 'variable 1' } }
    end

    context 'when there is no allowed_value_data metadata' do
      it_behaves_like 'invalid config', /variable_1 config value must be an alphanumeric string/
    end

    context 'when metadata has allow_array_value and allowed_value_data' do
      let(:metadata) { { allowed_value_data: %i[value description], allow_array_value: true } }

      let(:result) do
        { 'VARIABLE_1' => 'value1' }
      end

      it_behaves_like 'valid config'

      describe '#value_with_data' do
        it 'returns variable with data' do
          expect(entry.value_with_data).to eq(
            'VARIABLE_1' => { value: 'value1', value_options: %w[value1 value2], description: 'variable 1' }
          )
        end
      end
    end
  end

  context 'when entry config value has key-value pair and hash' do
    let(:config) do
      { 'VARIABLE_1' => { value: 'value 1', description: 'variable 1' },
        'VARIABLE_2' => 'value 2' }
    end

    it_behaves_like 'invalid config', /variable_1 config must be a string/

    context 'when metadata has allowed_value_data' do
      let(:metadata) { { allowed_value_data: %i[value description] } }

      let(:result) do
        { 'VARIABLE_1' => 'value 1', 'VARIABLE_2' => 'value 2' }
      end

      it_behaves_like 'valid config'

      describe '#value_with_data' do
        it 'returns variable with data' do
          expect(entry.value_with_data).to eq(
            'VARIABLE_1' => { value: 'value 1', description: 'variable 1' },
            'VARIABLE_2' => { value: 'value 2' }
          )
        end
      end
    end
  end

  context 'when entry value is an array' do
    let(:config) { [:VAR, 'test'] }

    it_behaves_like 'invalid config', /variables config should be a hash/
  end

  context 'when metadata has allowed_value_data' do
    let(:metadata) { { allowed_value_data: %i[value description] } }

    context 'when entry value has hash with other key-pairs' do
      let(:config) do
        { 'VARIABLE_1' => { value: 'value 1', hello: 'variable 1' },
          'VARIABLE_2' => 'value 2' }
      end

      it_behaves_like 'invalid config', /variable_1 config uses invalid data keys: hello/
    end

    context 'when entry config value has hash with nil description' do
      let(:config) do
        { 'VARIABLE_1' => { value: 'value 1', description: nil } }
      end

      it_behaves_like 'invalid config', /variable_1 config description must be an alphanumeric string/
    end

    context 'when entry config value has hash without description' do
      let(:config) do
        { 'VARIABLE_1' => { value: 'value 1' } }
      end

      let(:result) do
        { 'VARIABLE_1' => 'value 1' }
      end

      it_behaves_like 'valid config'
    end
  end
end