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

« back to all changes in this revision

Viewing changes to spec/unit/agent.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
#  Created by Luke Kanies on 2007-11-12.
 
4
#  Copyright (c) 2007. All rights reserved.
 
5
 
 
6
require File.dirname(__FILE__) + '/../spec_helper'
 
7
require 'puppet/agent'
 
8
 
 
9
class AgentTestClient
 
10
    def run
 
11
        # no-op
 
12
    end
 
13
    def stop
 
14
        # no-op
 
15
    end
 
16
end
 
17
 
 
18
describe Puppet::Agent do
 
19
    before do
 
20
        @agent = Puppet::Agent.new(AgentTestClient)
 
21
 
 
22
        # So we don't actually try to hit the filesystem.
 
23
        @agent.stubs(:lock).yields
 
24
    end
 
25
 
 
26
    it "should set its client class at initialization" do
 
27
        Puppet::Agent.new("foo").client_class.should == "foo"
 
28
    end
 
29
 
 
30
    it "should include the Locker module" do
 
31
        Puppet::Agent.ancestors.should be_include(Puppet::Agent::Locker)
 
32
    end
 
33
 
 
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
 
37
 
 
38
        client.expects(:run)
 
39
 
 
40
        @agent.stubs(:running?).returns false
 
41
        @agent.run
 
42
    end
 
43
 
 
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"
 
47
    end
 
48
 
 
49
    it "should be considered running if the lock file is locked" do
 
50
        lockfile = mock 'lockfile'
 
51
 
 
52
        @agent.expects(:lockfile).returns lockfile
 
53
        lockfile.expects(:locked?).returns true
 
54
 
 
55
        @agent.should be_running
 
56
    end
 
57
 
 
58
    describe "when being run" do
 
59
        before do
 
60
            @agent.stubs(:running?).returns false
 
61
        end
 
62
 
 
63
        it "should splay" do
 
64
            @agent.expects(:splay)
 
65
            @agent.stubs(:running?).returns false
 
66
 
 
67
            @agent.run
 
68
        end
 
69
 
 
70
        it "should do nothing if already running" do
 
71
            @agent.expects(:running?).returns true
 
72
            AgentTestClient.expects(:new).never
 
73
            @agent.run
 
74
        end
 
75
 
 
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
 
79
            @agent.run
 
80
        end
 
81
 
 
82
        it "should not fail if a client class instance cannot be created" do
 
83
            AgentTestClient.expects(:new).raises "eh"
 
84
            Puppet.expects(:err)
 
85
            @agent.run
 
86
        end
 
87
 
 
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"
 
92
            Puppet.expects(:err)
 
93
            @agent.run
 
94
        end
 
95
 
 
96
        it "should use a mutex to restrict multi-threading" do
 
97
            client = AgentTestClient.new
 
98
            AgentTestClient.expects(:new).returns client
 
99
 
 
100
            mutex = mock 'mutex'
 
101
            @agent.expects(:sync).returns mutex
 
102
 
 
103
            mutex.expects(:synchronize)
 
104
            client.expects(:run).never # if it doesn't run, then we know our yield is what triggers it
 
105
            @agent.run
 
106
        end
 
107
 
 
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
 
111
 
 
112
            @agent.expects(:lock)
 
113
 
 
114
            client.expects(:run).never # if it doesn't run, then we know our yield is what triggers it
 
115
            @agent.run
 
116
        end
 
117
 
 
118
        it "should make its client instance available while running" do
 
119
            client = AgentTestClient.new
 
120
            AgentTestClient.expects(:new).returns client
 
121
 
 
122
            client.expects(:run).with { @agent.client.should equal(client); true }
 
123
            @agent.run
 
124
        end
 
125
 
 
126
        it "should run the client instance with any arguments passed to it" do
 
127
            client = AgentTestClient.new
 
128
            AgentTestClient.expects(:new).returns client
 
