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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
require 'spec_helper'
require 'puppet/agent'
require 'puppet/application/agent'
require 'puppet/network/server'
require 'puppet/daemon'
describe Puppet::Application::Agent do
include PuppetSpec::Files
before :each do
@puppetd = Puppet::Application[:agent]
@daemon = Puppet::Daemon.new(nil)
allow(@daemon).to receive(:daemonize)
allow(@daemon).to receive(:start)
allow(@daemon).to receive(:stop)
allow(Puppet::Daemon).to receive(:new).and_return(@daemon)
Puppet[:daemonize] = false
@agent = double('agent')
allow(Puppet::Agent).to receive(:new).and_return(@agent)
@puppetd.preinit
allow(Puppet::Util::Log).to receive(:newdestination)
@ssl_host = double('ssl host')
allow(@ssl_host).to receive(:wait_for_cert)
allow(Puppet::SSL::Host).to receive(:new).and_return(@ssl_host)
allow(Puppet::Node.indirection).to receive(:terminus_class=)
allow(Puppet::Node.indirection).to receive(:cache_class=)
allow(Puppet::Node::Facts.indirection).to receive(:terminus_class=)
expect($stderr).not_to receive(:puts)
allow(Puppet.settings).to receive(:use)
end
it "should operate in agent run_mode" do
expect(@puppetd.class.run_mode.name).to eq(:agent)
end
it "should declare a main command" do
expect(@puppetd).to respond_to(:main)
end
it "should declare a onetime command" do
expect(@puppetd).to respond_to(:onetime)
end
it "should declare a fingerprint command" do
expect(@puppetd).to respond_to(:fingerprint)
end
it "should declare a preinit block" do
expect(@puppetd).to respond_to(:preinit)
end
describe "in preinit" do
it "should catch INT" do
expect(Signal).to receive(:trap).with(:INT)
@puppetd.preinit
end
it "should init fqdn to nil" do
@puppetd.preinit
expect(@puppetd.options[:fqdn]).to be_nil
end
it "should init serve to []" do
@puppetd.preinit
expect(@puppetd.options[:serve]).to eq([])
end
it "should use SHA256 as default digest algorithm" do
@puppetd.preinit
expect(@puppetd.options[:digest]).to eq('SHA256')
end
it "should not fingerprint by default" do
@puppetd.preinit
expect(@puppetd.options[:fingerprint]).to be_falsey
end
it "should init waitforcert to nil" do
@puppetd.preinit
expect(@puppetd.options[:waitforcert]).to be_nil
end
end
describe "when handling options" do
before do
allow(@puppetd.command_line).to receive(:args).and_return([])
end
[:enable, :debug, :fqdn, :test, :verbose, :digest].each do |option|
it "should declare handle_#{option} method" do
expect(@puppetd).to respond_to("handle_#{option}".to_sym)
end
it "should store argument value when calling handle_#{option}" do
@puppetd.send("handle_#{option}".to_sym, 'arg')
expect(@puppetd.options[option]).to eq('arg')
end
end
describe "when handling --disable" do
it "should set disable to true" do
@puppetd.handle_disable('')
expect(@puppetd.options[:disable]).to eq(true)
end
it "should store disable message" do
@puppetd.handle_disable('message')
expect(@puppetd.options[:disable_message]).to eq('message')
end
end
it "should set waitforcert to 0 with --onetime and if --waitforcert wasn't given" do
allow(@agent).to receive(:run).and_return(2)
Puppet[:onetime] = true
expect(@ssl_host).to receive(:wait_for_cert).with(0)
expect { execute_agent }.to exit_with 0
end
it "should use supplied waitforcert when --onetime is specified" do
allow(@agent).to receive(:run).and_return(2)
Puppet[:onetime] = true
@puppetd.handle_waitforcert(60)
expect(@ssl_host).to receive(:wait_for_cert).with(60)
expect { execute_agent }.to exit_with 0
end
it "should use a default value for waitforcert when --onetime and --waitforcert are not specified" do
expect(@ssl_host).to receive(:wait_for_cert).with(120)
execute_agent
end
it "should use the waitforcert setting when checking for a signed certificate" do
Puppet[:waitforcert] = 10
expect(@ssl_host).to receive(:wait_for_cert).with(10)
execute_agent
end
it "should set the log destination with --logdest" do
expect(Puppet::Log).to receive(:newdestination).with("console")
@puppetd.handle_logdest("console")
end
it "should put the setdest options to true" do
@puppetd.handle_logdest("console")
expect(@puppetd.options[:setdest]).to eq(true)
end
it "should parse the log destination from the command line" do
allow(@puppetd.command_line).to receive(:args).and_return(%w{--logdest /my/file})
expect(Puppet::Util::Log).to receive(:newdestination).with("/my/file")
@puppetd.parse_options
end
it "should store the waitforcert options with --waitforcert" do
@puppetd.handle_waitforcert("42")
expect(@puppetd.options[:waitforcert]).to eq(42)
end
end
describe "during setup" do
before :each do
allow(Puppet).to receive(:info)
Puppet[:libdir] = "/dev/null/lib"
allow(Puppet::Transaction::Report.indirection).to receive(:terminus_class=)
allow(Puppet::Transaction::Report.indirection).to receive(:cache_class=)
allow(Puppet::Resource::Catalog.indirection).to receive(:terminus_class=)
allow(Puppet::Resource::Catalog.indirection).to receive(:cache_class=)
allow(Puppet::Node::Facts.indirection).to receive(:terminus_class=)
allow(Puppet).to receive(:settraps)
end
it "should not run with extra arguments" do
allow(@puppetd.command_line).to receive(:args).and_return(%w{disable})
expect{@puppetd.setup}.to raise_error ArgumentError, /does not take parameters/
end
describe "with --test" do
it "should call setup_test" do
@puppetd.options[:test] = true
expect(@puppetd).to receive(:setup_test)
@puppetd.setup
end
it "should set options[:verbose] to true" do
@puppetd.setup_test
expect(@puppetd.options[:verbose]).to eq(true)
end
it "should set options[:onetime] to true" do
Puppet[:onetime] = false
@puppetd.setup_test
expect(Puppet[:onetime]).to eq(true)
end
it "should set options[:detailed_exitcodes] to true" do
@puppetd.setup_test
expect(@puppetd.options[:detailed_exitcodes]).to eq(true)
end
end
it "should call setup_logs" do
expect(@puppetd).to receive(:setup_logs)
@puppetd.setup
end
describe "when setting up logs" do
before :each do
allow(Puppet::Util::Log).to receive(:newdestination)
end
it "should set log level to debug if --debug was passed" do
@puppetd.options[:debug] = true
@puppetd.setup_logs
expect(Puppet::Util::Log.level).to eq(:debug)
end
it "should set log level to info if --verbose was passed" do
@puppetd.options[:verbose] = true
@puppetd.setup_logs
expect(Puppet::Util::Log.level).to eq(:info)
end
[:verbose, :debug].each do |level|
it "should set console as the log destination with level #{level}" do
@puppetd.options[level] = true
allow(Puppet::Util::Log).to receive(:newdestination)
expect(Puppet::Util::Log).to receive(:newdestination).with(:console).exactly(:once)
@puppetd.setup_logs
end
end
it "should set a default log destination if no --logdest" do
@puppetd.options[:setdest] = false
expect(Puppet::Util::Log).to receive(:setup_default)
@puppetd.setup_logs
end
end
it "should print puppet config if asked to in Puppet config" do
Puppet[:configprint] = "pluginsync"
expect(Puppet.settings).to receive(:print_configs).and_return(true)
expect { execute_agent }.to exit_with 0
end
it "should exit after printing puppet config if asked to in Puppet config" do
path = make_absolute('/my/path')
Puppet[:modulepath] = path
Puppet[:configprint] = "modulepath"
expect_any_instance_of(Puppet::Settings).to receive(:puts).with(path)
expect { execute_agent }.to exit_with 0
end
it "should use :main, :puppetd, and :ssl" do
expect(Puppet.settings).to receive(:use).with(:main, :agent, :ssl)
@puppetd.setup
end
it "should install a remote ca location" do
expect(Puppet::SSL::Host).to receive(:ca_location=).with(:remote)
@puppetd.setup
end
it "should install a none ca location in fingerprint mode" do
@puppetd.options[:fingerprint] = true
expect(Puppet::SSL::Host).to receive(:ca_location=).with(:none)
@puppetd.setup
end
it "should tell the report handler to use REST" do
expect(Puppet::Transaction::Report.indirection).to receive(:terminus_class=).with(:rest)
@puppetd.setup
end
it "should tell the report handler to cache locally as yaml" do
expect(Puppet::Transaction::Report.indirection).to receive(:cache_class=).with(:yaml)
@puppetd.setup
end
it "should default catalog_terminus setting to 'rest'" do
@puppetd.initialize_app_defaults
expect(Puppet[:catalog_terminus]).to eq(:rest)
end
it "should default node_terminus setting to 'rest'" do
@puppetd.initialize_app_defaults
expect(Puppet[:node_terminus]).to eq(:rest)
end
it "has an application default :catalog_cache_terminus setting of 'json'" do
expect(Puppet::Resource::Catalog.indirection).to receive(:cache_class=).with(:json)
@puppetd.initialize_app_defaults
@puppetd.setup
end
it "should tell the catalog cache class based on the :catalog_cache_terminus setting" do
Puppet[:catalog_cache_terminus] = "yaml"
expect(Puppet::Resource::Catalog.indirection).to receive(:cache_class=).with(:yaml)
@puppetd.initialize_app_defaults
@puppetd.setup
end
it "should not set catalog cache class if :catalog_cache_terminus is explicitly nil" do
Puppet[:catalog_cache_terminus] = nil
expect(Puppet::Resource::Catalog.indirection).not_to receive(:cache_class=)
@puppetd.initialize_app_defaults
@puppetd.setup
end
it "should default facts_terminus setting to 'facter'" do
@puppetd.initialize_app_defaults
expect(Puppet[:facts_terminus]).to eq(:facter)
end
it "should create an agent" do
allow(Puppet::Agent).to receive(:new).with(Puppet::Configurer)
@puppetd.setup
end
[:enable, :disable].each do |action|
it "should delegate to enable_disable_client if we #{action} the agent" do
@puppetd.options[action] = true
expect(@puppetd).to receive(:enable_disable_client).with(@agent)
@puppetd.setup
end
end
describe "when enabling or disabling agent" do
[:enable, :disable].each do |action|
it "should call client.#{action}" do
@puppetd.options[action] = true
expect(@agent).to receive(action)
expect { execute_agent }.to exit_with 0
end
end
it "should pass the disable message when disabling" do
@puppetd.options[:disable] = true
@puppetd.options[:disable_message] = "message"
expect(@agent).to receive(:disable).with("message")
expect { execute_agent }.to exit_with 0
end
it "should pass the default disable message when disabling without a message" do
@puppetd.options[:disable] = true
@puppetd.options[:disable_message] = nil
expect(@agent).to receive(:disable).with("reason not specified")
expect { execute_agent }.to exit_with 0
end
end
it "should inform the daemon about our agent if :client is set to 'true'" do
@puppetd.options[:client] = true
execute_agent
expect(@daemon.agent).to eq(@agent)
end
it "should daemonize if needed" do
allow(Puppet.features).to receive(:microsoft_windows?).and_return(false)
Puppet[:daemonize] = true
expect(@daemon).to receive(:daemonize)
execute_agent
end
it "should wait for a certificate" do
@puppetd.options[:waitforcert] = 123
expect(@ssl_host).to receive(:wait_for_cert).with(123)
execute_agent
end
it "should not wait for a certificate in fingerprint mode" do
@puppetd.options[:fingerprint] = true
@puppetd.options[:waitforcert] = 123
@puppetd.options[:digest] = 'MD5'
certificate = double('certificate')
allow(certificate).to receive(:digest).with('MD5').and_return('ABCDE')
allow(@ssl_host).to receive(:certificate).and_return(certificate)
expect(@ssl_host).not_to receive(:wait_for_cert)
expect(@puppetd).to receive(:puts).with('ABCDE')
execute_agent
end
describe "when setting up for fingerprint" do
before(:each) do
@puppetd.options[:fingerprint] = true
end
it "should not setup as an agent" do
expect(@puppetd).not_to receive(:setup_agent)
@puppetd.setup
end
it "should not create an agent" do
expect(Puppet::Agent).not_to receive(:new).with(Puppet::Configurer)
@puppetd.setup
end
it "should not daemonize" do
expect(@daemon).not_to receive(:daemonize)
@puppetd.setup
end
end
describe "when configuring agent for catalog run" do
it "should set should_fork as true when running normally" do
expect(Puppet::Agent).to receive(:new).with(anything, true)
@puppetd.setup
end
it "should not set should_fork as false for --onetime" do
Puppet[:onetime] = true
expect(Puppet::Agent).to receive(:new).with(anything, false)
@puppetd.setup
end
end
end
describe "when running" do
before :each do
@puppetd.options[:fingerprint] = false
end
it "should dispatch to fingerprint if --fingerprint is used" do
@puppetd.options[:fingerprint] = true
allow(@puppetd).to receive(:fingerprint)
execute_agent
end
it "should dispatch to onetime if --onetime is used" do
@puppetd.options[:onetime] = true
allow(@puppetd).to receive(:onetime)
execute_agent
end
it "should dispatch to main if --onetime and --fingerprint are not used" do
@puppetd.options[:onetime] = false
allow(@puppetd).to receive(:main)
execute_agent
end
describe "with --onetime" do
before :each do
allow(@agent).to receive(:run).and_return(:report)
Puppet[:onetime] = true
@puppetd.options[:client] = :client
@puppetd.options[:detailed_exitcodes] = false
end
it "should setup traps" do
expect(@daemon).to receive(:set_signal_traps)
expect { execute_agent }.to exit_with 0
end
it "should let the agent run" do
expect(@agent).to receive(:run).and_return(:report)
expect { execute_agent }.to exit_with 0
end
it "should run the agent with the supplied job_id" do
@puppetd.options[:job_id] = 'special id'
expect(@agent).to receive(:run).with(:job_id => 'special id').and_return(:report)
expect { execute_agent }.to exit_with 0
end
it "should stop the daemon" do
expect(@daemon).to receive(:stop).with(:exit => false)
expect { execute_agent }.to exit_with 0
end
describe "and --detailed-exitcodes" do
before :each do
@puppetd.options[:detailed_exitcodes] = true
end
it "should exit with agent computed exit status" do
Puppet[:noop] = false
allow(@agent).to receive(:run).and_return(666)
expect { execute_agent }.to exit_with 666
end
it "should exit with the agent's exit status, even if --noop is set." do
Puppet[:noop] = true
allow(@agent).to receive(:run).and_return(666)
expect { execute_agent }.to exit_with 666
end
end
end
describe "with --fingerprint" do
before :each do
@cert = double('cert')
@puppetd.options[:fingerprint] = true
@puppetd.options[:digest] = :MD5
end
it "should fingerprint the certificate if it exists" do
allow(@ssl_host).to receive(:certificate).and_return(@cert)
allow(@cert).to receive(:digest).with('MD5').and_return("fingerprint")
expect(@puppetd).to receive(:puts).with("fingerprint")
@puppetd.fingerprint
end
it "should fingerprint the certificate request if no certificate have been signed" do
allow(@ssl_host).to receive(:certificate).and_return(nil)
allow(@ssl_host).to receive(:certificate_request).and_return(@cert)
allow(@cert).to receive(:digest).with('MD5').and_return("fingerprint")
expect(@puppetd).to receive(:puts).with("fingerprint")
@puppetd.fingerprint
end
end
describe "without --onetime and --fingerprint" do
before :each do
allow(Puppet).to receive(:notice)
end
it "should start our daemon" do
expect(@daemon).to receive(:start)
execute_agent
end
end
end
describe "when starting in daemon mode on non-windows", :unless => Puppet.features.microsoft_windows? do
before :each do
allow(Puppet).to receive(:notice)
Puppet[:daemonize] = true
end
it "should not print config in default mode" do
execute_agent
expect(@logs).to be_empty
end
it "should print config in debug mode" do
@puppetd.options[:debug] = true
execute_agent
expect(@logs).to include(an_object_having_attributes(level: :debug, message: /agent_catalog_run_lockfile=/))
end
end
def execute_agent
@puppetd.setup
@puppetd.run_command
end
end
|