default_spec.rb 3.15 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
6
7
require 'spec_helper'

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

8
9
10
11
12
13
14
15
  it_behaves_like 'with inheritable CI config' do
    let(:inheritable_key) { nil }
    let(:inheritable_class) { Gitlab::Ci::Config::Entry::Root }

    # These are entries defined in Root
    # that we know that we don't want to inherit
    # as they do not have sense in context of Default
    let(:ignored_inheritable_columns) do
drew's avatar
drew committed
16
      %i[default include variables stages types workflow]
17
18
19
    end
  end

20
21
22
23
24
25
26
27
28
  describe '.nodes' do
    it 'returns a hash' do
      expect(described_class.nodes).to be_a(Hash)
    end

    context 'when filtering all the entry/node names' do
      it 'contains the expected node names' do
        expect(described_class.nodes.keys)
          .to match_array(%i[before_script image services
29
                             after_script cache interruptible
30
                             timeout retry tags])
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
      end
    end
  end

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

    context 'when default entry value is correct' do
      let(:config) { { before_script: ['rspec'] } }

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

    context 'when default entry is empty' do
      let(:config) { {} }

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

    context 'when default entry is not correct' do
      context 'incorrect config value type' do
        let(:config) { ['incorrect'] }

        describe '#errors' do
          it 'reports error about a config type' do
            expect(entry.errors)
              .to include 'default config should be a hash'
          end
        end
      end

      context 'when unknown keys detected' do
        let(:config) { { unknown: true } }

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

  describe '#compose!' do
    let(:specified) do
      double('specified', 'specified?' => true, value: 'specified')
    end

    let(:unspecified) { double('unspecified', 'specified?' => false) }
    let(:deps) { double('deps', '[]' => unspecified) }

    context 'when default entry inherits configuration from root' do
      let(:config) do
        { image: 'some_image' }
      end

      before do
        allow(deps).to receive('[]').with(:image).and_return(specified)
      end

      it 'raises error' do
        expect { entry.compose!(deps) }.to raise_error(
103
          Gitlab::Ci::Config::Entry::Default::InheritError)
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
      end
    end

    context 'when default entry inherits a non-defined configuration from root' do
      let(:config) do
        { image: 'some_image' }
      end

      before do
        allow(deps).to receive('[]').with(:after_script).and_return(specified)

        entry.compose!(deps)
      end

      it 'inherits non-defined configuration entries' do
        expect(entry[:image].value).to eq(name: 'some_image')
        expect(entry[:after_script].value).to eq('specified')
      end
    end
  end
end