~ubuntu-branches/ubuntu/lucid/puppet/lucid

« back to all changes in this revision

Viewing changes to spec/unit/provider/service/init.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
#
 
3
# Unit testing for the Init service Provider
 
4
#
 
5
 
 
6
require File.dirname(__FILE__) + '/../../../spec_helper'
 
7
 
 
8
provider_class = Puppet::Type.type(:service).provider(:init)
 
9
 
 
10
describe provider_class do
 
11
 
 
12
    before :each 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]"
 
19
        
 
20
        @provider = provider_class.new
 
21
        @provider.resource = @resource
 
22
    end
 
23
 
 
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"
 
28
        end
 
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"
 
32
        end
 
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'")
 
35
        end
 
36
    end
 
37
    
 
38
    describe "if the init script is present" do
 
39
        before :each do
 
40
            File.stubs(:stat).with("/service/path/myservice").returns true
 
41
        end
 
42
        
 
43
        [:start, :stop, :status, :restart].each do |method|
 
44
            it "should have a #{method} method" do
 
45
                @provider.should respond_to(method)
 
46
            end
 
47
            describe "when running #{method}" do
 
48
            
 
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)
 
53
                end
 
54
 
 
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)
 
59
                end            
 
60
            end
 
61
        end
 
62
 
 
63
        describe "when checking status" do
 
64
            describe "when hasstatus is :true" do
 
65
                before :each do
 
66
                    @resource.stubs(:[]).with(:hasstatus).returns :true
 
67
                end
 
68
                it "should execute the command" do
 
69
                    @provider.expects(:texecute).with(:status, ['/service/path/myservice', :status], false).returns("")
 
70
                    @provider.status
 
71
                end
 
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
 
76
                end
 
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
 
82
                    end
 
83
                }
 
84
            end
 
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
 
89
                end
 
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
 
93
                end
 
94
            end
 
95
        end
 
96
 
 
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)
 
102
                @provider.restart
 
103
            end
 
104
        end
 
105
    end
 
106
end