summaryrefslogtreecommitdiff
path: root/spec/unit
diff options
context:
space:
mode:
authorPhil Dibowitz <phil@ipom.com>2016-03-08 09:25:34 -0800
committerPhil Dibowitz <phil@ipom.com>2016-03-08 09:25:34 -0800
commitc54f2fe21ea9b0119b82bbcf7f313ee8f68f7b38 (patch)
treeef4e99a8a8195427a2a6d523c017841f348d75c3 /spec/unit
parent211ae6a47aedbd7a81042bdd42f577f0d11f8b85 (diff)
parent7f8af07e287282aefdb55bea57f50c05318787f2 (diff)
downloadchef-c54f2fe21ea9b0119b82bbcf7f313ee8f68f7b38.tar.gz
Merge pull request #4661 from davide125/systemd-user
Extend service resource to support systemd user services
Diffstat (limited to 'spec/unit')
-rw-r--r--spec/unit/provider/service/systemd_service_spec.rb106
1 files changed, 66 insertions, 40 deletions
diff --git a/spec/unit/provider/service/systemd_service_spec.rb b/spec/unit/provider/service/systemd_service_spec.rb
index cf76fdd07b..e0a94127b7 100644
--- a/spec/unit/provider/service/systemd_service_spec.rb
+++ b/spec/unit/provider/service/systemd_service_spec.rb
@@ -21,7 +21,16 @@ require "spec_helper"
describe Chef::Provider::Service::Systemd do
- let(:node) { Chef::Node.new }
+ let(:node) {
+ node = Chef::Node.new
+ node.default["etc"] = Hash.new
+ node.default["etc"]["passwd"] = {
+ "joe" => {
+ "uid" => 10000,
+ },
+ }
+ node
+ }
let(:events) { Chef::EventDispatch::Dispatcher.new }
@@ -171,15 +180,32 @@ describe Chef::Provider::Service::Systemd do
provider.start_service
end
- it "should call '#{systemctl_path} start service_name' if no start command is specified" do
- expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} start #{service_name}").and_return(shell_out_success)
- provider.start_service
+ context "when a user is not specified" do
+ it "should call '#{systemctl_path} --system start service_name' if no start command is specified" do
+ expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --system start #{service_name}", {}).and_return(shell_out_success)
+ provider.start_service
+ end
+
+ it "should not call '#{systemctl_path} --system start service_name' if it is already running" do
+ current_resource.running(true)
+ expect(provider).not_to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --system start #{service_name}", {})
+ provider.start_service
+ end
end
- it "should not call '#{systemctl_path} start service_name' if it is already running" do
- current_resource.running(true)
- expect(provider).not_to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} start #{service_name}")
- provider.start_service
+ context "when a user is specified" do
+ it "should call '#{systemctl_path} --user start service_name' if no start command is specified" do
+ current_resource.user("joe")
+ expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --user start #{service_name}", { "environment" => { "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/10000/bus" }, "user" => "joe" }).and_return(shell_out_success)
+ provider.start_service
+ end
+
+ it "should not call '#{systemctl_path} --user start service_name' if it is already running" do
+ current_resource.running(true)
+ current_resource.user("joe")
+ expect(provider).not_to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --user start #{service_name}", { "environment" => { "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/10000/bus" }, "user" => "joe" })
+ provider.start_service
+ end
end
it "should call the restart command if one is specified" do
@@ -189,9 +215,9 @@ describe Chef::Provider::Service::Systemd do
provider.restart_service
end
- it "should call '#{systemctl_path} restart service_name' if no restart command is specified" do
+ it "should call '#{systemctl_path} --system restart service_name' if no restart command is specified" do
current_resource.running(true)
- expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} restart #{service_name}").and_return(shell_out_success)
+ expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --system restart #{service_name}", {}).and_return(shell_out_success)
provider.restart_service
end
@@ -206,9 +232,9 @@ describe Chef::Provider::Service::Systemd do
end
context "when a reload command is not specified" do
- it "should call '#{systemctl_path} reload service_name' if the service is running" do
+ it "should call '#{systemctl_path} --system reload service_name' if the service is running" do
current_resource.running(true)
- expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} reload #{service_name}").and_return(shell_out_success)
+ expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --system reload #{service_name}", {}).and_return(shell_out_success)
provider.reload_service
end
@@ -227,15 +253,15 @@ describe Chef::Provider::Service::Systemd do
provider.stop_service
end
- it "should call '#{systemctl_path} stop service_name' if no stop command is specified" do
+ it "should call '#{systemctl_path} --system stop service_name' if no stop command is specified" do
current_resource.running(true)
- expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} stop #{service_name}").and_return(shell_out_success)
+ expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --system stop #{service_name}", {}).and_return(shell_out_success)
provider.stop_service
end
- it "should not call '#{systemctl_path} stop service_name' if it is already stopped" do
+ it "should not call '#{systemctl_path} --system stop service_name' if it is already stopped" do
current_resource.running(false)
- expect(provider).not_to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} stop #{service_name}")
+ expect(provider).not_to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --system stop #{service_name}", {})
provider.stop_service
end
end
@@ -247,13 +273,13 @@ describe Chef::Provider::Service::Systemd do
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
- it "should call '#{systemctl_path} enable service_name' to enable the service" do
- expect(provider).to receive(:shell_out!).with("#{systemctl_path} enable #{service_name}").and_return(shell_out_success)
+ it "should call '#{systemctl_path} --system enable service_name' to enable the service" do
+ expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system enable #{service_name}", {}).and_return(shell_out_success)
provider.enable_service
end
- it "should call '#{systemctl_path} disable service_name' to disable the service" do
- expect(provider).to receive(:shell_out!).with("#{systemctl_path} disable #{service_name}").and_return(shell_out_success)
+ it "should call '#{systemctl_path} --system disable service_name' to disable the service" do
+ expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system disable #{service_name}", {}).and_return(shell_out_success)
provider.disable_service
end
end
@@ -265,13 +291,13 @@ describe Chef::Provider::Service::Systemd do
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
- it "should call '#{systemctl_path} mask service_name' to mask the service" do
- expect(provider).to receive(:shell_out!).with("#{systemctl_path} mask #{service_name}").and_return(shell_out_success)
+ it "should call '#{systemctl_path} --system mask service_name' to mask the service" do
+ expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system mask #{service_name}", {}).and_return(shell_out_success)
provider.mask_service
end
- it "should call '#{systemctl_path} unmask service_name' to unmask the service" do
- expect(provider).to receive(:shell_out!).with("#{systemctl_path} unmask #{service_name}").and_return(shell_out_success)
+ it "should call '#{systemctl_path} --system unmask service_name' to unmask the service" do
+ expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system unmask #{service_name}", {}).and_return(shell_out_success)
provider.unmask_service
end
end
@@ -283,13 +309,13 @@ describe Chef::Provider::Service::Systemd do
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
- it "should return true if '#{systemctl_path} is-active service_name' returns 0" do
- expect(provider).to receive(:shell_out).with("#{systemctl_path} is-active #{service_name} --quiet").and_return(shell_out_success)
+ it "should return true if '#{systemctl_path} --system is-active service_name' returns 0" do
+ expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-active #{service_name} --quiet", {}).and_return(shell_out_success)
expect(provider.is_active?).to be true
end
- it "should return false if '#{systemctl_path} is-active service_name' returns anything except 0" do
- expect(provider).to receive(:shell_out).with("#{systemctl_path} is-active #{service_name} --quiet").and_return(shell_out_failure)
+ it "should return false if '#{systemctl_path} --system is-active service_name' returns anything except 0" do
+ expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-active #{service_name} --quiet", {}).and_return(shell_out_failure)
expect(provider.is_active?).to be false
end
end
@@ -301,13 +327,13 @@ describe Chef::Provider::Service::Systemd do
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
- it "should return true if '#{systemctl_path} is-enabled service_name' returns 0" do
- expect(provider).to receive(:shell_out).with("#{systemctl_path} is-enabled #{service_name} --quiet").and_return(shell_out_success)
+ it "should return true if '#{systemctl_path} --system is-enabled service_name' returns 0" do
+ expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name} --quiet", {}).and_return(shell_out_success)
expect(provider.is_enabled?).to be true
end
- it "should return false if '#{systemctl_path} is-enabled service_name' returns anything except 0" do
- expect(provider).to receive(:shell_out).with("#{systemctl_path} is-enabled #{service_name} --quiet").and_return(shell_out_failure)
+ it "should return false if '#{systemctl_path} --system is-enabled service_name' returns anything except 0" do
+ expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name} --quiet", {}).and_return(shell_out_failure)
expect(provider.is_enabled?).to be false
end
end
@@ -319,23 +345,23 @@ describe Chef::Provider::Service::Systemd do
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
- it "should return true if '#{systemctl_path} is-enabled service_name' returns 'masked' and returns anything except 0" do
- expect(provider).to receive(:shell_out).with("#{systemctl_path} is-enabled #{service_name}").and_return(double(:stdout => "masked", :exitstatus => shell_out_failure))
+ it "should return true if '#{systemctl_path} --system is-enabled service_name' returns 'masked' and returns anything except 0" do
+ expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name}", {}).and_return(double(:stdout => "masked", :exitstatus => shell_out_failure))
expect(provider.is_masked?).to be true
end
- it "should return true if '#{systemctl_path} is-enabled service_name' outputs 'masked-runtime' and returns anything except 0" do
- expect(provider).to receive(:shell_out).with("#{systemctl_path} is-enabled #{service_name}").and_return(double(:stdout => "masked-runtime", :exitstatus => shell_out_failure))
+ it "should return true if '#{systemctl_path} --system is-enabled service_name' outputs 'masked-runtime' and returns anything except 0" do
+ expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name}", {}).and_return(double(:stdout => "masked-runtime", :exitstatus => shell_out_failure))
expect(provider.is_masked?).to be true
end
- it "should return false if '#{systemctl_path} is-enabled service_name' returns 0" do
- expect(provider).to receive(:shell_out).with("#{systemctl_path} is-enabled #{service_name}").and_return(double(:stdout => "enabled", :exitstatus => shell_out_success))
+ it "should return false if '#{systemctl_path} --system is-enabled service_name' returns 0" do
+ expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name}", {}).and_return(double(:stdout => "enabled", :exitstatus => shell_out_success))
expect(provider.is_masked?).to be false
end
- it "should return false if '#{systemctl_path} is-enabled service_name' returns anything except 0 and outputs an error'" do
- expect(provider).to receive(:shell_out).with("#{systemctl_path} is-enabled #{service_name}").and_return(double(:stdout => "Failed to get unit file state for #{service_name}: No such file or directory", :exitstatus => shell_out_failure))
+ it "should return false if '#{systemctl_path} --system is-enabled service_name' returns anything except 0 and outputs an error'" do
+ expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name}", {}).and_return(double(:stdout => "Failed to get unit file state for #{service_name}: No such file or directory", :exitstatus => shell_out_failure))
expect(provider.is_masked?).to be false
end
end