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

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

require 'spec_helper'

RSpec.describe Gitlab::Ci::Config::Interpolation::TextTemplate, feature_category: :pipeline_composition do
  subject(:template) { described_class.new(config, ctx) }

  let(:config) do
    <<~CFG
    test:
      spec:
        env: $[[ inputs.env ]]

    $[[ inputs.key ]]:
      name: $[[ inputs.key ]]
      script: my-value
      parallel: $[[ inputs.parallel ]]
    CFG
  end

  let(:ctx) do
    { inputs: { env: 'dev', key: 'abc', parallel: 6 } }
  end

  it 'interpolates the values properly' do
    expect(template.interpolated).to eq <<~RESULT
    test:
      spec:
        env: dev

    abc:
      name: abc
      script: my-value
      parallel: 6
    RESULT
  end

  context 'when the config has an unknown interpolation key' do
    let(:config) { '$[[ xxx.yyy ]]: abc' }

    it 'does not interpolate the config' do
      expect(template).not_to be_valid
      expect(template.interpolated).to be_nil
      expect(template.errors).to contain_exactly('unknown interpolation key: `xxx`')
    end
  end

  context 'when template consists of nested arrays with hashes and values' do
    let(:config) do
      <<~CFG
      test:
        - a-$[[ inputs.key ]]-b
        - c-$[[ inputs.key ]]-d:
            d-$[[ inputs.key ]]-e
          val: 1
      CFG
    end

    it 'performs a valid interpolation' do
      result = <<~RESULT
      test:
        - a-abc-b
        - c-abc-d:
            d-abc-e
          val: 1
      RESULT

      expect(template).to be_valid
      expect(template.interpolated).to eq result
    end
  end

  context 'when template contains symbols that need interpolation' do
    subject(:template) do
      described_class.new("'$[[ inputs.key ]]': 'cde'", ctx)
    end

    it 'performs a valid interpolation' do
      expect(template).to be_valid
      expect(template.interpolated).to eq("'abc': 'cde'")
    end
  end

  context 'when template is too large' do
    before do
      stub_application_setting(ci_max_total_yaml_size_bytes: 1)
    end

    it 'returns an error' do
      expect(template.interpolated).to be_nil
      expect(template.errors).to contain_exactly('config too large')
    end
  end

  context 'when there are too many interpolation blocks' do
    before do
      stub_const("#{described_class}::MAX_BLOCKS", 1)
    end

    it 'returns an error' do
      expect(template.interpolated).to be_nil
      expect(template.errors).to contain_exactly('too many interpolation blocks')
    end
  end
end