forked from OpenVoxProject/openbolt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_spec.rb
More file actions
278 lines (219 loc) · 9.29 KB
/
config_spec.rb
File metadata and controls
278 lines (219 loc) · 9.29 KB
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
# frozen_string_literal: true
require 'spec_helper'
require 'addressable/uri'
require 'bolt/puppetdb/config'
require 'bolt/util'
describe Bolt::PuppetDB::Config do
context "with project available" do
let(:cacert) { File.expand_path('relative/to/cacert') }
let(:token) { File.expand_path('relative/to/token') }
let(:project) { '~/dirbolt' }
let(:options) do
{
'server_urls' => ['https://puppetdb:8081'],
'cacert' => cacert,
'token' => token
}
end
let(:config) { Bolt::PuppetDB::Config.new(config: options, project: project) }
it 'expands the cacert relative to the project if project is available' do
allow(config).to receive(:validate_file_exists).with('cacert').and_return true
expect(config.cacert).to eq(File.expand_path(cacert, project))
end
end
context "when validating that options" do
let(:cacert) { File.expand_path('/path/to/cacert') }
let(:token) { File.expand_path('/path/to/token') }
let(:cert) { File.expand_path('/path/to/cert') }
let(:key) { File.expand_path('/path/to/key') }
let(:connect_timeout) { 120 }
let(:read_timeout) { 120 }
let(:options) do
{
'server_urls' => ['https://puppetdb:8081'],
'cacert' => cacert,
'token' => token,
'cert' => cert,
'key' => key,
'connect_timeout' => connect_timeout,
'read_timeout' => read_timeout
}
end
let(:config) { Bolt::PuppetDB::Config.new(config: options) }
context "#uri" do
it 'uses server_urls value if it is a string' do
expect(config.uri).to eq(Addressable::URI.parse('https://puppetdb:8081'))
end
it 'uses the first item of server_urls when it is an array' do
options['server_urls'] = ['https://puppetdb:8081', 'https://shmuppetdb:8082']
expect(config.uri).to eq(Addressable::URI.parse('https://puppetdb:8081'))
end
it 'defaults to port 8081 if no port is specified' do
options['server_urls'] = ['https://puppetdb']
expect(config.uri).to eq(Addressable::URI.parse('https://puppetdb:8081'))
end
it 'fails if server_urls is not set' do
options.delete('server_urls')
expect { config.uri }.to raise_error(Bolt::PuppetDBError, /server_urls must be specified/)
end
end
context "token" do
context "token is valid" do
before :each do
options.delete('cert')
options.delete('key')
allow(File).to receive(:read).with(token).and_return 'footoken'
allow(File).to receive(:read).with(Bolt::PuppetDB::Config::DEFAULT_TOKEN).and_return 'bartoken'
end
it 'loads the token file if one was specified' do
expect(config.token).to eq('footoken')
end
it 'loads the default token if no file was specified' do
allow(File).to receive(:exist?).with(Bolt::PuppetDB::Config::DEFAULT_TOKEN).and_return true
options.delete('token')
expect(config.token).to eq('bartoken')
end
it 'returns nil if no file was specified and the default does not exist' do
allow(File).to receive(:exist?).with(Bolt::PuppetDB::Config::DEFAULT_TOKEN).and_return false
options.delete('token')
expect(config.token).to be_nil
end
end
context "token is invalid" do
before :each do
options.delete('cert')
options.delete('key')
allow(File).to receive(:read).with(token).and_return "footoken\n"
allow(File).to receive(:read).with(Bolt::PuppetDB::Config::DEFAULT_TOKEN).and_return "bartoken\n"
end
it 'loads and strips the token file if one was specified' do
expect(config.token).to eq('footoken')
end
it 'loads and strips the default token if no file was specified' do
allow(File).to receive(:exist?).with(Bolt::PuppetDB::Config::DEFAULT_TOKEN).and_return true
options.delete('token')
expect(config.token).to eq('bartoken')
end
end
context "both token and cert" do
it "returns nil for token when cert is configured" do
allow(config).to receive(:validate_file_exists).with('cert').and_return true
allow(File).to receive(:read).with(token).and_return 'footoken'
expect(config.token).to be_nil
end
end
end
context "cacert" do
it 'returns the cacert if it is set and exists' do
allow(config).to receive(:validate_file_exists).with('cacert').and_return true
expect(config.cacert).to eq(cacert)
end
it 'fails if the cacert is not set' do
options.delete('cacert')
expect { config.cacert }.to raise_error(Bolt::PuppetDBError, /cacert must be specified/)
end
it 'fails if the cacert does not exist' do
allow(File).to receive(:exist?).with(cacert).and_return false
expect { config.cacert }.to raise_error(Bolt::PuppetDBError, /cacert file .* does not exist/)
end
end
context "cert" do
it 'fails if cert is set but key is not set' do
options.delete('key')
expect { config.cert }.to raise_error(Bolt::PuppetDBError, /cert and key must be specified together/)
end
it 'returns nil if cert and key are both nil' do
options.delete('cert')
options.delete('key')
expect(config.cert).to be_nil
end
it 'returns cert if cert and key are both set' do
allow(config).to receive(:validate_file_exists).with('cert').and_return true
expect(config.cert).to eq(cert)
end
it 'fails if the cert does not exist' do
allow(File).to receive(:exist?).with(cert).and_return false
expect { config.cert }.to raise_error(Bolt::PuppetDBError, /cert file .* does not exist/)
end
end
context "key" do
it 'fails if key is set but cert is not set' do
options.delete('cert')
expect { config.key }.to raise_error(Bolt::PuppetDBError, /cert and key must be specified together/)
end
it 'returns nil if cert and key are both nil' do
options.delete('cert')
options.delete('key')
expect(config.key).to be_nil
end
it 'returns key if cert and key are both set' do
allow(config).to receive(:validate_file_exists).with('key').and_return true
expect(config.key).to eq(key)
end
it 'fails if the key does not exist' do
allow(File).to receive(:exist?).with(key).and_return false
expect { config.key }.to raise_error(Bolt::PuppetDBError, /key file .* does not exist/)
end
end
%w[read_timeout connect_timeout].each do |timeout|
context timeout do
it 'fails if not an integer' do
options[timeout] = '120'
expect { config.send(timeout.to_sym) }.to raise_error(
Bolt::PuppetDBError, /#{timeout} must be a positive integer/
)
end
it 'fails if not a positive integer' do
options[timeout] = 0
expect { config.send(timeout.to_sym) }.to raise_error(
Bolt::PuppetDBError, /#{timeout} must be a positive integer/
)
end
it 'returns nil if not set' do
options.delete(timeout)
expect(config.send(timeout.to_sym)).to eq(nil)
end
it 'returns value if set' do
expect(config.send(timeout.to_sym)).to eq(options[timeout])
end
end
end
end
context "load_defaults" do
it "on non-windows OS loads from default location" do
allow(Bolt::Util).to receive(:windows?).and_return(false)
expect(File).to receive(:exist?).with(Bolt::PuppetDB::Config::DEFAULT_CONFIG[:user])
expect(File).to receive(:exist?).with(Bolt::PuppetDB::Config::DEFAULT_CONFIG[:global])
Bolt::PuppetDB::Config.new(config: {}, load_defaults: true)
end
it "on windows OS loads from default location", if: Bolt::Util.windows? do
allow(Bolt::Util).to receive(:windows?).and_return(true)
expect(File).to receive(:exist?).with(Bolt::PuppetDB::Config::DEFAULT_CONFIG[:user])
expect(File).to receive(:exist?).with(Bolt::PuppetDB::Config.default_windows_config)
Bolt::PuppetDB::Config.new(config: {}, load_defaults: true)
end
it "Does not error if puppetdb.conf fails to load" do
allow(Bolt::Util).to receive(:windows?).and_return(false)
expect(File).to receive(:exist?).with(Bolt::PuppetDB::Config::DEFAULT_CONFIG[:user]).and_return true
expect(File).to receive(:read).with(Bolt::PuppetDB::Config::DEFAULT_CONFIG[:user]).and_return 'bad"json'
expect(JSON).to receive(:parse).and_raise(JSON::ParserError.new("unexpected token"))
Bolt::PuppetDB::Config.new(config: {}, load_defaults: true)
end
end
context 'when validating headers' do
let(:options) { { 'server_urls' => ['https://puppetdb:8081'], 'headers' => headers } }
let(:config) { Bolt::PuppetDB::Config.new(config: options) }
context 'with valid headers' do
let(:headers) { { 'Authorization' => 'Bearer token' } }
it 'returns the headers' do
expect(config.headers).to eq(headers)
end
end
context 'with invalid headers' do
let(:headers) { 'Authorization: Bearer token' }
it 'raises an error' do
expect { config.headers }.to raise_error(Bolt::PuppetDBError, "headers must be a Hash")
end
end
end
end