summaryrefslogtreecommitdiffstats
path: root/spec/unit/daemon_spec.rb
blob: fc43d93ad8c9e3fc37f3aa88630e549a026e96ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env rspec
require 'spec_helper'
require 'puppet/daemon'

def without_warnings
  flag = $VERBOSE
  $VERBOSE = nil
  yield
  $VERBOSE = flag
end

describe Puppet::Daemon do
  before do
    @daemon = Puppet::Daemon.new
  end

  it "should be able to manage an agent" do
    @daemon.should respond_to(:agent)
  end

  it "should be able to manage a network server" do
    @daemon.should respond_to(:server)
  end

  it "should reopen the Log logs when told to reopen logs" do
    Puppet::Util::Log.expects(:reopen)
    @daemon.reopen_logs
  end

  describe "when setting signal traps" do
    signals = {:INT => :stop, :TERM => :stop }
    signals.update({:HUP => :restart, :USR1 => :reload, :USR2 => :reopen_logs}) unless Puppet.features.microsoft_windows?
    signals.each do |signal, method|
      it "should log and call #{method} when it receives #{signal}" do
        Signal.expects(:trap).with(signal).yields

        Puppet.expects(:notice)

        @daemon.expects(method)

        @daemon.set_signal_traps
      end
    end
  end

  describe "when starting" do
    before do
      @daemon.stubs(:create_pidfile)
      @daemon.stubs(:set_signal_traps)
      EventLoop.current.stubs(:run)
    end

    it "should fail if it has neither agent nor server" do
      lambda { @daemon.start }.should raise_error(Puppet::DevError)
    end

    it "should create its pidfile" do
      @daemon.stubs(:agent).returns stub('agent', :start => nil)

      @daemon.expects(:create_pidfile)
      @daemon.start
    end

    it "should start the agent if the agent is configured" do
      agent = mock 'agent'
      agent.expects(:start)
      @daemon.stubs(:agent).returns agent

      @daemon.start
    end

    it "should start its server if one is configured" do
      server = mock 'server'
      server.expects(:start)
      @daemon.stubs(:server).returns server

      @daemon.start
    end

    it "should let the current EventLoop run" do
      @daemon.stubs(:agent).returns stub('agent', :start => nil)
      EventLoop.current.expects(:run)

      @daemon.start
    end
  end

  describe "when stopping" do
    before do
      @daemon.stubs(:remove_pidfile)
      Puppet::Util::Log.stubs(:close_all)
      # to make the global safe to mock, set it to a subclass of itself,
      # then restore it in an after pass
      without_warnings { Puppet::Application = Class.new(Puppet::Application) }
    end

    after do
      # restore from the superclass so we lose the stub garbage
      without_warnings { Puppet::Application = Puppet::Application.superclass }
    end

    it "should stop its server if one is configured" do
      server = mock 'server'
      server.expects(:stop)
      @daemon.stubs(:server).returns server
      expect { @daemon.stop }.to exit_with 0
    end

    it 'should request a stop from Puppet::Application' do
      Puppet::Application.expects(:stop!)
      expect { @daemon.stop }.to exit_with 0
    end

    it "should remove its pidfile" do
      @daemon.expects(:remove_pidfile)
      expect { @daemon.stop }.to exit_with 0
    end

    it "should close all logs" do
      Puppet::Util::Log.expects(:close_all)
      expect { @daemon.stop }.to exit_with 0
    end

    it "should exit unless called with ':exit => false'" do
      expect { @daemon.stop }.to exit_with 0
    end

    it "should not exit if called with ':exit => false'" do
      @daemon.stop :exit => false
    end
  end

  describe "when creating its pidfile" do
    it "should use an exclusive mutex" do
      Puppet.settings.expects(:value).with(:name).returns "me"
      Puppet::Util.expects(:synchronize_on).with("me",Sync::EX)
      @daemon.create_pidfile
    end

    it "should lock the pidfile using the Pidlock class" do
      pidfile = mock 'pidfile'

      Puppet.settings.stubs(:value).with(:name).returns "eh"
      Puppet.settings.expects(:value).with(:pidfile).returns "/my/file"

      Puppet::Util::Pidlock.expects(:new).with("/my/file").returns pidfile

      pidfile.expects(:lock).returns true
      @daemon.create_pidfile
    end

    it "should fail if it cannot lock" do
      pidfile = mock 'pidfile'

      Puppet.settings.stubs(:value).with(:name).returns "eh"
      Puppet.settings.stubs(:value).with(:pidfile).returns "/my/file"

      Puppet::Util::Pidlock.expects(:new).with("/my/file").returns pidfile

      pidfile.expects(:lock).returns false

      lambda { @daemon.create_pidfile }.should raise_error
    end
  end

  describe "when removing its pidfile" do
    it "should use an exclusive mutex" do
      Puppet.settings.expects(:value).with(:name).returns "me"

      Puppet::Util.expects(:synchronize_on).with("me",Sync::EX)

      @daemon.remove_pidfile
    end

    it "should do nothing if the pidfile is not present" do
      pidfile = mock 'pidfile', :locked? => false
      Puppet::Util::Pidlock.expects(:new).with("/my/file").returns pidfile

      Puppet.settings.stubs(:value).with(:name).returns "eh"
      Puppet.settings.stubs(:value).with(:pidfile).returns "/my/file"

      pidfile.expects(:unlock).never
      @daemon.remove_pidfile
    end

    it "should unlock the pidfile using the Pidlock class" do
      pidfile = mock 'pidfile', :locked? => true
      Puppet::Util::Pidlock.expects(:new).with("/my/file").returns pidfile
      pidfile.expects(:unlock).returns true

      Puppet.settings.stubs(:value).with(:name).returns "eh"
      Puppet.settings.stubs(:value).with(:pidfile).returns "/my/file"

      @daemon.remove_pidfile
    end

    it "should warn if it cannot remove the pidfile" do
      pidfile = mock 'pidfile', :locked? => true
      Puppet::Util::Pidlock.expects(:new).with("/my/file").returns pidfile
      pidfile.expects(:unlock).returns false

      Puppet.settings.stubs(:value).with(:name).returns "eh"
      Puppet.settings.stubs(:value).with(:pidfile).returns "/my/file"

      Puppet.expects :err
      @daemon.remove_pidfile
    end
  end

  describe "when reloading" do
    it "should do nothing if no agent is configured" do
      @daemon.reload
    end

    it "should do nothing if the agent is running" do
      agent = mock 'agent'
      agent.expects(:running?).returns true

      @daemon.stubs(:agent).returns agent

      @daemon.reload
    end

    it "should run the agent if one is available and it is not running" do
      agent = mock 'agent'
      agent.expects(:running?).returns false
      agent.expects :run

      @daemon.stubs(:agent).returns agent

      @daemon.reload
    end
  end

  describe "when restarting" do
    before do
      without_warnings { Puppet::Application = Class.new(Puppet::Application) }
    end

    after do
      without_warnings { Puppet::Application = Puppet::Application.superclass }
    end

    it 'should set Puppet::Application.restart!' do
      Puppet::Application.expects(:restart!)
      @daemon.stubs(:reexec)
      @daemon.restart
    end

    it "should reexec itself if no agent is available" do
      @daemon.expects(:reexec)

      @daemon.restart
    end

    it "should reexec itself if the agent is not running" do
      agent = mock 'agent'
      agent.expects(:running?).returns false
      @daemon.stubs(:agent).returns agent
      @daemon.expects(:reexec)

      @daemon.restart
    end
  end

  describe "when reexecing it self" do
    before do
      @daemon.stubs(:exec)
      @daemon.stubs(:stop)
    end

    it "should fail if no argv values are available" do
      @daemon.expects(:argv).returns nil
      lambda { @daemon.reexec }.should raise_error(Puppet::DevError)
    end

    it "should shut down without exiting" do
      @daemon.argv = %w{foo}
      @daemon.expects(:stop).with(:exit => false)

      @daemon.reexec
    end

    it "should call 'exec' with the original executable and arguments" do
      @daemon.argv = %w{foo}
      @daemon.expects(:exec).with($0 + " foo")

      @daemon.reexec
    end
  end
end