Skip to content

Commit

Permalink
COOK-3636 Added tests & clarified README.
Browse files Browse the repository at this point in the history
COOK-3636 Added tests for new attributes & clarified README.
  • Loading branch information
Charles Johnson committed Oct 1, 2013
1 parent 2eeb023 commit 18ccf37
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ suites:
- name: default
run_list:
- recipe[ntp::default]
attributes:
ntp:
sync_clock: true
sync_hw_clock: true
- name: undo
run_list:
- recipe[ntp::undo]
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ Attributes
* `ntp['restrictions']` - (applies to NTP Servers only)
- Array, should be a list of restrict lines to define access to NTP clients on your LAN.

* ntp['sync_clock'] (applies to NTP Servers and Clients)
* `ntp['sync_clock']` (applies to NTP Servers and Clients)
- Boolean. Defaults to false. Forces the ntp daemon to be halted, an ntp -q command to be issued, and the ntp daemon to be restarted again on every Chef-client run. Will have no effect if drift is over 1000 seconds.

- Boolean, determines if the ntpdate command is issued to sync the system clock
* `ntp['sync_hw_clock']` (applies to NTP Servers and Clients)
- Boolean. Defaults to false. On *nix-based systems, forces the 'hwclock --systohc' command to be issued on every Chef-client run. This will sync the hardware clock to the system clock.
- Not available on Windows.

### Platform specific

Expand Down Expand Up @@ -202,6 +205,7 @@ Copyright 2012, Fletcher Nichol
Copyright 2012, Webtrends, Inc.
Copyright 2013, Limelight Networks, Inc.
Copyright 2013, Brad Knowles
Copyright 2013, Brad Beam
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@
notifies :stop, "service[#{node['ntp']['service']}]", :immediately
end

execute 'Sync system clock with ntp server' do
execute 'Force sync system clock with ntp server' do
command 'ntpd -q'
action :run
notifies :start, "service[#{node['ntp']['service']}]"
end
end

if node['ntp']['sync_hw_clock'] && ! platform_family?('windows')
execute 'Sync hardware clock with system clock' do
execute 'Force sync hardware clock with system clock' do
command 'hwclock --systohc'
action :run
end
Expand Down
45 changes: 45 additions & 0 deletions spec/unit/recipes/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@
end
end

it 'does not execute the "Force sync system clock with ntp server" command' do
expect(chef_run).not_to execute_command('ntpd -q')
end

it 'does not execute the "Force sync hardware clock with system clock" command' do
expect(chef_run).not_to execute_command('hwclock --systohc')
end

it 'starts the ntpd service' do
expect(chef_run).to start_service('ntpd')
end
Expand All @@ -87,6 +95,43 @@
expect(chef_run).to set_service_to_start_on_boot('ntpd')
end

context 'the sync_clock attribute is set' do
let(:chef_run) do
runner = ChefSpec::ChefRunner.new
runner.node.set['ntp']['sync_clock'] = true
runner.converge('ntp::default')
end

it 'executes the "Force sync system clock with ntp server" command' do
expect(chef_run).to execute_command('ntpd -q')
end
end

context 'the sync_hw_clock attribute is set on a non-Windows OS' do
let(:chef_run) do
runner = ChefSpec::ChefRunner.new
runner.node.set['ntp']['sync_hw_clock'] = true
runner.converge('ntp::default')
end

it 'executes the "Force sync hardware clock with system clock" command' do
expect(chef_run).to execute_command('hwclock --systohc')
end
end

context 'the sync_hw_clock attribute is set on a Windows OS' do
let(:chef_run) do
runner = ChefSpec::ChefRunner.new(platform: 'windows', version: '2008R2')
runner.node.set['ntp']['sync_hw_clock'] = true
runner.converge('ntp::default')
end

it 'does not executes the "Force sync hardware clock with system clock" command' do
pending('ChefSpec does not yet understand the inherits attribute in cookbook_file resources')
expect(chef_run).not_to execute_command('hwclock --systohc')
end
end

context 'on CentOS 5' do
let(:chef_run) { ChefSpec::ChefRunner.new(platform: 'centos', version: '5.8').converge('ntp::default') }

Expand Down

0 comments on commit 18ccf37

Please sign in to comment.