summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab_spec.rb
blob: 49ba4debe3126d826b8aa19cd378a2151af2fcca (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab do
  %w[root extensions ee? jh?].each do |method_name|
    it "delegates #{method_name} to GitlabEdition" do
      expect(GitlabEdition).to receive(method_name)

      described_class.public_send(method_name)
    end
  end

  %w[ee jh].each do |method_name|
    it "delegates #{method_name} to GitlabEdition" do
      expect(GitlabEdition).to receive(method_name)

      described_class.public_send(method_name) {}
    end
  end

  describe '.revision' do
    let(:cmd) { %W[#{described_class.config.git.bin_path} log --pretty=format:%h --abbrev=11 -n 1] }

    around do |example|
      described_class.instance_variable_set(:@_revision, nil)
      example.run
      described_class.instance_variable_set(:@_revision, nil)
    end

    context 'when a REVISION file exists' do
      before do
        expect(File).to receive(:exist?)
                          .with(described_class.root.join('REVISION'))
                          .and_return(true)
      end

      it 'returns the actual Git revision' do
        expect_file_read(described_class.root.join('REVISION'), content: "abc123\n")

        expect(described_class.revision).to eq('abc123')
      end

      it 'memoizes the revision' do
        stub_file_read(described_class.root.join('REVISION'), content: "abc123\n")

        expect(File).to receive(:read)
          .once
          .with(described_class.root.join('REVISION'))

        2.times { described_class.revision }
      end
    end

    context 'when no REVISION file exist' do
      context 'when the Git command succeeds' do
        before do
          expect(Gitlab::Popen).to receive(:popen_with_detail)
                                     .with(cmd)
                                     .and_return(Gitlab::Popen::Result.new(cmd, 'abc123', '', double(success?: true)))
        end

        it 'returns the actual Git revision' do
          expect(described_class.revision).to eq('abc123')
        end
      end

      context 'when the Git command fails' do
        before do
          expect(Gitlab::Popen).to receive(:popen_with_detail)
                                     .with(cmd)
                                     .and_return(Gitlab::Popen::Result.new(cmd, '', 'fatal: Not a git repository', double('Process::Status', success?: false)))
        end

        it 'returns "Unknown"' do
          expect(described_class.revision).to eq('Unknown')
        end
      end
    end
  end

  describe '.com?' do
    it "is true when on #{Gitlab::Saas.com_url}" do
      stub_config_setting(url: Gitlab::Saas.com_url)

      expect(described_class.com?).to eq true
    end

    it "is true when on #{Gitlab::Saas.staging_com_url}" do
      stub_config_setting(url: Gitlab::Saas.staging_com_url)

      expect(described_class.com?).to eq true
    end

    it 'is true when on other gitlab subdomain' do
      url_with_subdomain = Gitlab::Saas.com_url.gsub('https://', 'https://example.')
      stub_config_setting(url: url_with_subdomain)

      expect(described_class.com?).to eq true
    end

    it 'is false when not on GitLab.com' do
      stub_config_setting(url: 'http://example.com')

      expect(described_class.com?).to eq false
    end
  end

  describe '.com' do
    subject { described_class.com { true } }

    before do
      allow(described_class).to receive(:com?).and_return(gl_com)
    end

    context 'when on GitLab.com' do
      let(:gl_com) { true }

      it { is_expected.to be true }
    end

    context 'when not on GitLab.com' do
      let(:gl_com) { false }

      it { is_expected.to be_nil }
    end
  end

  describe '.staging?' do
    subject { described_class.staging? }

    it "is false when on #{Gitlab::Saas.com_url}" do
      stub_config_setting(url: Gitlab::Saas.com_url)

      expect(subject).to eq false
    end

    it "is true when on #{Gitlab::Saas.staging_com_url}" do
      stub_config_setting(url: Gitlab::Saas.staging_com_url)

      expect(subject).to eq true
    end

    it 'is false when not on staging' do
      stub_config_setting(url: 'https://example.gitlab.com')

      expect(subject).to eq false
    end
  end

  describe '.canary?' do
    it 'is true when CANARY env var is set to true' do
      stub_env('CANARY', '1')

      expect(described_class.canary?).to eq true
    end

    it 'is false when CANARY env var is set to false' do
      stub_env('CANARY', '0')

      expect(described_class.canary?).to eq false
    end
  end

  describe '.com_and_canary?' do
    it 'is true when on .com and canary' do
      allow(described_class).to receive_messages(com?: true, canary?: true)

      expect(described_class.com_and_canary?).to eq true
    end

    it 'is false when on .com but not on canary' do
      allow(described_class).to receive_messages(com?: true, canary?: false)

      expect(described_class.com_and_canary?).to eq false
    end
  end

  describe '.com_but_not_canary?' do
    it 'is false when on .com and canary' do
      allow(described_class).to receive_messages(com?: true, canary?: true)

      expect(described_class.com_but_not_canary?).to eq false
    end

    it 'is true when on .com but not on canary' do
      allow(described_class).to receive_messages(com?: true, canary?: false)

      expect(described_class.com_but_not_canary?).to eq true
    end
  end

  describe '.dev_env_org_or_com?' do
    it 'is true when on .com' do
      allow(described_class).to receive_messages(com?: true, org?: false)

      expect(described_class.dev_env_org_or_com?).to eq true
    end

    it 'is true when org' do
      allow(described_class).to receive_messages(com?: false, org?: true)

      expect(described_class.dev_env_org_or_com?).to eq true
    end

    it 'is true when dev env' do
      allow(described_class).to receive_messages(com?: false, org?: false)
      stub_rails_env('development')

      expect(described_class.dev_env_org_or_com?).to eq true
    end

    it 'is false when not dev, org or com' do
      allow(described_class).to receive_messages(com?: false, org?: false)

      expect(described_class.dev_env_org_or_com?).to eq false
    end
  end

  describe '.dev_env_or_com?' do
    it 'is true when on .com' do
      allow(described_class).to receive(:com?).and_return(true)

      expect(described_class.dev_env_or_com?).to eq true
    end

    it 'is true when dev env' do
      allow(described_class).to receive(:com?).and_return(false)
      allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('development'))

      expect(described_class.dev_env_or_com?).to eq true
    end

    it 'is false when not dev or com' do
      allow(described_class).to receive(:com?).and_return(false)

      expect(described_class.dev_env_or_com?).to eq false
    end
  end

  describe '.dev_or_test_env?' do
    subject { described_class.dev_or_test_env? }

    it 'is true when test env' do
      expect(subject).to eq true
    end

    it 'is true when dev env' do
      allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('development'))

      expect(subject).to eq true
    end

    it 'is false when env is not dev or test' do
      allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('production'))

      expect(subject).to eq false
    end
  end

  describe '.http_proxy_env?' do
    it 'returns true when lower case https' do
      stub_env('https_proxy', 'https://my.proxy')

      expect(described_class.http_proxy_env?).to eq(true)
    end

    it 'returns true when upper case https' do
      stub_env('HTTPS_PROXY', 'https://my.proxy')

      expect(described_class.http_proxy_env?).to eq(true)
    end

    it 'returns true when lower case http' do
      stub_env('http_proxy', 'http://my.proxy')

      expect(described_class.http_proxy_env?).to eq(true)
    end

    it 'returns true when upper case http' do
      stub_env('HTTP_PROXY', 'http://my.proxy')

      expect(described_class.http_proxy_env?).to eq(true)
    end

    it 'returns false when not set' do
      expect(described_class.http_proxy_env?).to eq(false)
    end
  end

  describe '.maintenance_mode?' do
    it 'returns true when maintenance mode is enabled' do
      stub_maintenance_mode_setting(true)

      expect(described_class.maintenance_mode?).to eq(true)
    end

    it 'returns false when maintenance mode is disabled' do
      stub_maintenance_mode_setting(false)

      expect(described_class.maintenance_mode?).to eq(false)
    end

    it 'returns false when maintenance mode column is not present' do
      stub_maintenance_mode_setting(true)

      allow(::Gitlab::CurrentSettings.current_application_settings)
        .to receive(:respond_to?)
        .with(:maintenance_mode, false)
        .and_return(false)

      expect(described_class.maintenance_mode?).to eq(false)
    end
  end
end