3
# Created by Luke Kanies on 2007-11-12.
4
# Copyright (c) 2007. All rights reserved.
6
require File.dirname(__FILE__) + '/../spec_helper'
18
describe Puppet::Agent do
20
@agent = Puppet::Agent.new(AgentTestClient)
22
# So we don't actually try to hit the filesystem.
23
@agent.stubs(:lock).yields
26
it "should set its client class at initialization" do
27
Puppet::Agent.new("foo").client_class.should == "foo"
30
it "should include the Locker module" do
31
Puppet::Agent.ancestors.should be_include(Puppet::Agent::Locker)
34
it "should create an instance of its client class and run it when asked to run" do
35
client = mock 'client'
36
AgentTestClient.expects(:new).returns client
40
@agent.stubs(:running?).returns false
44
it "should determine its lock file path by asking the client class" do
45
AgentTestClient.expects(:lockfile_path).returns "/my/lock"
46
@agent.lockfile_path.should == "/my/lock"
49
it "should be considered running if the lock file is locked" do
50
lockfile = mock 'lockfile'
52
@agent.expects(:lockfile).returns lockfile
53
lockfile.expects(:locked?).returns true
55
@agent.should be_running
58
describe "when being run" do
60
@agent.stubs(:running?).returns false
64
@agent.expects(:splay)
65
@agent.stubs(:running?).returns false
70
it "should do nothing if already running" do
71
@agent.expects(:running?).returns true
72
AgentTestClient.expects(:new).never
76
it "should do nothing if it is in the process of stopping" do
77
@agent.expects(:stopping?).returns true
78
AgentTestClient.expects(:new).never
82
it "should not fail if a client class instance cannot be created" do
83
AgentTestClient.expects(:new).raises "eh"
88
it "should not fail if there is an exception while running its client" do
89
client = AgentTestClient.new
90
AgentTestClient.expects(:new).returns client
91
client.expects(:run).raises "eh"
96
it "should use a mutex to restrict multi-threading" do
97
client = AgentTestClient.new
98
AgentTestClient.expects(:new).returns client
101
@agent.expects(:sync).returns mutex
103
mutex.expects(:synchronize)
104
client.expects(:run).never # if it doesn't run, then we know our yield is what triggers it
108
it "should use a filesystem lock to restrict multiple processes running the agent" do
109
client = AgentTestClient.new
110
AgentTestClient.expects(:new).returns client
112
@agent.expects(:lock)
114
client.expects(:run).never # if it doesn't run, then we know our yield is what triggers it
118
it "should make its client instance available while running" do
119
client = AgentTestClient.new
120
AgentTestClient.expects(:new).returns client
122
client.expects(:run).with { @agent.client.should equal(client); true }
126
it "should run the client instance with any arguments passed to it" do
127
client = AgentTestClient.new
128
AgentTestClient.expects(:new).returns client
130
client.expects(:run).with("testargs")
131
@agent.run("testargs")
135
describe "when splaying" do
137
Puppet.settings.stubs(:value).with(:splay).returns true
138
Puppet.settings.stubs(:value).with(:splaylimit).returns "10"
141
it "should do nothing if splay is disabled" do
142
Puppet.settings.expects(:value).returns false
143
@agent.expects(:sleep).never
147
it "should do nothing if it has already splayed" do
148
@agent.expects(:splayed?).returns true
149
@agent.expects(:sleep).never
153
it "should log that it is splaying" do
159
it "should sleep for a random portion of the splaylimit plus 1" do
160
Puppet.settings.expects(:value).with(:splaylimit).returns "50"
161
@agent.expects(:rand).with(51).returns 10
162
@agent.expects(:sleep).with(10)
166
it "should mark that it has splayed" do
169
@agent.should be_splayed
173
describe "when stopping" do
174
it "should do nothing if already stopping" do
175
@agent.expects(:stopping?).returns true
179
it "should stop the client if one is available and it responds to 'stop'" do
180
client = AgentTestClient.new
182
@agent.stubs(:client).returns client
183
client.expects(:stop)
187
it "should mark itself as stopping while waiting for the client to stop" do
188
client = AgentTestClient.new
190
@agent.stubs(:client).returns client
191
client.expects(:stop).with { @agent.should be_stopping; true }
197
describe "when starting" do
199
@agent.stubs(:observe_signal)
202
it "should create a timer with the runinterval, a tolerance of 1, and :start? set to true" do
203
Puppet.settings.expects(:value).with(:runinterval).returns 5
204
timer = stub 'timer', :sound_alarm => nil
205
EventLoop::Timer.expects(:new).with(:interval => 5, :start? => true, :tolerance => 1).returns timer
211
it "should run once immediately" do
213
EventLoop::Timer.expects(:new).returns timer
215
timer.expects(:sound_alarm)
220
it "should run within the block passed to the timer" do
221
timer = stub 'timer', :sound_alarm => nil
222
EventLoop::Timer.expects(:new).returns(timer).yields
229
describe "when restarting" do
230
it "should configure itself for a delayed restart if currently running" do
231
@agent.expects(:running?).returns true
235
@agent.should be_needing_restart
238
it "should hup itself if not running" do
239
@agent.expects(:running?).returns false
241
Process.expects(:kill).with(:HUP, $$)
246
it "should turn off the needing_restart switch" do
247
@agent.expects(:running?).times(2).returns(true).then.returns false
251
# First call sets up the switch
254
# Second call should disable it
256
@agent.should_not be_needing_restart