Skip to content

Commit fae347d

Browse files
author
AJ Christensen
committed
CHEF-423: Gentoo service status broken long names
The gentoo service provider could not cope with long-named services due to the way rc-update -s plays its output. Switched out the parsing behaviour for some trivial Dir.glob pathwalking with Exists? and Readable? checking on service scripts detected. Specs.
1 parent 2f872b3 commit fae347d

2 files changed

Lines changed: 82 additions & 43 deletions

File tree

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#
22
# Author:: Lee Jensen (<ljensen@engineyard.com>)
3+
# Author:: AJ Christensen (<aj@opscode.com>)
34
# Copyright:: Copyright (c) 2008 Opscode, Inc.
45
# License:: Apache License, Version 2.0
56
#
@@ -19,42 +20,35 @@
1920
require 'chef/provider/service'
2021
require 'chef/mixin/command'
2122

22-
class Chef
23-
class Provider
24-
class Service
25-
class Gentoo < Chef::Provider::Service::Init
26-
def load_current_resource
23+
class Chef::Provider::Service::Gentoo < Chef::Provider::Service::Init
24+
def load_current_resource
2725

28-
@new_resource.supports[:status] = true
29-
@new_resource.supports[:restart] = true
26+
@new_resource.supports[:status] = true
27+
@new_resource.supports[:restart] = true
3028

31-
super
32-
33-
raise Chef::Exceptions::Service unless ::File.exists?("/sbin/rc-update")
34-
35-
status = popen4("/sbin/rc-update -s default") do |pid, stdin, stdout, stderr|
36-
stdout.each_line do |line|
37-
if line.match(/^\s*#{@current_resource.service_name}\s+/)
38-
@current_resource.enabled true
39-
end
40-
end
41-
end
42-
43-
unless status.exitstatus == 0
44-
raise Chef::Exceptions::Service, "/sbin/rc-update -s default failed - #{status.inspect}"
45-
end
46-
47-
@current_resource
48-
end
49-
50-
def enable_service()
51-
run_command(:command => "/sbin/rc-update add #{@new_resource.service_name} default")
52-
end
53-
54-
def disable_service()
55-
run_command(:command => "/sbin/rc-update del #{@new_resource.service_name} default")
56-
end
29+
super
30+
31+
raise Chef::Exceptions::Service unless ::File.exists?("/sbin/rc-update")
32+
33+
Chef::Log.debug "#{@new_resource}: checking service enable state"
34+
@current_resource.enabled(
35+
Dir.glob("/etc/runlevels/**/#{@current_resource.service_name}").any? do |file|
36+
exists = ::File.exists? file
37+
readable = ::File.readable? file
38+
Chef::Log.debug "#{@new_resource}: exists: #{exists}, readable: #{readable}"
39+
exists and readable
5740
end
58-
end
41+
)
42+
Chef::Log.debug "#{@new_resource}: enabled: #{@current_resource.enabled})"
43+
44+
@current_resource
45+
end
46+
47+
def enable_service()
48+
run_command(:command => "/sbin/rc-update add #{@new_resource.service_name} default")
49+
end
50+
51+
def disable_service()
52+
run_command(:command => "/sbin/rc-update del #{@new_resource.service_name} default")
5953
end
6054
end

‎chef/spec/unit/provider/service/gentoo_service_spec.rb‎

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#
22
# Author:: Lee Jensen (<ljensen@engineyard.com>)
3+
# Author:: AJ Christensen (<aj@opscode.com>)
34
# Copyright:: Copyright (c) 2008 Opscode, Inc.
45
# License:: Apache License, Version 2.0
56
#
@@ -28,23 +29,66 @@
2829

2930
@provider = Chef::Provider::Service::Gentoo.new(@node, @new_resource)
3031
Chef::Resource::Service.stub!(:new).and_return(@current_resource)
31-
File.stub!(:exists?).and_return(true)
32-
33-
@status = mock("Status", :exitstatus => 0)
3432
@provider.stub!(:popen4).and_return(@status)
35-
@stdin = mock("STDIN", :null_object => true)
36-
@stdout = mock("STDOUT", :null_object => true)
37-
@stdout.stub!(:each_line).and_yield(" gfs | default ")
38-
@stderr = mock("STDERR", :null_object => true)
39-
@pid = mock("PID", :null_object => true)
33+
@provider.stub!(:run_command).with(:command => "/etc/init.d/chef status").and_return(true)
34+
File.stub!(:exists?).with("/etc/init.d/chef").and_return(true)
35+
File.stub!(:exists?).with("/sbin/rc-update").and_return(true)
36+
File.stub!(:exists?).with("/etc/runlevels/default/chef").and_return(false)
37+
File.stub!(:readable?).with("/etc/runlevels/default/chef").and_return(false)
4038
end
4139

4240
describe "load_current_resource" do
4341
it "should raise Chef::Exceptions::Service if /sbin/rc-update does not exist" do
4442
File.should_receive(:exists?).with("/sbin/rc-update").and_return(false)
4543
lambda { @provider.load_current_resource }.should raise_error(Chef::Exceptions::Service)
4644
end
47-
45+
46+
describe "when detecting the service enable state" do
47+
describe "and the glob returns a default service script file" do
48+
before do
49+
Dir.stub!(:glob).with("/etc/runlevels/**/chef").and_return(["/etc/runlevels/default/chef"])
50+
end
51+
52+
describe "and the file exists and is readable" do
53+
before do
54+
File.stub!(:exists?).with("/etc/runlevels/default/chef").and_return(true)
55+
File.stub!(:readable?).with("/etc/runlevels/default/chef").and_return(true)
56+
end
57+
it "should set enabled to true" do
58+
@current_resource.should_receive(:enabled).with(true).and_return(true)
59+
@provider.load_current_resource
60+
end
61+
end
62+
63+
describe "and the file exists but is not readable" do
64+
before do
65+
File.stub!(:exists?).with("/etc/runlevels/default/chef").and_return(true)
66+
File.stub!(:readable?).with("/etc/runlevels/default/chef").and_return(false)
67+
end
68+
69+
it "should set enabled to false" do
70+
@current_resource.should_receive(:enabled).with(false).and_return(false)
71+
@provider.load_current_resource
72+
end
73+
end
74+
75+
describe "and the file does not exist" do
76+
before do
77+
File.stub!(:exists?).with("/etc/runlevels/default/chef").and_return(false)
78+
File.stub!(:readable?).with("/etc/runlevels/default/chef").and_return("foobarbaz")
79+
end
80+
81+
it "should set enabled to false" do
82+
@current_resource.should_receive(:enabled).with(false).and_return(false)
83+
@provider.load_current_resource
84+
end
85+
86+
end
87+
end
88+
89+
end
90+
91+
=begin
4892
it "should set enabled true if rc-update indicates service is in default runlevel" do
4993
@stdout.should_receive(:each_line).
5094
and_yield(' gfs | default ').
@@ -66,6 +110,7 @@
66110
@provider.load_current_resource
67111
@current_resource.enabled.should be_false
68112
end
113+
=end
69114

70115
it "should return the current_resource" do
71116
@provider.load_current_resource.should == @current_resource

0 commit comments

Comments
 (0)