1#!/usr/bin/env rspec
2
3require 'spec_helper'
4require File.expand_path(File.join(File.dirname(__FILE__),
5                                   '../../..', 'util', 'puppet_agent_mgr.rb'))
6
7
8module MCollective::Util
9  describe PuppetAgentMgr do
10
11    before :each do
12      MCollective::Util.stubs(:windows?).returns(true)
13      Puppet.stubs(:version).returns('3.0.0')
14      @manager = PuppetAgentMgr::manager(nil, nil, nil, true)
15    end
16
17    describe "#daemon_present?", :if => Puppet.windows? do
18      it "should return false if the service does not exist" do
19        Win32::Service.expects(:status).with('pe-puppet').raises(
20          Win32::Service::Error)
21        Windows.daemon_present?.should be_false
22      end
23
24      it "should return false if the service is stopped" do
25        Win32::Service.expects(:status).with('pe-puppet').returns(
26          stub(:current_state => 'stopped'))
27        Windows.daemon_present?.should be_false
28      end
29
30      ['running', 'continue pending', 'start pending'].each do |state|
31        it "should return true if the service is #{state}" do
32          Win32::Service.expects(:status).with('pe-puppet').returns(
33            stub(:current_state => state))
34          Windows.daemon_present?.should == true
35        end
36      end
37    end
38
39    describe "#applying?" do
40      it "should return false when disabled" do
41        @manager.expects(:disabled?).returns(true)
42        @manager.applying?.should == false
43      end
44
45      it "should return false when the lock file is absent" do
46        @manager.expects(:disabled?).returns(false)
47        File.expects(:read).with("agent_catalog_run_lockfile").raises(
48          Errno::ENOENT)
49        MCollective::Log.expects(:warn).never
50        @manager.applying?.should == false
51      end
52
53      it "should check the pid if the lock file is not empty" do
54        @manager.expects(:disabled?).returns(false)
55        File.expects(:read).with("agent_catalog_run_lockfile").returns("1")
56        @manager.expects(:has_process_for_pid?).with("1").returns(true)
57        @manager.applying?.should == true
58      end
59
60      it "should return false if the lockfile is empty" do
61        @manager.expects(:disabled?).returns(false)
62        File.expects(:read).with("agent_catalog_run_lockfile").returns("")
63        @manager.applying?.should == false
64      end
65
66      it "should return false if the lockfile is stale" do
67        @manager.expects(:disabled?).returns(false)
68        File.expects(:read).with("agent_catalog_run_lockfile").returns("1")
69        ::Process.stubs(:kill).with(0, 1).raises(Errno::ESRCH)
70        @manager.applying?.should == false
71      end
72
73      it "should return false on any error" do
74        @manager.expects(:disabled?).raises("fail")
75        MCollective::Log.expects(:warn)
76        @manager.applying?.should == false
77      end
78    end
79
80    describe "#background_run_allowed?" do
81      it "should be true if the daemon is present" do
82        @manager.stubs(:daemon_present?).returns true
83        @manager.background_run_allowed?.should be_true
84      end
85
86      it "should be true if the daemon is not present" do
87        @manager.stubs(:daemon_present?).returns false
88        @manager.background_run_allowed?.should be_true
89      end
90    end
91
92    describe "#signal_running_daemon" do
93      it "should not be supported" do
94        expect {
95          @manager.signal_running_daemon
96        }.to raise_error(
97              /Signalling the puppet daemon is not supported on Windows/)
98      end
99    end
100
101  end
102end
103