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('3.0.0') 14 @manager = PuppetAgentMgr::manager(nil, 'puppet', 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("agent_catalog_run_lockfile").returns(false) 34 File::Stat.expects(:new).never 35 MCollective::Log.expects(:warn).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("agent_catalog_run_lockfile").returns(true) 43 File.expects(:read).with("agent_catalog_run_lockfile").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.expects(:exist?).with("agent_catalog_run_lockfile").returns(true) 51 File::Stat.expects(:new).returns(stat) 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("agent_catalog_run_lockfile").returns(true) 59 File.expects(:read).with("agent_catalog_run_lockfile").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 77 @manager.signal_running_daemon 78 end 79 80 it "should fall back to background run if the pid is stale" do 81 File.expects(:read).with("pidfile").returns("1") 82 @manager.expects(:has_process_for_pid?).with("1").returns(false) 83 @manager.expects(:run_in_background) 84 85 @manager.signal_running_daemon 86 end 87 end 88 89 end 90end 91