From 745b2537e79eca59f8e9f2f15230482fcdf140aa Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 11:54:50 +0200 Subject: Refactor module * Use a top dhcp class with a $server boolean param * Refactor the dhcp::class to use packages/config/service classes * Don't use inheritance, put everything in dhcp::params --- manifests/params.pp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'manifests/params.pp') diff --git a/manifests/params.pp b/manifests/params.pp index d2ba584..20fbcd2 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -17,6 +17,13 @@ class dhcp::params { lenny => 'dhcp3-server', squeeze => 'isc-dhcp-server', } + + $service_pattern = $::lsbdistcodename? { + lenny => '/usr/sbin/dhcpd3', + squeeze => '/usr/sbin/dhcpd', + } + + $base_template = 'dhcp/dhcpd.conf.debian.erb' } default: { -- cgit v1.2.3 From 87096a875d4bee517e0c8c807218394a2024bd46 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 12:21:01 +0200 Subject: Add variable validations --- manifests/params.pp | 4 ++-- manifests/server/config.pp | 13 ++++++++++--- manifests/server/packages.pp | 7 +++++++ manifests/server/service.pp | 9 +++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) (limited to 'manifests/params.pp') diff --git a/manifests/params.pp b/manifests/params.pp index 20fbcd2..925f3f5 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -1,4 +1,4 @@ -# = Class: dhcp::params +# Class: dhcp::params # # Do NOT include this class - it won't do anything. # Set variables for names and paths @@ -23,7 +23,7 @@ class dhcp::params { squeeze => '/usr/sbin/dhcpd', } - $base_template = 'dhcp/dhcpd.conf.debian.erb' + $server_template = "${module_name}/dhcpd.conf.debian.erb" } default: { diff --git a/manifests/server/config.pp b/manifests/server/config.pp index c37d044..4898a60 100644 --- a/manifests/server/config.pp +++ b/manifests/server/config.pp @@ -1,7 +1,16 @@ +# Class: dhcp::server::config +# +# Configure the DHCP server +# class dhcp::server::config { include ::dhcp::params include ::concat::setup + validate_string($dhcp::params::config_dir) + validate_absolute_path($dhcp::params::config_dir) + validate_string($dhcp::params::server_template) + validate_re($dhcp::params::server_template, '^\S+$') + concat {"${dhcp::params::config_dir}/dhcpd.conf": owner => root, group => root, @@ -11,7 +20,7 @@ class dhcp::server::config { concat::fragment {'00.dhcp.server.base': ensure => present, target => "${dhcp::params::config_dir}/dhcpd.conf", - content => template($dhcp::params::base_template), + content => template($dhcp::params::server_template), } file {"${dhcp::params::config_dir}/dhcpd.conf.d": @@ -29,8 +38,6 @@ class dhcp::server::config { purge => true, force => true, source => "puppet:///modules/${module_name}/empty", - require => Package['dhcp-server'], - notify => Service['dhcpd'], } file {"${dhcp::params::config_dir}/hosts.d": diff --git a/manifests/server/packages.pp b/manifests/server/packages.pp index 34751e8..b89ede3 100644 --- a/manifests/server/packages.pp +++ b/manifests/server/packages.pp @@ -1,6 +1,13 @@ +# Class: dhcp::server::packages +# +# Install the DHCP server +# class dhcp::server::packages { include ::dhcp::params + validate_string($dhcp::params::srv_dhcpd) + validate_re($dhcp::params::srv_dhcpd, '^\S+$') + package {'dhcp-server': ensure => present, name => $dhcp::params::srv_dhcpd, diff --git a/manifests/server/service.pp b/manifests/server/service.pp index d531a7d..ef016c5 100644 --- a/manifests/server/service.pp +++ b/manifests/server/service.pp @@ -1,6 +1,15 @@ +# Class: dhcp::server::service +# +# Manage the DHCP server service +# class dhcp::server::service { include ::dhcp::params + validate_string($dhcp::params::srv_dhcpd) + validate_re($dhcp::params::srv_dhcpd, '^\S+$') + validate_string($dhcp::params::service_pattern) + validate_re($dhcp::params::service_pattern, '^\S+$') + service {'dhcpd': ensure => running, name => $dhcp::params::srv_dhcpd, -- cgit v1.2.3 From 4cc54cff2d04eedb672c7cc6b92a5edcccac63c9 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 12:34:23 +0200 Subject: Add spec tests for the dhcp class --- manifests/hosts.pp | 2 +- manifests/params.pp | 2 +- spec/classes/dhcp_spec.rb | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 spec/classes/dhcp_spec.rb (limited to 'manifests/params.pp') diff --git a/manifests/hosts.pp b/manifests/hosts.pp index b159449..426d661 100644 --- a/manifests/hosts.pp +++ b/manifests/hosts.pp @@ -42,7 +42,7 @@ define dhcp::hosts ( $template = 'dhcp/host.conf.erb', ) { - include dhcp::params + include ::dhcp::params concat::fragment {"dhcp.host.${name}": target => "${dhcp::params::config_dir}/hosts.d/${subnet}.conf", diff --git a/manifests/params.pp b/manifests/params.pp index 925f3f5..0938e4a 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -27,7 +27,7 @@ class dhcp::params { } default: { - fail "${name} is not available for ${::operatingsystem}/${::lsbdistcodename}" + fail "Unsupported OS ${::operatingsystem}/${::lsbdistcodename}" } } diff --git a/spec/classes/dhcp_spec.rb b/spec/classes/dhcp_spec.rb new file mode 100644 index 0000000..c833c52 --- /dev/null +++ b/spec/classes/dhcp_spec.rb @@ -0,0 +1,73 @@ +require 'spec_helper' + +describe 'dhcp' do + context 'When on an unsupported OS' do + let (:facts) { { + :operatingsystem => 'RedHat', + :osfamily => 'Redhat', + :lsbdistcodename => 'Santiago' + } } + + it 'should fail' do + expect { + should contain_package('dhcp-server') + }.to raise_error(Puppet::Error, /Unsupported OS RedHat\/Santiago/) + end + end + + context 'When on Debian lenny' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'lenny' + } } + + # Package + it { should contain_package('dhcp-server').with( + :name => 'dhcp3-server' + ) } + + # Config + it { should contain_concat('/etc/dhcp3/dhcpd.conf').with( + :owner => 'root', + :group => 'root', + :mode => '0644' + ) } + + # Service + it { should contain_service('dhcpd').with( + :ensure => 'running', + :name => 'dhcp3-server', + :enable => true, + :pattern => '/usr/sbin/dhcpd3' + ) } + end + + context 'When on Debian squeeze' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + + # Package + it { should contain_package('dhcp-server').with( + :name => 'isc-dhcp-server' + ) } + + # Config + it { should contain_concat('/etc/dhcp/dhcpd.conf').with( + :owner => 'root', + :group => 'root', + :mode => '0644' + ) } + + # Service + it { should contain_service('dhcpd').with( + :ensure => 'running', + :name => 'isc-dhcp-server', + :enable => true, + :pattern => '/usr/sbin/dhcpd' + ) } + end +end -- cgit v1.2.3