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

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
require 'spec_helper'

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

  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
                             after_script cache])
      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(
          Gitlab::Ci::Config::Entry::Default::DuplicateError)
      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