diff --git a/lib/puppet/provider/service/systemd.rb b/lib/puppet/provider/service/systemd.rb index 33ff52d9c..e206b1154 100755 --- a/lib/puppet/provider/service/systemd.rb +++ b/lib/puppet/provider/service/systemd.rb @@ -1,64 +1,64 @@ # Manage systemd services using /bin/systemctl Puppet::Type.type(:service).provide :systemd, :parent => :base do desc "Manages `systemd` services using `/bin/systemctl`." commands :systemctl => "/bin/systemctl" #defaultfor :osfamily => [:redhat, :suse] def self.instances i = [] - output = `systemctl list-units --full --all --no-pager` + output = systemctl('list-units', '--full', '--all', '--no-pager') output.scan(/^(\S+)\s+(loaded|error)\s+(active|inactive)\s+(active|waiting|running|plugged|mounted|dead|exited|listening|elapsed)\s*?(\S.*?)?$/i).each do |m| - i << m[0] + i << new(:name => m[0]) end return i rescue Puppet::ExecutionFailure return [] end def disable output = systemctl(:disable, @resource[:name]) rescue Puppet::ExecutionFailure raise Puppet::Error, "Could not disable #{self.name}: #{output}" end def enabled? begin systemctl("is-enabled", @resource[:name]) rescue Puppet::ExecutionFailure return :false end :true end def status begin output = systemctl("is-active", @resource[:name]) rescue Puppet::ExecutionFailure return :stopped end return :running end def enable output = systemctl("enable", @resource[:name]) rescue Puppet::ExecutionFailure raise Puppet::Error, "Could not enable #{self.name}: #{output}" end def restartcmd [command(:systemctl), "restart", @resource[:name]] end def startcmd [command(:systemctl), "start", @resource[:name]] end def stopcmd [command(:systemctl), "stop", @resource[:name]] end end diff --git a/spec/unit/provider/service/systemd_spec.rb b/spec/unit/provider/service/systemd_spec.rb index 8480c0149..184b4ad58 100755 --- a/spec/unit/provider/service/systemd_spec.rb +++ b/spec/unit/provider/service/systemd_spec.rb @@ -1,33 +1,42 @@ #!/usr/bin/env rspec # # Unit testing for the RedHat service Provider # require 'spec_helper' provider_class = Puppet::Type.type(:service).provider(:systemd) describe provider_class do before :each do @class = Puppet::Type.type(:service).provider(:redhat) @resource = stub 'resource' @resource.stubs(:[]).returns(nil) @resource.stubs(:[]).with(:name).returns "myservice.service" @provider = provider_class.new @resource.stubs(:provider).returns @provider @provider.resource = @resource end osfamily = [ 'redhat', 'suse' ] osfamily.each do |osfamily| it "should be the default provider on #{osfamily}" do pending "This test is pending the change in RedHat-related Linuxes to systemd for service management" end end [:enabled?, :enable, :disable, :start, :stop, :status, :restart].each do |method| it "should have a #{method} method" do @provider.should respond_to(method) end end + + + it 'should return resources from self.instances' do + provider_class.expects(:systemctl).with('list-units', '--full', '--all', '--no-pager').returns( + "my_service loaded active running\nmy_other_service loaded active running" + ) + provider_class.instances.map {|provider| provider.name}.should =~ ["my_service","my_other_service"] + end + end