From 5c1164ca093f105d7a3d1f643dee7b0e675f31f7 Mon Sep 17 00:00:00 2001 From: Ashley Penney Date: Mon, 29 Jul 2013 17:25:36 -0400 Subject: Add preferred_servers feature. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds: `preferred_servers` [Array]: The servers to prefer. As requested by Erik Dalén! --- manifests/config.pp | 23 ++++----- manifests/init.pp | 38 ++++++++------- manifests/params.pp | 23 ++++----- spec/classes/ntp_config_spec.rb | 89 ++++++++++++++++++++++------------- spec/system/ntp_config_spec.rb | 8 ++-- spec/system/preferred_servers_spec.rb | 20 ++++++++ templates/ntp.conf.erb | 2 +- 7 files changed, 125 insertions(+), 78 deletions(-) create mode 100644 spec/system/preferred_servers_spec.rb diff --git a/manifests/config.pp b/manifests/config.pp index 45279be..e5dd5ae 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -1,15 +1,16 @@ class ntp::config ( - $config = $ntp::config, - $config_template = $ntp::config_template, - $driftfile = $ntp::driftfile, - $keys_enable = $ntp::keys_enable, - $keys_file = $ntp::keys_file, - $keys_controlkey = $ntp::keys_controlkey, - $keys_requestkey = $ntp::keys_requestkey, - $keys_trusted = $ntp::keys_trusted, - $panic = $ntp::panic, - $restrict = $ntp::restrict, - $servers = $ntp::servers, + $config = $ntp::config, + $config_template = $ntp::config_template, + $driftfile = $ntp::driftfile, + $keys_enable = $ntp::keys_enable, + $keys_file = $ntp::keys_file, + $keys_controlkey = $ntp::keys_controlkey, + $keys_requestkey = $ntp::keys_requestkey, + $keys_trusted = $ntp::keys_trusted, + $panic = $ntp::panic, + $preferred_servers = $ntp::preferred_servers, + $restrict = $ntp::restrict, + $servers = $ntp::servers, ) inherits ntp { if $keys_enable { diff --git a/manifests/init.pp b/manifests/init.pp index d406fb3..2c8b9e4 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,22 +1,23 @@ class ntp ( - $autoupdate = $ntp::params::autoupdate, - $config = $ntp::params::config, - $config_template = $ntp::params::config_template, - $driftfile = $ntp::params::driftfile, - $keys_enable = $ntp::params::keys_enable, - $keys_file = $ntp::params::keys_file, - $keys_controlkey = $ntp::params::keys_controlkey, - $keys_requestkey = $ntp::params::keys_requestkey, - $keys_trusted = $ntp::params::keys_trusted, - $package_ensure = $ntp::params::package_ensure, - $package_name = $ntp::params::package_name, - $panic = $ntp::params::panic, - $restrict = $ntp::params::restrict, - $servers = $ntp::params::servers, - $service_enable = $ntp::params::service_enable, - $service_ensure = $ntp::params::service_ensure, - $service_manage = $ntp::params::service_manage, - $service_name = $ntp::params::service_name, + $autoupdate = $ntp::params::autoupdate, + $config = $ntp::params::config, + $config_template = $ntp::params::config_template, + $driftfile = $ntp::params::driftfile, + $keys_enable = $ntp::params::keys_enable, + $keys_file = $ntp::params::keys_file, + $keys_controlkey = $ntp::params::keys_controlkey, + $keys_requestkey = $ntp::params::keys_requestkey, + $keys_trusted = $ntp::params::keys_trusted, + $package_ensure = $ntp::params::package_ensure, + $package_name = $ntp::params::package_name, + $panic = $ntp::params::panic, + $preferred_servers = $ntp::params::preferred_servers, + $restrict = $ntp::params::restrict, + $servers = $ntp::params::servers, + $service_enable = $ntp::params::service_enable, + $service_ensure = $ntp::params::service_ensure, + $service_manage = $ntp::params::service_manage, + $service_name = $ntp::params::service_name, ) inherits ntp::params { validate_absolute_path($config) @@ -29,6 +30,7 @@ class ntp ( validate_string($package_ensure) validate_array($package_name) validate_bool($panic) + validate_array($preferred_servers) validate_bool($restrict) validate_array($servers) validate_bool($service_enable) diff --git a/manifests/params.pp b/manifests/params.pp index 9e91cce..ef037fc 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -1,16 +1,17 @@ class ntp::params { - $autoupdate = false - $config_template = 'ntp/ntp.conf.erb' - $keys_enable = false - $keys_controlkey = '' - $keys_requestkey = '' - $keys_trusted = [] - $package_ensure = 'present' - $restrict = true - $service_enable = true - $service_ensure = 'running' - $service_manage = true + $autoupdate = false + $config_template = 'ntp/ntp.conf.erb' + $keys_enable = false + $keys_controlkey = '' + $keys_requestkey = '' + $keys_trusted = [] + $package_ensure = 'present' + $preferred_servers = [] + $restrict = true + $service_enable = true + $service_ensure = 'running' + $service_manage = true # On virtual machines allow large clock skews. $panic = str2bool($::is_virtual) ? { diff --git a/spec/classes/ntp_config_spec.rb b/spec/classes/ntp_config_spec.rb index 53836a0..51db491 100644 --- a/spec/classes/ntp_config_spec.rb +++ b/spec/classes/ntp_config_spec.rb @@ -167,62 +167,85 @@ describe 'ntp::config' do (content.split("\n") & expected_lines).should == expected_lines end end - end - ['Debian', 'RedHat','SuSE', 'FreeBSD', 'Archlinux'].each do |osfamily| - describe "keys for osfamily #{osfamily}" do + ['Debian', 'RedHat','SuSE', 'FreeBSD', 'Archlinux'].each do |osfamily| + + describe "keys for osfamily #{osfamily}" do + context "when enabled" do + let(:facts) {{ :osfamily => osfamily }} + let(:params) {{ + :keys_enable => true, + :keys_file => '/etc/ntp/ntp.keys', + :keys_trusted => ['1', '2', '3'], + :keys_controlkey => '2', + :keys_requestkey => '3', + }} + + it { should contain_file('/etc/ntp').with({ + 'ensure' => 'directory'}) + } + it { should contain_file('/etc/ntp.conf').with({ + 'content' => /trustedkey 1 2 3/}) + } + it { should contain_file('/etc/ntp.conf').with({ + 'content' => /controlkey 2/}) + } + it { should contain_file('/etc/ntp.conf').with({ + 'content' => /requestkey 3/}) + } + end + end - context "when enabled" do + context "when disabled" do let(:facts) {{ :osfamily => osfamily }} let(:params) {{ - :keys_enable => true, + :keys_enable => false, :keys_file => '/etc/ntp/ntp.keys', :keys_trusted => ['1', '2', '3'], :keys_controlkey => '2', :keys_requestkey => '3', }} - it { should contain_file('/etc/ntp').with({ + it { should_not contain_file('/etc/ntp').with({ 'ensure' => 'directory'}) } - it { should contain_file('/etc/ntp.conf').with({ + it { should_not contain_file('/etc/ntp.conf').with({ 'content' => /trustedkey 1 2 3/}) } - it { should contain_file('/etc/ntp.conf').with({ + it { should_not contain_file('/etc/ntp.conf').with({ 'content' => /controlkey 2/}) } - it { should contain_file('/etc/ntp.conf').with({ + it { should_not contain_file('/etc/ntp.conf').with({ 'content' => /requestkey 3/}) } end end - context "when disabled" do - let(:facts) {{ :osfamily => osfamily }} - let(:params) {{ - :keys_enable => false, - :keys_file => '/etc/ntp/ntp.keys', - :keys_trusted => ['1', '2', '3'], - :keys_controlkey => '2', - :keys_requestkey => '3', - }} + describe 'preferred servers' do + context "when set" do + let(:facts) {{ :osfamily => osfamily }} + let(:params) {{ + :servers => ['a', 'b', 'c', 'd'], + :preferred_servers => ['a', 'b'] + }} + + it { should contain_file('/etc/ntp.conf').with({ + 'content' => /server a prefer\nserver b prefer\nserver c\nserver d/}) + } + end + context "when not set" do + let(:facts) {{ :osfamily => osfamily }} + let(:params) {{ + :servers => ['a', 'b', 'c', 'd'], + :preferred_servers => [] + }} - it { should_not contain_file('/etc/ntp').with({ - 'ensure' => 'directory'}) - } - it { should_not contain_file('/etc/ntp.conf').with({ - 'content' => /trustedkey 1 2 3/}) - } - it { should_not contain_file('/etc/ntp.conf').with({ - 'content' => /controlkey 2/}) - } - it { should_not contain_file('/etc/ntp.conf').with({ - 'content' => /requestkey 3/}) - } + it { should_not contain_file('/etc/ntp.conf').with({ + 'content' => /server a prefer/}) + } + end end end - - - end + end diff --git a/spec/system/ntp_config_spec.rb b/spec/system/ntp_config_spec.rb index 263bc9d..194cdf1 100644 --- a/spec/system/ntp_config_spec.rb +++ b/spec/system/ntp_config_spec.rb @@ -27,9 +27,9 @@ describe 'ntp::config class' do end end - describe file('/etc/ntp.conf') do - it { should be_file } - it { should contain line } - end + describe file('/etc/ntp.conf') do + it { should be_file } + it { should contain line } + end end diff --git a/spec/system/preferred_servers_spec.rb b/spec/system/preferred_servers_spec.rb new file mode 100644 index 0000000..686861b --- /dev/null +++ b/spec/system/preferred_servers_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper_system' + +describe 'preferred servers' do + it 'applies cleanly' do + puppet_apply(%{ + class { '::ntp': + servers => ['a', 'b', 'c', 'd'], + preferred_servers => ['c', 'd'], + } + }) + end + + describe file('/etc/ntp.conf') do + it { should be_file } + it { should contain 'server a' } + it { should contain 'server b' } + it { should contain 'server c prefer' } + it { should contain 'server d prefer' } + end +end diff --git a/templates/ntp.conf.erb b/templates/ntp.conf.erb index 0e86884..c0a821b 100644 --- a/templates/ntp.conf.erb +++ b/templates/ntp.conf.erb @@ -17,7 +17,7 @@ restrict -6 ::1 # Servers <% [@servers].flatten.each do |server| -%> -server <%= server %><%= ' prefer' if [@preferred_servers].include?(server) %> +server <%= server %><% if @preferred_servers.include?(server) -%> prefer<% end %> <% end -%> <% if scope.lookupvar('::is_virtual') == "false" -%> -- cgit v1.2.3