global_spec.rb 8.12 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::Ci::Config::Entry::Global do
4
  let(:global) { described_class.new(hash) }
5

6
  describe '.nodes' do
7
    it 'can contain global config keys' do
8
      expect(described_class.nodes).to include :before_script
9
    end
10 11

    it 'returns a hash' do
12
      expect(described_class.nodes).to be_a Hash
13
    end
14 15 16
  end

  context 'when hash is valid' do
17
    context 'when some entries defined' do
18 19 20 21 22
      let(:hash) do
        { before_script: ['ls', 'pwd'],
          image: 'ruby:2.2',
          services: ['postgres:9.1', 'mysql:5.5'],
          variables: { VAR: 'value' },
23
          after_script: ['make clean'],
24
          stages: ['build', 'pages'],
25
          cache: { key: 'k', untracked: true, paths: ['public/'] },
26
          rspec: { script: %w[rspec ls] },
27
          spinach: { before_script: [], variables: {}, script: 'spinach' } }
28
      end
29

30 31
      describe '#compose!' do
        before { global.compose! }
32

33
        it 'creates nodes hash' do
34
          expect(global.descendants).to be_an Array
35
        end
36

37
        it 'creates node object for each entry' do
38
          expect(global.descendants.count).to eq 8
39
        end
40

41
        it 'creates node object using valid class' do
42
          expect(global.descendants.first)
43
            .to be_an_instance_of Gitlab::Ci::Config::Entry::Script
44
          expect(global.descendants.second)
45
            .to be_an_instance_of Gitlab::Ci::Config::Entry::Image
46 47 48
        end

        it 'sets correct description for nodes' do
49
          expect(global.descendants.first.description)
50
            .to eq 'Script that will be executed before each job.'
51
          expect(global.descendants.second.description)
52 53
            .to eq 'Docker image that will be used to execute jobs.'
        end
54

55 56 57 58
        describe '#leaf?' do
          it 'is not leaf' do
            expect(global).not_to be_leaf
          end
59
        end
60
      end
61

62
      context 'when not composed' do
63 64 65 66 67
        describe '#before_script' do
          it 'returns nil' do
            expect(global.before_script).to be nil
          end
        end
68 69 70 71 72 73

        describe '#leaf?' do
          it 'is leaf' do
            expect(global).to be_leaf
          end
        end
74
      end
75

76 77
      context 'when composed' do
        before { global.compose! }
78

79 80 81 82 83 84
        describe '#errors' do
          it 'has no errors' do
            expect(global.errors).to be_empty
          end
        end

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
        describe '#before_script' do
          it 'returns correct script' do
            expect(global.before_script).to eq ['ls', 'pwd']
          end
        end

        describe '#image' do
          it 'returns valid image' do
            expect(global.image).to eq 'ruby:2.2'
          end
        end

        describe '#services' do
          it 'returns array of services' do
            expect(global.services).to eq ['postgres:9.1', 'mysql:5.5']
          end
        end

        describe '#after_script' do
          it 'returns after script' do
            expect(global.after_script).to eq ['make clean']
          end
        end

        describe '#variables' do
          it 'returns variables' do
            expect(global.variables).to eq(VAR: 'value')
          end
113
        end
114 115 116 117 118 119 120 121 122

        describe '#stages' do
          context 'when stages key defined' do
            it 'returns array of stages' do
              expect(global.stages).to eq %w[build pages]
            end
          end

          context 'when deprecated types key defined' do
123 124 125 126
            let(:hash) do
              { types: ['test', 'deploy'],
                rspec: { script: 'rspec' } }
            end
127 128 129 130 131 132

            it 'returns array of types as stages' do
              expect(global.stages).to eq %w[test deploy]
            end
          end
        end
133 134 135 136 137 138 139

        describe '#cache' do
          it 'returns cache configuration' do
            expect(global.cache)
              .to eq(key: 'k', untracked: true, paths: ['public/'])
          end
        end
140 141 142

        describe '#jobs' do
          it 'returns jobs configuration' do
