summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-03-23 17:02:04 -0700
committerTim Smith <tsmith84@gmail.com>2020-03-23 17:02:47 -0700
commit9a38a1d341c3260d0a8796d0afd302f6cca9c6a6 (patch)
tree1bd93803edc6668659cf2089f3da4b3a4646095a
parentcea81cc0c83ec374b26eb19a4f0ae007670edeb6 (diff)
downloadchef-9a38a1d341c3260d0a8796d0afd302f6cca9c6a6.tar.gz
Fix some bugs in command generation and add specs
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/chef_client_cron.rb7
-rw-r--r--spec/unit/resource/chef_client_cron_spec.rb36
2 files changed, 40 insertions, 3 deletions
diff --git a/lib/chef/resource/chef_client_cron.rb b/lib/chef/resource/chef_client_cron.rb
index 9d664b033a..5313800622 100644
--- a/lib/chef/resource/chef_client_cron.rb
+++ b/lib/chef/resource/chef_client_cron.rb
@@ -165,9 +165,10 @@ class Chef
def cron_command
cmd = ""
cmd << "/bin/sleep #{splay_sleep_time(new_resource.splay)}; "
- cmd << "#{new_resource.env_vars} " if new_resource.env_vars
- cmd << "#{new_resource.chef_binary_path} #{new_resource.daemon_options.join(" ")}"
- cmd << " #{new_resource.append_log_file ? ">>" : ">"} #{::File.join(new_resource.log_directory, new_resource.log_file_name)} 2>&1"
+ cmd << "#{new_resource.environment} " unless new_resource.environment.empty?
+ cmd << "#{new_resource.chef_binary_path} "
+ cmd << "#{new_resource.daemon_options.join(" ")} " unless new_resource.daemon_options.empty?
+ cmd << "#{new_resource.append_log_file ? ">>" : ">"} #{::File.join(new_resource.log_directory, new_resource.log_file_name)} 2>&1"
cmd << " || echo \"#{Chef::Dist::PRODUCT} execution failed\"" if new_resource.mailto
cmd
end
diff --git a/spec/unit/resource/chef_client_cron_spec.rb b/spec/unit/resource/chef_client_cron_spec.rb
index a62be5d49e..fec9461eeb 100644
--- a/spec/unit/resource/chef_client_cron_spec.rb
+++ b/spec/unit/resource/chef_client_cron_spec.rb
@@ -61,4 +61,40 @@ describe Chef::Resource::ChefClientCron do
expect(provider.splay_sleep_time(300)).to eql(114)
end
end
+
+ describe "#cron_command" do
+ before do
+ node.automatic_attrs[:shard_seed] = "73399073"
+ end
+
+ it "creates a valid command if using all default properties" do
+ expect(provider.cron_command).to eql("/bin/sleep 73399073; /opt/chef/bin/chef-client > /var/log/chef/client.log 2>&1")
+ end
+
+ it "uses daemon_options if set" do
+ resource.daemon_options ["--foo 1", "--bar 2"]
+ expect(provider.cron_command).to eql("/bin/sleep 73399073; /opt/chef/bin/chef-client --foo 1 --bar 2 > /var/log/chef/client.log 2>&1")
+ end
+
+ it "uses custom log files / paths if set" do
+ resource.log_file_name "my-client.log"
+ resource.log_directory "/var/log/my-chef/"
+ expect(provider.cron_command).to eql("/bin/sleep 73399073; /opt/chef/bin/chef-client > /var/log/my-chef/my-client.log 2>&1")
+ end
+
+ it "uses mailto if set" do
+ resource.mailto "bob@example.com"
+ expect(provider.cron_command).to eql("/bin/sleep 73399073; /opt/chef/bin/chef-client > /var/log/chef/client.log 2>&1 || echo \"Chef Infra Client execution failed\"")
+ end
+
+ it "uses custom chef-client binary if set" do
+ resource.chef_binary_path "/usr/local/bin/chef-client"
+ expect(provider.cron_command).to eql("/bin/sleep 73399073; /usr/local/bin/chef-client > /var/log/chef/client.log 2>&1")
+ end
+
+ it "appends to the log file appending if set" do
+ resource.append_log_file true
+ expect(provider.cron_command).to eql("/bin/sleep 73399073; /opt/chef/bin/chef-client >> /var/log/chef/client.log 2>&1")
+ end
+ end
end