Skip to content

Commit aab151b

Browse files
committed
Avoid using global variables
Global variables are unspecified and unreliable in where they come from. Instead, built in variables like $facts, $trusted and $server_facts are better sources.
1 parent 9421bd4 commit aab151b

File tree

6 files changed

+21
-32
lines changed

6 files changed

+21
-32
lines changed

manifests/config.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
}
4444
if $use_srv_records {
4545
unless $srv_domain {
46-
fail('$::domain fact found to be undefined and $srv_domain is undefined')
46+
fail('domain fact found to be undefined and $srv_domain is undefined')
4747
}
4848
puppet::config::main{
4949
'use_srv_records': value => true;

manifests/params.pp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,15 @@
3434
$dns_alt_names = []
3535
$use_srv_records = false
3636

37-
if defined('$::domain') {
38-
$srv_domain = $facts['networking']['domain']
39-
} else {
40-
$srv_domain = undef
41-
}
37+
$srv_domain = fact('networking.domain')
4238

4339
# lint:ignore:puppet_url_without_modules
4440
$pluginsource = 'puppet:///plugins'
4541
$pluginfactsource = 'puppet:///pluginfacts'
4642
# lint:endignore
4743
$classfile = '$statedir/classes.txt'
4844
$syslogfacility = undef
49-
$environment = $::environment
45+
$environment = $server_facts['environment']
5046

5147
# aio_agent_version is a core fact that's empty on non-AIO
5248
$aio_package = fact('aio_agent_version') =~ String[1]
@@ -199,13 +195,10 @@
199195

200196
# Will this host be a puppet agent ?
201197
$agent = true
202-
$client_certname = $::clientcert
198+
$client_certname = $trusted['certname']
203199

204-
if defined('$::puppetmaster') {
205-
$puppetmaster = $::puppetmaster
206-
} else {
207-
$puppetmaster = undef
208-
}
200+
# Set by the Foreman ENC
201+
$puppetmaster = getvar('puppetmaster')
209202

210203
# Hashes containing additional settings
211204
$additional_settings = {}
@@ -220,7 +213,7 @@
220213
$server_external_nodes = "${dir}/node.rb"
221214
$server_trusted_external_command = undef
222215
$server_request_timeout = 60
223-
$server_certname = $::clientcert
216+
$server_certname = $trusted['certname']
224217
$server_strict_variables = false
225218
$server_http = false
226219
$server_http_port = 8139
@@ -262,7 +255,7 @@
262255

263256
$server_storeconfigs = false
264257

265-
$puppet_major = regsubst($::puppetversion, '^(\d+)\..*$', '\1')
258+
$puppet_major = regsubst($facts['puppetversion'], '^(\d+)\..*$', '\1')
266259

267260
if ($facts['os']['family'] =~ /(FreeBSD|DragonFly)/) {
268261
$server_package = "puppetserver${puppet_major}"

manifests/server/config.pp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,19 @@
173173
command => "${puppet::puppetserver_cmd} ca migrate",
174174
creates => $puppet::server::cadir,
175175
onlyif => "test -d '${puppet::server::ssl_dir}/ca' && ! test -L '${puppet::server::ssl_dir}'",
176-
path => $::path,
176+
path => $facts['path'],
177177
before => Exec['puppet_server_config-generate_ca_cert'],
178178
}
179179
}
180180
} elsif $puppet::server::ca_crl_sync {
181181
# If not a ca AND sync the crl from the ca master
182-
if defined('$::servername') {
182+
if $server_facts['servername'] {
183183
file { $puppet::server::ssl_ca_crl:
184184
ensure => file,
185185
owner => $puppet::server::user,
186186
group => $puppet::server::group,
187187
mode => '0644',
188-
content => file($::settings::cacrl, $::settings::hostcrl, '/dev/null'),
188+
content => file($settings::cacrl, $settings::hostcrl, '/dev/null'),
189189
}
190190
}
191191
}

spec/classes/puppet_config_spec.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
context 'domain fact is unset' do
129129
let(:facts) { override_facts(super(), networking: {domain: nil}) }
130130

131-
it { is_expected.to raise_error(Puppet::Error, /\$::domain fact found to be undefined and \$srv_domain is undefined/) }
131+
it { is_expected.to raise_error(Puppet::Error, /domain fact found to be undefined and \$srv_domain is undefined/) }
132132
end
133133

134134
context 'is overriden via param' do
@@ -142,12 +142,9 @@
142142
end
143143

144144
describe 'client_certname' do
145-
context 'with client_certname => $::clientcert' do
146-
let :facts do
147-
# rspec-puppet(-facts) doesn't mock this
148-
super().merge(clientcert: 'client.example.com')
149-
end
145+
let(:node) { 'client.example.com' }
150146

147+
context 'with client_certname => trusted certname' do
151148
it { is_expected.to contain_puppet__config__main('certname').with_value('client.example.com') }
152149
end
153150

spec/classes/puppet_server_spec.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,11 @@
170170
let(:facts) do
171171
override_facts(super(),
172172
networking: {fqdn: 'PUPPETMASTER.example.com'},
173-
# clientcert is always lowercase by Puppet design
174-
clientcert: 'puppetmaster.example.com'
175173
)
176174
end
177175

178176
it { should compile.with_all_deps }
179-
180-
it 'should use lowercase certificates' do
181-
should contain_class('puppet::server::puppetserver')
182-
.with_server_ssl_cert("#{ssldir}/certs/puppetmaster.example.com.pem")
183-
.with_server_ssl_cert_key("#{ssldir}/private_keys/puppetmaster.example.com.pem")
184-
end
177+
it { should contain_class('puppet').with_server_foreman_url('https://puppetmaster.example.com') }
185178
end
186179

187180
describe 'with ip parameter' do
@@ -503,6 +496,8 @@
503496
end
504497

505498
it 'should not sync the crl' do
499+
# https://github.com/puppetlabs/rspec-puppet/issues/37
500+
pending("rspec-puppet always sets $server_facts['servername']")
506501
should_not contain_file('/etc/custom/puppetlabs/puppet/ssl/crl.pem')
507502
end
508503
end

spec/support/trusted.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RSpec.configure do |c|
2+
c.trusted_node_data = true
3+
c.trusted_server_facts = true
4+
end

0 commit comments

Comments
 (0)