We are using Puppet(v3.6.2) with the Foreman enc and have a lot of custom modules with the pattern demonstrated in the following illustration module;
#mkdir/manifests/init.pp
class mkdir ($path, $mode) {
class {'mkdir::file': }
}
#mkdir/manifests/file.pp
class mkdir::file {
file {$mkdir::path:
ensure => 'directory',
mode => $mkdir::mode,
}
}
#mkdir/spec/classes/mkdir_spec.rb
require 'spec_helper'
describe 'mkdir' do
let(:params) {{ :path=>'/foo', :mode=>'777' }}
it { should contain_class( 'mkdir::file' ) }
end
# All good so far, but here's the rub
# mkdir/spec/classes/file_spec.rb
require 'spec_helper'
describe 'mkdir::file' do
# how can I set $mkdir::path and $mkdir::mode???
let(:params) {{ :path=>'/bar', :mode=>'555' }}
it { should contain_file('/bar').with({
mode=>'555' })
}
end
As the code comment says, how module parameter $mkdir::path and $mkdir::mode be set within the file_spec test?
Obviously the module could be refactored to pass all needed parameters to each subclass, and that's what I'll do if it's the only way, but it seems unlikely that there isn't a way to test this pattern as it stands.
I have also seen lots of examples using a module::params pattern, and this is certainly a clean pattern for installations using hiera, but I haven't been able to make that pattern work for Foreman, and at best it would require including two puppet classes per parameterised module which is ugly.
Your conclusions are about right. But the
params
class pattern is independent of your scheme of node level configuration. It might be possible to tie Hiera into it more tightly, but here is what you generally want to do:params
class. Defaults might depend on Fact values such as$osfamily
.inherit
theparams
class and use the variables declared inparams
as default values for the module class parameters.params
class where appropriate.For example:
Writing tests should be very straight forward then.