diff options
author | Tim Smith <tsmith@chef.io> | 2020-08-28 12:59:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-28 12:59:20 -0700 |
commit | a024c79cff1a81ae5ff6c7479f2ee939201f40c6 (patch) | |
tree | 337836f69b47f5b3a3c011381f4f382194c0327f /spec | |
parent | ab528a841f14b4410c37f7f81da582b6220a2cd9 (diff) | |
parent | 1ebf2e1f232af535ff2c4cb78579de62707362cb (diff) | |
download | chef-a024c79cff1a81ae5ff6c7479f2ee939201f40c6.tar.gz |
Merge pull request #10362 from chef/client_updates
Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/resource/chef_client_cron_spec.rb | 40 | ||||
-rw-r--r-- | spec/unit/resource/chef_client_launchd_spec.rb | 62 |
2 files changed, 65 insertions, 37 deletions
diff --git a/spec/unit/resource/chef_client_cron_spec.rb b/spec/unit/resource/chef_client_cron_spec.rb index 00d12b82a8..121758ac73 100644 --- a/spec/unit/resource/chef_client_cron_spec.rb +++ b/spec/unit/resource/chef_client_cron_spec.rb @@ -29,6 +29,11 @@ describe Chef::Resource::ChefClientCron do expect(resource.action).to eql([:add]) end + it "supports :add and :remove actions" do + expect { resource.action :add }.not_to raise_error + expect { resource.action :remove }.not_to raise_error + end + it "coerces splay to an Integer" do resource.splay "10" expect(resource.splay).to eql(10) @@ -38,6 +43,10 @@ describe Chef::Resource::ChefClientCron do expect { resource.splay("-10") }.to raise_error(Chef::Exceptions::ValidationFailed) end + it "builds a default value for chef_binary_path dist values" do + expect(resource.chef_binary_path).to eql("/opt/chef/bin/chef-client") + end + it "raises an error if nice is less than -20" do expect { resource.nice(-21) }.to raise_error(Chef::Exceptions::ValidationFailed) end @@ -51,10 +60,6 @@ describe Chef::Resource::ChefClientCron do expect(resource.nice).to eql(10) end - it "builds a default value for chef_binary_path dist values" do - expect(resource.chef_binary_path).to eql("/opt/chef/bin/chef-client") - end - it "log_directory is /Library/Logs/Chef on macOS systems" do node.automatic_attrs[:platform_family] = "mac_os_x" node.automatic_attrs[:platform] = "mac_os_x" @@ -66,11 +71,6 @@ describe Chef::Resource::ChefClientCron do expect(resource.log_directory).to eql("/var/log/chef") end - it "supports :add and :remove actions" do - expect { resource.action :add }.not_to raise_error - expect { resource.action :remove }.not_to raise_error - end - describe "#splay_sleep_time" do it "uses shard_seed attribute if present" do node.automatic_attrs[:shard_seed] = "73399073" @@ -84,7 +84,7 @@ describe Chef::Resource::ChefClientCron do end end - describe "#cron_command" do + describe "#client_command" do before do allow(provider).to receive(:splay_sleep_time).and_return("123") end @@ -92,55 +92,55 @@ describe Chef::Resource::ChefClientCron do let(:root_path) { windows? ? "C:\\chef/client.rb" : "/etc/chef/client.rb" } it "creates a valid command if using all default properties" do - expect(provider.cron_command).to eql( + expect(provider.client_command).to eql( "/bin/sleep 123; /opt/chef/bin/chef-client -c #{root_path} -L /var/log/chef/client.log" ) end it "uses daemon_options if set" do resource.daemon_options ["--foo 1", "--bar 2"] - expect(provider.cron_command).to eql( + expect(provider.client_command).to eql( "/bin/sleep 123; /opt/chef/bin/chef-client --foo 1 --bar 2 -c #{root_path} -L /var/log/chef/client.log" ) end it "uses custom config dir if set" do resource.config_directory "/etc/some_other_dir" - expect(provider.cron_command).to eql("/bin/sleep 123; /opt/chef/bin/chef-client -c /etc/some_other_dir/client.rb -L /var/log/chef/client.log") + expect(provider.client_command).to eql("/bin/sleep 123; /opt/chef/bin/chef-client -c /etc/some_other_dir/client.rb -L /var/log/chef/client.log") 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( + expect(provider.client_command).to eql( "/bin/sleep 123; /opt/chef/bin/chef-client -c #{root_path} -L /var/log/my-chef/my-client.log" ) end it "uses mailto if set" do resource.mailto "bob@example.com" - expect(provider.cron_command).to eql( + expect(provider.client_command).to eql( "/bin/sleep 123; /opt/chef/bin/chef-client -c #{root_path} -L /var/log/chef/client.log || 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( + expect(provider.client_command).to eql( "/bin/sleep 123; /usr/local/bin/chef-client -c #{root_path} -L /var/log/chef/client.log" ) end it "appends to the log file appending if set to false" do resource.append_log_file false - expect(provider.cron_command).to eql( + expect(provider.client_command).to eql( "/bin/sleep 123; /opt/chef/bin/chef-client -c #{root_path} > /var/log/chef/client.log 2>&1" ) end it "sets the license acceptance flag if set" do resource.accept_chef_license true - expect(provider.cron_command).to eql( + expect(provider.client_command).to eql( "/bin/sleep 123; /opt/chef/bin/chef-client -c #{root_path} --chef-license accept -L /var/log/chef/client.log" ) end @@ -148,8 +148,8 @@ describe Chef::Resource::ChefClientCron do it "uses nice if set" do allow(provider).to receive(:which).with("nice").and_return("/usr/bin/nice") resource.nice(-15) - expect(provider.cron_command).to eql( - "/bin/sleep 123; /usr/bin/nice -n -15 /opt/chef/bin/chef-client -c /etc/chef/client.rb -L /var/log/chef/client.log" + expect(provider.client_command).to eql( + "/bin/sleep 123; /usr/bin/nice -n -15 /opt/chef/bin/chef-client -c #{root_path} -L /var/log/chef/client.log" ) end end diff --git a/spec/unit/resource/chef_client_launchd_spec.rb b/spec/unit/resource/chef_client_launchd_spec.rb index 4d07471ed5..1d0015cb0d 100644 --- a/spec/unit/resource/chef_client_launchd_spec.rb +++ b/spec/unit/resource/chef_client_launchd_spec.rb @@ -29,15 +29,24 @@ describe Chef::Resource::ChefClientLaunchd do expect(resource.action).to eql([:enable]) end - it "builds a default value for chef_binary_path dist values" do - expect(resource.chef_binary_path).to eql("/opt/chef/bin/chef-client") - end - it "supports :enable and :disable actions" do expect { resource.action :enable }.not_to raise_error expect { resource.action :disable }.not_to raise_error end + it "coerces splay to an Integer" do + resource.splay "10" + expect(resource.splay).to eql(10) + end + + it "raises an error if splay is not a positive number" do + expect { resource.splay("-10") }.to raise_error(Chef::Exceptions::ValidationFailed) + end + + it "builds a default value for chef_binary_path dist values" do + expect(resource.chef_binary_path).to eql("/opt/chef/bin/chef-client") + end + it "raises an error if interval is not a positive number" do expect { resource.interval("-10") }.to raise_error(Chef::Exceptions::ValidationFailed) end @@ -60,39 +69,58 @@ describe Chef::Resource::ChefClientLaunchd do expect(resource.nice).to eql(10) end - describe "#all_daemon_options" do - it "returns log and config flags if by default" do - expect(provider.all_daemon_options).to eql( - ["-L", "/Library/Logs/Chef/client.log", "-c", "/etc/chef/client.rb"] + describe "#splay_sleep_time" do + it "uses shard_seed attribute if present" do + node.automatic_attrs[:shard_seed] = "73399073" + expect(provider.splay_sleep_time(300)).to satisfy { |v| v >= 0 && v <= 300 } + end + + it "uses a hex conversion of a md5 hash of the splay if present" do + node.automatic_attrs[:shard_seed] = nil + allow(node).to receive(:name).and_return("test_node") + expect(provider.splay_sleep_time(300)).to satisfy { |v| v >= 0 && v <= 300 } + end + end + + describe "#client_command" do + before do + allow(provider).to receive(:splay_sleep_time).and_return("123") + end + + let(:root_path) { windows? ? "C:\\chef/client.rb" : "/etc/chef/client.rb" } + + it "creates a valid command if using all default properties" do + expect(provider.client_command).to eql( + "/bin/sleep 123; /opt/chef/bin/chef-client -c #{root_path} -L /Library/Logs/Chef/client.log" ) end - it "appends to any passed daemon options" do + it "adds custom daemon options from daemon_options property" do resource.daemon_options %w{foo bar} - expect(provider.all_daemon_options).to eql( - ["foo", "bar", "-L", "/Library/Logs/Chef/client.log", "-c", "/etc/chef/client.rb"] + expect(provider.client_command).to eql( + "/bin/sleep 123; /opt/chef/bin/chef-client foo bar -c #{root_path} -L /Library/Logs/Chef/client.log" ) end it "adds license acceptance flags if the property is set" do resource.accept_chef_license true - expect(provider.all_daemon_options).to eql( - ["-L", "/Library/Logs/Chef/client.log", "-c", "/etc/chef/client.rb", "--chef-license", "accept"] + expect(provider.client_command).to eql( + "/bin/sleep 123; /opt/chef/bin/chef-client -c #{root_path} -L /Library/Logs/Chef/client.log --chef-license accept" ) end it "uses custom config dir if set" do resource.config_directory "/etc/some_other_dir" - expect(provider.all_daemon_options).to eql( - ["-L", "/Library/Logs/Chef/client.log", "-c", "/etc/some_other_dir/client.rb"] + expect(provider.client_command).to eql( + "/bin/sleep 123; /opt/chef/bin/chef-client -c /etc/some_other_dir/client.rb -L /Library/Logs/Chef/client.log" ) 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.all_daemon_options).to eql( - ["-L", "/var/log/my-chef/my-client.log", "-c", "/etc/chef/client.rb"] + expect(provider.client_command).to eql( + "/bin/sleep 123; /opt/chef/bin/chef-client -c #{root_path} -L /var/log/my-chef/my-client.log" ) end end |