summaryrefslogtreecommitdiff
path: root/qa/spec/runtime/env_spec.rb
blob: 04085efe2cea9a7e01d41ba5588260d0d04d34ad (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
# frozen_string_literal: true

describe QA::Runtime::Env do
  include Helpers::StubENV

  shared_examples 'boolean method' do |**kwargs|
    it_behaves_like 'boolean method with parameter', kwargs
  end

  shared_examples 'boolean method with parameter' do |method:, param: nil, env_key:, default:|
    context 'when there is an env variable set' do
      it 'returns false when falsey values specified' do
        stub_env(env_key, 'false')
        expect(described_class.public_send(method, *param)).to be_falsey

        stub_env(env_key, 'no')
        expect(described_class.public_send(method, *param)).to be_falsey

        stub_env(env_key, '0')
        expect(described_class.public_send(method, *param)).to be_falsey
      end

      it 'returns true when anything else specified' do
        stub_env(env_key, 'true')
        expect(described_class.public_send(method, *param)).to be_truthy

        stub_env(env_key, '1')
        expect(described_class.public_send(method, *param)).to be_truthy

        stub_env(env_key, 'anything')
        expect(described_class.public_send(method, *param)).to be_truthy
      end
    end

    context 'when there is no env variable set' do
      it "returns the default, #{default}" do
        stub_env(env_key, nil)
        expect(described_class.public_send(method, *param)).to be(default)
      end
    end
  end

  describe '.signup_disabled?' do
    it_behaves_like 'boolean method',
      method: :signup_disabled?,
      env_key: 'SIGNUP_DISABLED',
      default: false
  end

  describe '.debug?' do
    it_behaves_like 'boolean method',
      method: :debug?,
      env_key: 'QA_DEBUG',
      default: false
  end

  describe '.chrome_headless?' do
    it_behaves_like 'boolean method',
      method: :chrome_headless?,
      env_key: 'CHROME_HEADLESS',
      default: true
  end

  describe '.running_in_ci?' do
    context 'when there is an env variable set' do
      it 'returns true if CI' do
        stub_env('CI', 'anything')
        expect(described_class.running_in_ci?).to be_truthy
      end

      it 'returns true if CI_SERVER' do
        stub_env('CI_SERVER', 'anything')
        expect(described_class.running_in_ci?).to be_truthy
      end
    end

    context 'when there is no env variable set' do
      it 'returns true' do
        stub_env('CI', nil)
        stub_env('CI_SERVER', nil)
        expect(described_class.running_in_ci?).to be_falsey
      end
    end
  end

  describe '.personal_access_token' do
    around do |example|
      described_class.instance_variable_set(:@personal_access_token, nil)
      example.run
      described_class.instance_variable_set(:@personal_access_token, nil)
    end

    context 'when GITLAB_QA_ACCESS_TOKEN is set' do
      before do
        stub_env('GITLAB_QA_ACCESS_TOKEN', 'a_token_too')
      end

      it 'returns specified token from env' do
        expect(described_class.personal_access_token).to eq 'a_token_too'
      end
    end

    context 'when @personal_access_token is set' do
      before do
        described_class.personal_access_token = 'another_token'
      end

      it 'returns the instance variable value' do
        expect(described_class.personal_access_token).to eq 'another_token'
      end
    end
  end

  describe '.personal_access_token=' do
    around do |example|
      described_class.instance_variable_set(:@personal_access_token, nil)
      example.run
      described_class.instance_variable_set(:@personal_access_token, nil)
    end

    it 'saves the token' do
      described_class.personal_access_token = 'a_token'

      expect(described_class.personal_access_token).to eq 'a_token'
    end
  end

  describe '.forker?' do
    before do
      stub_env('GITLAB_FORKER_USERNAME', nil)
      stub_env('GITLAB_FORKER_PASSWORD', nil)
    end

    it 'returns false if no forker credentials are defined' do
      expect(described_class).not_to be_forker
    end

    it 'returns false if only forker username is defined' do
      stub_env('GITLAB_FORKER_USERNAME', 'foo')

      expect(described_class).not_to be_forker
    end

    it 'returns false if only forker password is defined' do
      stub_env('GITLAB_FORKER_PASSWORD', 'bar')

      expect(described_class).not_to be_forker
    end

    it 'returns true if forker username and password are defined' do
      stub_env('GITLAB_FORKER_USERNAME', 'foo')
      stub_env('GITLAB_FORKER_PASSWORD', 'bar')

      expect(described_class).to be_forker
    end
  end

  describe '.github_access_token' do
    it 'returns "" if GITHUB_ACCESS_TOKEN is not defined' do
      stub_env('GITHUB_ACCESS_TOKEN', nil)

      expect(described_class.github_access_token).to eq('')
    end

    it 'returns stripped string if GITHUB_ACCESS_TOKEN is defined' do
      stub_env('GITHUB_ACCESS_TOKEN', ' abc123 ')
      expect(described_class.github_access_token).to eq('abc123')
    end
  end

  describe '.require_github_access_token!' do
    it 'raises ArgumentError if GITHUB_ACCESS_TOKEN is not defined' do
      stub_env('GITHUB_ACCESS_TOKEN', nil)

      expect { described_class.require_github_access_token! }.to raise_error(ArgumentError)
    end

    it 'does not raise if GITHUB_ACCESS_TOKEN is defined' do
      stub_env('GITHUB_ACCESS_TOKEN', ' abc123 ')

      expect { described_class.require_github_access_token! }.not_to raise_error
    end
  end

  describe '.log_destination' do
    it 'returns $stdout if QA_LOG_PATH is not defined' do
      stub_env('QA_LOG_PATH', nil)

      expect(described_class.log_destination).to eq($stdout)
    end

    it 'returns the path if QA_LOG_PATH is defined' do
      stub_env('QA_LOG_PATH', 'path/to_file')

      expect(described_class.log_destination).to eq('path/to_file')
    end
  end

  describe '.can_test?' do
    it_behaves_like 'boolean method with parameter',
      method: :can_test?,
      param: :git_protocol_v2,
      env_key: 'QA_CAN_TEST_GIT_PROTOCOL_V2',
      default: true

    it 'raises ArgumentError if feature is unknown' do
      expect { described_class.can_test? :foo }.to raise_error(ArgumentError, 'Unknown feature "foo"')
    end
  end

  describe 'remote grid credentials' do
    it 'is blank if username is empty' do
      stub_env('QA_REMOTE_GRID_USERNAME', nil)

      expect(described_class.send(:remote_grid_credentials)).to eq('')
    end

    it 'throws ArgumentError if GRID_ACCESS_KEY is not specified with USERNAME' do
      stub_env('QA_REMOTE_GRID_USERNAME', 'foo')

      expect { described_class.send(:remote_grid_credentials) }.to raise_error(ArgumentError, 'Please provide an access key for user "foo"')
    end

    it 'returns a user:key@ combination when all args are satiated' do
      stub_env('QA_REMOTE_GRID_USERNAME', 'foo')
      stub_env('QA_REMOTE_GRID_ACCESS_KEY', 'bar')

      expect(described_class.send(:remote_grid_credentials)).to eq('foo:bar@')
    end
  end

  describe '.remote_grid_protocol' do
    it 'defaults protocol to http' do
      stub_env('QA_REMOTE_GRID_PROTOCOL', nil)
      expect(described_class.remote_grid_protocol).to eq('http')
    end
  end

  describe '.remote_grid' do
    it 'is falsey if QA_REMOTE_GRID is not set' do
      expect(described_class.remote_grid).to be_falsey
    end

    it 'accepts https protocol' do
      stub_env('QA_REMOTE_GRID', 'localhost:4444')
      stub_env('QA_REMOTE_GRID_PROTOCOL', 'https')

      expect(described_class.remote_grid).to eq('https://localhost:4444/wd/hub')
    end

    context 'with credentials' do
      it 'has a grid of http://user:key@grid/wd/hub' do
        stub_env('QA_REMOTE_GRID_USERNAME', 'foo')
        stub_env('QA_REMOTE_GRID_ACCESS_KEY', 'bar')
        stub_env('QA_REMOTE_GRID', 'localhost:4444')

        expect(described_class.remote_grid).to eq('http://foo:bar@localhost:4444/wd/hub')
      end
    end

    context 'without credentials' do
      it 'has a grid of http://grid/wd/hub' do
        stub_env('QA_REMOTE_GRID', 'localhost:4444')

        expect(described_class.remote_grid).to eq('http://localhost:4444/wd/hub')
      end
    end
  end
end