summaryrefslogtreecommitdiff
path: root/qa/spec/service/docker_run/gitlab_runner_spec.rb
blob: a8838db10cf3817752d9aad9ac4128dded1439d3 (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

module QA
  RSpec.describe Service::DockerRun::GitlabRunner do
    let(:runner_name) { 'test-runner' }
    let(:address) { 'gitlab.test' }
    let(:token) { 'abc123' }

    let(:tags) { %w[qa test] }

    subject do
      described_class.new(runner_name).tap do |runner|
        runner.address = address
        runner.token = token
      end
    end

    it 'defaults to run untagged' do
      expect(subject.run_untagged).to be(true)
    end

    describe '#register!' do
      let(:register) { subject.send(:register!) }

      before do
        allow(subject).to receive(:shell)
      end

      context 'defaults' do
        before do
          register
        end

        it 'runs non-interactively' do
          expect(subject).to have_received(:shell).with(/ --non-interactive /)
        end

        it 'sets pertinent information' do
          expect(subject).to have_received(:shell).with(/--name #{runner_name} /)
          expect(subject).to have_received(:shell).with(/--url #{subject.address} /)
          expect(subject).to have_received(:shell).with(/--registration-token #{subject.token} /)
        end

        it 'runs untagged' do
          expect(subject).to have_received(:shell).with(/--run-untagged=true /)
        end

        it 'has no tags' do
          expect(subject.tags).to be_falsey
        end

        it 'runs daemonized' do
          expect(subject).to have_received(:shell).with(/ -d /)
        end

        it 'cleans itself up' do
          expect(subject).to have_received(:shell).with(/ --rm /)
        end
      end

      context 'running untagged' do
        before do
          register
        end

        it 'passes --run-untagged=true' do
          expect(subject).to have_received(:shell).with(/--run-untagged=true /)
        end

        it 'does not pass tag list' do
          expect(subject).not_to have_received(:shell).with(/--tag-list/)
        end
      end

      context 'running tagged' do
        context 'with only tags set' do
          before do
            subject.tags = tags

            register
          end

          it 'does not pass --run-untagged' do
            expect(subject).not_to have_received(:shell).with(/--run-untagged=true/)
          end

          it 'passes the tags with comma-separation' do
            expect(subject).to have_received(:shell).with(/--tag-list #{tags.join(',')} /)
          end
        end

        context 'with specifying only run_untagged' do
          before do
            subject.run_untagged = false
          end

          it 'raises an error if tags are not specified' do
            expect { register }.to raise_error(/must specify tags/i)
          end
        end

        context 'when specifying contradicting variables' do
          before do
            subject.tags = tags
            subject.run_untagged = true
          end

          it 'raises an error' do
            expect { register }.to raise_error(/conflicting options/i)
          end
        end
      end

      context 'executors' do
        it 'defaults to the shell executor' do
          register

          expect(subject).to have_received(:shell).with(/--executor shell /)
        end

        context 'docker' do
          before do
            subject.executor = :docker

            register
          end

          it 'specifies the docker executor' do
            expect(subject).to have_received(:shell).with(/--executor docker /)
          end

          it 'mounts the docker socket to the host runner' do
            expect(subject).to have_received(:shell).with(%r{-v /var/run/docker.sock:/var/run/docker.sock })
          end

          it 'runs in privileged mode' do
            expect(subject).to have_received(:shell).with(/--privileged /)
          end

          it 'has a default image' do
            expect(subject).to have_received(:shell).with(/--docker-image \b.+\b /)
          end

          it 'does not verify TLS' do
            expect(subject).to have_received(:shell).with(/--docker-tlsverify=false /)
          end

          it 'passes privileged mode' do
            expect(subject).to have_received(:shell).with(/--docker-privileged=true /)
          end

          it 'passes the host network' do
            expect(subject).to have_received(:shell).with(/--docker-network-mode=#{subject.network}/)
          end
        end
      end
    end

    describe '#tags=' do
      before do
        subject.tags = tags
      end

      it 'sets the tags' do
        expect(subject.tags).to eq(tags)
      end

      it 'sets run_untagged' do
        expect(subject.run_untagged).to be(false)
      end
    end
  end
end