3
# Unit testing for the Init service Provider
6
require File.dirname(__FILE__) + '/../../../spec_helper'
8
provider_class = Puppet::Type.type(:service).provider(:init)
10
describe provider_class do
13
@resource = stub 'resource'
14
@resource.stubs(:[]).returns(nil)
15
@resource.stubs(:[]).with(:name).returns "myservice"
16
# @resource.stubs(:[]).with(:ensure).returns :enabled
17
@resource.stubs(:[]).with(:path).returns ["/service/path","/alt/service/path"]
18
# @resource.stubs(:ref).returns "Service[myservice]"
20
@provider = provider_class.new
21
@provider.resource = @resource
24
describe "when serching for the init script" do
25
it "should be able to find the init script in the service path" do
26
File.expects(:stat).with("/service/path/myservice").returns true
27
@provider.initscript.should == "/service/path/myservice"
29
it "should be able to find the init script in the service path" do
30
File.expects(:stat).with("/alt/service/path/myservice").returns true
31
@provider.initscript.should == "/alt/service/path/myservice"
33
it "should fail if the service isn't there" do
34
lambda { @provider.initscript }.should raise_error(Puppet::Error, "Could not find init script for 'myservice'")
38
describe "if the init script is present" do
40
File.stubs(:stat).with("/service/path/myservice").returns true
43
[:start, :stop, :status, :restart].each do |method|
44
it "should have a #{method} method" do
45
@provider.should respond_to(method)
47
describe "when running #{method}" do
49
it "should use any provided explicit command" do
50
@resource.stubs(:[]).with(method).returns "/user/specified/command"
51
@provider.expects(:execute).with { |command, *args| command == ["/user/specified/command"] }
52
@provider.send(method)
55
it "should pass #{method} to the init script when no explicit command is provided" do
56
@resource.stubs(:[]).with("has#{method}".intern).returns :true
57
@provider.expects(:execute).with { |command, *args| command == ["/service/path/myservice",method]}
58
@provider.send(method)
63
describe "when checking status" do
64
describe "when hasstatus is :true" do
66
@resource.stubs(:[]).with(:hasstatus).returns :true
68
it "should execute the command" do
69
@provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("")
72
it "should consider the process running if the command returns 0" do
73
@provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("")
74
$?.stubs(:exitstatus).returns(0)
75
@provider.status.should == :running
77
[-10,-1,1,10].each { |ec|
78
it "should consider the process stopped if the command returns something non-0" do
79
@provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("")
80
$?.stubs(:exitstatus).returns(ec)
81
@provider.status.should == :stopped
85
describe "when hasstatus is not :true" do
86
it "should consider the service :running if it has a pid" do
87
@provider.expects(:getpid).returns "1234"
88
@provider.status.should == :running
90
it "should consider the service :stopped if it doesn't have a pid" do
91
@provider.expects(:getpid).returns nil
92
@provider.status.should == :stopped
97
describe "when restarting and hasrestart is not :true" do
98
it "should stop and restart the process" do
99
@provider.expects(:texecute).with(:stop, ['/service/path/myservice', :stop ], true).returns("")
100
@provider.expects(:texecute).with(:start,['/service/path/myservice', :start], true).returns("")
101
$?.stubs(:exitstatus).returns(0)