Skip to content

Commit 56c91a0

Browse files
committed
(PUP-11428) Cleanup agent unit test
Ensure scheduler only calls `Puppet::Agent#run` once so we don't infinite loop when `Puppet[:daemonize]` is true, which can occur if `Puppet[:onetime]` or the `--test` command line argument is omitted in a test. Remove `command_line` stubbing that interferes with the code being tested. Stub the `run` method on the client class, not `Puppet::Agent#run`. Use `hash_including` to only match parameter arguments the test cares about.
1 parent dae6a92 commit 56c91a0

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

spec/unit/application/agent_spec.rb

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
require 'puppet/application/agent'
55
require 'puppet/daemon'
66

7+
class TestAgentClientClass
8+
def initialize(transaction_uuid = nil, job_id = nil); end
9+
def run(options = {}); end
10+
end
11+
712
describe Puppet::Application::Agent do
813
include PuppetSpec::Files
914

@@ -12,13 +17,20 @@
1217
before :each do
1318
@puppetd = Puppet::Application[:agent]
1419

15-
@agent = double('agent')
20+
@client = TestAgentClientClass.new
21+
allow(TestAgentClientClass).to receive(:new).and_return(@client)
22+
23+
@agent = Puppet::Agent.new(TestAgentClientClass, false)
1624
allow(Puppet::Agent).to receive(:new).and_return(@agent)
1725

18-
@daemon = Puppet::Daemon.new(@agent, nil)
26+
Puppet[:pidfile] = tmpfile('pidfile')
27+
@daemon = Puppet::Daemon.new(@agent, Puppet::Util::Pidlock.new(Puppet[:pidfile]))
1928
allow(@daemon).to receive(:daemonize)
20-
allow(@daemon).to receive(:start)
2129
allow(@daemon).to receive(:stop)
30+
# simulate one run so we don't infinite looptwo runs of the agent, then return so we don't infinite loop
31+
allow(@daemon).to receive(:run_event_loop) do
32+
@agent.run(splay: false)
33+
end
2234
allow(Puppet::Daemon).to receive(:new).and_return(@daemon)
2335
Puppet[:daemonize] = false
2436

@@ -92,10 +104,6 @@
92104
end
93105

94106
describe "when handling options" do
95-
before do
96-
allow(@puppetd.command_line).to receive(:args).and_return([])
97-
end
98-
99107
[:enable, :debug, :fqdn, :test, :verbose, :digest].each do |option|
100108
it "should declare handle_#{option} method" do
101109
expect(@puppetd).to respond_to("handle_#{option}".to_sym)
@@ -127,32 +135,34 @@
127135
end
128136

129137
it "should set waitforcert to 0 with --onetime and if --waitforcert wasn't given" do
130-
allow(@agent).to receive(:run).and_return(2)
138+
allow(@client).to receive(:run).and_return(2)
131139
Puppet[:onetime] = true
132140

133-
expect(Puppet::SSL::StateMachine).to receive(:new).with(waitforcert: 0).and_return(machine)
141+
expect(Puppet::SSL::StateMachine).to receive(:new).with(hash_including(waitforcert: 0)).and_return(machine)
134142

135143
expect { execute_agent }.to exit_with 0
136144
end
137145

138146
it "should use supplied waitforcert when --onetime is specified" do
139-
allow(@agent).to receive(:run).and_return(2)
147+
allow(@client).to receive(:run).and_return(2)
140148
Puppet[:onetime] = true
141149
@puppetd.handle_waitforcert(60)
142150

143-
expect(Puppet::SSL::StateMachine).to receive(:new).with(waitforcert: 60).and_return(machine)
151+
expect(Puppet::SSL::StateMachine).to receive(:new).with(hash_including(waitforcert: 60)).and_return(machine)
144152

145153
expect { execute_agent }.to exit_with 0
146154
end
147155

148156
it "should use a default value for waitforcert when --onetime and --waitforcert are not specified" do
149-
expect(Puppet::SSL::StateMachine).to receive(:new).with(waitforcert: 120).and_return(machine)
157+
allow(@client).to receive(:run).and_return(2)
158+
159+
expect(Puppet::SSL::StateMachine).to receive(:new).with(hash_including(waitforcert: 120)).and_return(machine)
150160

151161
execute_agent
152162
end
153163

154164
it "should register ssl OIDs" do
155-
expect(Puppet::SSL::StateMachine).to receive(:new).with(waitforcert: 120).and_return(double(ensure_client_certificate: nil))
165+
expect(Puppet::SSL::StateMachine).to receive(:new).with(hash_including(waitforcert: 120)).and_return(machine)
156166
expect(Puppet::SSL::Oids).to receive(:register_puppet_oids)
157167

158168
execute_agent
@@ -161,7 +171,7 @@
161171
it "should use the waitforcert setting when checking for a signed certificate" do
162172
Puppet[:waitforcert] = 10
163173

164-
expect(Puppet::SSL::StateMachine).to receive(:new).with(waitforcert: 10).and_return(machine)
174+
expect(Puppet::SSL::StateMachine).to receive(:new).with(hash_including(waitforcert: 10)).and_return(machine)
165175

166176
execute_agent
167177
end
@@ -413,9 +423,9 @@
413423
end
414424

415425
it "should wait for a certificate" do
416-
@puppetd.options[:waitforcert] = 123
426+
Puppet[:waitforcert] = 123
417427

418-
expect(Puppet::SSL::StateMachine).to receive(:new).with(waitforcert: 123).and_return(machine)
428+
expect(Puppet::SSL::StateMachine).to receive(:new).with(hash_including(waitforcert: 123)).and_return(machine)
419429

420430
execute_agent
421431
end

0 commit comments

Comments
 (0)