-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chef-client] Enable chef-client scheduled task to behave like cron, …
…with predictable splay and start time Signed-off-by: George Holt <[email protected]>
- Loading branch information
George Holt
authored and
George Holt
committed
Jul 21, 2021
1 parent
8556de0
commit 4cd29c2
Showing
14 changed files
with
163 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,57 @@ | ||
# Chefspec and windows aren't the best of friends. Running this on a non-windows | ||
# host results in win32ole load errors. | ||
|
||
# require 'spec_helper' | ||
# | ||
# describe 'chef-client::task' do | ||
# context 'when given override attributes' do | ||
# let(:chef_run) do | ||
# ChefSpec::ServerRunner.new(platform: 'windows', version: '2012R2', step_into: ['chef_client_scheduled_task']) do |node| | ||
# node.override['chef_client']['task']['start_time'] = 'Tue Sep 13 15:46:33 EDT 2016' | ||
# node.override['chef_client']['task']['user'] = 'system' | ||
# node.override['chef_client']['task']['password'] = 'secret' | ||
# node.override['chef_client']['task']['frequency'] = 'hourly' | ||
# node.override['chef_client']['task']['frequency_modifier'] = 60 | ||
# end.converge(described_recipe) | ||
# end | ||
# | ||
# it 'creates the windows_task resource with desired settings' do | ||
# expect(chef_run).to create_windows_task('chef-client').with( | ||
# command: 'cmd /c "C:/opscode/chef/bin/chef-client -L C:/chef/log/client.log -c C:/chef/client.rb -s 300 ^> NUL 2^>^&1"', | ||
# user: 'system', | ||
# password: 'secret', | ||
# frequency: :hourly, | ||
# frequency_modifier: 60, | ||
# start_time: 'Tue Sep 13 15:46:33 EDT 2016' | ||
# ) | ||
# end | ||
# end | ||
# end | ||
require 'spec_helper' | ||
|
||
describe 'chef-client::task' do | ||
let(:local_system) { instance_double('LocalSystem', account_simple_name: 'system') } | ||
let(:sid_class) { class_double('Chef::ReservedNames::Win32::Security::SID', LocalSystem: local_system, system_user?: true) } | ||
let(:node) { runner.node } | ||
let(:chef_run) { runner.converge(described_recipe) } | ||
let(:runner) { ChefSpec::SoloRunner.new(platform: 'windows', version: '2012R2', step_into: ['chef_client_scheduled_task']) } | ||
|
||
before do | ||
# Mock up the environment to behave like Windows | ||
allow(ENV).to receive(:[]).and_call_original | ||
allow(ENV).to receive(:[]).with('COMSPEC').and_return('cmd') | ||
stub_const('Chef::ReservedNames::Win32::Security::SID', sid_class) | ||
node.automatic['chef_client']['bin'] = 'C:/opscode/chef/bin/chef-client' | ||
end | ||
|
||
context 'when given override attributes' do | ||
before do | ||
node.override['chef_client']['task']['start_time'] = '16:10' | ||
node.override['chef_client']['task']['frequency'] = 'hourly' | ||
node.override['chef_client']['task']['frequency_modifier'] = 1 | ||
node.override['chef_client']['daemon_options'] = ['-s', '300', '^>', 'NUL', '2^>^&1'] | ||
end | ||
|
||
it 'creates the windows_task resource with desired settings' do | ||
expect(chef_run).to create_windows_task('chef-client').with( | ||
command: 'cmd /c "C:/opscode/chef/bin/chef-client -L C:/chef/log/client.log -c C:/chef/client.rb -s 300 ^> NUL 2^>^&1"', | ||
user: 'SYSTEM', | ||
frequency: :hourly, | ||
frequency_modifier: 1, | ||
start_time: '16:10' | ||
) | ||
end | ||
end | ||
|
||
context 'when configured to use a consistent splay and snap frequency time' do | ||
let(:now) { Time.new('2021', '4', '15', '16', '7', '12') } | ||
before do | ||
node.override['chef_client']['task']['use_consistent_splay'] = true | ||
node.override['chef_client']['task']['snap_time_to_frequency'] = true | ||
allow(Time).to receive(:now).and_return(now) | ||
allow_any_instance_of(Chef::Resource::ChefClientScheduledTask).to receive(:splay_sleep_time).and_return(222) | ||
end | ||
|
||
it 'creates the windows_task resource with desired settings' do | ||
expect(chef_run).to create_windows_task('chef-client').with( | ||
command: 'cmd /c "C:/windows/system32/windowspowershell/v1.0/powershell.exe Start-Sleep -s 222 && C:/opscode/chef/bin/chef-client -L C:/chef/log/client.log -c C:/chef/client.rb"', | ||
start_day: '04/15/2021', | ||
start_time: '16:30' | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
apt_update 'update' | ||
include_recipe 'test::config' | ||
include_recipe 'cron::default' | ||
package 'crontabs' if platform?('fedora') # ensures we actually have the /etc/cron.d dir | ||
include_recipe 'chef-client::cron' | ||
include_recipe 'chef-client::delete_validation' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
node.override['chef_client']['interval'] = 900 | ||
node.override['chef_client']['task']['snap_time_to_frequency'] = true | ||
node.override['chef_client']['task']['use_consistent_splay'] = true | ||
|
||
include_recipe 'test::config' | ||
include_recipe 'chef-client::task' | ||
include_recipe 'chef-client::delete_validation' | ||
|
||
chef_client_scheduled_task 'Chef Client on start' do | ||
user node['chef_client']['task']['user'] | ||
password node['chef_client']['task']['password'] | ||
frequency 'onstart' | ||
config_directory node['chef_client']['conf_dir'] | ||
log_directory node['chef_client']['log_dir'] | ||
log_file_name node['chef_client']['log_file'] | ||
chef_binary_path node['chef_client']['bin'] | ||
daemon_options node['chef_client']['daemon_options'] | ||
task_name "#{node['chef_client']['task']['name']}-onstart" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
describe command('C:/opscode/chef/embedded/bin/ohai virtualization -c C:/chef/client.rb') do | ||
its('exit_status') { should eq 0 } | ||
end | ||
|
||
describe file('C:/chef/client.rb') do | ||
its('content') { should match(/ohai.disabled_plugins = \["Mdadm"\]/) } | ||
its('content') { should match(/ohai.optional_plugins = \["Passwd"\]/) } | ||
its('content') { should match(%r{ohai.plugin_path << "/tmp/kitchen/ohai/plugins"}) } | ||
end | ||
|
||
# the inspec resource requires PS 3.0+ and 2k8r2 only has PS 2.0 by default | ||
unless os.release.to_f == 6.1 | ||
describe windows_task('chef-client') do | ||
it { should be_enabled } | ||
its('run_as_user') { should eq 'SYSTEM' } | ||
its('task_to_run') { should match %r{cmd.exe /c C:/windows/system32/windowspowershell/v1.0/powershell.exe Start-Sleep -s ([0-9]|[1-9][0-9]|[1-9][0-9][0-9]) C:/opscode/chef/bin/chef-client -L C:/chef/log/client.log -c C:/chef/client.rb -s 300} } | ||
end | ||
|
||
describe windows_task('chef-client-onstart') do | ||
it { should be_enabled } | ||
end | ||
end |