diff options
author | Tim Smith <tsmith@chef.io> | 2019-05-29 21:39:43 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-29 21:39:43 -0700 |
commit | 75322df36a8ca313ef3352bfcb444fa5a21700c9 (patch) | |
tree | 404e7a05c7ebc4bde75acbbeb6807dc001e0e7e4 /spec | |
parent | f7383fee35e7df412b4bc4e7c96df578581bee18 (diff) | |
parent | 2f87006b10d8663d4580a0acbb7ba561561a02c2 (diff) | |
download | chef-75322df36a8ca313ef3352bfcb444fa5a21700c9.tar.gz |
Merge pull request #8610 from chef/backport_9
Fix for cron resource get confused by environment/property mismatch
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/provider/cron_spec.rb | 143 |
1 files changed, 123 insertions, 20 deletions
diff --git a/spec/unit/provider/cron_spec.rb b/spec/unit/provider/cron_spec.rb index 03e3599b35..2377fe7dbc 100644 --- a/spec/unit/provider/cron_spec.rb +++ b/spec/unit/provider/cron_spec.rb @@ -690,33 +690,136 @@ CRONTAB end context "when there is a crontab with a matching and identical section" do - before :each do - @provider.cron_exists = true - allow(@provider).to receive(:cron_different?).and_return(false) - allow(@provider).to receive(:read_crontab).and_return(<<~CRONTAB) - 0 2 * * * /some/other/command + context "when environment variable is not used" do + before :each do + @provider.cron_exists = true + allow(@provider).to receive(:cron_different?).and_return(false) + allow(@provider).to receive(:read_crontab).and_return(<<~CRONTAB) + 0 2 * * * /some/other/command - # Chef Name: cronhole some stuff - * 5 * * * /bin/true + # Chef Name: cronhole some stuff + SHELL=/bash + * 5 * * * /bin/true - # Another comment -CRONTAB - end + # Another comment + CRONTAB + end - it "should not update the crontab" do - expect(@provider).not_to receive(:write_crontab) - @provider.run_action(:create) + it "should not update the crontab" do + expect(@provider).not_to receive(:write_crontab) + @provider.run_action(:create) + end + + it "should not mark the resource as updated" do + @provider.run_action(:create) + expect(@new_resource).not_to be_updated_by_last_action + end + + it "should log nothing changed" do + expect(logger).to receive(:trace).with("Found cron '#{@new_resource.name}'") + expect(logger).to receive(:trace).with("Skipping existing cron entry '#{@new_resource.name}'") + @provider.run_action(:create) + end end - it "should not mark the resource as updated" do - @provider.run_action(:create) - expect(@new_resource).not_to be_updated_by_last_action + context "when environment variable is used" do + before :each do + @provider.cron_exists = true + allow(@provider).to receive(:read_crontab).and_return(<<~CRONTAB) + 0 2 * * * /some/other/command + + # Chef Name: cronhole some stuff + SHELL=/bash + ENV=environment + 30 * * * * /bin/true + + # Another comment + CRONTAB + end + context "contains an entry that can also be specified as a `property`" do + before :each do + @new_resource.environment = { "SHELL" => "/bash", "ENV" => "environment" } + end + + it "should raise a warning for idempotency" do + expect(logger).to receive(:warn).with("cronhole some stuff: the environment property contains the 'SHELL' variable, which should be set separately as a property.") + @provider.run_action(:create) + end + + it "should not update the crontab" do + expect(@provider).not_to receive(:write_crontab) + @provider.run_action(:create) + end + + it "should not mark the resource as updated" do + expect(@new_resource).not_to be_updated_by_last_action + @provider.run_action(:create) + end + end + + context "contains an entry that cannot be specified as a `property`" do + before :each do + @new_resource.environment = { "ENV" => "environment" } + @new_resource.shell "/bash" + end + + it "should not raise a warning for idempotency" do + expect(logger).not_to receive(:warn).with("cronhole some stuff: the environment property contains the 'SHELL' variable, which should be set separately as a property.") + @provider.run_action(:create) + end + + it "should not update the crontab" do + expect(@provider).not_to receive(:write_crontab) + @provider.run_action(:create) + end + + it "should not mark the resource as updated" do + @provider.run_action(:create) + expect(@new_resource).not_to be_updated_by_last_action + end + end end - it "should log nothing changed" do - expect(logger).to receive(:trace).with("Found cron '#{@new_resource.name}'") - expect(logger).to receive(:trace).with("Skipping existing cron entry '#{@new_resource.name}'") - @provider.run_action(:create) + context "when environment variable is used with property" do + before :each do + @provider.cron_exists = true + allow(@provider).to receive(:read_crontab).and_return(<<~CRONTAB) + 0 2 * * * /some/other/command + + # Chef Name: cronhole some stuff + SHELL=/bash + ENV=environment + 30 * * * * /bin/true + + # Another comment + CRONTAB + end + + context "when environment variable is same as property" do + it "should throw an error" do + @new_resource.shell "/bash" + @new_resource.environment "SHELL" => "/bash" + expect do + @provider.run_action(:create) + end.to raise_error(Chef::Exceptions::Cron, /cronhole some stuff: the 'SHELL' property is set and environment property also contains the 'SHELL' variable. Remove the variable from the environment property./) + end + end + + context "when environment variable is different from property" do + it "should not update the crontab" do + @new_resource.shell "/bash" + @new_resource.environment "ENV" => "environment" + expect(@provider).not_to receive(:write_crontab) + @provider.run_action(:create) + end + + it "should not mark the resource as updated" do + @new_resource.shell "/bash" + @new_resource.environment "ENV" => "environment" + @provider.run_action(:create) + expect(@new_resource).not_to be_updated_by_last_action + end + end end end end |