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 --- templates/dhcpd.conf.debian.erb | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'templates') 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 f4e7bdf477586e34e47a972172c15809ac92fe3c Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 10:38:14 +0200 Subject: Improve template host.conf.erb with validations and add spec for dhcp::hosts --- manifests/hosts.pp | 26 ++- spec/defines/dhcp_hosts_spec.rb | 411 ++++++++++++++++++++++++++++++++++++++++ templates/host.conf.erb | 32 ++-- 3 files changed, 451 insertions(+), 18 deletions(-) create mode 100644 spec/defines/dhcp_hosts_spec.rb (limited to 'templates') diff --git a/manifests/hosts.pp b/manifests/hosts.pp index 426d661..f8e5faf 100644 --- a/manifests/hosts.pp +++ b/manifests/hosts.pp @@ -1,4 +1,4 @@ -# = Definition: dhcp::hosts +# Definition: dhcp::hosts # # Creates a dhcp configuration for given hosts # @@ -38,15 +38,27 @@ define dhcp::hosts ( $hash_data, $subnet, - $global_options = false, - $template = 'dhcp/host.conf.erb', + $ensure = present, + $global_options = [], + $template = "${module_name}/host.conf.erb", ) { include ::dhcp::params - concat::fragment {"dhcp.host.${name}": - target => "${dhcp::params::config_dir}/hosts.d/${subnet}.conf", - content => template($template), - notify => Service['dhcpd'], + validate_string($ensure) + validate_re($ensure, ['present', 'absent'], + "\$ensure must be either 'present' or 'absent', got '${ensure}'") + validate_hash($hash_data) + validate_string($subnet) + validate_re($subnet, '^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$') + validate_array($global_options) + validate_string($template) + + if ($ensure == 'present') { + concat::fragment {"dhcp.host.${name}": + target => "${dhcp::params::config_dir}/hosts.d/${subnet}.conf", + content => template($template), + notify => Service['dhcpd'], + } } } diff --git a/spec/defines/dhcp_hosts_spec.rb b/spec/defines/dhcp_hosts_spec.rb new file mode 100644 index 0000000..658be20 --- /dev/null +++ b/spec/defines/dhcp_hosts_spec.rb @@ -0,0 +1,411 @@ +require 'spec_helper' + +describe 'dhcp::hosts' do + let (:title) { 'My hosts' } + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze', + } } + + context 'when passing wrong value for ensure' do + let (:params) { { + :hash_data => {}, + :subnet => '1.2.3.4', + :ensure => 'running' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /\$ensure must be either 'present' or 'absent', got 'running'/) + end + end + + context 'when hash_data is not passed' do + let (:params) { { + :subnet => '1.2.3.4', + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /Must pass hash_data to Dhcp::Hosts/) + end + end + + context 'when passing wrong type for hash_data' do + let (:params) { { + :hash_data => 'foo', + :subnet => '1.2.3.4', + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /"foo" is not a Hash\./) + end + end + + context 'when subnet is not passed' do + let (:params) { { + :hash_data => {}, + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /Must pass subnet to Dhcp::Hosts/) + end + end + + context 'when passing wrong type for subnet' do + let (:params) { { + :hash_data => {}, + :subnet => true + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /true is not a string\./) + end + end + + context 'when passing wrong value for subnet' do + let (:params) { { + :hash_data => {}, + :subnet => 'foo' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /"foo" does not match/) + end + end + + context 'when passing wrong type for global_options' do + let (:params) { { + :hash_data => {}, + :subnet => '1.2.3.4', + :global_options => 'foo' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /"foo" is not an Array\./) + end + end + + context 'when passing wrong type for template' do + let (:params) { { + :hash_data => {}, + :subnet => '1.2.3.4', + :template => true + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /true is not a string\./) + end + end + + context 'when passing one entry in hash_data' do + context 'when passing wrong type for an host data' do + let (:params) { { + :hash_data => { + 'host1' => true, + }, + :subnet => '1.2.3.4' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /true is not a Hash/) + end + end + + context 'when interfaces is not passed' do + let (:params) { { + :hash_data => { + 'host1' => { + }, + }, + :subnet => '1.2.3.4' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /Missing interfaces hash for host 'host1'/) + end + end + + context 'when passing wrong value for an interface' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth 0' => '00:11:22:33:44:55', + }, + }, + }, + :subnet => '1.2.3.4' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /"eth 0" does not match/) + end + end + + context 'when passing wrong type for a mac address' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => true, + }, + }, + }, + :subnet => '1.2.3.4' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /true is not a string\./) + end + end + + context 'when passing wrong value for a mac address' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => 'my mac', + }, + }, + }, + :subnet => '1.2.3.4' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /"my mac" does not match/) + end + end + + context 'when passing wrong type for fixed_address' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + }, + 'fixed_address' => true, + }, + }, + :subnet => '1.2.3.4' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /true is not a string/) + end + end + + context 'when passing wrong value for fixed_address' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + }, + 'fixed_address' => 'my wrong value', + }, + }, + :subnet => '1.2.3.4' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /"my wrong value" does not match/) + end + end + + context 'when not passing fixed_address' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + }, + }, + }, + :subnet => '1.2.3.4' + } } + + it { should contain_concat__fragment('dhcp.host.My hosts').with( + :content => /fixed-address host1;/ + ) } + end + + context 'when not passing options' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + }, + }, + }, + :subnet => '1.2.3.4', + :global_options => ['foo', 'bar'], + } } + + it { should contain_concat__fragment('dhcp.host.My hosts').with( + :content => /foo;\nbar;\n/ + ) } + end + + context 'when overriding options' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + }, + 'options' => ['baz'], + }, + }, + :subnet => '1.2.3.4', + :global_options => ['foo', 'bar'], + } } + + it { should contain_concat__fragment('dhcp.host.My hosts').with( + :content => /baz;\n/ + ) } + end + + context 'when passing wrong type for options' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + }, + 'options' => true, + }, + }, + :subnet => '1.2.3.4', + :global_options => ['foo', 'bar'], + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /true is not an Array\./) + end + end + + context 'when passing wrong value for fixed_address' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + }, + 'fixed_address' => 'my wrong value', + }, + }, + :subnet => '1.2.3.4' + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp.host.My hosts') + }.to raise_error(Puppet::Error, /"my wrong value" does not match/) + end + end + + context 'when not passing fixed_address' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + }, + }, + }, + :subnet => '1.2.3.4' + } } + + it { should contain_concat__fragment('dhcp.host.My hosts').with( + :content => /fixed-address host1;/ + ) } + end + + context 'when not passing options' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + }, + }, + }, + :subnet => '1.2.3.4', + :global_options => ['foo', 'bar'], + } } + + it { should contain_concat__fragment('dhcp.host.My hosts').with( + :content => /foo;\nbar;\n/ + ) } + end + + context 'when passing two hosts' do + let (:params) { { + :hash_data => { + 'host1' => { + 'interfaces' => { + 'eth0' => '00:11:22:33:44:55', + 'wlan0' => '00:aa:bb:44:55:ff', + }, + }, + 'host2' => { + 'interfaces' => { + 'eth1' => '00:11:af:33:44:55', + }, + 'fixed_address' => 'foo.example.com', + 'options' => ['opt1'], + }, + }, + :subnet => '1.2.3.4' + } } + + it { should contain_concat__fragment('dhcp.host.My hosts').with_content( + /host host1-eth0 \{\n hardware ethernet 00:11:22:33:44:55;\n fixed-address host1;\n\}/).with_content( + /host host1-wlan0 \{\n hardware ethernet 00:aa:bb:44:55:ff;\n fixed-address host1;\n\}/).with_content( + /host host2-eth1 \{\n hardware ethernet 00:11:af:33:44:55;\n fixed-address foo\.example\.com;\n opt1;\n\}/) + } + end + end + + context 'when passing two entries in hash_data' do + end + + context 'when passing global_options' do + end + + context 'when overriding template' do + end +end diff --git a/templates/host.conf.erb b/templates/host.conf.erb index e6a7073..398d798 100644 --- a/templates/host.conf.erb +++ b/templates/host.conf.erb @@ -1,16 +1,26 @@ -<%- @hash_data.sort.each do |host, datas| -%> -<%- datas.fetch('interfaces').sort.each do |if_name, if_mac| -%> +<%- @hash_data.sort.each do |host, data| + scope.function_validate_hash([data]) -%> +<%- + raise Puppet::ParseError, "Missing interfaces hash for host '#{host}'" unless data.has_key? 'interfaces' + data.fetch('interfaces').sort.each do |if_name, if_mac| + scope.function_validate_re([if_name, '^\S+$']) + scope.function_validate_string([if_mac]) + scope.function_validate_re([if_mac, '^[a-f0-9:.]+$']) + -%> host <%= host %>-<%= if_name %> { hardware ethernet <%= if_mac %>; -<% if datas.fetch('fixed_address', false) -%> - fixed-address <%= datas.fetch('fixed_address') %>; -<% else -%> - fixed-address <%= host %>; -<% end -%> -<% if datas.fetch('option', false) -%> - <%= datas.fetch('option') %> -<% elsif @global_options -%> - <%= @global_options %> +<% + fixed_address = data.fetch('fixed_address', nil) || host + scope.function_validate_string([fixed_address]) + scope.function_validate_re([fixed_address, '^\S+$']) + -%> + fixed-address <%= fixed_address %>; +<% + options = data.fetch('options', nil) || @global_options + scope.function_validate_array([options]) + unless options.empty? + -%> + <%= options.join(";\n") %>; <% end -%> } <% end -%> -- cgit v1.2.3 From 558df46dc9bec98c6c90719d9aed11ec7a338411 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 11:05:42 +0200 Subject: Simplify template --- templates/host.conf.erb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'templates') diff --git a/templates/host.conf.erb b/templates/host.conf.erb index 398d798..1c690b4 100644 --- a/templates/host.conf.erb +++ b/templates/host.conf.erb @@ -1,6 +1,5 @@ <%- @hash_data.sort.each do |host, data| - scope.function_validate_hash([data]) -%> -<%- + scope.function_validate_hash([data]) raise Puppet::ParseError, "Missing interfaces hash for host '#{host}'" unless data.has_key? 'interfaces' data.fetch('interfaces').sort.each do |if_name, if_mac| scope.function_validate_re([if_name, '^\S+$']) -- cgit v1.2.3 From b35e1322d0a791ab22cb2c385ec913492c75026d Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 11:10:45 +0200 Subject: Rename dhcp::shared-network to dhcp::shared_network, add specs --- manifests/shared_network.pp | 15 ++++--- spec/defines/dhcp_shared_network_spec.rb | 71 ++++++++++++++++++++++++++++++++ templates/shared-network.erb | 9 ++-- 3 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 spec/defines/dhcp_shared_network_spec.rb (limited to 'templates') diff --git a/manifests/shared_network.pp b/manifests/shared_network.pp index 9a02a5b..38f14fb 100644 --- a/manifests/shared_network.pp +++ b/manifests/shared_network.pp @@ -8,17 +8,22 @@ # - subnets must exists # - subnets must have $is_shared set to true (default is false) # -define dhcp::shared-network( +define dhcp::shared_network( $ensure = present, - $subnets = [] + $subnets = [], ) { - include dhcp::params + include ::dhcp::params - concat::fragment {"shared-${name}": + validate_string($ensure) + validate_re($ensure, ['present', 'absent'], + "\$ensure must be either 'present' or 'absent', got '${ensure}'") + validate_array($subnets) + + concat::fragment {"dhcp-shared-${name}": ensure => $ensure, target => "${dhcp::params::config_dir}/dhcpd.conf", - content => template('dhcp/shared-network.erb'), + content => template("${module_name}/shared-network.erb"), require => Dhcp::Subnet[$subnets], } diff --git a/spec/defines/dhcp_shared_network_spec.rb b/spec/defines/dhcp_shared_network_spec.rb new file mode 100644 index 0000000..ff5a2c5 --- /dev/null +++ b/spec/defines/dhcp_shared_network_spec.rb @@ -0,0 +1,71 @@ +require 'spec_helper' + +describe 'dhcp::shared_network' do + let (:title) { 'My network' } + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze' + } } + + context 'when passing wrong value for ensure' do + let (:params) { { + :ensure => 'running', + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp-shared-My network') + }.to raise_error(Puppet::Error, /\$ensure must be either 'present' or 'absent', got 'running'/) + end + end + + context 'when passing wrong type for subnets' do + let (:params) { { + :subnets => true, + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp-shared-My network') + }.to raise_error(Puppet::Error, /true is not an Array\./) + end + end + + context 'when passing no parameters' do + it { should contain_concat__fragment('dhcp-shared-My network').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf' + ).with_content( + /shared-network My network/ + ) + } + end + + context 'when passing wrong type for a subnet' do + let (:params) { { + :subnets => [true], + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp-shared-My network') + }.to raise_error(Puppet::Error, /true is not a string\./) + end + end + + context 'when passing wrong value for a subnet' do + let (:params) { { + :subnets => ['wrong value'], + } } + + it 'should fail' do + expect { + should contain_concat__fragment('dhcp-shared-My network') + }.to raise_error(Puppet::Error, /"wrong value" does not match/) + end + end + + context 'when passing subnets' do + end +end diff --git a/templates/shared-network.erb b/templates/shared-network.erb index 99a7aca..407106a 100644 --- a/templates/shared-network.erb +++ b/templates/shared-network.erb @@ -1,6 +1,9 @@ -#### dhcp::shared-network <%= name %> -shared-network <%= name %> { -<% subnets.each do |subnet| -%> +#### dhcp::shared_network <%= @name %> +shared-network <%= @name %> { +<% @subnets.each do |subnet| + scope.function_validate_string([subnet]) + scope.function_validate_re([subnet, '^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$']) +-%> include "<%= scope.lookupvar("dhcp::params::config_dir") %>/subnets/<%= subnet %>.conf"; <% end -%> } -- cgit v1.2.3 From e7cdc4e685a73d05be05ea9d6d0f854c4a3a6ab2 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 11:18:56 +0200 Subject: Test working dhcp::shared_network --- spec/defines/dhcp_shared_network_spec.rb | 12 +++++++++++- templates/shared-network.erb | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'templates') diff --git a/spec/defines/dhcp_shared_network_spec.rb b/spec/defines/dhcp_shared_network_spec.rb index ff5a2c5..d59f73c 100644 --- a/spec/defines/dhcp_shared_network_spec.rb +++ b/spec/defines/dhcp_shared_network_spec.rb @@ -37,7 +37,7 @@ describe 'dhcp::shared_network' do :ensure => 'present', :target => '/etc/dhcp/dhcpd.conf' ).with_content( - /shared-network My network/ + /shared-network My network \{\n\}/ ) } end @@ -67,5 +67,15 @@ describe 'dhcp::shared_network' do end context 'when passing subnets' do + let (:params) { { + :subnets => ['1.2.3.4', '5.6.7.8'], + } } + + it { should contain_concat__fragment('dhcp-shared-My network').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf' + ).with_content( + /shared-network My network \{\n include "\/etc\/dhcp\/subnets\/1\.2\.3\.4\.conf";\n include "\/etc\/dhcp\/subnets\/5\.6\.7\.8\.conf";\n\}/) + } end end diff --git a/templates/shared-network.erb b/templates/shared-network.erb index 407106a..33810b8 100644 --- a/templates/shared-network.erb +++ b/templates/shared-network.erb @@ -7,4 +7,4 @@ shared-network <%= @name %> { include "<%= scope.lookupvar("dhcp::params::config_dir") %>/subnets/<%= subnet %>.conf"; <% end -%> } -#### END <%= name %> +#### END <%= @name %> -- cgit v1.2.3 From eda301b7ba8f8827d1c9973ffaba0c1c7a029d98 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 12:29:45 +0200 Subject: Add spec for dhcp::subnet and improve template and definition --- manifests/subnet.pp | 57 +++++---- spec/defines/dhcp_subnet_spec.rb | 252 +++++++++++++++++++++++++++++++++++++++ templates/subnet.conf.erb | 49 ++++---- 3 files changed, 307 insertions(+), 51 deletions(-) create mode 100644 spec/defines/dhcp_subnet_spec.rb (limited to 'templates') diff --git a/manifests/subnet.pp b/manifests/subnet.pp index 18ffdc5..b9826da 100644 --- a/manifests/subnet.pp +++ b/manifests/subnet.pp @@ -27,16 +27,29 @@ # define dhcp::subnet( $broadcast, - $ensure=present, - $netmask=false, - $routers=false, - $subnet_mask=false, - $domain_name=false, - $other_opts=false, - $is_shared=false + $ensure = present, + $netmask = undef, + $routers = [], + $subnet_mask = undef, + $domain_name = undef, + $other_opts = [], + $is_shared = false ) { - include dhcp::params + include ::dhcp::params + + $ip_re = '^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$' + + validate_string($ensure) + validate_re($ensure, ['present', 'absent'], + "\$ensure must be either 'present' or 'absent', got '${ensure}'") + validate_string($broadcast) + validate_re($broadcast, $ip_re) + validate_string($netmask) + validate_array($routers) + validate_string($subnet_mask) + validate_string($domain_name) + validate_bool($is_shared) concat {"${dhcp::params::config_dir}/hosts.d/${name}.conf": owner => root, @@ -48,34 +61,30 @@ define dhcp::subnet( ensure => $ensure, owner => root, group => root, - content => template('dhcp/subnet.conf.erb'), + content => template("${module_name}/subnet.conf.erb"), notify => Service['dhcpd'], } - if ! $is_shared { - concat::fragment {"dhcp.${name}": - ensure => $ensure, - target => "${dhcp::params::config_dir}/dhcpd.conf", - content => "include \"${dhcp::params::config_dir}/subnets/${name}.conf\";\n", - } - } else { - concat::fragment {"dhcp.${name}": - ensure => absent, - target => "${dhcp::params::config_dir}/dhcpd.conf", - content => "include \"${dhcp::params::config_dir}/subnets/${name}.conf\";\n", - } - + $ensure_shared = $is_shared ? { + true => $ensure, + false => 'absent', + } + concat::fragment {"dhcp.subnet.${name}": + ensure => $ensure_shared, + target => "${dhcp::params::config_dir}/dhcpd.conf", + content => "include \"${dhcp::params::config_dir}/subnets/${name}.conf\";\n", } - concat::fragment {"subnet.${name}.hosts": + concat::fragment {"dhcp.subnet.${name}.hosts": ensure => $ensure, target => "${dhcp::params::config_dir}/dhcpd.conf", content => "include \"${dhcp::params::config_dir}/hosts.d/${name}.conf\";\n", } - concat::fragment {"00.dhcp.${name}.base": + concat::fragment {"dhcp.subnet.${name}.base": ensure => $ensure, target => "${dhcp::params::config_dir}/hosts.d/${name}.conf", content => "# File managed by puppet\n", + order => '00', } } diff --git a/spec/defines/dhcp_subnet_spec.rb b/spec/defines/dhcp_subnet_spec.rb new file mode 100644 index 0000000..48ab71b --- /dev/null +++ b/spec/defines/dhcp_subnet_spec.rb @@ -0,0 +1,252 @@ +require 'spec_helper' + +describe 'dhcp::subnet' do + let (:title) { '1.2.3.4' } + let (:facts) { { + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :lsbdistcodename => 'squeeze', + :netmask_eth0 => '255.255.255.0', + :domain => 'example.com', + } } + + context 'when passing wrong value for ensure' do + let (:params) { { + :ensure => 'running', + :broadcast => '1.2.3.4', + } } + + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /\$ensure must be either 'present' or 'absent', got 'running'/) + end + end + + context 'when not passing broadcast' do + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /Must pass broadcast to Dhcp::Subnet/) + end + end + + context 'when passing wrong type for broadcast' do + let (:params) { { + :broadcast => true, + } } + + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /true is not a string\./) + end + end + + context 'when passing wrong value for broadcast' do + let (:params) { { + :broadcast => 'foo', + } } + + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /"foo" does not match/) + end + end + + context 'when passing wrong type for netmask' do + let (:params) { { + :broadcast => '1.2.3.4', + :netmask => true, + } } + + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /true is not a string\./) + end + end + + context 'when passing wrong value for netmask' do + let (:params) { { + :broadcast => '1.2.3.4', + :netmask => 'foo', + } } + + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /"foo" does not match/) + end + end + + context 'when passing wrong type for routers' do + let (:params) { { + :broadcast => '1.2.3.4', + :routers => true, + } } + + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /true is not an Array\./) + end + end + + context 'when passing wrong type for subnet_mask' do + let (:params) { { + :broadcast => '1.2.3.4', + :subnet_mask => true, + } } + + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /true is not a string\./) + end + end + + context 'when passing wrong type for domain_name' do + let (:params) { { + :broadcast => '1.2.3.4', + :domain_name => true, + } } + + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /true is not a string\./) + end + end + + context 'when passing wrong type for is_shared' do + let (:params) { { + :broadcast => '1.2.3.4', + :is_shared => 'foo', + } } + + it 'should fail' do + expect { + should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf') + }.to raise_error(Puppet::Error, /"foo" is not a boolean\./) + end + end + + context 'when using defaults' do + let (:params) { { + :broadcast => '1.2.3.4', + } } + + it { should contain_concat('/etc/dhcp/hosts.d/1.2.3.4.conf').with( + :owner => 'root', + :group => 'root', + :mode => '0644' + ) } + + it { should contain_file('/etc/dhcp/subnets/1.2.3.4.conf').with( + :ensure => 'present', + :owner => 'root', + :group => 'root' + ).with_content( + /subnet 1\.2\.3\.4 netmask 255\.255\.255\.0 \{\n/ + ).with_content( + /option routers 255\.255\.255\.0;\n/ + ).with_content( + /option subnet-mask 255\.255\.255\.0;\n/ + ).with_content( + /option broadcast-address 1\.2\.3\.4;\n/ + ).with_content( + /option domain-name "example\.com";\n/ + ) } + + it { should contain_concat__fragment('dhcp.subnet.1.2.3.4').with( + :ensure => 'absent' + ) } + + it { should contain_concat__fragment('dhcp.subnet.1.2.3.4.hosts').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => "include \"/etc/dhcp/hosts.d/1\.2\.3\.4\.conf\";\n" + ) } + + it { should contain_concat__fragment('dhcp.subnet.1.2.3.4.base').with( + :ensure => 'present', + :target => '/etc/dhcp/hosts.d/1.2.3.4.conf', + :content => "# File managed by puppet\n", + :order => '00' + ) } + end + + context 'when is_shared is true' do + let (:params) { { + :broadcast => '1.2.3.4', + :is_shared => true, + } } + + it { should contain_concat__fragment('dhcp.subnet.1.2.3.4').with( + :ensure => 'present', + :target => '/etc/dhcp/dhcpd.conf', + :content => "include \"/etc/dhcp/subnets/1.2.3.4.conf\";\n" + ) } + end + + context 'when passing other_opts as array' do + let (:params) { { + :broadcast => '1.2.3.4', + :other_opts => ['foo', 'bar'], + } } + + it { should contain_file('/etc/dhcp/subnets/1.2.3.4.conf').with( + :ensure => 'present', + :owner => 'root', + :group => 'root' + ).with_content( + / foo;\n bar;\n/ + ) } + end + + context 'when passing other_opts as string' do + let (:params) { { + :broadcast => '1.2.3.4', + :other_opts => 'bar', + } } + + it { should contain_file('/etc/dhcp/subnets/1.2.3.4.conf').with( + :ensure => 'present', + :owner => 'root', + :group => 'root' + ).with_content( + / bar;\n/ + ) } + end + + context 'when overriding all parameters' do + let (:params) { { + :broadcast => '1.2.3.4', + :netmask => '255.1.2.0', + :routers => ['2.3.4.5', '3.4.5.6'], + :subnet_mask => '255.255.1.0', + :domain_name => 'foo.io', + :other_opts => ['foo', 'bar'], + } } + + it { should contain_file('/etc/dhcp/subnets/1.2.3.4.conf').with( + :ensure => 'present', + :owner => 'root', + :group => 'root' + ).with_content( + /subnet 1\.2\.3\.4 netmask 255\.1\.2\.0 \{\n/ + ).with_content( + /option routers 2\.3\.4\.5,3\.4\.5\.6;\n/ + ).with_content( + /option subnet-mask 255\.255\.1\.0;\n/ + ).with_content( + /option broadcast-address 1\.2\.3\.4;\n/ + ).with_content( + /option domain-name "foo\.io";\n/ + ).with_content( + / foo;\n bar;\n/ + ) } + end +end diff --git a/templates/subnet.conf.erb b/templates/subnet.conf.erb index 89ca82f..11f9ed4 100644 --- a/templates/subnet.conf.erb +++ b/templates/subnet.conf.erb @@ -1,33 +1,28 @@ # File managed by puppet -<% if netmask -%> -subnet <%=name%> netmask <%=netmask%> { -<% else -%> -subnet <%=name%> netmask <%=netmask_eth0%> { -<% end -%> -<% if routers and not routers.empty? -%> - option routers <%= routers.collect! {|i| "#{i}" }.join(",") %>; -<% else -%> - option routers <%=network_eth0%>; -<% end -%> -<% if subnet_mask -%> - option subnet-mask <%=subnet_mask%>; -<% elsif netmask -%> - option subnet-mask <%=netmask%>; -<% else -%> - option subnet-mask <%=netmask_eth0%>; -<% end -%> - option broadcast-address <%=broadcast%>; -<% if domain_name -%> - option domain-name "<%=domain_name%>"; -<% else -%> - option domain-name "<%=domain%>"; -<% end -%> -<% if other_opts and not other_opts.empty? -%> - <% if other_opts.is_a?(Array) -%> -<%= other_opts.collect! {|i| " #{i};"}.join("\n") %> +<%- + _netmask = @netmask || scope.lookupvar('::netmask_eth0') + scope.function_validate_re([_netmask, @ip_re]) -%> +subnet <%= @name %> netmask <%= _netmask %> { +<%- + _routers = @routers unless @routers.empty? + _routers ||= [scope.lookupvar('::netmask_eth0')] + scope.function_validate_array([_routers]) -%> + option routers <%= _routers.collect! {|i| "#{i}" }.join(',') %>; +<%- + _subnet_mask = @subnet_mask || _netmask + scope.function_validate_re([_subnet_mask, @ip_re]) -%> + option subnet-mask <%= _subnet_mask %>; + option broadcast-address <%= @broadcast %>; +<%- + _domain_name = @domain_name || scope.lookupvar('::domain') + scope.function_validate_re([_domain_name, '^\S+$']) -%> + option domain-name "<%= _domain_name %>"; +<% unless @other_opts.empty? -%> + <% if @other_opts.is_a?(Array) -%> +<%= @other_opts.collect! {|i| " #{i};"}.join("\n") %> <% else -%> - <%=other_opts%>; + <%= @other_opts %>; <% end -%> <% end -%> } -- cgit v1.2.3 From 1ad8359dc57c99c1671d7cb41b961c451fb1c22f Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 14:27:05 +0200 Subject: capital letters are valid in mac addresses --- spec/defines/dhcp_hosts_spec.rb | 4 ++-- templates/host.conf.erb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'templates') diff --git a/spec/defines/dhcp_hosts_spec.rb b/spec/defines/dhcp_hosts_spec.rb index f5f44ca..8170ed5 100644 --- a/spec/defines/dhcp_hosts_spec.rb +++ b/spec/defines/dhcp_hosts_spec.rb @@ -350,7 +350,7 @@ describe 'dhcp::hosts' do }, 'host2' => { 'interfaces' => { - 'eth1' => '00:11:af:33:44:55', + 'eth1' => '00:11:AF:33:44:55', }, 'fixed_address' => 'foo.example.com', 'options' => ['opt1'], @@ -363,7 +363,7 @@ describe 'dhcp::hosts' do :target => '/etc/dhcp/hosts.d/1.2.3.4.conf').with_content( /host host1-eth0 \{\n hardware ethernet 00:11:22:33:44:55;\n fixed-address host1;\n\}/).with_content( /host host1-wlan0 \{\n hardware ethernet 00:aa:bb:44:55:ff;\n fixed-address host1;\n\}/).with_content( - /host host2-eth1 \{\n hardware ethernet 00:11:af:33:44:55;\n fixed-address foo\.example\.com;\n opt1;\n\}/) + /host host2-eth1 \{\n hardware ethernet 00:11:AF:33:44:55;\n fixed-address foo\.example\.com;\n opt1;\n\}/) } end end diff --git a/templates/host.conf.erb b/templates/host.conf.erb index 1c690b4..c483d62 100644 --- a/templates/host.conf.erb +++ b/templates/host.conf.erb @@ -4,7 +4,7 @@ data.fetch('interfaces').sort.each do |if_name, if_mac| scope.function_validate_re([if_name, '^\S+$']) scope.function_validate_string([if_mac]) - scope.function_validate_re([if_mac, '^[a-f0-9:.]+$']) + scope.function_validate_re([if_mac, '^[A-Fa-f0-9:.]+$']) -%> host <%= host %>-<%= if_name %> { hardware ethernet <%= if_mac %>; -- cgit v1.2.3