From 1d8bd0d9308413b2f0772a65ee76d69d8fa5959b Mon Sep 17 00:00:00 2001 From: Jesse Wolfe Date: Tue, 13 Apr 2010 14:53:44 -0700 Subject: Fix #3552 single executable should display usage Added some tests to make the single executable command behavior explicit. Added logic to display the usage message if we're on a tty and no arguments are passed. Signed-off-by: Jesse Wolfe --- bin/main | 71 -------- lib/puppet/application/apply.rb | 183 +++++++++++++++++++ lib/puppet/application/main.rb | 183 ------------------- lib/puppet/util/command_line.rb | 10 +- spec/unit/application/apply.rb | 385 ++++++++++++++++++++++++++++++++++++++++ spec/unit/application/main.rb | 384 --------------------------------------- spec/unit/util/command_line.rb | 20 +-- 7 files changed, 582 insertions(+), 654 deletions(-) delete mode 100644 bin/main create mode 100644 lib/puppet/application/apply.rb delete mode 100644 lib/puppet/application/main.rb create mode 100755 spec/unit/application/apply.rb delete mode 100755 spec/unit/application/main.rb diff --git a/bin/main b/bin/main deleted file mode 100644 index 542bf4095..000000000 --- a/bin/main +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env ruby - -# -# = Synopsis -# -# Run a stand-alone +puppet+ manifest. -# -# = Usage -# -# puppet [-h|--help] [-V|--version] [-d|--debug] [-v|--verbose] [-e|--execute] -# [--detailed-exitcodes] [-l|--logdest ] -# -# = Description -# -# This is the standalone puppet execution tool; use it to execute -# individual manifests that you write. If you need to execute site-wide -# manifests, use +puppetd+ and +puppetmasterd+. -# -# = Options -# -# Note that any configuration parameter that's valid in the configuration file -# is also a valid long argument. For example, 'ssldir' is a valid configuration -# parameter, so you can specify '--ssldir ' as an argument. -# -# See the configuration file documentation at -# http://reductivelabs.com/trac/puppet/wiki/ConfigurationReference for -# the full list of acceptable parameters. A commented list of all -# configuration options can also be generated by running puppet with -# '--genconfig'. -# -# debug:: -# Enable full debugging. -# -# detailed-exitcodes:: -# Provide transaction information via exit codes. If this is enabled, an exit -# code of '2' means there were changes, and an exit code of '4' means that there -# were failures during the transaction. -# -# help:: -# Print this help message -# -# loadclasses:: -# Load any stored classes. +puppetd+ caches configured classes (usually at -# /etc/puppet/classes.txt), and setting this option causes all of those classes -# to be set in your +puppet+ manifest. -# -# logdest:: -# Where to send messages. Choose between syslog, the console, and a log file. -# Defaults to sending messages to the console. -# -# execute:: -# Execute a specific piece of Puppet code -# -# verbose:: -# Print extra information. -# -# = Example -# -# puppet -l /tmp/manifest.log manifest.pp -# -# = Author -# -# Luke Kanies -# -# = Copyright -# -# Copyright (c) 2005 Reductive Labs, LLC -# Licensed under the GNU Public License - -require 'puppet/application/main' -Puppet::Application[:main].run diff --git a/lib/puppet/application/apply.rb b/lib/puppet/application/apply.rb new file mode 100644 index 000000000..787ce375f --- /dev/null +++ b/lib/puppet/application/apply.rb @@ -0,0 +1,183 @@ +require 'puppet' +require 'puppet/application' +require 'puppet/configurer' +require 'puppet/network/handler' +require 'puppet/network/client' + +Puppet::Application.new(:apply) do + + should_parse_config + + option("--debug","-d") + option("--execute EXECUTE","-e") do |arg| + options[:code] = arg + end + option("--loadclasses","-L") + option("--verbose","-v") + option("--use-nodes") + option("--detailed-exitcodes") + + option("--apply catalog", "-a catalog") do |arg| + options[:catalog] = arg + end + + option("--logdest LOGDEST", "-l") do |arg| + begin + Puppet::Util::Log.newdestination(arg) + options[:logset] = true + rescue => detail + $stderr.puts detail.to_s + end + end + + dispatch do + if options[:catalog] + :apply + elsif Puppet[:parseonly] + :parseonly + else + :main + end + end + + command(:apply) do + require 'puppet/configurer' + + if options[:catalog] == "-" + text = $stdin.read + else + text = File.read(options[:catalog]) + end + + begin + catalog = Puppet::Resource::Catalog.convert_from(Puppet::Resource::Catalog.default_format,text) + unless catalog.is_a?(Puppet::Resource::Catalog) + catalog = Puppet::Resource::Catalog.pson_create(catalog) + end + rescue => detail + raise Puppet::Error, "Could not deserialize catalog from pson: %s" % detail + end + + catalog = catalog.to_ral + + configurer = Puppet::Configurer.new + configurer.run :catalog => catalog + end + + command(:parseonly) do + # Set our code or file to use. + if options[:code] or ARGV.length == 0 + Puppet[:code] = options[:code] || STDIN.read + else + Puppet[:manifest] = ARGV.shift + end + begin + Puppet::Resource::TypeCollection.new(Puppet[:environment]).perform_initial_import + rescue => detail + Puppet.err detail + exit 1 + end + exit 0 + end + + command(:main) do + # Set our code or file to use. + if options[:code] or ARGV.length == 0 + Puppet[:code] = options[:code] || STDIN.read + else + Puppet[:manifest] = ARGV.shift + end + + # Collect our facts. + unless facts = Puppet::Node::Facts.find(Puppet[:certname]) + raise "Could not find facts for %s" % Puppet[:certname] + end + + # Find our Node + unless node = Puppet::Node.find(Puppet[:certname]) + raise "Could not find node %s" % Puppet[:certname] + end + + # Merge in the facts. + node.merge(facts.values) + + # Allow users to load the classes that puppetd creates. + if options[:loadclasses] + file = Puppet[:classfile] + if FileTest.exists?(file) + unless FileTest.readable?(file) + $stderr.puts "%s is not readable" % file + exit(63) + end + node.classes = File.read(file).split(/[\s\n]+/) + end + end + + begin + # Compile our catalog + starttime = Time.now + catalog = Puppet::Resource::Catalog.find(node.name, :use_node => node) + + # Translate it to a RAL catalog + catalog = catalog.to_ral + + catalog.host_config = true if Puppet[:graph] or Puppet[:report] + + catalog.finalize + + catalog.retrieval_duration = Time.now - starttime + + configurer = Puppet::Configurer.new + configurer.execute_prerun_command + + # And apply it + transaction = catalog.apply + + configurer.execute_postrun_command + + status = 0 + if not Puppet[:noop] and options[:detailed_exitcodes] then + transaction.generate_report + exit(transaction.report.exit_status) + else + exit(0) + end + rescue => detail + puts detail.backtrace if Puppet[:trace] + if detail.is_a?(XMLRPC::FaultException) + $stderr.puts detail.message + else + $stderr.puts detail + end + exit(1) + end + end + + setup do + 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 + + unless options[:logset] + Puppet::Util::Log.newdestination(:console) + end + client = nil + server = nil + + trap(:INT) do + $stderr.puts "Exiting" + exit(1) + end + + if options[:debug] + Puppet::Util::Log.level = :debug + elsif options[:verbose] + Puppet::Util::Log.level = :info + end + end +end diff --git a/lib/puppet/application/main.rb b/lib/puppet/application/main.rb deleted file mode 100644 index 2952ef4ae..000000000 --- a/lib/puppet/application/main.rb +++ /dev/null @@ -1,183 +0,0 @@ -require 'puppet' -require 'puppet/application' -require 'puppet/configurer' -require 'puppet/network/handler' -require 'puppet/network/client' - -Puppet::Application.new(:main) do - - should_parse_config - - option("--debug","-d") - option("--execute EXECUTE","-e") do |arg| - options[:code] = arg - end - option("--loadclasses","-L") - option("--verbose","-v") - option("--use-nodes") - option("--detailed-exitcodes") - - option("--apply catalog", "-a catalog") do |arg| - options[:catalog] = arg - end - - option("--logdest LOGDEST", "-l") do |arg| - begin - Puppet::Util::Log.newdestination(arg) - options[:logset] = true - rescue => detail - $stderr.puts detail.to_s - end - end - - dispatch do - if options[:catalog] - :apply - elsif Puppet[:parseonly] - :parseonly - else - :main - end - end - - command(:apply) do - require 'puppet/configurer' - - if options[:catalog] == "-" - text = $stdin.read - else - text = File.read(options[:catalog]) - end - - begin - catalog = Puppet::Resource::Catalog.convert_from(Puppet::Resource::Catalog.default_format,text) - unless catalog.is_a?(Puppet::Resource::Catalog) - catalog = Puppet::Resource::Catalog.pson_create(catalog) - end - rescue => detail - raise Puppet::Error, "Could not deserialize catalog from pson: %s" % detail - end - - catalog = catalog.to_ral - - configurer = Puppet::Configurer.new - configurer.run :catalog => catalog - end - - command(:parseonly) do - # Set our code or file to use. - if options[:code] or ARGV.length == 0 - Puppet[:code] = options[:code] || STDIN.read - else - Puppet[:manifest] = ARGV.shift - end - begin - Puppet::Resource::TypeCollection.new(Puppet[:environment]).perform_initial_import - rescue => detail - Puppet.err detail - exit 1 - end - exit 0 - end - - command(:main) do - # Set our code or file to use. - if options[:code] or ARGV.length == 0 - Puppet[:code] = options[:code] || STDIN.read - else - Puppet[:manifest] = ARGV.shift - end - - # Collect our facts. - unless facts = Puppet::Node::Facts.find(Puppet[:certname]) - raise "Could not find facts for %s" % Puppet[:certname] - end - - # Find our Node - unless node = Puppet::Node.find(Puppet[:certname]) - raise "Could not find node %s" % Puppet[:certname] - end - - # Merge in the facts. - node.merge(facts.values) - - # Allow users to load the classes that puppetd creates. - if options[:loadclasses] - file = Puppet[:classfile] - if FileTest.exists?(file) - unless FileTest.readable?(file) - $stderr.puts "%s is not readable" % file - exit(63) - end - node.classes = File.read(file).split(/[\s\n]+/) - end - end - - begin - # Compile our catalog - starttime = Time.now - catalog = Puppet::Resource::Catalog.find(node.name, :use_node => node) - - # Translate it to a RAL catalog - catalog = catalog.to_ral - - catalog.host_config = true if Puppet[:graph] or Puppet[:report] - - catalog.finalize - - catalog.retrieval_duration = Time.now - starttime - - configurer = Puppet::Configurer.new - configurer.execute_prerun_command - - # And apply it - transaction = catalog.apply - - configurer.execute_postrun_command - - status = 0 - if not Puppet[:noop] and options[:detailed_exitcodes] then - transaction.generate_report - exit(transaction.report.exit_status) - else - exit(0) - end - rescue => detail - puts detail.backtrace if Puppet[:trace] - if detail.is_a?(XMLRPC::FaultException) - $stderr.puts detail.message - else - $stderr.puts detail - end - exit(1) - end - end - - setup do - 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 - - unless options[:logset] - Puppet::Util::Log.newdestination(:console) - end - client = nil - server = nil - - trap(:INT) do - $stderr.puts "Exiting" - exit(1) - end - - if options[:debug] - Puppet::Util::Log.level = :debug - elsif options[:verbose] - Puppet::Util::Log.level = :info - end - end -end diff --git a/lib/puppet/util/command_line.rb b/lib/puppet/util/command_line.rb index 59454364c..ba474df91 100644 --- a/lib/puppet/util/command_line.rb +++ b/lib/puppet/util/command_line.rb @@ -2,12 +2,10 @@ module Puppet module Util module CommandLine def self.shift_subcommand_from_argv( argv = ARGV, stdin = STDIN ) - if ! argv.first - "main" unless stdin.tty? # ttys get usage info - elsif argv.first =~ /^-|\.pp$|\.rb$/ - "main" - else - argv.shift + case argv.first + when nil; "apply" unless stdin.tty? # ttys get usage info + when /^-|\.pp$|\.rb$/; "apply" + else argv.shift end end end diff --git a/spec/unit/application/apply.rb b/spec/unit/application/apply.rb new file mode 100755 index 000000000..e9a1c72a0 --- /dev/null +++ b/spec/unit/application/apply.rb @@ -0,0 +1,385 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/application/apply' + +describe "Puppet" do + before :each do + @apply = Puppet::Application[:apply] + Puppet::Util::Log.stubs(:newdestination) + Puppet::Util::Log.stubs(:level=) + end + + [:debug,:loadclasses,:verbose,:use_nodes,:detailed_exitcodes].each do |option| + it "should declare handle_#{option} method" do + @apply.should respond_to("handle_#{option}".to_sym) + end + + it "should store argument value when calling handle_#{option}" do + @apply.options.expects(:[]=).with(option, 'arg') + @apply.send("handle_#{option}".to_sym, 'arg') + end + end + + it "should set the code to the provided code when :execute is used" do + @apply.options.expects(:[]=).with(:code, 'arg') + @apply.send("handle_execute".to_sym, 'arg') + end + + it "should ask Puppet::Application to parse Puppet configuration file" do + @apply.should_parse_config?.should be_true + end + + describe "when applying options" do + + it "should set the log destination with --logdest" do + Puppet::Log.expects(:newdestination).with("console") + + @apply.handle_logdest("console") + end + + it "should put the logset options to true" do + @apply.options.expects(:[]=).with(:logset,true) + + @apply.handle_logdest("console") + end + end + + describe "during setup" do + + before :each do + Puppet::Log.stubs(:newdestination) + Puppet.stubs(:trap) + Puppet::Log.stubs(:level=) + Puppet.stubs(:parse_config) + require 'lib/puppet/file_bucket/dipper' + Puppet::FileBucket::Dipper.stubs(:new) + STDIN.stubs(:read) + + @apply.options.stubs(:[]).with(any_parameters) + end + + it "should set show_diff on --noop" do + Puppet.stubs(:[]=) + Puppet.stubs(:[]).with(:config) + Puppet.stubs(:[]).with(:noop).returns(true) + + Puppet.expects(:[]=).with(:show_diff, true) + + @apply.run_setup + end + + it "should set console as the log destination if logdest option wasn't provided" do + Puppet::Log.expects(:newdestination).with(:console) + + @apply.run_setup + end + + it "should set INT trap" do + @apply.expects(:trap).with(:INT) + + @apply.run_setup + end + + it "should set log level to debug if --debug was passed" do + @apply.options.stubs(:[]).with(:debug).returns(true) + + Puppet::Log.expects(:level=).with(:debug) + + @apply.run_setup + end + + it "should set log level to info if --verbose was passed" do + @apply.options.stubs(:[]).with(:verbose).returns(true) + + Puppet::Log.expects(:level=).with(:info) + + @apply.run_setup + end + + it "should print puppet config if asked to in Puppet config" do + @apply.stubs(:exit) + Puppet.settings.stubs(:print_configs?).returns(true) + + Puppet.settings.expects(:print_configs) + + @apply.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 { @apply.run_setup }.should raise_error(SystemExit) + end + + end + + describe "when executing" do + + it "should dispatch to parseonly if parseonly is set" do + @apply.stubs(:options).returns({}) + Puppet.stubs(:[]).with(:parseonly).returns(true) + + @apply.get_command.should == :parseonly + end + + it "should dispatch to 'apply' if it was called with 'apply'" do + @apply.options[:catalog] = "foo" + + @apply.get_command.should == :apply + end + + it "should dispatch to main if parseonly is not set" do + @apply.stubs(:options).returns({}) + Puppet.stubs(:[]).with(:parseonly).returns(false) + + @apply.get_command.should == :main + end + + describe "the parseonly command" do + before :each do + Puppet.stubs(:[]).with(:environment) + Puppet.stubs(:[]).with(:manifest).returns("site.pp") + Puppet.stubs(:err) + @apply.stubs(:exit) + @apply.options.stubs(:[]).with(:code).returns "some code" + @collection = stub_everything + Puppet::Resource::TypeCollection.stubs(:new).returns(@collection) + end + + it "should use a Puppet Resource Type Collection to parse the file" do + @collection.expects(:perform_initial_import) + @apply.parseonly + end + + it "should exit with exit code 0 if no error" do + @apply.expects(:exit).with(0) + @apply.parseonly + end + + it "should exit with exit code 1 if error" do + @collection.stubs(:perform_initial_import).raises(Puppet::ParseError) + @apply.expects(:exit).with(1) + @apply.parseonly + end + end + + describe "the main command" do + before :each do + Puppet.stubs(:[]) + Puppet.settings.stubs(:use) + Puppet.stubs(:[]).with(:prerun_command).returns "" + Puppet.stubs(:[]).with(:postrun_command).returns "" + Puppet.stubs(:[]).with(:trace).returns(true) + + @apply.options.stubs(:[]) + + @facts = stub_everything 'facts' + Puppet::Node::Facts.stubs(:find).returns(@facts) + + @node = stub_everything 'node' + Puppet::Node.stubs(:find).returns(@node) + + @catalog = stub_everything 'catalog' + @catalog.stubs(:to_ral).returns(@catalog) + Puppet::Resource::Catalog.stubs(:find).returns(@catalog) + + STDIN.stubs(:read) + + @transaction = stub_everything 'transaction' + @catalog.stubs(:apply).returns(@transaction) + + @apply.stubs(:exit) + end + + it "should set the code to run from --code" do + @apply.options.stubs(:[]).with(:code).returns("code to run") + Puppet.expects(:[]=).with(:code,"code to run") + + @apply.main + end + + it "should set the code to run from STDIN if no arguments" do + ARGV.stubs(:length).returns(0) + STDIN.stubs(:read).returns("code to run") + + Puppet.expects(:[]=).with(:code,"code to run") + + @apply.main + end + + it "should set the manifest if some files are passed on command line" do + ARGV.stubs(:length).returns(1) + ARGV.stubs(:shift).returns("site.pp") + + Puppet.expects(:[]=).with(:manifest,"site.pp") + + @apply.main + end + + it "should collect the node facts" do + Puppet::Node::Facts.expects(:find).returns(@facts) + + @apply.main + end + + it "should raise an error if we can't find the node" do + Puppet::Node::Facts.expects(:find).returns(nil) + + lambda { @apply.main }.should raise_error + end + + it "should find the node" do + Puppet::Node.expects(:find).returns(@node) + + @apply.main + end + + it "should raise an error if we can't find the node" do + Puppet::Node.expects(:find).returns(nil) + + lambda { @apply.main }.should raise_error + end + + it "should merge in our node the loaded facts" do + @facts.stubs(:values).returns("values") + + @node.expects(:merge).with("values") + + @apply.main + end + + it "should load custom classes if loadclasses" do + @apply.options.stubs(:[]).with(:loadclasses).returns(true) + Puppet.stubs(:[]).with(:classfile).returns("/etc/puppet/classes.txt") + FileTest.stubs(:exists?).with("/etc/puppet/classes.txt").returns(true) + FileTest.stubs(:readable?).with("/etc/puppet/classes.txt").returns(true) + File.stubs(:read).with("/etc/puppet/classes.txt").returns("class") + + @node.expects(:classes=) + + @apply.main + end + + it "should compile the catalog" do + Puppet::Resource::Catalog.expects(:find).returns(@catalog) + + @apply.main + end + + it "should transform the catalog to ral" do + + @catalog.expects(:to_ral).returns(@catalog) + + @apply.main + end + + it "should finalize the catalog" do + @catalog.expects(:finalize) + + @apply.main + end + + it "should call the prerun and postrun commands on a Configurer instance" do + configurer = stub 'configurer' + + Puppet::Configurer.expects(:new).returns configurer + configurer.expects(:execute_prerun_command) + configurer.expects(:execute_postrun_command) + + @apply.main + end + + it "should apply the catalog" do + @catalog.expects(:apply) + + @apply.main + end + + describe "with detailed_exitcodes" do + it "should exit with report's computed exit status" do + Puppet.stubs(:[]).with(:noop).returns(false) + @apply.options.stubs(:[]).with(:detailed_exitcodes).returns(true) + report = stub 'report', :exit_status => 666 + @transaction.stubs(:report).returns(report) + @apply.expects(:exit).with(666) + + @apply.main + end + + it "should always exit with 0 if option is disabled" do + Puppet.stubs(:[]).with(:noop).returns(false) + @apply.options.stubs(:[]).with(:detailed_exitcodes).returns(false) + report = stub 'report', :exit_status => 666 + @transaction.stubs(:report).returns(report) + @apply.expects(:exit).with(0) + + @apply.main + end + + it "should always exit with 0 if --noop" do + Puppet.stubs(:[]).with(:noop).returns(true) + @apply.options.stubs(:[]).with(:detailed_exitcodes).returns(true) + report = stub 'report', :exit_status => 666 + @transaction.stubs(:report).returns(report) + @apply.expects(:exit).with(0) + + @apply.main + end + end + end + + describe "the 'apply' command" do + it "should read the catalog in from disk if a file name is provided" do + @apply.options[:catalog] = "/my/catalog.pson" + File.expects(:read).with("/my/catalog.pson").returns "something" + Puppet::Resource::Catalog.stubs(:convert_from).with(:pson,'something').returns Puppet::Resource::Catalog.new + @apply.apply + end + + it "should read the catalog in from stdin if '-' is provided" do + @apply.options[:catalog] = "-" + $stdin.expects(:read).returns "something" + Puppet::Resource::Catalog.stubs(:convert_from).with(:pson,'something').returns Puppet::Resource::Catalog.new + @apply.apply + end + + it "should deserialize the catalog from the default format" do + @apply.options[:catalog] = "/my/catalog.pson" + File.stubs(:read).with("/my/catalog.pson").returns "something" + Puppet::Resource::Catalog.stubs(:default_format).returns :rot13_piglatin + Puppet::Resource::Catalog.stubs(:convert_from).with(:rot13_piglatin,'something').returns Puppet::Resource::Catalog.new + @apply.apply + end + + it "should fail helpfully if deserializing fails" do + @apply.options[:catalog] = "/my/catalog.pson" + File.stubs(:read).with("/my/catalog.pson").returns "something syntacically invalid" + lambda { @apply.apply }.should raise_error(Puppet::Error) + end + + it "should convert plain data structures into a catalog if deserialization does not do so" do + @apply.options[:catalog] = "/my/catalog.pson" + File.stubs(:read).with("/my/catalog.pson").returns "something" + Puppet::Resource::Catalog.stubs(:convert_from).with(:pson,"something").returns({:foo => "bar"}) + Puppet::Resource::Catalog.expects(:pson_create).with({:foo => "bar"}).returns(Puppet::Resource::Catalog.new) + @apply.apply + end + + it "should convert the catalog to a RAL catalog and use a Configurer instance to apply it" do + @apply.options[:catalog] = "/my/catalog.pson" + File.stubs(:read).with("/my/catalog.pson").returns "something" + catalog = Puppet::Resource::Catalog.new + Puppet::Resource::Catalog.stubs(:convert_from).with(:pson,'something').returns catalog + catalog.expects(:to_ral).returns "mycatalog" + + configurer = stub 'configurer' + Puppet::Configurer.expects(:new).returns configurer + configurer.expects(:run).with(:catalog => "mycatalog") + + @apply.apply + end + end + end +end diff --git a/spec/unit/application/main.rb b/spec/unit/application/main.rb deleted file mode 100755 index ea8c43fe6..000000000 --- a/spec/unit/application/main.rb +++ /dev/null @@ -1,384 +0,0 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../spec_helper' - -require 'puppet/application/main' - -describe "Puppet" do - before :each do - @main = Puppet::Application[:main] - Puppet::Util::Log.stubs(:newdestination) - Puppet::Util::Log.stubs(:level=) - end - - [:debug,:loadclasses,:verbose,:use_nodes,:detailed_exitcodes].each do |option| - it "should declare handle_#{option} method" do - @main.should respond_to("handle_#{option}".to_sym) - end - - it "should store argument value when calling handle_#{option}" do - @main.options.expects(:[]=).with(option, 'arg') - @main.send("handle_#{option}".to_sym, 'arg') - end - end - - it "should set the code to the provided code when :execute is used" do - @main.options.expects(:[]=).with(:code, 'arg') - @main.send("handle_execute".to_sym, 'arg') - end - - it "should ask Puppet::Application to parse Puppet configuration file" do - @main.should_parse_config?.should be_true - end - - describe "when applying options" do - - it "should set the log destination with --logdest" do - Puppet::Log.expects(:newdestination).with("console") - - @main.handle_logdest("console") - end - - it "should put the logset options to true" do - @main.options.expects(:[]=).with(:logset,true) - - @main.handle_logdest("console") - end - end - - describe "during setup" do - - before :each do - Puppet::Log.stubs(:newdestination) - Puppet.stubs(:trap) - Puppet::Log.stubs(:level=) - Puppet.stubs(:parse_config) - Puppet::FileBucket::Dipper.stubs(:new) - STDIN.stubs(:read) - - @main.options.stubs(:[]).with(any_parameters) - end - - it "should set show_diff on --noop" do - Puppet.stubs(:[]=) - Puppet.stubs(:[]).with(:config) - Puppet.stubs(:[]).with(:noop).returns(true) - - Puppet.expects(:[]=).with(:show_diff, true) - - @main.run_setup - end - - it "should set console as the log destination if logdest option wasn't provided" do - Puppet::Log.expects(:newdestination).with(:console) - - @main.run_setup - end - - it "should set INT trap" do - @main.expects(:trap).with(:INT) - - @main.run_setup - end - - it "should set log level to debug if --debug was passed" do - @main.options.stubs(:[]).with(:debug).returns(true) - - Puppet::Log.expects(:level=).with(:debug) - - @main.run_setup - end - - it "should set log level to info if --verbose was passed" do - @main.options.stubs(:[]).with(:verbose).returns(true) - - Puppet::Log.expects(:level=).with(:info) - - @main.run_setup - end - - it "should print puppet config if asked to in Puppet config" do - @main.stubs(:exit) - Puppet.settings.stubs(:print_configs?).returns(true) - - Puppet.settings.expects(:print_configs) - - @main.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 { @main.run_setup }.should raise_error(SystemExit) - end - - end - - describe "when executing" do - - it "should dispatch to parseonly if parseonly is set" do - @main.stubs(:options).returns({}) - Puppet.stubs(:[]).with(:parseonly).returns(true) - - @main.get_command.should == :parseonly - end - - it "should dispatch to 'apply' if it was called with 'apply'" do - @main.options[:catalog] = "foo" - - @main.get_command.should == :apply - end - - it "should dispatch to main if parseonly is not set" do - @main.stubs(:options).returns({}) - Puppet.stubs(:[]).with(:parseonly).returns(false) - - @main.get_command.should == :main - end - - describe "the parseonly command" do - before :each do - Puppet.stubs(:[]).with(:environment) - Puppet.stubs(:[]).with(:manifest).returns("site.pp") - Puppet.stubs(:err) - @main.stubs(:exit) - @main.options.stubs(:[]).with(:code).returns "some code" - @collection = stub_everything - Puppet::Resource::TypeCollection.stubs(:new).returns(@collection) - end - - it "should use a Puppet Resource Type Collection to parse the file" do - @collection.expects(:perform_initial_import) - @main.parseonly - end - - it "should exit with exit code 0 if no error" do - @main.expects(:exit).with(0) - @main.parseonly - end - - it "should exit with exit code 1 if error" do - @collection.stubs(:perform_initial_import).raises(Puppet::ParseError) - @main.expects(:exit).with(1) - @main.parseonly - end - end - - describe "the main command" do - before :each do - Puppet.stubs(:[]) - Puppet.settings.stubs(:use) - Puppet.stubs(:[]).with(:prerun_command).returns "" - Puppet.stubs(:[]).with(:postrun_command).returns "" - Puppet.stubs(:[]).with(:trace).returns(true) - - @main.options.stubs(:[]) - - @facts = stub_everything 'facts' - Puppet::Node::Facts.stubs(:find).returns(@facts) - - @node = stub_everything 'node' - Puppet::Node.stubs(:find).returns(@node) - - @catalog = stub_everything 'catalog' - @catalog.stubs(:to_ral).returns(@catalog) - Puppet::Resource::Catalog.stubs(:find).returns(@catalog) - - STDIN.stubs(:read) - - @transaction = stub_everything 'transaction' - @catalog.stubs(:apply).returns(@transaction) - - @main.stubs(:exit) - end - - it "should set the code to run from --code" do - @main.options.stubs(:[]).with(:code).returns("code to run") - Puppet.expects(:[]=).with(:code,"code to run") - - @main.main - end - - it "should set the code to run from STDIN if no arguments" do - ARGV.stubs(:length).returns(0) - STDIN.stubs(:read).returns("code to run") - - Puppet.expects(:[]=).with(:code,"code to run") - - @main.main - end - - it "should set the manifest if some files are passed on command line" do - ARGV.stubs(:length).returns(1) - ARGV.stubs(:shift).returns("site.pp") - - Puppet.expects(:[]=).with(:manifest,"site.pp") - - @main.main - end - - it "should collect the node facts" do - Puppet::Node::Facts.expects(:find).returns(@facts) - - @main.main - end - - it "should raise an error if we can't find the node" do - Puppet::Node::Facts.expects(:find).returns(nil) - - lambda { @main.main }.should raise_error - end - - it "should find the node" do - Puppet::Node.expects(:find).returns(@node) - - @main.main - end - - it "should raise an error if we can't find the node" do - Puppet::Node.expects(:find).returns(nil) - - lambda { @main.main }.should raise_error - end - - it "should merge in our node the loaded facts" do - @facts.stubs(:values).returns("values") - - @node.expects(:merge).with("values") - - @main.main - end - - it "should load custom classes if loadclasses" do - @main.options.stubs(:[]).with(:loadclasses).returns(true) - Puppet.stubs(:[]).with(:classfile).returns("/etc/puppet/classes.txt") - FileTest.stubs(:exists?).with("/etc/puppet/classes.txt").returns(true) - FileTest.stubs(:readable?).with("/etc/puppet/classes.txt").returns(true) - File.stubs(:read).with("/etc/puppet/classes.txt").returns("class") - - @node.expects(:classes=) - - @main.main - end - - it "should compile the catalog" do - Puppet::Resource::Catalog.expects(:find).returns(@catalog) - - @main.main - end - - it "should transform the catalog to ral" do - - @catalog.expects(:to_ral).returns(@catalog) - - @main.main - end - - it "should finalize the catalog" do - @catalog.expects(:finalize) - - @main.main - end - - it "should call the prerun and postrun commands on a Configurer instance" do - configurer = stub 'configurer' - - Puppet::Configurer.expects(:new).returns configurer - configurer.expects(:execute_prerun_command) - configurer.expects(:execute_postrun_command) - - @main.main - end - - it "should apply the catalog" do - @catalog.expects(:apply) - - @main.main - end - - describe "with detailed_exitcodes" do - it "should exit with report's computed exit status" do - Puppet.stubs(:[]).with(:noop).returns(false) - @main.options.stubs(:[]).with(:detailed_exitcodes).returns(true) - report = stub 'report', :exit_status => 666 - @transaction.stubs(:report).returns(report) - @main.expects(:exit).with(666) - - @main.main - end - - it "should always exit with 0 if option is disabled" do - Puppet.stubs(:[]).with(:noop).returns(false) - @main.options.stubs(:[]).with(:detailed_exitcodes).returns(false) - report = stub 'report', :exit_status => 666 - @transaction.stubs(:report).returns(report) - @main.expects(:exit).with(0) - - @main.main - end - - it "should always exit with 0 if --noop" do - Puppet.stubs(:[]).with(:noop).returns(true) - @main.options.stubs(:[]).with(:detailed_exitcodes).returns(true) - report = stub 'report', :exit_status => 666 - @transaction.stubs(:report).returns(report) - @main.expects(:exit).with(0) - - @main.main - end - end - end - - describe "the 'apply' command" do - it "should read the catalog in from disk if a file name is provided" do - @main.options[:catalog] = "/my/catalog.pson" - File.expects(:read).with("/my/catalog.pson").returns "something" - Puppet::Resource::Catalog.stubs(:convert_from).with(:pson,'something').returns Puppet::Resource::Catalog.new - @main.apply - end - - it "should read the catalog in from stdin if '-' is provided" do - @main.options[:catalog] = "-" - $stdin.expects(:read).returns "something" - Puppet::Resource::Catalog.stubs(:convert_from).with(:pson,'something').returns Puppet::Resource::Catalog.new - @main.apply - end - - it "should deserialize the catalog from the default format" do - @main.options[:catalog] = "/my/catalog.pson" - File.stubs(:read).with("/my/catalog.pson").returns "something" - Puppet::Resource::Catalog.stubs(:default_format).returns :rot13_piglatin - Puppet::Resource::Catalog.stubs(:convert_from).with(:rot13_piglatin,'something').returns Puppet::Resource::Catalog.new - @main.apply - end - - it "should fail helpfully if deserializing fails" do - @main.options[:catalog] = "/my/catalog.pson" - File.stubs(:read).with("/my/catalog.pson").returns "something syntacically invalid" - lambda { @main.apply }.should raise_error(Puppet::Error) - end - - it "should convert plain data structures into a catalog if deserialization does not do so" do - @main.options[:catalog] = "/my/catalog.pson" - File.stubs(:read).with("/my/catalog.pson").returns "something" - Puppet::Resource::Catalog.stubs(:convert_from).with(:pson,"something").returns({:foo => "bar"}) - Puppet::Resource::Catalog.expects(:pson_create).with({:foo => "bar"}).returns(Puppet::Resource::Catalog.new) - @main.apply - end - - it "should convert the catalog to a RAL catalog and use a Configurer instance to apply it" do - @main.options[:catalog] = "/my/catalog.pson" - File.stubs(:read).with("/my/catalog.pson").returns "something" - catalog = Puppet::Resource::Catalog.new - Puppet::Resource::Catalog.stubs(:convert_from).with(:pson,'something').returns catalog - catalog.expects(:to_ral).returns "mycatalog" - - configurer = stub 'configurer' - Puppet::Configurer.expects(:new).returns configurer - configurer.expects(:run).with(:catalog => "mycatalog") - - @main.apply - end - end - end -end diff --git a/spec/unit/util/command_line.rb b/spec/unit/util/command_line.rb index d6bbcd1be..b8fd87643 100644 --- a/spec/unit/util/command_line.rb +++ b/spec/unit/util/command_line.rb @@ -19,35 +19,35 @@ describe Puppet::Util::CommandLine do args.should == %w( --help whatever.pp ) end - it "should use main if the first argument looks like a .pp file" do + it "should use 'apply' if the first argument looks like a .pp file" do args = %w( whatever.pp ) command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, @tty ) - command.should == "main" + command.should == "apply" args.should == %w( whatever.pp ) end - it "should use main if the first argument looks like a .rb file" do + it "should use 'apply' if the first argument looks like a .rb file" do args = %w( whatever.rb ) command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, @tty ) - command.should == "main" + command.should == "apply" args.should == %w( whatever.rb ) end - it "should use main if the first argument looks like a flag" do + it "should use 'apply' if the first argument looks like a flag" do args = %w( --debug ) command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, @tty ) - command.should == "main" + command.should == "apply" args.should == %w( --debug ) end - it "should use main if the first argument is -" do + it "should use 'apply' if the first argument is -" do args = %w( - ) command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, @tty ) - command.should == "main" + command.should == "apply" args.should == %w( - ) end @@ -59,11 +59,11 @@ describe Puppet::Util::CommandLine do args.should == [] end - it "should use main if there are no arguments on a pipe" do + it "should use 'apply' if there are no arguments on a pipe" do args = [] command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, @pipe ) - command.should == "main" + command.should == "apply" args.should == [] end -- cgit