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

require 'spec_helper'

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

  before do
    entry.compose!
  end

  context 'when configuration is a string' do
    let(:config) { 80 }

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

    describe '#value' do
      it 'returns valid hash' do
        expect(entry.value).to eq(number: 80)
      end
    end

    describe '#number' do
      it "returns port number" do
        expect(entry.number).to eq 80
      end
    end

    describe '#protocol' do
      it "is nil" do
        expect(entry.protocol).to be_nil
      end
    end

    describe '#name' do
      it "is nil" do
        expect(entry.name).to be_nil
      end
    end
  end

  context 'when configuration is a hash' do
    context 'with the complete hash' do
      let(:config) do
        { number: 80,
          protocol: 'http',
          name:  'foobar' }
      end

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

      describe '#value' do
        it 'returns valid hash' do
          expect(entry.value).to eq config
        end
      end

      describe '#number' do
        it "returns port number" do
          expect(entry.number).to eq 80
        end
      end

      describe '#protocol' do
        it "returns port protocol" do
          expect(entry.protocol).to eq 'http'
        end
      end

      describe '#name' do
        it "returns port name" do
          expect(entry.name).to eq 'foobar'
        end
      end
    end

    context 'with only the port number' do
      let(:config) { { number: 80 } }

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

      describe '#value' do
        it 'returns valid hash' do
          expect(entry.value).to eq(number: 80)
        end
      end

      describe '#number' do
        it "returns port number" do
          expect(entry.number).to eq 80
        end
      end

      describe '#protocol' do
        it "is nil" do
          expect(entry.protocol).to be_nil
        end
      end

      describe '#name' do
        it "is nil" do
          expect(entry.name).to be_nil
        end
      end
    end

    context 'without the number' do
      let(:config) { { protocol: 'http' } }

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

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

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

  context 'when protocol' do
    let(:config) { { number: 80, protocol: protocol, name: 'foobar' } }

    context 'is http' do
      let(:protocol) { 'http' }

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

    context 'is https' do
      let(:protocol) { 'https' }

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

    context 'is neither http nor https' do
      let(:protocol) { 'foo' }

      describe '#valid?' do
        it 'is invalid' do
          expect(entry.errors).to include("port protocol should be http or https")
        end
      end
    end
  end
end