summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Dodge <mikedodge04@fb.com>2015-03-12 00:01:08 -0700
committerMike Dodge <mikedodge04@fb.com>2015-03-12 00:01:08 -0700
commita3fd8c19d9634588444c11928337ad6b52520572 (patch)
tree889f498ee438b8acba15698ed05b77970b824bc4
parentd931e5eab8fae4204e7415d82f6b492f490df18d (diff)
downloadchef-a3fd8c19d9634588444c11928337ad6b52520572.tar.gz
First pass at fixing the unit tests
-rw-r--r--lib/chef/provider/service/macosx.rb13
-rw-r--r--lib/chef/resource/macosx_service.rb5
-rw-r--r--lib/chef/resource/service.rb1
-rw-r--r--spec/unit/provider/service/macosx_spec.rb85
4 files changed, 67 insertions, 37 deletions
diff --git a/lib/chef/provider/service/macosx.rb b/lib/chef/provider/service/macosx.rb
index 4a782146c0..fa178466a9 100644
--- a/lib/chef/provider/service/macosx.rb
+++ b/lib/chef/provider/service/macosx.rb
@@ -20,6 +20,7 @@
require 'etc'
require 'rexml/document'
require 'chef/resource/service'
+require 'chef/resource/macosx_service'
require 'chef/provider/service/simple'
require 'chef/util/path_helper'
@@ -29,6 +30,7 @@ class Chef
class Macosx < Chef::Provider::Service::Simple
provides :service, os: "darwin"
+ provides :macosx_service, os: "darwin"
def self.gather_plist_dirs
locations = %w{/Library/LaunchAgents
@@ -43,7 +45,7 @@ class Chef
PLIST_DIRS = gather_plist_dirs
def load_current_resource
- @current_resource = Chef::Resource::Service.new(@new_resource.name)
+ @current_resource = Chef::Resource::MacosxService.new(@new_resource.name)
@current_resource.service_name(@new_resource.service_name)
@plist_size = 0
@plist = @new_resource.plist ? @new_resource.plist : find_service_plist
@@ -51,7 +53,7 @@ class Chef
@service_label = find_service_label
Chef::Log.debug("Service_Label: '#{@service_label}'")
# LauchAgents should be loaded as the console user.
- @console_user = @plist.include?('LaunchAgents')
+ @console_user = @plist ? @plist.include?('LaunchAgents') : false
@session_type = @new_resource.session_type
if @console_user
@@ -59,7 +61,7 @@ class Chef
Chef::Log.debug("Console User: '#{@console_user}'")
cmd = "su "
param = !node['platform_version'].include?('10.10') ? '-l ' : ''
- @base_user_cmd = cmd + param + "#{@console_user} -c "
+ @base_user_cmd = cmd + param + "#{@console_user} -c"
# Default LauchAgent session should be Aqua
@session_type = 'Aqua' if @session_type.nil?
end
@@ -153,14 +155,13 @@ class Chef
end
def load_service
- session = @session_type ? "-S #{@session_type}" : ''
+ session = @session_type ? "-S #{@session_type} " : ''
cmd = 'launchctl load -w ' + session + @plist
shell_out_with_su?(cmd)
end
def unload_service
- session = @session_type ? "-S #{@session_type}" : ''
- cmd = 'launchctl unload -w ' + session + @plist
+ cmd = 'launchctl unload -w ' + @plist
shell_out_with_su?(cmd)
end
diff --git a/lib/chef/resource/macosx_service.rb b/lib/chef/resource/macosx_service.rb
index 715e9a80ae..744a71c23f 100644
--- a/lib/chef/resource/macosx_service.rb
+++ b/lib/chef/resource/macosx_service.rb
@@ -26,8 +26,13 @@ class Chef
provides :service, os: "darwin"
provides :macosx_service, os: "darwin"
+ identity_attr :service_name
+
+ state_attrs :enabled, :running
+
def initialize(name, run_context=nil)
super
+ @resource_name = :macosx_service
@plist = nil
@session_type = nil
end
diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb
index ac85ca3336..36df7c859a 100644
--- a/lib/chef/resource/service.rb
+++ b/lib/chef/resource/service.rb
@@ -1,4 +1,3 @@
-# vim: syntax=ruby:expandtab:shiftwidth=2:softtabstop=2:tabstop=2
#
# Author:: AJ Christensen (<aj@hjksolutions.com>)
# Author:: Tyler Cloke (<tyler@opscode.com>)
diff --git a/spec/unit/provider/service/macosx_spec.rb b/spec/unit/provider/service/macosx_spec.rb
index fb751592df..d124d007dd 100644
--- a/spec/unit/provider/service/macosx_spec.rb
+++ b/spec/unit/provider/service/macosx_spec.rb
@@ -60,16 +60,14 @@ XML
["redis-server", "io.redis.redis-server"].each do |service_name|
before do
+ allow(node).to receive(:[]).with("platform_version").and_return('10.10')
+ allow(Etc).to receive(:getlogin).and_return('igor')
allow(Dir).to receive(:glob).and_return(["/Users/igor/Library/LaunchAgents/io.redis.redis-server.plist"], [])
- allow(provider).to receive(:shell_out!).
- with("launchctl list", {:group => 1001, :user => 101}).
- and_return(double("Status", :stdout => launchctl_stdout))
- allow(provider).to receive(:shell_out).
- with(/launchctl list /,
- {:group => nil, :user => nil}).
+ allow(provider).to receive(:shell_out_with_systems_locale).
+ with("su igor -c 'launchctl list #{service_name}'").
and_return(double("Status",
:stdout => launchctl_stdout, :exitstatus => 0))
- allow(provider).to receive(:shell_out!).
+ allow(provider).to receive(:shell_out_with_systems_locale!).
with(/plutil -convert xml1 -o/).
and_return(double("Status", :stdout => plutil_stdout))
@@ -77,8 +75,8 @@ XML
end
context "#{service_name}" do
- let(:new_resource) { Chef::Resource::Service.new(service_name) }
- let!(:current_resource) { Chef::Resource::Service.new(service_name) }
+ let(:new_resource) { Chef::Resource::MacosxService.new(service_name) }
+ let!(:current_resource) { Chef::Resource::MacosxService.new(service_name) }
describe "#load_current_resource" do
@@ -120,10 +118,20 @@ XML
context "when launchctl returns pid in service list" do
let(:launchctl_stdout) { StringIO.new <<-SVC_LIST }
- 12761 - 0x100114220.old.machinit.thing
- 7777 - io.redis.redis-server
- - - com.lol.stopped-thing
- SVC_LIST
+{
+ "LimitLoadToSessionType" = "System";
+ "Label" = "io.redis.redis-server";
+ "TimeOut" = 30;
+ "OnDemand" = false;
+ "LastExitStatus" = 0;
+ "PID" = 62803;
+ "Program" = "do_some.sh";
+ "ProgramArguments" = (
+ "path/to/do_something.sh";
+ "-f";
+ );
+};
+SVC_LIST
before do
provider.load_current_resource
@@ -140,9 +148,19 @@ XML
describe "running unsupported actions" do
let(:launchctl_stdout) { StringIO.new <<-SVC_LIST }
-12761 - 0x100114220.old.machinit.thing
-7777 - io.redis.redis-server
-- - com.lol.stopped-thing
+{
+ "LimitLoadToSessionType" = "System";
+ "Label" = "io.redis.redis-server";
+ "TimeOut" = 30;
+ "OnDemand" = false;
+ "LastExitStatus" = 0;
+ "PID" = 62803;
+ "Program" = "do_some.sh";
+ "ProgramArguments" = (
+ "path/to/do_something.sh";
+ "-f";
+ );
+};
SVC_LIST
before do
@@ -154,10 +172,19 @@ SVC_LIST
end
context "when launchctl returns empty service pid" do
let(:launchctl_stdout) { StringIO.new <<-SVC_LIST }
- 12761 - 0x100114220.old.machinit.thing
- - - io.redis.redis-server
- - - com.lol.stopped-thing
- SVC_LIST
+{
+ "LimitLoadToSessionType" = "System";
+ "Label" = "io.redis.redis-server";
+ "TimeOut" = 30;
+ "OnDemand" = false;
+ "LastExitStatus" = 0;
+ "Program" = "do_some.sh";
+ "ProgramArguments" = (
+ "path/to/do_something.sh";
+ "-f";
+ );
+};
+SVC_LIST
before do
provider.load_current_resource
@@ -232,15 +259,14 @@ SVC_LIST
it "shows warning message if service is already running" do
allow(current_resource).to receive(:running).and_return(true)
- expect(Chef::Log).to receive(:debug).with("service[#{service_name}] already running, not starting")
+ expect(Chef::Log).to receive(:debug).with("macosx_service[#{service_name}] already running, not starting")
provider.start_service
end
it "starts service via launchctl if service found" do
- expect(provider).to receive(:shell_out_with_systems_locale!).
- with("launchctl load -w '/Users/igor/Library/LaunchAgents/io.redis.redis-server.plist'",
- :group => 1001, :user => 101).
+ expect(provider).to receive(:shell_out_with_systems_locale).
+ with("su igor -c 'launchctl load -w -S Aqua /Users/igor/Library/LaunchAgents/io.redis.redis-server.plist'").
and_return(0)
provider.start_service
@@ -264,15 +290,14 @@ SVC_LIST
it "shows warning message if service is not running" do
allow(current_resource).to receive(:running).and_return(false)
- expect(Chef::Log).to receive(:debug).with("service[#{service_name}] not running, not stopping")
+ expect(Chef::Log).to receive(:debug).with("macosx_service[#{service_name}] not running, not stopping")
provider.stop_service
end
it "stops the service via launchctl if service found" do
- expect(provider).to receive(:shell_out_with_systems_locale!).
- with("launchctl unload '/Users/igor/Library/LaunchAgents/io.redis.redis-server.plist'",
- :group => 1001, :user => 101).
+ expect(provider).to receive(:shell_out_with_systems_locale).
+ with("su igor -c 'launchctl unload -w /Users/igor/Library/LaunchAgents/io.redis.redis-server.plist'").
and_return(0)
provider.stop_service
@@ -296,8 +321,8 @@ SVC_LIST
end
it "stops and then starts service" do
- expect(provider).to receive(:stop_service)
- expect(provider).to receive(:start_service);
+ expect(provider).to receive(:unload_service)
+ expect(provider).to receive(:load_service);
provider.restart_service
end