summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/build/image_spec.rb
blob: 04bab9c58b8cfe00dc80ae8d0d75090335c7d952 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Ci::Build::Image do
  let(:job) { create(:ci_build, :no_options) }

  describe '#from_image' do
    subject { described_class.from_image(job) }

    context 'when image is defined in job' do
      let(:image_name) { 'ruby:2.1' }
      let(:job) { create(:ci_build, options: { image: image_name } ) }

      context 'when image is defined as string' do
        it 'fabricates an object of the proper class' do
          is_expected.to be_kind_of(described_class)
        end

        it 'populates fabricated object with the proper name attribute' do
          expect(subject.name).to eq(image_name)
        end

        it 'does not populate the ports' do
          expect(subject.ports).to be_empty
        end
      end

      context 'when image is defined as hash' do
        let(:entrypoint) { '/bin/sh' }

        let(:job) { create(:ci_build, options: { image: { name: image_name, entrypoint: entrypoint, ports: [80] } } ) }

        it 'fabricates an object of the proper class' do
          is_expected.to be_kind_of(described_class)
        end

        it 'populates fabricated object with the proper attributes' do
          expect(subject.name).to eq(image_name)
          expect(subject.entrypoint).to eq(entrypoint)
        end

        it 'populates the ports' do
          port = subject.ports.first
          expect(port.number).to eq 80
          expect(port.protocol).to eq 'http'
          expect(port.name).to eq 'default_port'
        end
      end

      context 'when image name is empty' do
        let(:image_name) { '' }

        it 'does not fabricate an object' do
          is_expected.to be_nil
        end
      end
    end

    context 'when image is not defined in job' do
      it 'does not fabricate an object' do
        is_expected.to be_nil
      end
    end
  end

  describe '#from_services' do
    subject { described_class.from_services(job) }

    context 'when services are defined in job' do
      let(:service_image_name) { 'postgres' }
      let(:job) { create(:ci_build, options: { services: [service_image_name] }) }

      context 'when service is defined as string' do
        it 'fabricates an non-empty array of objects' do
          is_expected.to be_kind_of(Array)
          is_expected.not_to be_empty
        end

        it 'populates fabricated objects with the proper name attributes' do
          expect(subject.first).to be_kind_of(described_class)
          expect(subject.first.name).to eq(service_image_name)
        end

        it 'does not populate the ports' do
          expect(subject.first.ports).to be_empty
        end
      end

      context 'when service is defined as hash' do
        let(:service_entrypoint) { '/bin/sh' }
        let(:service_alias) { 'db' }
        let(:service_command) { 'sleep 30' }
        let(:job) do
          create(:ci_build, options: { services: [{ name: service_image_name, entrypoint: service_entrypoint,
                                                    alias: service_alias, command: service_command, ports: [80] }] })
        end

        it 'fabricates an non-empty array of objects' do
          is_expected.to be_kind_of(Array)
          is_expected.not_to be_empty
          expect(subject.first).to be_kind_of(described_class)
        end

        it 'populates fabricated objects with the proper attributes' do
          expect(subject.first.name).to eq(service_image_name)
          expect(subject.first.entrypoint).to eq(service_entrypoint)
          expect(subject.first.alias).to eq(service_alias)
          expect(subject.first.command).to eq(service_command)

          port = subject.first.ports.first
          expect(port.number).to eq 80
          expect(port.protocol).to eq 'http'
          expect(port.name).to eq 'default_port'
        end
      end

      context 'when service image name is empty' do
        let(:service_image_name) { '' }

        it 'fabricates an empty array' do
          is_expected.to be_kind_of(Array)
          is_expected.to be_empty
        end
      end
    end

    context 'when services are not defined in job' do
      it 'fabricates an empty array' do
        is_expected.to be_kind_of(Array)
        is_expected.to be_empty
      end
    end
  end
end