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(false)
13      Puppet.stubs(:version).returns('2.7.12')
14      @manager = PuppetAgentMgr::manager(nil, nil, nil, true)
15    end
16
17    describe "#daemon_present?" do
18      it "should return false if the pidfile does not exist" do
19        File.expects(:exist?).with("pidfile").returns(false)
20        @manager.daemon_present?.should == false
21      end
22
23      it "should check the pid if the pidfile exist" do
24        File.expects(:exist?).with("pidfile").returns(true)
25        File.expects(:read).with("pidfile").returns(1)
26        @manager.expects(:has_process_for_pid?).with(1).returns(true)
27        @manager.daemon_present?.should == true
28      end
29    end
30
31    describe "#applying?" do
32      it "should return false when the lock file is absent" do
33        File.expects(:exist?).with("puppetdlockfile").returns(false)
34        MCollective::Log.expects(:warn).never
35        File::Stat.expects(:new).never
36        @manager.applying?.should == false
37      end
38
39      it "should check the pid if the lock file is not empty" do
40        stat = OpenStruct.new(:size => 1)
41        File::Stat.expects(:new).returns(stat)
42        File.expects(:exist?).with("puppetdlockfile").returns(true)
43        File.expects(:read).with("puppetdlockfile").returns("1")
44        @manager.expects(:has_process_for_pid?).with("1").returns(true)
45        @manager.applying?.should == true
46      end
47
48      it "should return false if the lockfile is empty" do
49        stat = OpenStruct.new(:size => 0)
50        File::Stat.expects(:new).returns(stat)
51        File.expects(:exist?).with("puppetdlockfile").returns(true)
52        @manager.applying?.should == false
53      end
54
55      it "should return false if the lockfile is stale" do
56        stat = OpenStruct.new(:size => 1)
57        File::Stat.expects(:new).returns(stat)
58        File.expects(:exist?).with("puppetdlockfile").returns(true)
59        File.expects(:read).with("puppetdlockfile").returns("1")
60        @manager.expects(:has_process_for_pid?).with("1").returns(false)
61        @manager.applying?.should == false
62      end
63
64      it "should return false on any error" do
65        @manager.expects(:platform_applying?).raises("fail")
66        MCollective::Log.expects(:warn)
67        @manager.applying?.should == false
68      end
69    end
70
71    describe "#signal_running_daemon" do
72      it "should check if the process is present and send USR1 if present" do
73        File.expects(:read).with("pidfile").returns("1")
74        @manager.expects(:has_process_for_pid?).with("1").returns(true)
75        ::Process.expects(:kill).with("USR1", 1)
76        @manager.signal_running_daemon
77      end
78
79      it "should fall back to background run if the pid is stale" do
80        File.expects(:read).with("pidfile").returns("1")
81        @manager.expects(:has_process_for_pid?).with("1").returns(false)
82        @manager.expects(:run_in_background)
83
84        @manager.signal_running_daemon
85      end
86    end
87
88  end
89end
90