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

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

require 'spec_helper'

RSpec.describe Gitlab::Ci::Variables::Downstream::Generator, feature_category: :secrets_management do
  let(:bridge_variables) do
    Gitlab::Ci::Variables::Collection.fabricate(
      [
        { key: 'REF1', value: 'ref 1' },
        { key: 'REF2', value: 'ref 2' }
      ]
    )
  end

  let(:yaml_variables) do
    [
      { key: 'VAR1', value: 'variable 1' },
      { key: 'VAR2', value: 'variable 2' },
      { key: 'RAW_VAR3', value: '$REF1', raw: true },
      { key: 'INTERPOLATION_VAR4', value: 'interpolate $REF1 $REF2' }
    ]
  end

  let(:pipeline_variables) do
    [
      { key: 'PIPELINE_VAR1', value: 'variable 1' },
      { key: 'PIPELINE_VAR2', value: 'variable 2' },
      { key: 'PIPELINE_RAW_VAR3', value: '$REF1', raw: true },
      { key: 'PIPELINE_INTERPOLATION_VAR4', value: 'interpolate $REF1 $REF2' }
    ]
  end

  let(:pipeline_schedule_variables) do
    [
      { key: 'PIPELINE_SCHEDULE_VAR1', value: 'variable 1' },
      { key: 'PIPELINE_SCHEDULE_VAR2', value: 'variable 2' },
      { key: 'PIPELINE_SCHEDULE_RAW_VAR3', value: '$REF1', raw: true },
      { key: 'PIPELINE_SCHEDULE_INTERPOLATION_VAR4', value: 'interpolate $REF1 $REF2' }
    ]
  end

  let(:bridge) do
    instance_double(
      'Ci::Bridge',
      variables: bridge_variables,
      forward_yaml_variables?: true,
      forward_pipeline_variables?: true,
      expand_file_refs?: false,
      yaml_variables: yaml_variables,
      pipeline_variables: pipeline_variables,
      pipeline_schedule_variables: pipeline_schedule_variables
    )
  end

  subject(:generator) { described_class.new(bridge) }

  describe '#calculate' do
    it 'creates attributes for downstream pipeline variables from the ' \
       'given yaml variables, pipeline variables and pipeline schedule variables' do
      expected = [
        { key: 'VAR1', value: 'variable 1' },
        { key: 'VAR2', value: 'variable 2' },
        { key: 'RAW_VAR3', value: '$REF1', raw: true },
        { key: 'INTERPOLATION_VAR4', value: 'interpolate ref 1 ref 2' },
        { key: 'PIPELINE_VAR1', value: 'variable 1' },
        { key: 'PIPELINE_VAR2', value: 'variable 2' },
        { key: 'PIPELINE_RAW_VAR3', value: '$REF1', raw: true },
        { key: 'PIPELINE_INTERPOLATION_VAR4', value: 'interpolate ref 1 ref 2' },
        { key: 'PIPELINE_SCHEDULE_VAR1', value: 'variable 1' },
        { key: 'PIPELINE_SCHEDULE_VAR2', value: 'variable 2' },
        { key: 'PIPELINE_SCHEDULE_RAW_VAR3', value: '$REF1', raw: true },
        { key: 'PIPELINE_SCHEDULE_INTERPOLATION_VAR4', value: 'interpolate ref 1 ref 2' }
      ]

      expect(generator.calculate).to contain_exactly(*expected)
    end

    it 'returns empty array when bridge has no variables' do
      allow(bridge).to receive(:yaml_variables).and_return([])
      allow(bridge).to receive(:pipeline_variables).and_return([])
      allow(bridge).to receive(:pipeline_schedule_variables).and_return([])

      expect(generator.calculate).to be_empty
    end

    context 'with file variable interpolation' do
      let(:bridge_variables) do
        Gitlab::Ci::Variables::Collection.fabricate(
          [
            { key: 'REF1', value: 'ref 1' },
            { key: 'FILE_REF3', value: 'ref 3', file: true }
          ]
        )
      end

      let(:yaml_variables) do
        [{ key: 'INTERPOLATION_VAR', value: 'interpolate $REF1 $REF2 $FILE_REF3 $FILE_REF4' }]
      end

      let(:pipeline_variables) do
        [{ key: 'PIPELINE_INTERPOLATION_VAR', value: 'interpolate $REF1 $REF2 $FILE_REF3 $FILE_REF4' }]
      end

      let(:pipeline_schedule_variables) do
        [{ key: 'PIPELINE_SCHEDULE_INTERPOLATION_VAR', value: 'interpolate $REF1 $REF2 $FILE_REF3 $FILE_REF4' }]
      end

      context 'when expand_file_refs is true' do
        before do
          allow(bridge).to receive(:expand_file_refs?).and_return(true)
        end

        it 'expands file variables' do
          expected = [
            { key: 'INTERPOLATION_VAR', value: 'interpolate ref 1  ref 3 ' },
            { key: 'PIPELINE_INTERPOLATION_VAR', value: 'interpolate ref 1  ref 3 ' },
            { key: 'PIPELINE_SCHEDULE_INTERPOLATION_VAR', value: 'interpolate ref 1  ref 3 ' }
          ]

          expect(generator.calculate).to contain_exactly(*expected)
        end
      end

      context 'when expand_file_refs is false' do
        before do
          allow(bridge).to receive(:expand_file_refs?).and_return(false)
        end

        it 'does not expand file variables and adds file variables' do
          expected = [
            { key: 'INTERPOLATION_VAR', value: 'interpolate ref 1  $FILE_REF3 ' },
            { key: 'PIPELINE_INTERPOLATION_VAR', value: 'interpolate ref 1  $FILE_REF3 ' },
            { key: 'PIPELINE_SCHEDULE_INTERPOLATION_VAR', value: 'interpolate ref 1  $FILE_REF3 ' },
            { key: 'FILE_REF3', value: 'ref 3', variable_type: :file }
          ]

          expect(generator.calculate).to contain_exactly(*expected)
        end
      end
    end
  end
end