diff --git a/manifests/config.pp b/manifests/config.pp index 5e076c6..439740a 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -1,10 +1,11 @@ # class autofs::config inherits autofs { + autofs::mapfile { $autofs::master_config: directory => undef, } - create_resources('autofs::includes', $autofs::includes) + create_resources('autofs::include', $autofs::includes) create_resources('autofs::mapfile', $autofs::mapfiles) create_resources('autofs::mount', $autofs::mounts) -} \ No newline at end of file +} diff --git a/manifests/include.pp b/manifests/include.pp index 06f81e2..3b84368 100644 --- a/manifests/include.pp +++ b/manifests/include.pp @@ -1,11 +1,12 @@ -define autofs::include( - $mapfile = $name, - $order = undef +# +define autofs::include ( + String $mapfile = $title, + Optional[String] $order = undef ) { include ::autofs if $mapfile == $autofs::master_config { - fail("${autofs::mapfile_config_dir}/${autofs::master_config} can't include itself!") + fail("${autofs::map_config_dir}/${autofs::master_config} can't include itself!") } concat::fragment { "${autofs::master_config}/${mapfile}": @@ -13,4 +14,4 @@ content => "+${mapfile}", order => $order; } -} \ No newline at end of file +} diff --git a/manifests/init.pp b/manifests/init.pp index 4a1ff77..571f146 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,38 +1,25 @@ -# == Define: autofs +# Class: autofs -class autofs( - $config_file_owner = $autofs::params::config_file_owner, - $config_file_group = $autofs::params::config_file_group, - $master_config = $autofs::params::master_config, - $map_config_dir = $autofs::params::map_config_dir, - $package_manage = $autofs::params::package_manage, - $package_name = $autofs::params::package_name, - $package_ensure = $autofs::params::package_ensure, - $service_name = $autofs::params::service_name, - $service_manage = $autofs::params::service_manage, - $service_hasrestart = $autofs::params::service_hasrestart, - $service_hasstatus = $autofs::params::service_hasstatus, - $service_ensure = $autofs::params::service_ensure, - $service_enable = $autofs::params::service_enable, - $service_restart = $autofs::params::service_restart, - $mapfiles = {}, - $mounts = {}, - $includes = {}, +class autofs ( + String $config_file_owner = $autofs::params::config_file_owner, + String $config_file_group = $autofs::params::config_file_group, + String $master_config = $autofs::params::master_config, + Stdlib::Absolutepath $map_config_dir = $autofs::params::map_config_dir, + Boolean $package_manage = $autofs::params::package_manage, + Array[String] $package_name = $autofs::params::package_name, + String $package_ensure = $autofs::params::package_ensure, + String $service_name = $autofs::params::service_name, + Boolean $service_manage = $autofs::params::service_manage, + Boolean $service_hasrestart = $autofs::params::service_hasrestart, + Boolean $service_hasstatus = $autofs::params::service_hasstatus, + Variant[Enum[running, stopped], Boolean] + $service_ensure = $autofs::params::service_ensure, + Boolean $service_enable = $autofs::params::service_enable, + Optional[String] $service_restart = $autofs::params::service_restart, + Hash $mapfiles = {}, + Hash $mounts = {}, + Hash $includes = {}, ) inherits autofs::params { - validate_string($master_config) - validate_absolute_path($map_config_dir) - validate_bool($package_manage) - validate_string($package_ensure) - validate_string($service_name) - validate_bool($service_manage) - validate_bool($service_hasrestart) - validate_bool($service_hasstatus) - validate_string($service_ensure) - validate_bool($service_enable) - validate_string($service_restart) - validate_hash($mapfiles) - validate_hash($mounts) - validate_hash($includes) # Anchor this as per #8040 - this ensures that classes won't float off and # mess everything up. You can read about this at: diff --git a/manifests/install.pp b/manifests/install.pp index 71dc392..2e1cbe4 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -6,4 +6,4 @@ ensure => $autofs::package_ensure, } } -} \ No newline at end of file +} diff --git a/manifests/mapfile.pp b/manifests/mapfile.pp index 4b73a18..7d566ce 100644 --- a/manifests/mapfile.pp +++ b/manifests/mapfile.pp @@ -1,42 +1,29 @@ # -define autofs::mapfile( - $directory, - $ensure = present, - $mapfile = $name, - $options = undef, - $order = undef, - $maptype = 'file' , - $mounts = {} +define autofs::mapfile ( + Optional[Stdlib::Absolutepath] $directory, + Enum[present, absent, purged] $ensure = present, + String $mapfile = $title, + Optional[String] $options = undef, + Optional[String] $order = undef, + String $maptype = file, + Hash $mounts = {} ) { - include ::autofs - validate_string($mapfile) - validate_string($options) - validate_hash($mounts) - - validate_re($ensure, '^present$|^absent$|^purged$', 'ensure must be one of: present, absent, or purged') - - # surround $supported_map_types list items with ^...$ for regular expression matching - $supported_map_types_re = regsubst($::autofs::supported_map_types, '^.*$', '^\0$') - # join $supported_map_types list with commas, put an 'or' before the - # last supported type, and remove the comma if only two items in list. - $supported_map_types_str = regsubst(regsubst(join($::autofs::supported_map_types, ', '), ', ([^,]*)$', ', or \1'), '^([^,]*),( or [^,]*)$', '\1\2') - # check $maptype is valid - validate_re($maptype, $supported_map_types_re, "maptype must be one of: ${supported_map_types_str}") + if !($maptype in $::autofs::supported_map_types) { + fail("maptype parameter must be one of ${::autofs::supported_map_types}") + } # $mapfile_prefix is equal to "${maptype}:", _unless_: # 1) $maptype == 'file' # 2) $use_map_prefix is false - if $maptype != 'file' and $::autofs::use_map_prefix { + if $maptype != file and $::autofs::use_map_prefix { $mapfile_prefix = "${maptype}:" } else { $mapfile_prefix = '' } - if $mapfile != $autofs::master_config { - validate_absolute_path($directory) - + if $mapfile != $autofs::master_config and $directory != undef { if $ensure == present { concat::fragment { "${autofs::master_config}/${mapfile}": target => $autofs::master_config, @@ -59,7 +46,7 @@ } # Only create the mapfile and any mounts if $maptype == file - if $maptype == 'file' { + if $maptype == file { concat { $mapfile: ensure => $concat_ensure, owner => $autofs::config_file_owner, @@ -77,3 +64,4 @@ }) } } + diff --git a/manifests/mount.pp b/manifests/mount.pp index 12963ea..913e8c7 100644 --- a/manifests/mount.pp +++ b/manifests/mount.pp @@ -1,24 +1,16 @@ # define autofs::mount( - $mapfile, - $map, - $mount = $name, - $ensure = 'present', - $options = '-rw', - $order = undef, + String $mapfile, + String $map, + String $mount = $title, + Enum[present, absent] $ensure = present, + Autofs::MountOptions $options = '-rw', + Optional[String] $order = undef, ) { - validate_string($mapfile) - validate_string($map) - validate_string($mount) - validate_string($options) - - validate_re($ensure, '^present$|^absent$', 'ensure must be one of: present or absen') - validate_re($options, '^-', 'options must start with a hypen') - include ::autofs if $mapfile == $autofs::master_config { - fail("You can't add mounts directly to ${autofs::mapfile_config_dir}/${autofs::master_config}!") + fail("You can't add mounts directly to ${autofs::map_config_dir}/${autofs::master_config}!") } if $ensure == 'present' { @@ -28,4 +20,4 @@ order => $order; } } -} \ No newline at end of file +} diff --git a/manifests/service.pp b/manifests/service.pp index 76c07d7..4f7edfc 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -1,8 +1,5 @@ # class autofs::service inherits autofs { - if ! ($autofs::service_ensure in [ 'running', 'stopped' ]) { - fail('service_ensure parameter must be running or stopped') - } if $autofs::service_manage == true { service { 'autofs': @@ -14,4 +11,4 @@ restart => $autofs::service_restart; } } -} \ No newline at end of file +} diff --git a/metadata.json b/metadata.json index 2d8a3ca..261a7c1 100644 --- a/metadata.json +++ b/metadata.json @@ -10,11 +10,11 @@ "dependencies": [ { "name": "puppetlabs/stdlib", - "version_requirement": ">= 3.0.0 < 5.0.0" + "version_requirement": ">= 4.0.0 < 5.0.0" }, { "name": "puppetlabs/concat", - "version_requirement": ">= 1.0.4 < 3.0.0" + "version_requirement": ">= 4.0.0 < 5.0.0" } ], "operatingsystem_support": [ @@ -76,7 +76,7 @@ "requirements": [ { "name": "puppet", - "version_requirement": ">= 3.2.0 < 5.0.0" + "version_requirement": ">= 4.7.0 < 6.0.0" } ], "tags": [ diff --git a/spec/defines/resource_include_spec.rb b/spec/defines/resource_include_spec.rb index dac3d3c..f82273f 100644 --- a/spec/defines/resource_include_spec.rb +++ b/spec/defines/resource_include_spec.rb @@ -1,25 +1,32 @@ require 'spec_helper' describe 'autofs::include' do - let(:title) { 'auto.include' } - let(:facts) { { osfamily: 'RedHat', concat_basedir: '/mock_dir' } } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end - describe 'testing class' do - it { is_expected.not_to raise_error } - it do - is_expected.to contain_concat__fragment('auto.master/auto.include') - .with_content(%r{\+auto\.include}) - end - end + let(:title) { 'auto.include' } + + describe 'testing class' do + it { is_expected.not_to raise_error } + it do + is_expected.to contain_concat__fragment('auto.master/auto.include') + .with_content(%r{\+auto\.include}) + end + end - describe 'testing all parameters' do - let(:params) { { mapfile: 'auto.foo', order: '5' } } + describe 'testing all parameters' do + let(:params) { { mapfile: 'auto.foo', order: '5' } } - it { is_expected.not_to raise_error } - it do - is_expected.to contain_concat__fragment('auto.master/auto.foo') - .with_content(%r{\+auto\.foo}) - .with_order('5') + it { is_expected.not_to raise_error } + it do + is_expected.to contain_concat__fragment('auto.master/auto.foo') + .with_content(%r{\+auto\.foo}) + .with_order('5') + end + end end end end diff --git a/spec/defines/resource_mapfile_spec.rb b/spec/defines/resource_mapfile_spec.rb index d9bb4b1..89cb138 100644 --- a/spec/defines/resource_mapfile_spec.rb +++ b/spec/defines/resource_mapfile_spec.rb @@ -1,77 +1,104 @@ require 'spec_helper' describe 'autofs::mapfile' do - let(:title) { 'auto.foo' } - let(:facts) { { osfamily: 'RedHat', concat_basedir: '/mock_dir' } } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end - describe 'passing something other than present, absent, or purged for ensure' do - let(:params) { { directory: '/foo', ensure: 'foo' } } + let(:title) { 'auto.foo' } - it { is_expected.to raise_error(Puppet::Error, %r{ensure must be one of}) } - end + describe 'passing something other than present, absent, or purged for ensure' do + let(:params) { { directory: '/foo', ensure: 'foo' } } - describe 'passing present for ensure' do - let(:params) { { directory: '/foo', ensure: 'present' } } + it { is_expected.to raise_error(Puppet::Error) } + end - it { is_expected.not_to raise_error } - it do - is_expected.to contain_concat__fragment('auto.master/auto.foo') - .with_content(%r{/foo auto.foo}) - end - it do - is_expected.to contain_concat('auto.foo') - .with_ensure('present') - end - end + describe 'passing present for ensure' do + let(:params) { { directory: '/foo', ensure: 'present' } } - describe 'passing absent for ensure' do - let(:params) { { directory: '/foo', ensure: 'absent' } } + it { is_expected.not_to raise_error } + it do + is_expected.to contain_concat__fragment('auto.master/auto.foo') + .with_content(%r{/foo auto.foo}) + end + it do + is_expected.to contain_concat('auto.foo') + .with_ensure('present') + end + end - it { is_expected.not_to raise_error } - it do - is_expected.not_to contain_concat__fragment('auto.master/auto.foo') - end - it do - is_expected.to contain_concat('auto.foo') - .with_ensure('absent') - end - end + describe 'passing foo as maptype should fail' do + let(:params) { { directory: '/foo', maptype: 'foo' } } - describe 'passing purged for ensure' do - let(:params) { { directory: '/foo', ensure: 'purged' } } + it { is_expected.to raise_error } + end - it { is_expected.not_to raise_error } - it do - is_expected.not_to contain_concat__fragment('auto.master/auto.foo') - end - it do - is_expected.to contain_concat('auto.foo') - .with_ensure('absent') - end - it do - is_expected.to contain_file('/foo').with( - ensure: 'absent', - force: true, - ) - end - end + describe 'passing file as maptype should pass' do + let(:params) { { directory: '/foo', maptype: 'file' } } - describe 'with an invalid map type' do - let(:params) { { directory: '/foo', maptype: 'bar' } } + it { is_expected.not_to raise_error } + it do + is_expected.to contain_concat__fragment('auto.master/auto.foo') + .with_content(%r{/foo auto.foo}) + end + it do + is_expected.to contain_concat('auto.foo') + .with_ensure('present') + end + end - it { is_expected.to raise_error(Puppet::Error, %r{maptype must be one of}) } - end + describe 'passing absent for ensure' do + let(:params) { { directory: '/foo', ensure: 'absent' } } - describe 'with a different map type' do - let(:params) { { directory: '/foo', maptype: 'program' } } + it { is_expected.not_to raise_error } + it do + is_expected.not_to contain_concat__fragment('auto.master/auto.foo') + end + it do + is_expected.to contain_concat('auto.foo') + .with_ensure('absent') + end + end - it { is_expected.not_to raise_error } - it do - is_expected.to contain_concat__fragment('auto.master/auto.foo') - .with_content(%r{program:auto.foo}) - end - it do - is_expected.not_to contain_concat('auto.foo') + describe 'passing purged for ensure' do + let(:params) { { directory: '/foo', ensure: 'purged' } } + + it { is_expected.not_to raise_error } + it do + is_expected.not_to contain_concat__fragment('auto.master/auto.foo') + end + it do + is_expected.to contain_concat('auto.foo') + .with_ensure('absent') + end + it do + is_expected.to contain_file('/foo').with( + ensure: 'absent', + force: true, + ) + end + end + + describe 'with an invalid map type' do + let(:params) { { directory: '/foo', maptype: 'bar' } } + + it { is_expected.to raise_error(Puppet::Error) } + end + + describe 'with a different map type' do + let(:params) { { directory: '/foo', maptype: 'program' } } + + it { is_expected.not_to raise_error } + it do + is_expected.to contain_concat__fragment('auto.master/auto.foo') + .with_content(%r{program:auto.foo}) + end + it do + is_expected.not_to contain_concat('auto.foo') + end + end end end end diff --git a/spec/defines/resource_mount_spec.rb b/spec/defines/resource_mount_spec.rb index d37e928..dd22cb4 100644 --- a/spec/defines/resource_mount_spec.rb +++ b/spec/defines/resource_mount_spec.rb @@ -1,54 +1,61 @@ require 'spec_helper' describe 'autofs::mount' do - let(:title) { '/mnt/foo' } - let(:facts) { { osfamily: 'RedHat', concat_basedir: '/mock_dir' } } - - describe 'passing something other than present, absent, or purged for ensure' do - let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', ensure: 'foo' } } - - it { is_expected.to raise_error(Puppet::Error, %r{ensure must be one of}) } - end - - describe 'passing present for ensure' do - let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', ensure: 'present' } } - - it { is_expected.not_to raise_error } - it do - is_expected.to contain_concat__fragment('/mnt/foo@auto.foo').with( - 'content' => %r{/mnt/foo -rw nfsserver:/nfs/share}, - ) - end - end - - describe 'passing absent for ensure' do - let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', ensure: 'absent' } } - - it { is_expected.not_to raise_error } - it do - is_expected.not_to contain_concat__fragment('/mnt/foo@auto.foo') - end - end - - describe 'testing parameter options' do - let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', options: '-ro', ensure: 'present' } } - - it { is_expected.not_to raise_error } - it do - is_expected.to contain_concat__fragment('/mnt/foo@auto.foo').with( - 'content' => %r{/mnt/foo -ro nfsserver:/nfs/share}, - ) - end - end - - describe 'testing parameter order' do - let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', order: '5', ensure: 'present' } } - - it { is_expected.not_to raise_error } - it do - is_expected.to contain_concat__fragment('/mnt/foo@auto.foo').with( - 'order' => '5', - ) + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end + + let(:title) { '/mnt/foo' } + + describe 'passing something other than present, absent, or purged for ensure' do + let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', ensure: 'foo' } } + + it { is_expected.to raise_error(Puppet::Error) } + end + + describe 'passing present for ensure' do + let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', ensure: 'present' } } + + it { is_expected.not_to raise_error } + it do + is_expected.to contain_concat__fragment('/mnt/foo@auto.foo').with( + 'content' => %r{/mnt/foo -rw nfsserver:/nfs/share}, + ) + end + end + + describe 'passing absent for ensure' do + let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', ensure: 'absent' } } + + it { is_expected.not_to raise_error } + it do + is_expected.not_to contain_concat__fragment('/mnt/foo@auto.foo') + end + end + + describe 'testing parameter options' do + let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', options: '-ro', ensure: 'present' } } + + it { is_expected.not_to raise_error } + it do + is_expected.to contain_concat__fragment('/mnt/foo@auto.foo').with( + 'content' => %r{/mnt/foo -ro nfsserver:/nfs/share}, + ) + end + end + + describe 'testing parameter order' do + let(:params) { { mapfile: 'auto.foo', map: 'nfsserver:/nfs/share', order: '5', ensure: 'present' } } + + it { is_expected.not_to raise_error } + it do + is_expected.to contain_concat__fragment('/mnt/foo@auto.foo').with( + 'order' => '5', + ) + end + end end end end diff --git a/spec/type_aliases/mountoptions_spec.rb b/spec/type_aliases/mountoptions_spec.rb new file mode 100644 index 0000000..920763b --- /dev/null +++ b/spec/type_aliases/mountoptions_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.7.0') >= 0 + describe 'Autofs::MountOptions' do + describe 'accepts any string that starts with hyphen' do + [ + '-fstype=cifs,rw,vers=3.0,sec=ntlmssp,gid=rehan,uid=rehan,file_mode=0777,dir_mode=0777', + '-fstype=cifs,rw,vers=3.0,sec=ntlmssp', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + [], + {}, + 'abc1', + true, + 'atun1000', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/types/mountoptions.pp b/types/mountoptions.pp new file mode 100644 index 0000000..22a44ff --- /dev/null +++ b/types/mountoptions.pp @@ -0,0 +1,2 @@ + +type Autofs::MountOptions = Pattern[/^-(.+)*/]