summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config/entry/services_spec.rb
blob: d5a1316f66503f81000ea3c551f93d0803a65a68 (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
require 'spec_helper'

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

  before do
    entry.compose!
  end

  context 'when configuration is valid' do
    let(:config) { ['postgresql:9.5', { name: 'postgresql:9.1', alias: 'postgres_old' }] }

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

    describe '#value' do
      it 'returns valid array' do
        expect(entry.value).to eq([{ name: 'postgresql:9.5' }, { name: 'postgresql:9.1', alias: 'postgres_old' }])
      end
    end
  end

  context 'when configuration is invalid' do
    let(:config) { 'postgresql:9.5' }

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

  context 'when configuration has ports' do
    let(:ports) { [{ number: 80, protocol: 'http', name: 'foobar' }] }
    let(:config) { ['postgresql:9.5', { name: 'postgresql:9.1', alias: 'postgres_old', ports: ports }] }
    let(:entry) { described_class.new(config, { with_image_ports: image_ports }) }
    let(:image_ports) { false }

    context 'when with_image_ports metadata is not enabled' do
      describe '#valid?' do
        it 'is not valid' do
          expect(entry).not_to be_valid
          expect(entry.errors).to include("service config contains disallowed keys: ports")
        end
      end
    end

    context 'when with_image_ports metadata is enabled' do
      let(:image_ports) { true }

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

      describe '#value' do
        it 'returns valid array' do
          expect(entry.value).to eq([{ name: 'postgresql:9.5' }, { name: 'postgresql:9.1', alias: 'postgres_old', ports: ports }])
        end
      end

      describe 'services alias' do
        context 'when they are not unique' do
          let(:config) do
            ['postgresql:9.5',
             { name: 'postgresql:9.1', alias: 'postgres_old', ports: [80] },
             { name: 'ruby', alias: 'postgres_old', ports: [81] }]
          end

          describe '#valid?' do
            it 'is invalid' do
              expect(entry).not_to be_valid
              expect(entry.errors).to include("services config alias must be unique in services with ports")
            end
          end
        end

        context 'when they are unique' do
          let(:config) do
            ['postgresql:9.5',
             { name: 'postgresql:9.1', alias: 'postgres_old', ports: [80] },
             { name: 'ruby', alias: 'ruby', ports: [81] }]
          end

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

        context 'when one of the duplicated alias is in a service without ports' do
          let(:config) do
            ['postgresql:9.5',
             { name: 'postgresql:9.1', alias: 'postgres_old', ports: [80] },
             { name: 'ruby', alias: 'postgres_old' }]
          end

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

        context 'when there are not any ports' do
          let(:config) do
            ['postgresql:9.5',
             { name: 'postgresql:9.1', alias: 'postgres_old' },
             { name: 'ruby', alias: 'postgres_old' }]
          end

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