diff --git a/lib/puppet/provider/service/daemontools.rb b/lib/puppet/provider/service/daemontools.rb index 422275e9a10..b493c4bae41 100644 --- a/lib/puppet/provider/service/daemontools.rb +++ b/lib/puppet/provider/service/daemontools.rb @@ -46,14 +46,8 @@ class << self # Determine the daemon path. def defpath - unless @defpath - ["/var/lib/service", "/etc"].each do |path| - if Puppet::FileSystem.exist?(path) - @defpath = path - break - end - end - raise "Could not find the daemon directory (tested [/var/lib/service,/etc])" unless @defpath + @defpath ||= ["/var/lib/service", "/etc"].find do |path| + Puppet::FileSystem.exist?(path) && FileTest.directory?(path) end @defpath end @@ -65,6 +59,10 @@ def defpath # ie enabled or not def self.instances path = self.defpath + unless path + Puppet.info("#{self.name} is unsuitable because service directory is nil") + return + end unless FileTest.directory?(path) Puppet.notice "Service path #{path} does not exist" return @@ -109,7 +107,9 @@ def service # note that this path can be overridden in the resource # definition def daemon - File.join(resource[:path], resource[:name]) + path = resource[:path] + raise Puppet::Error.new("#{self.class.name} must specify a path for daemon directory") unless path + File.join(path, resource[:name]) end def status diff --git a/lib/puppet/provider/service/runit.rb b/lib/puppet/provider/service/runit.rb index 2cb138d7877..fb96be17a37 100644 --- a/lib/puppet/provider/service/runit.rb +++ b/lib/puppet/provider/service/runit.rb @@ -40,14 +40,8 @@ class << self # this is necessary to autodetect a valid resource # default path, since there is no standard for such directory. def defpath - unless @defpath - ["/etc/sv", "/var/lib/service"].each do |path| - if Puppet::FileSystem.exist?(path) - @defpath = path - break - end - end - raise "Could not find the daemon directory (tested [/etc/sv,/var/lib/service])" unless @defpath + @defpath ||= ["/var/lib/service", "/etc/sv"].find do |path| + Puppet::FileSystem.exist?(path) && FileTest.directory?(path) end @defpath end diff --git a/spec/unit/provider/service/daemontools_spec.rb b/spec/unit/provider/service/daemontools_spec.rb index faf98420d21..4c5bb3c6069 100644 --- a/spec/unit/provider/service/daemontools_spec.rb +++ b/spec/unit/provider/service/daemontools_spec.rb @@ -160,4 +160,28 @@ expect(@provider.status).to eq(:stopped) end end + + context '.instances' do + before do + allow(described_class).to receive(:defpath).and_return(path) + end + + context 'when defpath is nil' do + let(:path) { nil } + + it 'returns info message' do + expect(Puppet).to receive(:info).with(/daemontools is unsuitable because service directory is nil/) + described_class.instances + end + end + + context 'when defpath does not exist' do + let(:path) { '/inexistent_path' } + + it 'returns notice about missing path' do + expect(Puppet).to receive(:notice).with(/Service path #{path} does not exist/) + described_class.instances + end + end + end end diff --git a/spec/unit/provider/service/runit_spec.rb b/spec/unit/provider/service/runit_spec.rb index cde0e5c85ca..caf4396589b 100644 --- a/spec/unit/provider/service/runit_spec.rb +++ b/spec/unit/provider/service/runit_spec.rb @@ -134,4 +134,28 @@ expect(@provider.status).to eq(:stopped) end end + + context '.instances' do + before do + allow(described_class).to receive(:defpath).and_return(path) + end + + context 'when defpath is nil' do + let(:path) { nil } + + it 'returns info message' do + expect(Puppet).to receive(:info).with(/runit is unsuitable because service directory is nil/) + described_class.instances + end + end + + context 'when defpath does not exist' do + let(:path) { '/inexistent_path' } + + it 'returns notice about missing path' do + expect(Puppet).to receive(:notice).with(/Service path #{path} does not exist/) + described_class.instances + end + end + end end