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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
require 'spec_helper'
require 'puppet/agent'
require 'puppet/configurer'
class AgentTestClient
def initialize(transaction_uuid = nil, job_id = nil)
end
def run(client_args)
# no-op
end
def stop
# no-op
end
end
def without_warnings
flag = $VERBOSE
$VERBOSE = nil
yield
$VERBOSE = flag
end
describe Puppet::Agent do
before do
@agent = Puppet::Agent.new(AgentTestClient, false)
# make Puppet::Application safe for stubbing; restore in an :after block; silence warnings for this.
without_warnings { Puppet::Application = Class.new(Puppet::Application) }
allow(Puppet::Application).to receive(:clear?).and_return(true)
Puppet::Application.class_eval do
class << self
def controlled_run(&block)
block.call
end
end
end
ssl_context = Puppet::SSL::SSLContext.new
machine = instance_double("Puppet::SSL::StateMachine", ensure_client_certificate: ssl_context)
allow(Puppet::SSL::StateMachine).to receive(:new).and_return(machine)
end
after do
# restore Puppet::Application from stub-safe subclass, and silence warnings
without_warnings { Puppet::Application = Puppet::Application.superclass }
end
it "should set its client class at initialization" do
expect(Puppet::Agent.new("foo", false).client_class).to eq("foo")
end
it "should include the Locker module" do
expect(Puppet::Agent.ancestors).to be_include(Puppet::Agent::Locker)
end
it "should create an instance of its client class and run it when asked to run" do
client = double('client')
allow(AgentTestClient).to receive(:new).with(nil, nil).and_return(client)
allow(@agent).to receive(:disabled?).and_return(false)
expect(client).to receive(:run)
@agent.run
end
it "should initialize the client's transaction_uuid if passed as a client_option" do
client = double('client')
transaction_uuid = 'foo'
expect(AgentTestClient).to receive(:new).with(transaction_uuid, nil).and_return(client)
expect(client).to receive(:run)
allow(@agent).to receive(:disabled?).and_return(false)
@agent.run(:transaction_uuid => transaction_uuid)
end
it "should initialize the client's job_id if passed as a client_option" do
client = double('client')
job_id = '289'
expect(AgentTestClient).to receive(:new).with(anything, job_id).and_return(client)
expect(client).to receive(:run)
allow(@agent).to receive(:disabled?).and_return(false)
@agent.run(:job_id => job_id)
end
it "should be considered running if the lock file is locked" do
lockfile = double('lockfile')
expect(@agent).to receive(:lockfile).and_return(lockfile)
expect(lockfile).to receive(:locked?).and_return(true)
expect(@agent).to be_running
end
describe "when being run" do
before do
allow(@agent).to receive(:disabled?).and_return(false)
end
it "should splay" do
Puppet[:splay] = true
expect(@agent).to receive(:splay)
@agent.run
end
it "should do nothing if disabled" do
expect(@agent).to receive(:disabled?).and_return(true)
expect(AgentTestClient).not_to receive(:new)
@agent.run
end
it "(#11057) should notify the user about why a run is skipped" do
allow(Puppet::Application).to receive(:controlled_run).and_return(false)
allow(Puppet::Application).to receive(:run_status).and_return('MOCK_RUN_STATUS')
# This is the actual test that we inform the user why the run is skipped.
# We assume this information is contained in
# Puppet::Application.run_status
expect(Puppet).to receive(:notice).with(/MOCK_RUN_STATUS/)
@agent.run
end
it "should display an informative message if the agent is administratively disabled" do
expect(@agent).to receive(:disabled?).and_return(true)
expect(@agent).to receive(:disable_message).and_return("foo")
expect(Puppet).to receive(:notice).with(/Skipping run of .*; administratively disabled.*\(Reason: 'foo'\)/)
@agent.run
end
it "should use Puppet::Application.controlled_run to manage process state behavior" do
expect(Puppet::Application).to receive(:controlled_run).ordered.and_yield
expect(AgentTestClient).to receive(:new).ordered.once
@agent.run
end
it "should not fail if a client class instance cannot be created" do
expect(AgentTestClient).to receive(:new).and_raise("eh")
expect(Puppet).to receive(:log_exception)
@agent.run
end
it "should not fail if there is an exception while running its client" do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(client).to receive(:run).and_raise("eh")
expect(Puppet).to receive(:log_exception)
@agent.run
end
it "should use a filesystem lock to restrict multiple processes running the agent" do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(@agent).to receive(:lock)
expect(client).not_to receive(:run) # if it doesn't run, then we know our yield is what triggers it
@agent.run
end
it "should make its client instance available while running" do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(client).to receive(:run) { expect(@agent.client).to equal(client); nil }
@agent.run
end
it "should run the client instance with any arguments passed to it" do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(client).to receive(:run).with({:pluginsync => true, :other => :options})
@agent.run(:other => :options)
end
it "should return the agent result" do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(@agent).to receive(:lock).and_return(:result)
expect(@agent.run).to eq(:result)
end
it "should check if it's disabled after splaying and log a message" do
Puppet[:splay] = true
Puppet[:splaylimit] = '5s'
Puppet[:onetime] = true
expect(@agent).to receive(:disabled?).and_return(false, true)
allow(Puppet).to receive(:notice).and_call_original
expect(Puppet).to receive(:notice).with(/Skipping run of .*; administratively disabled.*/)
@agent.run
end
it "should check if it's disabled after acquiring the lock and log a message" do
expect(@agent).to receive(:disabled?).and_return(false, true)
allow(Puppet).to receive(:notice).and_call_original
expect(Puppet).to receive(:notice).with(/Skipping run of .*; administratively disabled.*/)
@agent.run
end
describe "and a puppet agent is already running" do
before(:each) do
allow_any_instance_of(Object).to receive(:sleep)
lockfile = double('lockfile')
expect(@agent).to receive(:lockfile).and_return(lockfile).at_least(:once)
# so the lock method raises Puppet::LockError
allow(lockfile).to receive(:lock).and_return(false)
end
it "should notify that a run is already in progress" do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(Puppet).to receive(:notice).with(/Run of .* already in progress; skipping .* exists/)
@agent.run
end
it "should inform that a run is already in progress and try to run every X seconds if waitforlock is used" do
# so the locked file exists
allow(File).to receive(:file?).and_return(true)
# so we don't have to wait again for the run to exit (default maxwaitforcert is 60)
# first 0 is to get the time, second 0 is to inform user, then 1000 so the time expires
allow(Time).to receive(:now).and_return(0, 0, 1000)
allow(Puppet).to receive(:info)
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
Puppet[:waitforlock] = 1
Puppet[:maxwaitforlock] = 2
expect(Puppet).to receive(:info).with(/Another puppet instance is already running; --waitforlock flag used, waiting for running instance to finish./)
expect(Puppet).to receive(:info).with(/Will try again in #{Puppet[:waitforlock]} seconds./)
@agent.run
end
it "should notify that the run is exiting if waitforlock is used and maxwaitforlock is exceeded" do
# so we don't have to wait again for the run to exit (default maxwaitforcert is 60)
# first 0 is to get the time, then 1000 so that the time expires
allow(Time).to receive(:now).and_return(0, 1000)
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
Puppet[:waitforlock] = 1
expect(Puppet).to receive(:notice).with(/Exiting now because the maxwaitforlock timeout has been exceeded./)
@agent.run
end
end
describe "when should_fork is true", :if => Puppet.features.posix? && RUBY_PLATFORM != 'java' do
before do
@agent = Puppet::Agent.new(AgentTestClient, true)
# So we don't actually try to hit the filesystem.
allow(@agent).to receive(:lock).and_yield
end
it "should run the agent in a forked process" do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(client).to receive(:run).and_return(0)
expect(Kernel).to receive(:fork).and_yield
expect { @agent.run }.to exit_with(0)
end
it "should exit child process if child exit" do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(client).to receive(:run).and_raise(SystemExit.new(-1))
expect(Kernel).to receive(:fork).and_yield
expect { @agent.run }.to exit_with(-1)
end
it 'should exit with 1 if an exception is raised' do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(client).to receive(:run).and_raise(StandardError)
expect(Kernel).to receive(:fork).and_yield
expect { @agent.run }.to exit_with(1)
end
it 'should exit with 254 if NoMemoryError exception is raised' do
client = AgentTestClient.new
expect(AgentTestClient).to receive(:new).and_return(client)
expect(client).to receive(:run).and_raise(NoMemoryError)
expect(Kernel).to receive(:fork).and_yield
expect { @agent.run }.to exit_with(254)
end
it "should return the block exit code as the child exit code" do
expect(Kernel).to receive(:fork).and_yield
expect {
@agent.run_in_fork {
777
}
}.to exit_with(777)
end
it "should return `1` exit code if the block returns `nil`" do
expect(Kernel).to receive(:fork).and_yield
expect {
@agent.run_in_fork {
nil
}
}.to exit_with(1)
end
it "should return `1` exit code if the block returns `false`" do
expect(Kernel).to receive(:fork).and_yield
expect {
@agent.run_in_fork {
false
}
}.to exit_with(1)
end
end
describe "on Windows", :if => Puppet::Util::Platform.windows? do
it "should never fork" do
agent = Puppet::Agent.new(AgentTestClient, true)
expect(agent.should_fork).to be_falsey
end
end
describe 'when runtimeout is set' do
before(:each) do
Puppet[:runtimeout] = 1
end
it 'times out when a run exceeds the set limit' do
client = AgentTestClient.new
client.instance_eval do
# Stub methods used to set test expectations.
def processing; end
def handling; end
def run(client_options = {})
# Simulate a hanging agent operation that also traps errors.
begin
::Kernel.sleep(5)
processing()
rescue
handling()
end
end
end
expect(AgentTestClient).to receive(:new).and_return(client)
expect(client).not_to receive(:processing)
expect(client).not_to receive(:handling)
expect(Puppet).to receive(:log_exception).with(be_an_instance_of(Puppet::Agent::RunTimeoutError), anything)
expect(@agent.run).to eq(nil)
end
end
end
describe "when checking execution state" do
describe 'with regular run status' do
before :each do
allow(Puppet::Application).to receive(:restart_requested?).and_return(false)
allow(Puppet::Application).to receive(:stop_requested?).and_return(false)
allow(Puppet::Application).to receive(:interrupted?).and_return(false)
allow(Puppet::Application).to receive(:clear?).and_return(true)
end
it 'should be false for :stopping?' do
expect(@agent.stopping?).to be_falsey
end
it 'should be false for :needing_restart?' do
expect(@agent.needing_restart?).to be_falsey
end
end
describe 'with a stop requested' do
before :each do
allow(Puppet::Application).to receive(:clear?).and_return(false)
allow(Puppet::Application).to receive(:restart_requested?).and_return(false)
allow(Puppet::Application).to receive(:stop_requested?).and_return(true)
allow(Puppet::Application).to receive(:interrupted?).and_return(true)
end
it 'should be true for :stopping?' do
expect(@agent.stopping?).to be_truthy
end
it 'should be false for :needing_restart?' do
expect(@agent.needing_restart?).to be_falsey
end
end
describe 'with a restart requested' do
before :each do
allow(Puppet::Application).to receive(:clear?).and_return(false)
allow(Puppet::Application).to receive(:restart_requested?).and_return(true)
allow(Puppet::Application).to receive(:stop_requested?).and_return(false)
allow(Puppet::Application).to receive(:interrupted?).and_return(true)
end
it 'should be false for :stopping?' do
expect(@agent.stopping?).to be_falsey
end
it 'should be true for :needing_restart?' do
expect(@agent.needing_restart?).to be_truthy
end
end
end
end
|