diff --git a/.kitchen.yml b/.kitchen.yml index c3a6f2c..f21d037 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -1,58 +1,32 @@ --- driver_plugin: vagrant -driver_config: - require_chef_omnibus: true -platforms: -- name: centos-5.9 - driver_config: - box: opscode-centos-5.9 - box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_centos-5.9_provisionerless.box - run_list: - -- name: centos-6.4 - driver_config: - box: opscode-centos-6.4 - box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_centos-6.4_provisionerless.box - run_list: - -- name: debian-7.1 - driver_config: - box: opscode-debian-7.1 - box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_debian-7.1.0_provisionerless.box - run_list: - - recipe[apt] - -- name: ubuntu-10.04 - driver_config: - box: opscode-ubuntu-10.04 - box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-10.04_provisionerless.box - run_list: - - recipe[apt] +provisioner: + name: chef_zero +platforms: +- name: centos-5.11 +- name: centos-6.6 +- name: centos-7.1 +- name: fedora-21 +- name: debian-7.8 - name: ubuntu-12.04 - driver_config: - box: opscode-ubuntu-12.04 - box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box - run_list: - - recipe[apt] - -- name: ubuntu-13.04 - driver_config: - box: opscode-ubuntu-13.04 - box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-13.04_provisionerless.box - run_list: - - recipe[apt] +- name: ubuntu-14.04 suites: - name: default run_list: - - recipe[minitest-handler] - recipe[dnsmasq_test::default] attributes: dnsmasq: + enable_dns: true + enable_dhcp: true dhcp_options: ['dhcp-host=01:23:ab:cd:01:02,larry,10.0.0.10'] + dns: + server: '8.8.8.8' dhcp: dhcp-range: 'eth1,10.0.0.5,10.0.0.15,12h' domain: 'test.lab' tftp-root: '/var/lib/tftpboot' + enable-tftp: null + interface: 'eth1' diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..829f1b0 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,4 @@ +HashSyntax: + Enabled: false +LineLength: + Enabled: false diff --git a/Berksfile b/Berksfile index c2e34bd..9727625 100644 --- a/Berksfile +++ b/Berksfile @@ -1,10 +1,7 @@ -site :opscode +source 'https://supermarket.chef.io' metadata group :integration do - cookbook "minitest-handler" - cookbook "apt" - cookbook "hosts_file" - cookbook "dnsmasq_test", :path => "./test/cookbooks/dnsmasq_test" + cookbook 'dnsmasq_test', :path => './test/cookbooks/dnsmasq_test' end diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a2f8139 --- /dev/null +++ b/Gemfile @@ -0,0 +1,11 @@ +source 'https://rubygems.org' + +gem 'berkshelf', '~> 3.0' +gem 'chefspec', '~> 4.0' +gem 'foodcritic', '~> 4.0' +gem 'rubocop' + +group :integration do + gem 'test-kitchen' + gem 'kitchen-vagrant', '~> 0.18' +end diff --git a/attributes/default.rb b/attributes/default.rb index a8e9265..65439bc 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,14 +1,13 @@ -default[:dnsmasq][:enable_dhcp] = false -default[:dnsmasq][:dhcp] = {} -default[:dnsmasq][:dhcp_options] = [] -default[:dnsmasq][:enable_dns] = true -default[:dnsmasq][:dns] = { +default['dnsmasq']['enable_dhcp'] = false +default['dnsmasq']['dhcp'] = {} +default['dnsmasq']['dhcp_options'] = [] +default['dnsmasq']['enable_dns'] = true +default['dnsmasq']['dns'] = { 'no-poll' => nil, 'no-resolv' => nil, 'server' => '127.0.0.1' } -default[:dnsmasq][:dns_options] = [] -default[:dnsmasq][:managed_hosts] = {} -default[:dnsmasq][:managed_hosts_bag] = "managed_hosts" -default[:dnsmasq][:user] = 'dnsmasq' - +default['dnsmasq']['dns_options'] = [] +default['dnsmasq']['managed_hosts'] = {} +default['dnsmasq']['managed_hosts_bag'] = 'managed_hosts' +default['dnsmasq']['user'] = 'dnsmasq' diff --git a/metadata.rb b/metadata.rb index fa85d96..e62b879 100644 --- a/metadata.rb +++ b/metadata.rb @@ -3,11 +3,10 @@ maintainer_email 'chrisroberts.code@gmail.com' license 'Apache 2.0' description 'Installs and configures dnsmasq' -long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.2.0' depends 'hosts_file' -%w{ubuntu debian redhat centos scientific oracle}.each do |os| +%w(ubuntu debian redhat centos scientific oracle).each do |os| supports os end diff --git a/recipes/default.rb b/recipes/default.rb index 36c2bb3..742a039 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -1,13 +1,8 @@ package 'dnsmasq' +include_recipe 'dnsmasq::dns' if node['dnsmasq']['enable_dns'] +include_recipe 'dnsmasq::dhcp' if node['dnsmasq']['enable_dhcp'] + service 'dnsmasq' do action [:enable, :start] end - -if(node[:dnsmasq][:enable_dns]) - include_recipe 'dnsmasq::dns' -end - -if(node[:dnsmasq][:enable_dhcp]) - include_recipe 'dnsmasq::dhcp' -end diff --git a/recipes/dhcp.rb b/recipes/dhcp.rb index 45ccc83..36e5818 100644 --- a/recipes/dhcp.rb +++ b/recipes/dhcp.rb @@ -1,22 +1,20 @@ - include_recipe 'dnsmasq::default' -if node['dnsmasq']['dhcp']['tftp-root'] - directory node['dnsmasq']['dhcp']['tftp-root'] do - owner node['dnsmasq']['user'] - mode 0755 - recursive true - action :create - end -end +directory node['dnsmasq']['dhcp']['tftp-root'] do + owner node['dnsmasq']['user'] + mode 0755 + recursive true + action :create +end if node['dnsmasq']['dhcp']['tftp-root'] # ~FC023 template '/etc/dnsmasq.d/dhcp.conf' do source 'dynamic_config.erb' mode 0644 - variables lazy {{ - :config => node['dnsmasq']['dhcp'].to_hash, - :list => node['dnsmasq']['dhcp_options'] - }} - notifies :restart, resources(:service => 'dnsmasq'), :immediately + variables lazy { + { + :config => node['dnsmasq']['dhcp'].to_hash, + :list => node['dnsmasq']['dhcp_options'] + } + } + notifies :restart, 'service[dnsmasq]', :immediately end - diff --git a/recipes/dns.rb b/recipes/dns.rb index 93e78f7..d4882d0 100644 --- a/recipes/dns.rb +++ b/recipes/dns.rb @@ -1,10 +1,8 @@ include_recipe 'dnsmasq::default' include_recipe 'dnsmasq::manage_hostsfile' -dns_config = node[:dnsmasq][:dns].to_hash -unless(node[:dnsmasq][:enable_dhcp]) - dns_config['no-dhcp-interface='] = nil -end +dns_config = node['dnsmasq']['dns'].to_hash +dns_config['no-dhcp-interface='] = nil unless node['dnsmasq']['enable_dhcp'] template '/etc/dnsmasq.d/dns.conf' do source 'dynamic_config.erb' @@ -13,5 +11,5 @@ :config => dns_config, :list => node['dnsmasq']['dns_options'] ) - notifies :restart, resources(:service => 'dnsmasq'), :immediately + notifies :restart, 'service[dnsmasq]', :immediately end diff --git a/recipes/manage_hostsfile.rb b/recipes/manage_hostsfile.rb index 6d59d42..4824714 100644 --- a/recipes/manage_hostsfile.rb +++ b/recipes/manage_hostsfile.rb @@ -1,19 +1,19 @@ begin - managed_hosts_bag = data_bag_item('dnsmasq', node[:dnsmasq][:managed_hosts_bag]) + managed_hosts_bag = data_bag_item('dnsmasq', node['dnsmasq']['managed_hosts_bag']) rescue - Chef::Log.debug "No data bag found for DNSMasq managed hosts file" + Chef::Log.debug 'No data bag found for DNSMasq managed hosts file' end managed_hosts = {} managed_hosts.merge!(managed_hosts_bag['maps']) if managed_hosts_bag -managed_hosts.merge!(node[:dnsmasq][:managed_hosts].to_hash) if node[:dnsmasq][:managed_hosts] +managed_hosts.merge!(node['dnsmasq']['managed_hosts'].to_hash) if node['dnsmasq']['managed_hosts'] managed_hosts.each do |ip, host| - host = host.is_a?(Array) ? host.map{|i|i} : host.split(' ') + host = host.is_a?(Array) ? host.map { |i| i } : host.split(' ') hosts_file_entry ip do hostname host.shift aliases host unless host.empty? comment 'dnsmasq managed entry' - notifies :restart, resources(:service => 'dnsmasq') + notifies :restart, 'service[dnsmasq]' end end diff --git a/test/cookbooks/dnsmasq_test/files/default/tests/minitest/default_test.rb b/test/cookbooks/dnsmasq_test/files/default/tests/minitest/default_test.rb deleted file mode 100644 index 10e36c0..0000000 --- a/test/cookbooks/dnsmasq_test/files/default/tests/minitest/default_test.rb +++ /dev/null @@ -1,31 +0,0 @@ -require_relative './helpers.rb' - -describe_recipe 'dnsmasq_test::default' do - include DnsMasqTestHelper - - describe 'dnsmaq' do - it 'should start dnsmasq' do - service('dnsmasq').must_be_enabled - service('dnsmasq').must_be_running - end - - it 'should have the right dhcp config file' do - file('/etc/dnsmasq.d/dhcp.conf').must_have(:mode, '644') - file('/etc/dnsmasq.d/dhcp.conf').must_match /^dhcp-host=01:23:ab:cd:01:02,larry,10\.0\.0\.10$/ - file('/etc/dnsmasq.d/dhcp.conf').must_match /^dhcp-range=eth1,10\.0\.0\.5,10\.0\.0\.15,12h$/ - file('/etc/dnsmasq.d/dhcp.conf').must_match /^domain=test.lab$/ - file('/etc/dnsmasq.d/dhcp.conf').must_match /^enable-tftp$/ - file('/etc/dnsmasq.d/dhcp.conf').must_match /^interface=eth1$/ - file('/etc/dnsmasq.d/dhcp.conf').must_match /^tftp-root=\/var\/lib\/tftpboot$/ - end - - it 'should have the right dns config file' do - file('/etc/dnsmasq.d/dns.conf').must_have(:mode, '644') - file('/etc/dnsmasq.d/dns.conf').must_match /^no-poll$/ - file('/etc/dnsmasq.d/dns.conf').must_match /^no-resolv$/ - file('/etc/dnsmasq.d/dns.conf').must_match /^server=127\.0\.0\.1$/ - end - - end - -end diff --git a/test/cookbooks/dnsmasq_test/files/default/tests/minitest/helpers.rb b/test/cookbooks/dnsmasq_test/files/default/tests/minitest/helpers.rb deleted file mode 100644 index 96136df..0000000 --- a/test/cookbooks/dnsmasq_test/files/default/tests/minitest/helpers.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'minitest/spec' - -module DnsMasqTestHelper - include MiniTest::Chef::Assertions - include MiniTest::Chef::Context - include MiniTest::Chef::Resources -end diff --git a/test/cookbooks/dnsmasq_test/metadata.rb b/test/cookbooks/dnsmasq_test/metadata.rb index 6e8cf65..f760219 100644 --- a/test/cookbooks/dnsmasq_test/metadata.rb +++ b/test/cookbooks/dnsmasq_test/metadata.rb @@ -1 +1,3 @@ +name 'dnsmasq_test' +version '0.0.1' depends 'dnsmasq' diff --git a/test/cookbooks/dnsmasq_test/recipes/default.rb b/test/cookbooks/dnsmasq_test/recipes/default.rb index 8b3f698..9485739 100644 --- a/test/cookbooks/dnsmasq_test/recipes/default.rb +++ b/test/cookbooks/dnsmasq_test/recipes/default.rb @@ -1,9 +1,3 @@ -node.default[:dnsmasq][:enable_dns] = true -node.default[:dnsmasq][:enable_dhcp] = true - -node.default[:dnsmasq][:dhcp] = { - 'enable-tftp' => nil, - 'interface' => 'eth1' -} - +execute 'apt-get update' if platform_family?('debian') +package 'bind-utils' if platform_family?('rhel', 'fedora') include_recipe 'dnsmasq' diff --git a/test/integration/default/serverspec/default_spec.rb b/test/integration/default/serverspec/default_spec.rb new file mode 100644 index 0000000..d97b4bf --- /dev/null +++ b/test/integration/default/serverspec/default_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +describe 'dnsmasq::default' do + describe service('dnsmasq') do + it { should be_enabled } + it { should be_running } + end + + describe file('/etc/dnsmasq.d/dhcp.conf') do + it { should be_mode '644' } + its(:content) { should match(/^dhcp-host=01:23:ab:cd:01:02,larry,10\.0\.0\.10$/) } + its(:content) { should match(/^dhcp-range=eth1,10\.0\.0\.5,10\.0\.0\.15,12h$/) } + its(:content) { should match(/^domain=test.lab$/) } + its(:content) { should match(/^enable-tftp$/) } + its(:content) { should match(/^interface=eth1$/) } + its(:content) { should match(%r{^tftp-root=\/var\/lib\/tftpboot$}) } + end + + describe file('/etc/dnsmasq.d/dns.conf') do + it { should be_mode '644' } + its(:content) { should match(/^no-poll$/) } + its(:content) { should match(/^no-resolv$/) } + its(:content) { should match(/^server=8\.8\.8\.8$/) } + end + + describe command('host google.com') do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/^google.com has address/) } + end + + describe port(53) do + it { should be_listening } + end + + # bootp/dhcp + describe port(67) do + it { should be_listening } + end + + # tftp + describe port(69) do + it { should be_listening } + end +end diff --git a/test/integration/helpers/serverspec/spec_helper.rb b/test/integration/helpers/serverspec/spec_helper.rb new file mode 100644 index 0000000..37af1b4 --- /dev/null +++ b/test/integration/helpers/serverspec/spec_helper.rb @@ -0,0 +1,3 @@ +require 'serverspec' + +set :backend, :exec