diff options
-rwxr-xr-x | bin/puppetd | 272 | ||||
-rw-r--r-- | lib/puppet/application/puppetd.rb | 260 | ||||
-rwxr-xr-x | spec/unit/application/puppetd.rb | 487 |
3 files changed, 749 insertions, 270 deletions
diff --git a/bin/puppetd b/bin/puppetd index 4c7f1d131..c4c106c6d 100755 --- a/bin/puppetd +++ b/bin/puppetd @@ -155,273 +155,5 @@ # Copyright (c) 2005, 2006 Reductive Labs, LLC # Licensed under the GNU Public License -# Do an initial trap, so that cancels don't get a stack trace. -trap(:INT) do - $stderr.puts "Cancelling startup" - exit(0) -end - -require 'puppet' -require 'puppet/agent' -require 'puppet/daemon' -require 'puppet/configurer' -require 'getoptlong' - -# Create this now, so it has access to ARGV -daemon = Puppet::Daemon.new -daemon.argv = ARGV.dup - -options = [ - [ "--centrallogging", GetoptLong::NO_ARGUMENT ], - [ "--disable", GetoptLong::NO_ARGUMENT ], - [ "--debug", "-d", GetoptLong::NO_ARGUMENT ], - [ "--enable", GetoptLong::NO_ARGUMENT ], - [ "--fqdn", "-f", GetoptLong::REQUIRED_ARGUMENT ], - [ "--help", "-h", GetoptLong::NO_ARGUMENT ], - [ "--logdest", "-l", GetoptLong::REQUIRED_ARGUMENT ], - [ "--onetime", "-o", GetoptLong::NO_ARGUMENT ], - [ "--test", "-t", GetoptLong::NO_ARGUMENT ], - [ "--serve", "-s", GetoptLong::REQUIRED_ARGUMENT ], - [ "--no-client", GetoptLong::NO_ARGUMENT ], - [ "--verbose", "-v", GetoptLong::NO_ARGUMENT ], - [ "--version", "-V", GetoptLong::NO_ARGUMENT ], - [ "--waitforcert", "-w", GetoptLong::REQUIRED_ARGUMENT ] -] - -# Add all of the config parameters as valid options. -Puppet.settings.addargs(options) - -result = GetoptLong.new(*options) - -args = {} - -options = { - :waitforcert => 120, # Default to checking for certs every 5 minutes - :onetime => false, - :verbose => false, - :debug => false, - :centrallogs => false, - :setdest => false, - :enable => false, - :disable => false, - :client => true, - :fqdn => nil, - :serve => {} -} - -begin - explicit_waitforcert = false - result.each { |opt,arg| - case opt - # First check to see if the argument is a valid configuration parameter; - # if so, set it. NOTE: there is a catch-all at the bottom for defaults.rb - when "--disable" - options[:disable] = true - when "--serve" - if Puppet::Network::Handler.handler(arg) - options[:serve] << arg.to_sym - else - raise "Could not find handler for %s" % arg - end - when "--enable" - options[:enable] = true - when "--test" - options[:test] = true - when "--centrallogging" - options[:centrallogs] = true - when "--help" - if Puppet.features.usage? - RDoc::usage && exit - else - puts "No help available unless you have RDoc::usage installed" - exit - end - when "--version" - puts "%s" % Puppet.version - exit - when "--verbose" - options[:verbose] = true - when "--debug" - options[:debug] = true - when "--fqdn" - options[:fqdn] = arg - when "--no-client" - options[:client] = false - when "--onetime" - options[:onetime] = true - options[:waitforcert] = 0 unless explicit_waitforcert - when "--port" - args[:Port] = arg - when "--logdest" - begin - Puppet::Util::Log.newdestination(arg) - options[:setdest] = true - rescue => detail - if Puppet[:debug] - puts detail.backtrace - end - $stderr.puts detail.to_s - end - when "--waitforcert" - options[:waitforcert] = arg.to_i - explicit_waitforcert = true - else - Puppet.settings.handlearg(opt, arg) - end - } -rescue GetoptLong::InvalidOption => detail - $stderr.puts detail - $stderr.puts "Try '#{$0} --help'" - exit(1) -end - -# Now parse the config -Puppet.parse_config - -if options[:test] - # Enable all of the most common test options. - Puppet.settings.handlearg("--ignorecache") - Puppet.settings.handlearg("--no-usecacheonfailure") - Puppet.settings.handlearg("--no-splay") - Puppet.settings.handlearg("--show_diff") - Puppet.settings.handlearg("--no-daemonize") - options[:verbose] = true - options[:onetime] = true - options[:waitforcert] = 0 -end - -# Handle the logging settings. -if options[:debug] or options[:verbose] - Puppet::Util::Log.newdestination(:console) - if options[:debug] - Puppet::Util::Log.level = :debug - else - Puppet::Util::Log.level = :info - end -end - -unless options[:setdest] - Puppet::Util::Log.newdestination(:syslog) -end - -if Puppet.settings.print_configs? - exit(Puppet.settings.print_configs ? 0 : 1) -end - -# If noop is set, then also enable diffs -if Puppet[:noop] - Puppet[:show_diff] = true -end - -args[:Server] = Puppet[:server] -if options[:fqdn] - args[:FQDN] = options[:fqdn] - Puppet[:certname] = options[:fqdn] -end - -if options[:centrallogs] - logdest = args[:Server] - - if args.include?(:Port) - logdest += ":" + args[:Port] - end - Puppet::Util::Log.newdestination(logdest) -end - -# We need to specify a ca location for things to work, but -# until the REST cert transfers are working, it needs to -# be local. -Puppet::SSL::Host.ca_location = :remote - -Puppet::Transaction::Report.terminus_class = :rest - -Puppet::Resource::Catalog.terminus_class = :rest -Puppet::Resource::Catalog.cache_class = :yaml - -Puppet::Node::Facts.terminus_class = :facter -Puppet::Node::Facts.cache_class = :rest - -# We need tomake the client either way, we just don't start it -# if --no-client is set. -agent = Puppet::Agent.new(Puppet::Configurer) -if options[:enable] - agent.enable -elsif options[:disable] - agent.disable -end - -if options[:enable] or options[:disable] - exit(0) -end - -daemon.agent = agent - -server = nil - -# It'd be nice to daemonize later, but we have to daemonize before the -# waitforcert happens. -if Puppet[:daemonize] - daemon.daemonize -end - -host = Puppet::SSL::Host.new -cert = host.wait_for_cert(options[:waitforcert]) - -objects = [] - -# This has to go after the certs are dealt with. -if Puppet[:listen] and ! options[:onetime] - unless FileTest.exists?(Puppet[:authconfig]) - Puppet.err "Will not start without authorization file %s" % - Puppet[:authconfig] - exit(14) - end - - # FIXME: we should really figure out how to distribute the CRL - # to clients. In the meantime, we just disable CRL checking if - # the CRL file doesn't exist - unless File::exist?(Puppet[:cacrl]) - Puppet[:cacrl] = 'false' - end - - handlers = nil - - if options[:serve].empty? - handlers = [:Runner] - else - handlers = options[:serve] - end - - require 'puppet/network/server' - # No REST handlers yet. - server = Puppet::Network::Server.new(:handlers => [:facts], :xmlrpc_handlers => handlers, :port => Puppet[:puppetport]) - - daemon.server = server -elsif options[:onetime] and Puppet[:listen] - Puppet.notice "Ignoring --listen on onetime run" -end - -# If --onetime is specified, we don't run 'start', which means we don't -# create a pidfile. -if options[:onetime] - unless options[:client] - $stderr.puts "onetime is specified but there is no client" - exit(43) - end - - daemon.set_signal_traps - - begin - agent.run - rescue => detail - if Puppet[:trace] - puts detail.backtrace - end - Puppet.err detail.to_s - end - exit(0) -else - Puppet.notice "Starting Puppet client version %s" % [Puppet.version] - - daemon.start -end +require 'puppet/application/puppetd' +Puppet::Application[:puppetd].run diff --git a/lib/puppet/application/puppetd.rb b/lib/puppet/application/puppetd.rb new file mode 100644 index 000000000..4c050c7a8 --- /dev/null +++ b/lib/puppet/application/puppetd.rb @@ -0,0 +1,260 @@ +require 'puppet' +require 'puppet/application' +require 'puppet/agent' +require 'puppet/daemon' +require 'puppet/configurer' +require 'puppet/network/client' + +Puppet::Application.new(:puppetd) do + + should_parse_config + + attr_accessor :explicit_waitforcert, :args, :agent, :daemon + + preinit do + # Do an initial trap, so that cancels don't get a stack trace. + trap(:INT) do + $stderr.puts "Cancelling startup" + exit(0) + end + + { + :waitforcert => 120, # Default to checking for certs every 5 minutes + :onetime => false, + :verbose => false, + :debug => false, + :centrallogs => false, + :setdest => false, + :enable => false, + :disable => false, + :client => true, + :fqdn => nil, + :serve => [] + }.each do |opt,val| + options[opt] = val + end + + @explicit_waitforcert = false + @args = {} + @daemon = Puppet::Daemon.new + @daemon.argv = ARGV.dup + end + + option("--centrallogging") + option("--disable") + option("--enable") + option("--debug","-d") + option("--fqdn FQDN","-f") + option("--test","-t") + option("--verbose","-v") + + option("--serve HANDLER", "-s") do |arg| + if Puppet::Network::Handler.handler(arg) + options[:serve] << arg.to_sym + else + raise "Could not find handler for %s" % arg + end + end + + option("--version", "-V") do |arg| + puts "%s" % Puppet.version + exit + end + + option("--no-client") do |arg| + options[:client] = false + end + + option("--onetime", "-o") do |arg| + options[:onetime] = true + options[:waitforcert] = 0 unless @explicit_waitforcert + end + + option("--logdest", "-l") do |arg| + begin + Puppet::Util::Log.newdestination(arg) + options[:setdest] = true + rescue => detail + if Puppet[:debug] + puts detail.backtrace + end + $stderr.puts detail.to_s + end + end + + option("--waitforcert WAITFORCERT", "-w") do |arg| + options[:waitforcert] = arg.to_i + @explicit_waitforcert = true + end + + option("--port PORT","-p") do |arg| + @args[:Port] = arg + end + + dispatch do + return :onetime if options[:onetime] + return :main + end + + command(:onetime) do + unless options[:client] + $stderr.puts "onetime is specified but there is no client" + exit(43) + end + + @daemon.set_signal_traps + + begin + @agent.run + rescue => detail + if Puppet[:trace] + puts detail.backtrace + end + Puppet.err detail.to_s + end + exit(0) + end + + command(:main) do + Puppet.notice "Starting Puppet client version %s" % [Puppet.version] + + @daemon.start + end + + # Enable all of the most common test options. + def setup_test + Puppet.settings.handlearg("--ignorecache") + Puppet.settings.handlearg("--no-usecacheonfailure") + Puppet.settings.handlearg("--no-splay") + Puppet.settings.handlearg("--show_diff") + Puppet.settings.handlearg("--no-daemonize") + options[:verbose] = true + options[:onetime] = true + options[:waitforcert] = 0 + end + + # Handle the logging settings. + def setup_logs + if options[:debug] or options[:verbose] + Puppet::Util::Log.newdestination(:console) + if options[:debug] + Puppet::Util::Log.level = :debug + else + Puppet::Util::Log.level = :info + end + end + + unless options[:setdest] + Puppet::Util::Log.newdestination(:syslog) + end + end + + def enable_disable_client(agent) + if options[:enable] + agent.enable + elsif options[:disable] + agent.disable + end + exit(0) + end + + def setup_listen + unless FileTest.exists?(Puppet[:authconfig]) + Puppet.err "Will not start without authorization file %s" % + Puppet[:authconfig] + exit(14) + end + + # FIXME: we should really figure out how to distribute the CRL + # to clients. In the meantime, we just disable CRL checking if + # the CRL file doesn't exist + unless File::exist?(Puppet[:cacrl]) + Puppet[:cacrl] = 'false' + end + + handlers = nil + + if options[:serve].empty? + handlers = [:Runner] + else + handlers = options[:serve] + end + + require 'puppet/network/server' + # No REST handlers yet. + server = Puppet::Network::Server.new(:handlers => [:facts], :xmlrpc_handlers => handlers, :port => Puppet[:puppetport]) + + @daemon.server = server + end + + setup do + setup_test if options[:test] + + setup_logs + + if Puppet.settings.print_configs? + exit(Puppet.settings.print_configs ? 0 : 1) + end + + # If noop is set, then also enable diffs + if Puppet[:noop] + Puppet[:show_diff] = true + end + + args[:Server] = Puppet[:server] + if options[:fqdn] + args[:FQDN] = options[:fqdn] + Puppet[:certname] = options[:fqdn] + end + + if options[:centrallogs] + logdest = args[:Server] + + if args.include?(:Port) + logdest += ":" + args[:Port] + end + Puppet::Util::Log.newdestination(logdest) + end + + # We need to specify a ca location for things to work, but + # until the REST cert transfers are working, it needs to + # be local. + Puppet::SSL::Host.ca_location = :remote + + Puppet::Transaction::Report.terminus_class = :rest + + Puppet::Resource::Catalog.terminus_class = :rest + Puppet::Resource::Catalog.cache_class = :yaml + + Puppet::Node::Facts.terminus_class = :facter + Puppet::Node::Facts.cache_class = :rest + + # We need tomake the client either way, we just don't start it + # if --no-client is set. + @agent = Puppet::Agent.new(Puppet::Configurer) + + enable_disable_client(@agent) if options[:enable] or options[:disable] + + @daemon.agent = agent + + # It'd be nice to daemonize later, but we have to daemonize before the + # waitforcert happens. + if Puppet[:daemonize] + @daemon.daemonize + end + + host = Puppet::SSL::Host.new + cert = host.wait_for_cert(options[:waitforcert]) + + @objects = [] + + # This has to go after the certs are dealt with. + if Puppet[:listen] + unless options[:onetime] + setup_listen + else + Puppet.notice "Ignoring --listen on onetime run" + end + end + end +end diff --git a/spec/unit/application/puppetd.rb b/spec/unit/application/puppetd.rb new file mode 100755 index 000000000..7bfd822ea --- /dev/null +++ b/spec/unit/application/puppetd.rb @@ -0,0 +1,487 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/application/puppetd' +require 'puppet/network/server' + +describe "puppetd" do + before :each do + @puppetd = Puppet::Application[:puppetd] + @puppetd.stubs(:puts) + @daemon = stub_everything 'daemon' + Puppet::Daemon.stubs(:new).returns(@daemon) + @agent = stub_everything 'agent' + Puppet::Agent.stubs(:new).returns(@agent) + @puppetd.run_preinit + end + + it "should ask Puppet::Application to parse Puppet configuration file" do + @puppetd.should_parse_config?.should be_true + end + + it "should declare a main command" do + @puppetd.should respond_to(:main) + end + + it "should declare a onetime command" do + @puppetd.should respond_to(:onetime) + end + + it "should declare a preinit block" do + @puppetd.should respond_to(:run_preinit) + end + + it "should declare a version option" do + @puppetd.should respond_to(:handle_version) + end + + describe "in preinit" do + before :each do + @pupetd.stubs(:trap) + end + + it "should catch INT" do + @puppetd.expects(:trap).with { |arg,block| arg == :INT } + + @puppetd.run_preinit + end + + it "should set waitforcert to 120" do + @puppetd.run_preinit + + @puppetd.options[:waitforcert].should == 120 + end + + it "should init client to true" do + @puppetd.run_preinit + + @puppetd.options[:client].should be_true + end + + it "should init fqdn to nil" do + @puppetd.run_preinit + + @puppetd.options[:fqdn].should be_nil + end + + it "should init serve to []" do + @puppetd.run_preinit + + @puppetd.options[:serve].should == [] + end + + end + + describe "when handling options" do + + [:centrallogging, :disable, :enable, :debug, :fqdn, :test, :verbose].each do |option| + it "should declare handle_#{option} method" do + @puppetd.should respond_to("handle_#{option}".to_sym) + end + + it "should store argument value when calling handle_#{option}" do + @puppetd.options.expects(:[]=).with(option, 'arg') + @puppetd.send("handle_#{option}".to_sym, 'arg') + end + end + + it "should set an existing handler on server" do + Puppet::Network::Handler.stubs(:handler).with("handler").returns(true) + + @puppetd.handle_serve("handler") + @puppetd.options[:serve].should == [ :handler ] + end + + it "should exit after printing the version" do + lambda { @puppetd.handle_version(nil) }.should raise_error(SystemExit) + end + + it "should set client to false with --no-client" do + @puppetd.handle_no_client(nil) + @puppetd.options[:client].should be_false + end + + it "should set onetime to ture with --onetime" do + @puppetd.handle_onetime(nil) + @puppetd.options[:onetime].should be_true + end + + it "should set waitforcert to 0 with --onetime and if --waitforcert wasn't given" do + @puppetd.explicit_waitforcert = false + @puppetd.handle_onetime(nil) + @puppetd.options[:waitforcert].should == 0 + end + + it "should not reset waitforcert with --onetime when --waitforcert is used" do + @puppetd.explicit_waitforcert = true + @puppetd.handle_onetime(nil) + @puppetd.options[:waitforcert].should_not == 0 + end + + it "should set the log destination with --logdest" do + @puppetd.options.stubs(:[]=).with { |opt,val| opt == :setdest } + Puppet::Log.expects(:newdestination).with("console") + + @puppetd.handle_logdest("console") + end + + it "should put the setdest options to true" do + @puppetd.options.expects(:[]=).with(:setdest,true) + + @puppetd.handle_logdest("console") + end + + it "should store the waitforcert options with --waitforcert" do + @puppetd.options.expects(:[]=).with(:waitforcert,42) + + @puppetd.handle_waitforcert("42") + end + + it "should mark explicit_waitforcert to true with --waitforcert" do + @puppetd.options.stubs(:[]=) + + @puppetd.handle_waitforcert("42") + @puppetd.explicit_waitforcert.should be_true + end + + it "should set args[:Port] with --port" do + @puppetd.handle_port("42") + @puppetd.args[:Port].should == "42" + end + + end + + describe "during setup" do + before :each do + @puppetd.options.stubs(:[]) + Puppet.stubs(:info) + FileTest.stubs(:exists?).returns(true) + Puppet.stubs(:[]) + Puppet.settings.stubs(:print_config?) + Puppet.settings.stubs(:print_config) + Puppet::SSL::Host.stubs(:ca_location=) + Puppet::Transaction::Report.stubs(:terminus_class=) + Puppet::Resource::Catalog.stubs(:terminus_class=) + Puppet::Resource::Catalog.stubs(:cache_class=) + Puppet::Node::Facts.stubs(:terminus_class=) + Puppet::Node::Facts.stubs(:cache_class=) + @host = stub_everything 'host' + Puppet::SSL::Host.stubs(:new).returns(@host) + Puppet.stubs(:settraps) + end + + describe "with --test" do + before :each do + Puppet.settings.stubs(:handlearg) + @puppetd.options.stubs(:[]=) + end + + it "should call setup_test" do + @puppetd.options.stubs(:[]).with(:test).returns(true) + @puppetd.expects(:setup_test) + @puppetd.run_setup + end + + it "should set options[:verbose] to true" do + @puppetd.options.expects(:[]=).with(:verbose,true) + @puppetd.setup_test + end + it "should set options[:onetime] to true" do + @puppetd.options.expects(:[]=).with(:onetime,true) + @puppetd.setup_test + end + it "should set waitforcert to 0" do + @puppetd.options.expects(:[]=).with(:waitforcert,0) + @puppetd.setup_test + end + end + + it "should call setup_logs" do + @puppetd.expects(:setup_logs) + @puppetd.run_setup + end + + describe "when setting up logs" do + before :each do + Puppet::Util::Log.stubs(:newdestination) + end + + it "should set log level to debug if --debug was passed" do + @puppetd.options.stubs(:[]).with(:debug).returns(true) + + Puppet::Util::Log.expects(:level=).with(:debug) + + @puppetd.setup_logs + end + + it "should set log level to info if --verbose was passed" do + @puppetd.options.stubs(:[]).with(:verbose).returns(true) + + Puppet::Util::Log.expects(:level=).with(:info) + + @puppetd.setup_logs + end + + [:verbose, :debug].each do |level| + it "should set console as the log destination with level #{level}" do + @puppetd.options.stubs(:[]).with(level).returns(true) + + Puppet::Util::Log.expects(:newdestination).with(:console) + + @puppetd.setup_logs + end + end + + it "should set syslog as the log destination if no --logdest" do + @puppetd.options.stubs(:[]).with(:setdest).returns(false) + + Puppet::Util::Log.expects(:newdestination).with(:syslog) + + @puppetd.setup_logs + end + + end + + it "should print puppet config if asked to in Puppet config" do + @puppetd.stubs(:exit) + Puppet.settings.stubs(:print_configs?).returns(true) + + Puppet.settings.expects(:print_configs) + + @puppetd.run_setup + end + + it "should exit after printing puppet config if asked to in Puppet config" do + Puppet.settings.stubs(:print_configs?).returns(true) + + lambda { @puppetd.run_setup }.should raise_error(SystemExit) + end + + it "should set a central log destination with --centrallogs" do + @puppetd.options.stubs(:[]).with(:centrallogs).returns(true) + Puppet.stubs(:[]).with(:server).returns("puppet.reductivelabs.com") + Puppet::Util::Log.stubs(:newdestination).with(:syslog) + + Puppet::Util::Log.expects(:newdestination).with("puppet.reductivelabs.com") + + @puppetd.run_setup + end + + it "should install a remote ca location" do + Puppet::SSL::Host.expects(:ca_location=).with(:remote) + + @puppetd.run_setup + end + + it "should tell the report handler to use REST" do + Puppet::Transaction::Report.expects(:terminus_class=).with(:rest) + + @puppetd.run_setup + end + + it "should tell the catalog handler to use REST" do + Puppet::Resource::Catalog.expects(:terminus_class=).with(:rest) + + @puppetd.run_setup + end + + it "should tell the catalog handler to use cache" do + Puppet::Resource::Catalog.expects(:cache_class=).with(:yaml) + + @puppetd.run_setup + end + + it "should tell the facts to use facter" do + Puppet::Node::Facts.expects(:terminus_class=).with(:facter) + + @puppetd.run_setup + end + + it "should tell the facts cache facts through REST" do + Puppet::Node::Facts.expects(:cache_class=).with(:rest) + + @puppetd.run_setup + end + + it "should create an agent" do + Puppet::Agent.stubs(:new).with(Puppet::Configurer) + + @puppetd.run_setup + end + + [:enable, :disable].each do |action| + it "should delegate to enable_disable_client if we #{action} the agent" do + @puppetd.options.stubs(:[]).with(action).returns(true) + @puppetd.expects(:enable_disable_client).with(@agent) + + @puppetd.run_setup + end + end + + describe "when enabling or disabling agent" do + [:enable, :disable].each do |action| + it "should call client.#{action}" do + @puppetd.stubs(:exit) + @puppetd.options.stubs(:[]).with(action).returns(true) + + @agent.expects(action) + + @puppetd.enable_disable_client(@agent) + end + end + + it "should finally exit" do + lambda { @puppetd.enable_disable_client(@agent) }.should raise_error(SystemExit) + end + end + + it "should inform the daemon about our agent" do + @daemon.expects(:agent=).with(@agent) + @puppetd.run_setup + end + + it "should daemonize if needed" do + Puppet.stubs(:[]).with(:daemonize).returns(true) + + @daemon.expects(:daemonize) + + @puppetd.run_setup + end + + it "should wait for a certificate" do + @puppetd.options.stubs(:[]).with(:waitforcert).returns(123) + @host.expects(:wait_for_cert).with(123) + + @puppetd.run_setup + end + + it "should setup listen if told to and not onetime" do + Puppet.stubs(:[]).with(:listen).returns(true) + @puppetd.options.stubs(:[]).with(:onetime).returns(false) + + @puppetd.expects(:setup_listen) + + @puppetd.run_setup + end + + describe "when setting up listen" do + before :each do + Puppet.stubs(:[]).with(:authconfig).returns('auth') + FileTest.stubs(:exists?).with('auth').returns(true) + File.stubs(:exist?).returns(true) + @puppetd.options.stubs(:[]).with(:serve).returns([]) + @puppetd.stubs(:exit) + @server = stub_everything 'server' + Puppet::Network::Server.stubs(:new).returns(@server) + end + + + it "should exit if no authorization file" do + Puppet.stubs(:err) + FileTest.stubs(:exists?).with('auth').returns(false) + + @puppetd.expects(:exit) + + @puppetd.setup_listen + end + + it "should create a server to listen on at least the Runner handler" do + Puppet::Network::Server.expects(:new).with { |args| args[:xmlrpc_handlers] == [:Runner] } + + @puppetd.setup_listen + end + + it "should create a server to listen for specific handlers" do + @puppetd.options.stubs(:[]).with(:serve).returns([:handler]) + Puppet::Network::Server.expects(:new).with { |args| args[:xmlrpc_handlers] == [:handler] } + + @puppetd.setup_listen + end + + it "should create a server with facts REST handler" do + Puppet::Network::Server.expects(:new).with { |args| args[:handlers] == [:facts] } + + @puppetd.setup_listen + end + + it "should use puppet default port" do + Puppet.stubs(:[]).with(:puppetport).returns(:port) + + Puppet::Network::Server.expects(:new).with { |args| args[:port] == :port } + + @puppetd.setup_listen + end + end + end + + + describe "when running" do + before :each do + @puppetd.agent = @agent + @puppetd.daemon = @daemon + end + + it "should dispatch to onetime if --onetime is used" do + @puppetd.options.stubs(:[]).with(:onetime).returns(true) + + @puppetd.get_command.should == :onetime + end + + it "should dispatch to main if --onetime is not used" do + @puppetd.options.stubs(:[]).with(:onetime).returns(false) + + @puppetd.get_command.should == :main + end + + describe "with --onetime" do + + before :each do + @puppetd.options.stubs(:[]).with(:client).returns(:client) + @puppetd.stubs(:exit).with(0) + Puppet.stubs(:newservice) + end + + it "should exit if no defined --client" do + $stderr.stubs(:puts) + @puppetd.options.stubs(:[]).with(:client).returns(nil) + + @puppetd.expects(:exit).with(43) + + @puppetd.onetime + end + + it "should setup traps" do + @daemon.expects(:set_signal_traps) + + @puppetd.onetime + end + + it "should let the agent run" do + @agent.expects(:run) + + @puppetd.onetime + end + + it "should finish by exiting with 0 error code" do + @puppetd.expects(:exit).with(0) + + @puppetd.onetime + end + + end + + describe "without --onetime" do + before :each do + Puppet.stubs(:notice) + @puppetd.options.stubs(:[]).with(:client) + end + + it "should start our daemon" do + @daemon.expects(:start) + + @puppetd.main + end + end + end +end |