129
 
 
130
            client.expects(:run).with("testargs")
 
131
            @agent.run("testargs")
 
132
        end
 
133
    end
 
134
 
 
135
    describe "when splaying" do
 
136
        before do
 
137
            Puppet.settings.stubs(:value).with(:splay).returns true
 
138
            Puppet.settings.stubs(:value).with(:splaylimit).returns "10"
 
139
        end
 
140
 
 
141
        it "should do nothing if splay is disabled" do
 
142
            Puppet.settings.expects(:value).returns false
 
143
            @agent.expects(:sleep).never
 
144
            @agent.splay
 
145
        end
 
146
 
 
147
        it "should do nothing if it has already splayed" do
 
148
            @agent.expects(:splayed?).returns true
 
149
            @agent.expects(:sleep).never
 
150
            @agent.splay
 
151
        end
 
152
 
 
153
        it "should log that it is splaying" do
 
154
            @agent.stubs :sleep
 
155
            Puppet.expects :info
 
156
            @agent.splay
 
157
        end
 
158
 
 
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)
 
163
            @agent.splay
 
164
        end
 
165
 
 
166
        it "should mark that it has splayed" do
 
167
            @agent.stubs(:sleep)
 
168
            @agent.splay
 
169
            @agent.should be_splayed
 
170
        end
 
171
    end
 
172
 
 
173
    describe "when stopping" do
 
174
        it "should do nothing if already stopping" do
 
175
            @agent.expects(:stopping?).returns true
 
176
            @agent.stop
 
177
        end
 
178
 
 
179
        it "should stop the client if one is available and it responds to 'stop'" do
 
180
            client = AgentTestClient.new
 
181
 
 
182
            @agent.stubs(:client).returns client
 
183
            client.expects(:stop)
 
184
            @agent.stop
 
185
        end
 
186
 
 
187
        it "should mark itself as stopping while waiting for the client to stop" do
 
188
            client = AgentTestClient.new
 
189
 
 
190
            @agent.stubs(:client).returns client
 
191
            client.expects(:stop).with { @agent.should be_stopping; true }
 
192
 
 
193
            @agent.stop
 
194
        end
 
195
    end
 
196
 
 
197
    describe "when starting" do
 
198
        before do
 
199
            @agent.stubs(:observe_signal)
 
200
        end
 
201
 
 
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
 
206
 
 
207
            @agent.stubs(:run)
 
208
            @agent.start
 
209
        end
 
210
 
 
211
        it "should run once immediately" do
 
212
            timer = mock 'timer'
 
213
            EventLoop::Timer.expects(:new).returns timer
 
214
 
 
215
            timer.expects(:sound_alarm)
 
216
 
 
217
            @agent.start
 
218
        end
 
219
 
 
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
 
223
            @agent.expects(:run)
 
224
 
 
225
            @agent.start
 
226
        end
 
227
    end
 
228
 
 
229
    describe "when restarting" do
 
230
        it "should configure itself for a delayed restart if currently running" do
 
231
            @agent.expects(:running?).returns true
 
232
 
 
233
            @agent.restart
 
234
 
 
235
            @agent.should be_needing_restart
 
236
        end
 
237
 
 
238
        it "should hup itself if not running" do
 
239
            @agent.expects(:running?).returns false
 
240
 
 
241
            Process.expects(:kill).with(:HUP, $$)
 
242
 
 
243
            @agent.restart
 
244
        end
 
245
 
 
246
        it "should turn off the needing_restart switch" do
 
247
            @agent.expects(:running?).times(2).returns(true).then.returns false
 
248
 
 
249
            Process.stubs(:kill)
 
250
 
 
251
            # First call sets up the switch
 
252
            @agent.restart
 
253
 
 
254
            # Second call should disable it
 
255
            @agent.restart
 
256
            @agent.should_not be_needing_restart
 
257
        end
 
258
    end
 
259
end