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

require 'fast_spec_helper'

RSpec.describe ::Gitlab::Ci::Config::Entry::Include do
  subject(:include_entry) { described_class.new(config) }

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

    context 'when value is a string' do
      let(:config) { 'test.yml' }

      it { is_expected.to be_valid }
    end

    context 'when value is hash' do
      context 'when using not allowed keys' do
        let(:config) do
          { not_allowed: 'key' }
        end

        it { is_expected.not_to be_valid }
      end

      context 'when using "local"' do
        let(:config) { { local: 'test.yml' } }

        it { is_expected.to be_valid }
      end

      context 'when using "file"' do
        let(:config) { { file: 'test.yml' } }

        it { is_expected.to be_valid }
      end

      context 'when using "template"' do
        let(:config) { { template: 'test.yml' } }

        it { is_expected.to be_valid }
      end

      context 'when using "artifact"' do
        context 'and specifying "job"' do
          let(:config) { { artifact: 'test.yml', job: 'generator' } }

          it { is_expected.to be_valid }
        end

        context 'without "job"' do
          let(:config) { { artifact: 'test.yml' } }

          it { is_expected.not_to be_valid }

          it 'has specific error' do
            expect(include_entry.errors)
              .to include('include config must specify the job where to fetch the artifact from')
          end
        end
      end

      context 'when using "project"' do
        context 'and specifying "ref" and "file"' do
          let(:config) { { project: 'my-group/my-pipeline-library', ref: 'master', file: 'test.yml' } }

          it { is_expected.to be_valid }
        end

        context 'without "ref"' do
          let(:config) { { project: 'my-group/my-pipeline-library', file: 'test.yml' } }

          it { is_expected.to be_valid }
        end

        context 'without "file"' do
          let(:config) { { project: 'my-group/my-pipeline-library' } }

          it { is_expected.not_to be_valid }

          it 'has specific error' do
            expect(include_entry.errors)
              .to include('include config must specify the file where to fetch the config from')
          end
        end
      end

      context 'when using with "rules"' do
        let(:config) { { local: 'test.yml', rules: [{ if: '$VARIABLE' }] } }

        it { is_expected.to be_valid }

        context 'when rules is not an array of hashes' do
          let(:config) { { local: 'test.yml', rules: ['$VARIABLE'] } }

          it { is_expected.not_to be_valid }

          it 'has specific error' do
            expect(include_entry.errors).to include('include rules should be an array of hashes')
          end
        end
      end
    end

    context 'when value is something else' do
      let(:config) { 123 }

      it { is_expected.not_to be_valid }
    end
  end

  describe '#value' do
    subject(:value) { include_entry.value }

    context 'when config is a string' do
      let(:config) { 'test.yml' }

      it { is_expected.to eq('test.yml') }
    end

    context 'when config is a hash' do
      let(:config) { { local: 'test.yml' } }

      it { is_expected.to eq(local: 'test.yml') }
    end

    context 'when config has "rules"' do
      let(:config) { { local: 'test.yml', rules: [{ if: '$VARIABLE' }] } }

      it { is_expected.to eq(local: 'test.yml', rules: [{ if: '$VARIABLE' }]) }
    end
  end
end