-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathrun_mode_spec.rb
More file actions
294 lines (241 loc) · 10.6 KB
/
run_mode_spec.rb
File metadata and controls
294 lines (241 loc) · 10.6 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
require 'spec_helper'
describe Puppet::Util::RunMode do
before do
@run_mode = Puppet::Util::RunMode.new('fake')
end
describe Puppet::Util::UnixRunMode, :unless => Puppet::Util::Platform.windows? do
before do
@run_mode = Puppet::Util::UnixRunMode.new('fake')
end
describe "#conf_dir" do
it "has confdir /etc/puppetlabs/puppet when run as root" do
allow(@run_mode).to receive(:find_or_first).and_return('/etc/puppetlabs/puppet')
as_root { expect(@run_mode.conf_dir).to eq(File.expand_path('/etc/puppetlabs/puppet')) }
end
it "has confdir ~/.puppetlabs/etc/puppet when run as non-root" do
as_non_root { expect(@run_mode.conf_dir).to eq(File.expand_path('~/.puppetlabs/etc/puppet')) }
end
context "server run mode" do
before do
@run_mode = Puppet::Util::UnixRunMode.new('server')
end
it "has confdir ~/.puppetlabs/etc/puppet when run as non-root and server run mode" do
as_non_root { expect(@run_mode.conf_dir).to eq(File.expand_path('~/.puppetlabs/etc/puppet')) }
end
end
end
describe "#code_dir" do
it "has codedir /etc/puppetlabs/code when run as root" do
allow(@run_mode).to receive(:find_or_first).and_return('/etc/puppetlabs/code')
as_root { expect(@run_mode.code_dir).to eq(File.expand_path('/etc/puppetlabs/code')) }
end
it "has codedir ~/.puppetlabs/etc/code when run as non-root" do
as_non_root { expect(@run_mode.code_dir).to eq(File.expand_path('~/.puppetlabs/etc/code')) }
end
context "server run mode" do
before do
@run_mode = Puppet::Util::UnixRunMode.new('server')
end
it "has codedir ~/.puppetlabs/etc/code when run as non-root and server run mode" do
as_non_root { expect(@run_mode.code_dir).to eq(File.expand_path('~/.puppetlabs/etc/code')) }
end
end
end
describe "#var_dir" do
it "has vardir /opt/puppetlabs/puppet/cache when run as root" do
allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/cache')
as_root { expect(@run_mode.var_dir).to eq(File.expand_path('/opt/puppetlabs/puppet/cache')) }
end
it "has vardir ~/.puppetlabs/opt/puppet/cache when run as non-root" do
as_non_root { expect(@run_mode.var_dir).to eq(File.expand_path('~/.puppetlabs/opt/puppet/cache')) }
end
end
describe "#public_dir" do
it "has publicdir /opt/puppetlabs/puppet/public when run as root" do
allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/public')
as_root { expect(@run_mode.public_dir).to eq(File.expand_path('/opt/puppetlabs/puppet/public')) }
end
it "has publicdir ~/.puppetlabs/opt/puppet/public when run as non-root" do
as_non_root { expect(@run_mode.public_dir).to eq(File.expand_path('~/.puppetlabs/opt/puppet/public')) }
end
end
describe "#log_dir" do
describe "when run as root" do
it "has logdir /var/log/puppetlabs/puppet" do
allow(@run_mode).to receive(:find_or_first).and_return('/var/log/puppetlabs/puppet')
as_root { expect(@run_mode.log_dir).to eq(File.expand_path('/var/log/puppetlabs/puppet')) }
end
end
describe "when run as non-root" do
it "has default logdir ~/.puppetlabs/var/log" do
as_non_root { expect(@run_mode.log_dir).to eq(File.expand_path('~/.puppetlabs/var/log')) }
end
end
end
describe "#run_dir" do
describe "when run as root" do
it "has rundir /var/run/puppetlabs" do
allow(@run_mode).to receive(:find_or_first).and_return('/var/run/puppetlabs')
as_root { expect(@run_mode.run_dir).to eq(File.expand_path('/var/run/puppetlabs')) }
end
end
describe "when run as non-root" do
it "has default rundir ~/.puppetlabs/var/run" do
as_non_root { expect(@run_mode.run_dir).to eq(File.expand_path('~/.puppetlabs/var/run')) }
end
end
end
describe "#pkg_config_path" do
it "has pkg config path /opt/puppetlabs/puppet/lib/pkgconfig" do
allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/lib/pkgconfig')
expect(@run_mode.pkg_config_path).to eq('/opt/puppetlabs/puppet/lib/pkgconfig')
end
end
describe "#gem_cmd" do
it "has gem cmd /opt/puppetlabs/puppet/bin/gem" do
allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/bin/gem')
expect(@run_mode.gem_cmd).to eq('/opt/puppetlabs/puppet/bin/gem')
end
end
describe "#common_module_dir" do
it "has common module dir /opt/puppetlabs/puppet/modules" do
allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/modules')
expect(@run_mode.common_module_dir).to eq('/opt/puppetlabs/puppet/modules')
end
end
describe "#vendor_module_dir" do
it "has vendor module dir /opt/puppetlabs/puppet/vendor_modules" do
allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/vendor_modules')
expect(@run_mode.vendor_module_dir).to eq('/opt/puppetlabs/puppet/vendor_modules')
end
end
end
describe Puppet::Util::WindowsRunMode, :if => Puppet::Util::Platform.windows? do
before do
@run_mode = Puppet::Util::WindowsRunMode.new('fake')
end
describe "#conf_dir" do
it "has confdir ending in Puppetlabs/puppet/etc when run as root" do
as_root { expect(@run_mode.conf_dir).to eq(File.expand_path(File.join(ENV['ALLUSERSPROFILE'], "PuppetLabs", "puppet", "etc"))) }
end
it "has confdir in ~/.puppetlabs/etc/puppet when run as non-root" do
as_non_root { expect(@run_mode.conf_dir).to eq(File.expand_path("~/.puppetlabs/etc/puppet")) }
end
end
describe "#code_dir" do
it "has codedir ending in PuppetLabs/code when run as root" do
as_root { expect(@run_mode.code_dir).to eq(File.expand_path(File.join(ENV['ALLUSERSPROFILE'], "PuppetLabs", "code"))) }
end
it "has codedir in ~/.puppetlabs/etc/code when run as non-root" do
as_non_root { expect(@run_mode.code_dir).to eq(File.expand_path("~/.puppetlabs/etc/code")) }
end
end
describe "#var_dir" do
it "has vardir ending in PuppetLabs/puppet/cache when run as root" do
as_root { expect(@run_mode.var_dir).to eq(File.expand_path(File.join(ENV['ALLUSERSPROFILE'], "PuppetLabs", "puppet", "cache"))) }
end
it "has vardir in ~/.puppetlabs/opt/puppet/cache when run as non-root" do
as_non_root { expect(@run_mode.var_dir).to eq(File.expand_path("~/.puppetlabs/opt/puppet/cache")) }
end
end
describe "#public_dir" do
it "has publicdir ending in PuppetLabs/puppet/public when run as root" do
as_root { expect(@run_mode.public_dir).to eq(File.expand_path(File.join(ENV['ALLUSERSPROFILE'], "PuppetLabs", "puppet", "public"))) }
end
it "has publicdir in ~/.puppetlabs/opt/puppet/public when run as non-root" do
as_non_root { expect(@run_mode.public_dir).to eq(File.expand_path("~/.puppetlabs/opt/puppet/public")) }
end
end
describe "#log_dir" do
describe "when run as root" do
it "has logdir ending in PuppetLabs/puppet/var/log" do
as_root { expect(@run_mode.log_dir).to eq(File.expand_path(File.join(ENV['ALLUSERSPROFILE'], "PuppetLabs", "puppet", "var", "log"))) }
end
end
describe "when run as non-root" do
it "has default logdir ~/.puppetlabs/var/log" do
as_non_root { expect(@run_mode.log_dir).to eq(File.expand_path('~/.puppetlabs/var/log')) }
end
end
end
describe "#run_dir" do
describe "when run as root" do
it "has rundir ending in PuppetLabs/puppet/var/run" do
as_root { expect(@run_mode.run_dir).to eq(File.expand_path(File.join(ENV['ALLUSERSPROFILE'], "PuppetLabs", "puppet", "var", "run"))) }
end
end
describe "when run as non-root" do
it "has default rundir ~/.puppetlabs/var/run" do
as_non_root { expect(@run_mode.run_dir).to eq(File.expand_path('~/.puppetlabs/var/run')) }
end
end
end
describe '#gem_cmd' do
before do
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with('PUPPET_DIR', nil).and_return(puppetdir)
end
context 'when PUPPET_DIR is not set' do
let(:puppetdir) { nil }
before do
allow(Gem).to receive(:default_bindir).and_return('default_gem_bin')
end
it 'uses Gem.default_bindir' do
expected_path = File.join('default_gem_bin', 'gem.bat')
expect(@run_mode.gem_cmd).to eql(expected_path)
end
end
context 'when PUPPET_DIR is set' do
let(:puppetdir) { 'puppet_dir' }
it 'uses Gem.default_bindir' do
expected_path = File.join('puppet_dir', 'bin', 'gem.bat')
expect(@run_mode.gem_cmd).to eql(expected_path)
end
end
end
describe '#common_module_dir' do
before do
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with('FACTER_env_windows_installdir', nil).and_return(installdir)
end
context 'when installdir is not set' do
let(:installdir) { nil }
it 'returns nil' do
expect(@run_mode.common_module_dir).to be(nil)
end
end
context 'with installdir' do
let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' }
it 'returns INSTALLDIR/puppet/modules' do
expect(@run_mode.common_module_dir).to eq('C:\Program Files\Puppet Labs\Puppet/puppet/modules')
end
end
end
describe '#vendor_module_dir' do
before do
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with('FACTER_env_windows_installdir', nil).and_return(installdir)
end
context 'when installdir is not set' do
let(:installdir) { nil }
it 'returns nil' do
expect(@run_mode.vendor_module_dir).to be(nil)
end
end
context 'with installdir' do
let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' }
it 'returns INSTALLDIR\puppet\vendor_modules' do
expect(@run_mode.vendor_module_dir).to eq('C:\Program Files\Puppet Labs\Puppet\puppet\vendor_modules')
end
end
end
end
def as_root
allow(Puppet.features).to receive(:root?).and_return(true)
yield
end
def as_non_root
allow(Puppet.features).to receive(:root?).and_return(false)
yield
end
end