Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rules_spec.rb « external « config « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8674af7ab65c7deb8da481f5c002c180db5b8927 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Config::External::Rules, feature_category: :pipeline_composition do
  let(:context) { double(variables_hash: {}) }
  let(:rule_hashes) { [{ if: '$MY_VAR == "hello"' }] }

  subject(:rules) { described_class.new(rule_hashes) }

  describe '#evaluate' do
    subject(:result) { rules.evaluate(context).pass? }

    context 'when there is no rule' do
      let(:rule_hashes) {}

      it { is_expected.to eq(true) }
    end

    shared_examples 'when there is a rule with if' do |rule_matched_result = true, rule_not_matched_result = false|
      context 'when the rule matches' do
        let(:context) { double(variables_hash: { 'MY_VAR' => 'hello' }) }

        it { is_expected.to eq(rule_matched_result) }
      end

      context 'when the rule does not match' do
        let(:context) { double(variables_hash: { 'MY_VAR' => 'invalid' }) }

        it { is_expected.to eq(rule_not_matched_result) }
      end
    end

    shared_examples 'when there is a rule with exists' do |file_exists_result = true, file_not_exists_result = false|
      let(:project) { create(:project, :repository) }

      context 'when the file exists' do
        let(:context) { double(project: project, sha: project.repository.tree.sha, top_level_worktree_paths: ['Dockerfile']) }

        before do
          project.repository.create_file(project.first_owner, 'Dockerfile', "commit", message: 'test', branch_name: "master")
        end

        it { is_expected.to eq(file_exists_result) }
      end

      context 'when the file does not exist' do
        let(:context) { double(project: project, sha: project.repository.tree.sha, top_level_worktree_paths: ['test.md']) }

        it { is_expected.to eq(file_not_exists_result) }
      end
    end

    it_behaves_like 'when there is a rule with if'

    context 'when there is a rule with exists' do
      let(:rule_hashes) { [{ exists: 'Dockerfile' }] }

      it_behaves_like 'when there is a rule with exists'
    end

    context 'when there is a rule with if and when' do
      context 'with when: never' do
        let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: 'never' }] }

        it_behaves_like 'when there is a rule with if', false, false
      end

      context 'with when: always' do
        let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: 'always' }] }

        it_behaves_like 'when there is a rule with if'
      end

      context 'with when: <invalid string>' do
        let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: 'on_success' }] }

        it 'raises an error' do
          expect { result }.to raise_error(described_class::InvalidIncludeRulesError, /when unknown value: on_success/)
        end
      end

      context 'with when: null' do
        let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: nil }] }

        it_behaves_like 'when there is a rule with if'
      end
    end

    context 'when there is a rule with exists and when' do
      context 'with when: never' do
        let(:rule_hashes) { [{ exists: 'Dockerfile', when: 'never' }] }

        it_behaves_like 'when there is a rule with exists', false, false
      end

      context 'with when: always' do
        let(:rule_hashes) { [{ exists: 'Dockerfile', when: 'always' }] }

        it_behaves_like 'when there is a rule with exists'
      end

      context 'with when: <invalid string>' do
        let(:rule_hashes) { [{ exists: 'Dockerfile', when: 'on_success' }] }

        it 'raises an error' do
          expect { result }.to raise_error(described_class::InvalidIncludeRulesError, /when unknown value: on_success/)
        end
      end

      context 'with when: null' do
        let(:rule_hashes) { [{ exists: 'Dockerfile', when: nil }] }

        it_behaves_like 'when there is a rule with exists'
      end
    end

    context 'when there is a rule with changes' do
      let(:rule_hashes) { [{ changes: ['$MY_VAR'] }] }

      it 'raises an error' do
        expect { result }.to raise_error(described_class::InvalidIncludeRulesError, /contains unknown keys: changes/)
      end
    end

    context 'when FF `ci_refactor_external_rules` is disabled' do
      before do
        stub_feature_flags(ci_refactor_external_rules: false)
      end

      context 'when there is no rule' do
        let(:rule_hashes) {}

        it { is_expected.to eq(true) }
      end

      it_behaves_like 'when there is a rule with if'

      context 'when there is a rule with exists' do
        let(:rule_hashes) { [{ exists: 'Dockerfile' }] }

        it_behaves_like 'when there is a rule with exists'
      end

      context 'when there is a rule with if and when' do
        context 'with when: never' do
          let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: 'never' }] }

          it_behaves_like 'when there is a rule with if', false, false
        end

        context 'with when: always' do
          let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: 'always' }] }

          it_behaves_like 'when there is a rule with if'
        end

        context 'with when: <invalid string>' do
          let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: 'on_success' }] }

          it 'raises an error' do
            expect { result }.to raise_error(described_class::InvalidIncludeRulesError,
                                             'invalid include rule: {:if=>"$MY_VAR == \"hello\"", :when=>"on_success"}')
          end
        end

        context 'with when: null' do
          let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: nil }] }

          it_behaves_like 'when there is a rule with if'
        end
      end

      context 'when there is a rule with exists and when' do
        context 'with when: never' do
          let(:rule_hashes) { [{ exists: 'Dockerfile', when: 'never' }] }

          it_behaves_like 'when there is a rule with exists', false, false
        end

        context 'with when: always' do
          let(:rule_hashes) { [{ exists: 'Dockerfile', when: 'always' }] }

          it_behaves_like 'when there is a rule with exists'
        end

        context 'with when: <invalid string>' do
          let(:rule_hashes) { [{ exists: 'Dockerfile', when: 'on_success' }] }

          it 'raises an error' do
            expect { result }.to raise_error(described_class::InvalidIncludeRulesError,
                                             'invalid include rule: {:exists=>"Dockerfile", :when=>"on_success"}')
          end
        end

        context 'with when: null' do
          let(:rule_hashes) { [{ exists: 'Dockerfile', when: nil }] }

          it_behaves_like 'when there is a rule with exists'
        end
      end

      context 'when there is a rule with changes' do
        let(:rule_hashes) { [{ changes: ['$MY_VAR'] }] }

        it 'raises an error' do
          expect { result }.to raise_error(described_class::InvalidIncludeRulesError,
                                           'invalid include rule: {:changes=>["$MY_VAR"]}')
        end
      end
    end
  end
end