143
            expect(global.jobs).to eq(
144 145
              rspec: { name: :rspec,
                       script: %w[rspec ls],
146
                       before_script: ['ls', 'pwd'],
147
                       commands: "ls\npwd\nrspec\nls",
148 149 150 151 152 153
                       image: 'ruby:2.2',
                       services: ['postgres:9.1', 'mysql:5.5'],
                       stage: 'test',
                       cache: { key: 'k', untracked: true, paths: ['public/'] },
                       variables: { VAR: 'value' },
                       after_script: ['make clean'] },
154
              spinach: { name: :spinach,
155
                         before_script: [],
156 157
                         script: %w[spinach],
                         commands: 'spinach',
158 159 160 161 162 163
                         image: 'ruby:2.2',
                         services: ['postgres:9.1', 'mysql:5.5'],
                         stage: 'test',
                         cache: { key: 'k', untracked: true, paths: ['public/'] },
                         variables: {},
                         after_script: ['make clean'] },
164
            )
165 166
          end
        end
167 168
      end
    end
169

170
    context 'when most of entires not defined' do
171 172 173 174 175
      before { global.compose! }

      let(:hash) do
        { cache: { key: 'a' }, rspec: { script: %w[ls] } }
      end
176

177 178
      describe '#nodes' do
        it 'instantizes all nodes' do
179
          expect(global.descendants.count).to eq 8
180 181
        end

182
        it 'contains unspecified nodes' do
183
          expect(global.descendants.first)
184
            .to be_an_instance_of Gitlab::Ci::Config::Entry::Unspecified
185
        end
186
      end
187

188 189 190
      describe '#variables' do
        it 'returns default value for variables' do
          expect(global.variables).to eq({})
191 192
        end
      end
193 194 195 196 197 198

      describe '#stages' do
        it 'returns an array of default stages' do
          expect(global.stages).to eq %w[build test deploy]
        end
      end
199 200 201 202 203 204

      describe '#cache' do
        it 'returns correct cache definition' do
          expect(global.cache).to eq(key: 'a')
        end
      end
205
    end
206

207 208 209 210 211 212 213
    ##
    # When nodes are specified but not defined, we assume that
    # configuration is valid, and we asume that entry is simply undefined,
    # despite the fact, that key is present. See issue #18775 for more
    # details.
    #
    context 'when entires specified but not defined' do
214 215 216 217 218
      before { global.compose! }

      let(:hash) do
        { variables: nil, rspec: { script: 'rspec' } }
      end
219 220

      describe '#variables' do
221 222
        it 'undefined entry returns a default value' do
          expect(global.variables).to eq({})
223 224
        end
      end
225
    end
226
  end
227 228

  context 'when hash is not valid' do
229
    before { global.compose! }
230

231 232 233 234 235 236 237 238 239 240 241 242 243
    let(:hash) do
      { before_script: 'ls' }
    end

    describe '#valid?' do
      it 'is not valid' do
        expect(global).not_to be_valid
      end
    end

    describe '#errors' do
      it 'reports errors from child nodes' do
        expect(global.errors)
244
          .to include 'before_script config should be an array of strings'
245 246
      end
    end
247 248

    describe '#before_script' do
249 250
      it 'returns nil' do
        expect(global.before_script).to be_nil
251 252
      end
    end
253
  end
254 255 256 257 258 259 260 261 262

  context 'when value is not a hash' do
    let(:hash) { [] }

    describe '#valid?' do
      it 'is not valid' do
        expect(global).not_to be_valid
      end
    end
263 264 265 266 267 268

    describe '#errors' do
      it 'returns error about invalid type' do
        expect(global.errors.first).to match /should be a hash/
      end
    end
269
  end
270

271
  describe '#specified?' do
272
    it 'is concrete entry that is defined' do
273
      expect(global.specified?).to be true
274 275
    end
  end
276 277 278 279 280 281 282 283 284 285 286

  describe '#[]' do
    before { global.compose! }

    let(:hash) do
      { cache: { key: 'a' }, rspec: { script: 'ls' } }
    end

    context 'when node exists' do
      it 'returns correct entry' do
        expect(global[:cache])
287
          .to be_an_instance_of Gitlab::Ci::Config::Entry::Cache
288 289 290 291 292 293 294 295 296 297 298
        expect(global[:jobs][:rspec][:script].value).to eq ['ls']
      end
    end

    context 'when node does not exist' do
      it 'always return unspecified node' do
        expect(global[:some][:unknown][:node])
          .not_to be_specified
      end
    end
  end
299
end