summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config/entry/trigger_spec.rb
blob: 752c3f59a9545983689ea22211827f0854202700 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# frozen_string_literal: true

require 'spec_helper'

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

  context 'when trigger config is a non-empty string' do
    let(:config) { 'some/project' }

    describe '#valid?' do
      it { is_expected.to be_valid }
    end

    describe '#value' do
      it 'returns a trigger configuration hash' do
        expect(subject.value).to eq(project: 'some/project')
      end
    end
  end

  context 'when trigger config an empty string' do
    let(:config) { '' }

    describe '#valid?' do
      it { is_expected.not_to be_valid }
    end

    describe '#errors' do
      it 'returns an error about an empty config' do
        expect(subject.errors.first)
          .to match /config can't be blank/
      end
    end
  end

  context 'when trigger is a hash' do
    context 'when branch is provided' do
      let(:config) { { project: 'some/project', branch: 'feature' } }

      describe '#valid?' do
        it { is_expected.to be_valid }
      end

      describe '#value' do
        it 'returns a trigger configuration hash' do
          expect(subject.value)
            .to eq(project: 'some/project', branch: 'feature')
        end
      end
    end

    context 'when strategy is provided' do
      context 'when strategy is depend' do
        let(:config) { { project: 'some/project', strategy: 'depend' } }

        describe '#valid?' do
          it { is_expected.to be_valid }
        end

        describe '#value' do
          it 'returns a trigger configuration hash' do
            expect(subject.value)
              .to eq(project: 'some/project', strategy: 'depend')
          end
        end
      end

      context 'when strategy is invalid' do
        let(:config) { { project: 'some/project', strategy: 'notdepend' } }

        describe '#valid?' do
          it { is_expected.not_to be_valid }
        end

        describe '#errors' do
          it 'returns an error about unknown config key' do
            expect(subject.errors.first)
              .to match /trigger strategy should be depend/
          end
        end
      end
    end

    describe '#include' do
      context 'with simple include' do
        let(:config) { { include: 'path/to/config.yml' } }

        it { is_expected.to be_valid }

        it 'returns a trigger configuration hash' do
          expect(subject.value).to eq(include: 'path/to/config.yml' )
        end
      end

      context 'with project' do
        let(:config) { { project: 'some/project', include: 'path/to/config.yml' } }

        it { is_expected.not_to be_valid }

        it 'returns an error' do
          expect(subject.errors.first)
            .to match /config contains unknown keys: project/
        end
      end

      context 'with branch' do
        let(:config) { { branch: 'feature', include: 'path/to/config.yml' } }

        it { is_expected.not_to be_valid }

        it 'returns an error' do
          expect(subject.errors.first)
            .to match /config contains unknown keys: branch/
        end
      end

      context 'when feature flag is off' do
        before do
          stub_feature_flags(ci_parent_child_pipeline: false)
        end

        let(:config) { { include: 'path/to/config.yml' } }

        it 'is returns an error if include is used' do
          expect(subject.errors.first)
            .to match /config must specify project/
        end
      end
    end

    context 'when config contains unknown keys' do
      let(:config) { { project: 'some/project', unknown: 123 } }

      describe '#valid?' do
        it { is_expected.not_to be_valid }
      end

      describe '#errors' do
        it 'returns an error about unknown config key' do
          expect(subject.errors.first)
            .to match /config contains unknown keys: unknown/
        end
      end
    end
  end

  context 'when trigger configuration is not valid' do
    context 'when branch is not provided' do
      let(:config) { 123 }

      describe '#valid?' do
        it { is_expected.not_to be_valid }
      end

      describe '#errors' do
        it 'returns an error message' do
          expect(subject.errors.first)
            .to match /has to be either a string or a hash/
        end
      end
    end
  end
end