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 --- spec/classes/dhcp_spec.rb | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 spec/classes/dhcp_spec.rb (limited to 'spec/classes') 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 From 65bea20e0d8e61de7519bb1ab0677597500aaac7 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 14:25:15 +0200 Subject: Get rid of unscoped variables in dhcp::server, fix template --- manifests/init.pp | 9 +++++- manifests/server.pp | 20 +++++++----- manifests/server/config.pp | 4 +++ spec/classes/dhcp_spec.rb | 68 +++++++++++++++++++++++++++++++++++++++++ templates/dhcpd.conf.debian.erb | 16 +++------- 5 files changed, 97 insertions(+), 20 deletions(-) (limited to 'spec/classes') diff --git a/manifests/init.pp b/manifests/init.pp index 8a14b13..e1e05b3 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,7 +1,14 @@ class dhcp ( $server = true, + $server_ddns_update = undef, + $server_authoritative = undef, + $server_opts = undef, ) { if $server { - class { '::dhcp::server': } + class { '::dhcp::server': + ddns_update => $server_ddns_update, + authoritative => $server_authoritative, + opts => $server_opts, + } } } diff --git a/manifests/server.pp b/manifests/server.pp index ceffa14..d83c26f 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -5,15 +5,17 @@ # module "common": git://github.com/camptocamp/puppet-common.git # # facultative argument: -# *$dhcpd_ddns_update* : ddns-update-style option (default to none) -# *$dhcpd_authoritative* : set it if you want that your DHCP server is -# authoritative (default to no) -# *$dhcpd_opts* : any other DHCPD valid options +# *$ddns_update* : ddns-update-style option (default to none) +# *$authoritative* : set it if you want that your DHCP server is +# authoritative (default to no) +# *$opts* : any other DHCPD valid options # # Example: # node "dhcp.toto.ltd" { -# $dhcpd_opts = ['domain-name "toto.ltd"', "domain-name-servers 192.168.21.1"] -# include dhcp::server +# class { 'dhcp::server': +# opts => ['domain-name "toto.ltd"', +# 'domain-name-servers 192.168.21.1'], +# } # # dhcp::subnet {"10.27.20.0": # ensure => present, @@ -29,7 +31,11 @@ # } # } # -class dhcp::server { +class dhcp::server ( + $ddns_update = 'none', + $authoritative = false, + $opts = [], +) { class { '::dhcp::server::packages': } -> class { '::dhcp::server::config': } ~> class { '::dhcp::server::service': } diff --git a/manifests/server/config.pp b/manifests/server/config.pp index 4898a60..0c1a74a 100644 --- a/manifests/server/config.pp +++ b/manifests/server/config.pp @@ -11,6 +11,10 @@ class dhcp::server::config { validate_string($dhcp::params::server_template) validate_re($dhcp::params::server_template, '^\S+$') + validate_string($dhcp::server::ddns_update) + validate_bool($dhcp::server::authoritative) + validate_array($dhcp::server::opts) + concat {"${dhcp::params::config_dir}/dhcpd.conf": owner => root, group => root, diff --git a/spec/classes/dhcp_spec.rb b/spec/classes/dhcp_spec.rb index c833c52..ce9228e 100644 --- a/spec/classes/dhcp_spec.rb +++ b/spec/classes/dhcp_spec.rb @@ -34,6 +34,13 @@ describe 'dhcp' do :mode => '0644' ) } + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp3/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/#authoritative/) + } + # Service it { should contain_service('dhcpd').with( :ensure => 'running', @@ -62,6 +69,13 @@ describe 'dhcp' do :mode => '0644' ) } + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/#authoritative/) + } + # Service it { should contain_service('dhcpd').with( :ensure => 'running', @@ -70,4 +84,58 @@ describe 'dhcp' do :pattern => '/usr/sbin/dhcpd' ) } end + + context 'When passing ddns_update' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_ddns_update => 'foo' + } } + + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style foo;/).with_content(/#authoritative/) + } + end + + context 'When passing authoritative' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_authoritative => true + } } + + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/[^#]authoritative/) + } + end + + context 'When passing opts' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_opts => ['foo', 'bar', 'baz'] + } } + + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/#authoritative/).with_content(/foo;\nbar;\nbaz;\n/) + } + end end diff --git a/templates/dhcpd.conf.debian.erb b/templates/dhcpd.conf.debian.erb index ee95bee..7dfd168 100644 --- a/templates/dhcpd.conf.debian.erb +++ b/templates/dhcpd.conf.debian.erb @@ -4,22 +4,14 @@ # attempt to do a DNS update when a lease is confirmed. We default to the # behavior of the version 2 packages ('none', since DHCP v2 didn't # have support for DDNS.) -<% if has_variable?('dhcpd_ddns_update') -%> -ddns-update-style <%=dhcpd_ddns_update%>; -<% else -%> -ddns-update-style none; -<% end -%> +ddns-update-style <%= @ddns_update %>; # If this DHCP server is the official DHCP server for the local # network, the authoritative directive should be uncommented. -<% if has_variable?('dhcpd_authoritative') -%> -authoritative; -<% else -%> -#authoritative; -<% end -%> +<% unless @authoritative -%>#<%- end -%>authoritative; -<% if has_variable?('dhcpd_opts') and not dhcpd_opts.empty? -%> -<%= dhcpd_opts.join(";\n") %> +<% unless @opts.empty? -%> +<%= @opts.join(";\n") %>; <% end -%> # Use this to send dhcp log messages to a different log file (you also -- cgit v1.2.3 From d7efc5772d475e3ad374e6000f2e706be42c85da Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 11 Apr 2013 14:31:39 +0200 Subject: Check param validation in dhcp::server::config --- spec/classes/dhcp_spec.rb | 141 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 99 insertions(+), 42 deletions(-) (limited to 'spec/classes') diff --git a/spec/classes/dhcp_spec.rb b/spec/classes/dhcp_spec.rb index ce9228e..5eb7172 100644 --- a/spec/classes/dhcp_spec.rb +++ b/spec/classes/dhcp_spec.rb @@ -86,56 +86,113 @@ describe 'dhcp' do end context 'When passing ddns_update' do - let (:facts) { { - :operatingsystem => 'Debian', - :osfamily => 'Debian', - :lsbdistcodename => 'squeeze' - } } - let (:params) { { - :server_ddns_update => 'foo' - } } + context 'When passing wrong type' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_ddns_update => true + } } + + it 'should fail' do + expect { + should contain_concat__fragment('00.dhcp.server.base') + }.to raise_error(Puppet::Error, /true is not a string./) + end + end - it { should contain_concat__fragment('00.dhcp.server.base').with( - :ensure => 'present', - :target => '/etc/dhcp/dhcpd.conf', - :content => /log-facility/ - ).with_content(/ddns-update-style foo;/).with_content(/#authoritative/) - } + context 'When passing valid value' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_ddns_update => 'foo' + } } + + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style foo;/).with_content(/#authoritative/) + } + end end context 'When passing authoritative' do - let (:facts) { { - :operatingsystem => 'Debian', - :osfamily => 'Debian', - :lsbdistcodename => 'squeeze' - } } - let (:params) { { - :server_authoritative => true - } } + context 'When passing wrong type' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_authoritative => 'foo' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('00.dhcp.server.base') + }.to raise_error(Puppet::Error, /"foo" is not a boolean./) + end + end - it { should contain_concat__fragment('00.dhcp.server.base').with( - :ensure => 'present', - :target => '/etc/dhcp/dhcpd.conf', - :content => /log-facility/ - ).with_content(/ddns-update-style none;/).with_content(/[^#]authoritative/) - } + context 'When passing valid value' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_authoritative => true + } } + + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/[^#]authoritative/) + } + end end context 'When passing opts' do - let (:facts) { { - :operatingsystem => 'Debian', - :osfamily => 'Debian', - :lsbdistcodename => 'squeeze' - } } - let (:params) { { - :server_opts => ['foo', 'bar', 'baz'] - } } + context 'When passing wrong type' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_opts => 'foo' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('00.dhcp.server.base') + }.to raise_error(Puppet::Error, /"foo" is not an Array./) + end + end - it { should contain_concat__fragment('00.dhcp.server.base').with( - :ensure => 'present', - :target => '/etc/dhcp/dhcpd.conf', - :content => /log-facility/ - ).with_content(/ddns-update-style none;/).with_content(/#authoritative/).with_content(/foo;\nbar;\nbaz;\n/) - } + context 'When passing valid value' do + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + let (:params) { { + :server_opts => ['foo', 'bar', 'baz'] + } } + + it { should contain_concat__fragment('00.dhcp.server.base').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => /log-facility/ + ).with_content(/ddns-update-style none;/).with_content(/#authoritative/).with_content(/foo;\nbar;\nbaz;\n/) + } + end end end -- cgit v1.2.3 From 0689631b833d5683bdef5885de5893450f4928ca Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 08:52:01 +0200 Subject: small letters --- spec/classes/dhcp_spec.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'spec/classes') diff --git a/spec/classes/dhcp_spec.rb b/spec/classes/dhcp_spec.rb index 5eb7172..9db478c 100644 --- a/spec/classes/dhcp_spec.rb +++ b/spec/classes/dhcp_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe 'dhcp' do - context 'When on an unsupported OS' do + context 'when on an unsupported OS' do let (:facts) { { :operatingsystem => 'RedHat', :osfamily => 'Redhat', @@ -15,7 +15,7 @@ describe 'dhcp' do end end - context 'When on Debian lenny' do + context 'when on Debian lenny' do let (:facts) { { :operatingsystem => 'Debian', :osfamily => 'Debian', @@ -50,7 +50,7 @@ describe 'dhcp' do ) } end - context 'When on Debian squeeze' do + context 'when on Debian squeeze' do let (:facts) { { :operatingsystem => 'Debian', :osfamily => 'Debian', @@ -85,8 +85,8 @@ describe 'dhcp' do ) } end - context 'When passing ddns_update' do - context 'When passing wrong type' do + context 'when passing ddns_update' do + context 'when passing wrong type' do let (:facts) { { :operatingsystem => 'Debian', :osfamily => 'Debian', @@ -103,7 +103,7 @@ describe 'dhcp' do end end - context 'When passing valid value' do + context 'when passing valid value' do let (:facts) { { :operatingsystem => 'Debian', :osfamily => 'Debian', @@ -122,8 +122,8 @@ describe 'dhcp' do end end - context 'When passing authoritative' do - context 'When passing wrong type' do + context 'when passing authoritative' do + context 'when passing wrong type' do let (:facts) { { :operatingsystem => 'Debian', :osfamily => 'Debian', @@ -140,7 +140,7 @@ describe 'dhcp' do end end - context 'When passing valid value' do + context 'when passing valid value' do let (:facts) { { :operatingsystem => 'Debian', :osfamily => 'Debian', @@ -159,8 +159,8 @@ describe 'dhcp' do end end - context 'When passing opts' do - context 'When passing wrong type' do + context 'when passing opts' do + context 'when passing wrong type' do let (:facts) { { :operatingsystem => 'Debian', :osfamily => 'Debian', @@ -177,7 +177,7 @@ describe 'dhcp' do end end - context 'When passing valid value' do + context 'when passing valid value' do let (:facts) { { :operatingsystem => 'Debian', :osfamily => 'Debian', -- cgit v1.2.3