diff options
author | Phil Dibowitz <phil@ipom.com> | 2019-11-01 17:59:04 -0700 |
---|---|---|
committer | Phil Dibowitz <phil@ipom.com> | 2019-11-04 15:46:07 -0800 |
commit | d1305e8f114f89bf4a518afbdebba23544f73fd5 (patch) | |
tree | 7f0d35902dc6168dfef292dd838167a0eb257562 /spec | |
parent | a16e94afeefe34abe1c57e6c4f44e25721db7082 (diff) | |
download | chef-d1305e8f114f89bf4a518afbdebba23544f73fd5.tar.gz |
Fix enable on indirect units
This follows this `is_masked` path to ensure we don't try to enable OR
disable systemd units if they are indirect
Fixes #9041
Signed-off-by: Phil Dibowitz <phil@ipom.com>
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/provider/service/systemd_service_spec.rb | 28 | ||||
-rw-r--r-- | spec/unit/provider/systemd_unit_spec.rb | 57 |
2 files changed, 79 insertions, 6 deletions
diff --git a/spec/unit/provider/service/systemd_service_spec.rb b/spec/unit/provider/service/systemd_service_spec.rb index 15b79922b0..983d524d25 100644 --- a/spec/unit/provider/service/systemd_service_spec.rb +++ b/spec/unit/provider/service/systemd_service_spec.rb @@ -36,11 +36,11 @@ describe Chef::Provider::Service::Systemd do let(:provider) { Chef::Provider::Service::Systemd.new(new_resource, run_context) } let(:shell_out_success) do - double("shell_out", exitstatus: 0, error?: false) + double("shell_out", exitstatus: 0, error?: false, stdout: "") end let(:shell_out_failure) do - double("shell_out", exitstatus: 1, error?: true) + double("shell_out", exitstatus: 1, error?: true, stdout: "") end let(:current_resource) { Chef::Resource::Service.new(service_name) } @@ -56,6 +56,7 @@ describe Chef::Provider::Service::Systemd do allow(provider).to receive(:is_active?).and_return(false) allow(provider).to receive(:is_enabled?).and_return(false) allow(provider).to receive(:is_masked?).and_return(false) + allow(provider).to receive(:is_indirect?).and_return(false) end it "should create a current resource with the name of the new resource" do @@ -359,6 +360,29 @@ describe Chef::Provider::Service::Systemd do expect(provider.is_masked?).to be false end end + + describe "is_indirect?" do + before(:each) do + provider.current_resource = current_resource + current_resource.service_name(service_name) + allow(provider).to receive(:which).with("systemctl").and_return(systemctl_path.to_s) + end + + it "should return true if '#{systemctl_path} --system is-enabled service_name' returns 'indirect'" do + expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name_escaped}", {}).and_return(double(stdout: "indirect", exitstatus: shell_out_success)) + expect(provider.is_indirect?).to be true + end + + it "should return false if '#{systemctl_path} --system is-enabled service_name' returns 0 and outputs something other than 'indirect'" do + expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name_escaped}", {}).and_return(double(stdout: "enabled", exitstatus: shell_out_success)) + expect(provider.is_indirect?).to be false + end + + it "should return false if '#{systemctl_path} --system is-enabled service_name' returns anything except 0 and outputs somethign other than 'indirect''" do + expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name_escaped}", {}).and_return(double(stdout: "enabled", exitstatus: shell_out_failure)) + expect(provider.is_indirect?).to be false + end + end end end end diff --git a/spec/unit/provider/systemd_unit_spec.rb b/spec/unit/provider/systemd_unit_spec.rb index 899d3dc87e..cf26c99b77 100644 --- a/spec/unit/provider/systemd_unit_spec.rb +++ b/spec/unit/provider/systemd_unit_spec.rb @@ -65,11 +65,19 @@ describe Chef::Provider::SystemdUnit do end let(:shell_out_masked) do - double("shell_out", exit_status: 0, error?: false, stdout: "masked") + double("shell_out", exitstatus: 0, error?: false, stdout: "masked") end let(:shell_out_static) do - double("shell_out", exit_status: 0, error?: false, stdout: "static") + double("shell_out", exitstatus: 0, error?: false, stdout: "static") + end + + let(:shell_out_disabled) do + double("shell_out", exitstatus: 1, error?: true, stdout: "disabled") + end + + let(:shell_out_indirect) do + double("shell_out", exitstatus: 0, error?: true, stdout: "indirect") end before(:each) do @@ -859,7 +867,7 @@ describe Chef::Provider::SystemdUnit do current_resource.user(user_name) expect(provider).to receive(:shell_out_compacted) .with(systemctl_path, "--user", "is-enabled", unit_name, user_cmd_opts) - .and_return(shell_out_failure) + .and_return(shell_out_disabled) expect(provider.enabled?).to be false end end @@ -875,7 +883,7 @@ describe Chef::Provider::SystemdUnit do it "returns false when unit is not enabled" do expect(provider).to receive(:shell_out_compacted) .with(systemctl_path, "--system", "is-enabled", unit_name) - .and_return(shell_out_failure) + .and_return(shell_out_disabled) expect(provider.enabled?).to be false end end @@ -962,6 +970,47 @@ describe Chef::Provider::SystemdUnit do end end end + + describe "#indirect?" do + before(:each) do + provider.current_resource = current_resource + allow(provider).to receive(:which).with("systemctl").and_return(systemctl_path.to_s) + end + + context "when a user is specified" do + it "returns true when the unit is indirect" do + current_resource.user(user_name) + expect(provider).to receive(:shell_out_compacted) + .with(systemctl_path, "--user", "is-enabled", unit_name, user_cmd_opts) + .and_return(shell_out_indirect) + expect(provider.indirect?).to be true + end + + it "returns false when the unit is not indirect" do + current_resource.user(user_name) + expect(provider).to receive(:shell_out_compacted) + .with(systemctl_path, "--user", "is-enabled", unit_name, user_cmd_opts) + .and_return(shell_out_static) + expect(provider.indirect?).to be false + end + end + + context "when no user is specified" do + it "returns true when the unit is indirect" do + expect(provider).to receive(:shell_out_compacted) + .with(systemctl_path, "--system", "is-enabled", unit_name) + .and_return(shell_out_indirect) + expect(provider.indirect?).to be true + end + + it "returns false when the unit is not indirect" do + expect(provider).to receive(:shell_out_compacted) + .with(systemctl_path, "--system", "is-enabled", unit_name) + .and_return(shell_out_static) + expect(provider.indirect?).to be false + end + end + end end end end |