summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config/entry/jobs_spec.rb
blob: 61c8956d41f9b594574ae31ad14ed5a3bd083950 (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
# frozen_string_literal: true

require 'spec_helper'

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

  describe '.all_types' do
    subject { described_class.all_types }

    it { is_expected.to include(::Gitlab::Ci::Config::Entry::Hidden) }
    it { is_expected.to include(::Gitlab::Ci::Config::Entry::Job) }
  end

  describe '.find_type' do
    using RSpec::Parameterized::TableSyntax

    let(:config) do
      {
        '.hidden_job'.to_sym => { script: 'something' },
        regular_job: { script: 'something' },
        invalid_job: 'text'
      }
    end

    where(:name, :type) do
      :'.hidden_job'    | ::Gitlab::Ci::Config::Entry::Hidden
      :regular_job      | ::Gitlab::Ci::Config::Entry::Job
      :invalid_job      | nil
    end

    subject { described_class.find_type(name, config[name]) }

    with_them do
      it { is_expected.to eq(type) }
    end
  end

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

    context 'when entry config value is correct' do
      let(:config) { { rspec: { script: 'rspec' } } }

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

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

          it 'returns error about incorrect type' do
            expect(entry.errors)
              .to include 'jobs config should be a hash'
          end
        end

        context 'when job is invalid' do
          let(:config) { { rspec: nil } }

          it 'reports error' do
            expect(entry.errors).to include "jobs config should contain valid jobs"
          end
        end

        context 'when no visible jobs present' do
          let(:config) { { '.hidden'.to_sym => { script: [] } } }

          it 'returns error about no visible jobs defined' do
            expect(entry.errors)
              .to include 'jobs config should contain at least one visible job'
          end
        end
      end
    end
  end

  describe '.compose!' do
    context 'when valid job entries composed' do
      before do
        entry.compose!
      end

      let(:config) do
        { rspec: { script: 'rspec' },
          spinach: { script: 'spinach' },
          '.hidden'.to_sym => {} }
      end

      describe '#value' do
        it 'returns key value' do
          expect(entry.value).to eq(
            rspec: { name: :rspec,
                    script: %w[rspec],
                    ignore: false,
                    stage: 'test',
                    only: { refs: %w[branches tags] },
                    variables: {} },
            spinach: { name: :spinach,
                      script: %w[spinach],
                      ignore: false,
                      stage: 'test',
                      only: { refs: %w[branches tags] },
                      variables: {} })
        end
      end

      describe '#descendants' do
        it 'creates valid descendant nodes' do
          expect(entry.descendants.count).to eq 3
          expect(entry.descendants.first(2))
            .to all(be_an_instance_of(Gitlab::Ci::Config::Entry::Job))
          expect(entry.descendants.last)
            .to be_an_instance_of(Gitlab::Ci::Config::Entry::Hidden)
        end
      end

      describe '#value' do
        it 'returns value of visible jobs only' do
          expect(entry.value.keys).to eq [:rspec, :spinach]
        end
      end
    end
  end
end