-
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 <gholtiii@me.com>
- Loading branch information
George Holt
authored and
George Holt
committed
Jul 19, 2021
1 parent
8556de0
commit 361b86e
Showing
13 changed files
with
164 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
require 'chefspec' | ||
require 'chefspec/berkshelf' | ||
require 'chefspec/policyfile' | ||
|
||
RSpec.configure do |config| | ||
config.color = true # Use color in STDOUT | ||
config.formatter = :documentation # Use the specified formatter | ||
config.log_level = :error # Avoid deprecation notice SPAM | ||
|
||
config.before do | ||
unless RUBY_PLATFORM =~ /mswin|mingw32|windows/ | ||
# This enables spec testing of windows recipes on nix OSes. This is used | ||
# to address the use of Win32::Service in recipe chef-client::task | ||
Win32 = Module.new unless defined?(Win32) | ||
Win32::Service = Class.new unless defined?(Win32::Service) | ||
allow(::Win32::Service).to receive(:exists?).with(anything).and_return(false) | ||
allow(::Win32::Service).to receive(:exists?).with('chef-client').and_return(true) | ||
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,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 |
---|---|---|
@@ -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 |