summaryrefslogtreecommitdiffstats
path: root/spec/unit/application
diff options
context:
space:
mode:
authorNick Lewis <nick@puppetlabs.com>2011-06-06 15:59:01 -0700
committerNick Lewis <nick@puppetlabs.com>2011-06-06 15:59:01 -0700
commit3d09ca82e57d0c8836b77623d876cd5dc9a3a5e6 (patch)
tree8a55f2319be5042200b66e6c547f02ad52ac3c0f /spec/unit/application
parent8ebec1effc8038b1f59537450f3fe27249d543ee (diff)
parent1c70f0ce54022b55119b9e2d6d60cd1ae9bc019e (diff)
Merge branch 'ticket/2.6.x/2128' into 2.6.x
Diffstat (limited to 'spec/unit/application')
-rwxr-xr-xspec/unit/application/apply_spec.rb112
1 files changed, 62 insertions, 50 deletions
diff --git a/spec/unit/application/apply_spec.rb b/spec/unit/application/apply_spec.rb
index 67edd4ed7..cf9d44ca2 100755
--- a/spec/unit/application/apply_spec.rb
+++ b/spec/unit/application/apply_spec.rb
@@ -5,6 +5,7 @@ require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/application/apply'
require 'puppet/file_bucket/dipper'
require 'puppet/configurer'
+require 'fileutils'
describe Puppet::Application::Apply do
before :each do
@@ -182,38 +183,39 @@ describe Puppet::Application::Apply do
end
describe "the main command" do
+ include PuppetSpec::Files
+
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)
+ Puppet[:prerun_command] = ''
+ Puppet[:postrun_command] = ''
- @apply.options.stubs(:[])
+ Puppet::Node::Facts.terminus_class = :memory
+ Puppet::Node.terminus_class = :memory
- @facts = stub_everything 'facts'
- Puppet::Node::Facts.stubs(:find).returns(@facts)
+ @facts = Puppet::Node::Facts.new(Puppet[:node_name_value])
+ @facts.save
- @node = stub_everything 'node'
- Puppet::Node.stubs(:find).returns(@node)
+ @node = Puppet::Node.new(Puppet[:node_name_value])
+ @node.save
- @catalog = stub_everything 'catalog'
+ @catalog = Puppet::Resource::Catalog.new
@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)
+ @transaction = Puppet::Transaction.new(@catalog)
+ @catalog.stubs(:apply).returns(@transaction)
+
Puppet::Util::Storage.stubs(:load)
Puppet::Configurer.any_instance.stubs(:save_last_run_summary) # to prevent it from trying to write files
end
it "should set the code to run from --code" do
- @apply.options.stubs(:[]).with(:code).returns("code to run")
+ @apply.options[:code] = "code to run"
Puppet.expects(:[]=).with(:code,"code to run")
@apply.main
@@ -229,47 +231,58 @@ describe Puppet::Application::Apply do
end
it "should set the manifest if a file is passed on command line and the file exists" do
- File.stubs(:exist?).with('site.pp').returns true
- @apply.command_line.stubs(:args).returns(['site.pp'])
+ manifest = tmpfile('site.pp')
+ FileUtils.touch(manifest)
+ @apply.command_line.stubs(:args).returns([manifest])
- Puppet.expects(:[]=).with(:manifest,"site.pp")
+ Puppet.expects(:[]=).with(:manifest,manifest)
@apply.main
end
it "should raise an error if a file is passed on command line and the file does not exist" do
- File.stubs(:exist?).with('noexist.pp').returns false
- @apply.command_line.stubs(:args).returns(['noexist.pp'])
- lambda { @apply.main }.should raise_error(RuntimeError, 'Could not find file noexist.pp')
+ noexist = tmpfile('noexist.pp')
+ @apply.command_line.stubs(:args).returns([noexist])
+ lambda { @apply.main }.should raise_error(RuntimeError, "Could not find file #{noexist}")
end
it "should set the manifest to the first file and warn other files will be skipped" do
- File.stubs(:exist?).with('starwarsIV').returns true
- File.expects(:exist?).with('starwarsI').never
- @apply.command_line.stubs(:args).returns(['starwarsIV', 'starwarsI', 'starwarsII'])
+ manifest = tmpfile('starwarsIV')
+ FileUtils.touch(manifest)
+
+ @apply.command_line.stubs(:args).returns([manifest, 'starwarsI', 'starwarsII'])
- Puppet.expects(:[]=).with(:manifest,"starwarsIV")
+ Puppet.expects(:[]=).with(:manifest,manifest)
Puppet.expects(:warning).with('Only one file can be applied per run. Skipping starwarsI, starwarsII')
@apply.main
end
- it "should collect the node facts" do
- Puppet::Node::Facts.expects(:find).returns(@facts)
+ it "should set the facts name based on the node_name_fact" do
+ @facts = Puppet::Node::Facts.new(Puppet[:node_name_value], 'my_name_fact' => 'other_node_name')
+ @facts.save
+ Puppet::Node.new('other_node_name').save
+ Puppet[:node_name_fact] = 'my_name_fact'
@apply.main
+
+ @facts.name.should == 'other_node_name'
end
- it "should raise an error if we can't find the node" do
- Puppet::Node::Facts.expects(:find).returns(nil)
+ it "should set the node_name_value based on the node_name_fact" do
+ Puppet::Node::Facts.new(Puppet[:node_name_value], 'my_name_fact' => 'other_node_name').save
+ Puppet::Node.new('other_node_name').save
+ Puppet[:node_name_fact] = 'my_name_fact'
- lambda { @apply.main }.should raise_error
+ @apply.main
+
+ Puppet[:node_name_value].should == 'other_node_name'
end
- it "should look for the node" do
- Puppet::Node.expects(:find).returns(@node)
+ it "should raise an error if we can't find the facts" do
+ Puppet::Node::Facts.expects(:find).returns(nil)
- @apply.main
+ lambda { @apply.main }.should raise_error
end
it "should raise an error if we can't find the node" do
@@ -279,21 +292,20 @@ describe Puppet::Application::Apply do
end
it "should merge in our node the loaded facts" do
- @facts.stubs(:values).returns("values")
-
- @node.expects(:merge).with("values")
+ @facts.values = {'key' => 'value'}
@apply.main
+
+ @node.parameters['key'].should == 'value'
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")
+ @apply.options[:loadclasses] = true
+ classfile = tmpfile('classfile')
+ File.open(classfile, 'w') { |c| c.puts 'class' }
+ Puppet[:classfile] = classfile
- @node.expects(:classes=)
+ @node.expects(:classes=).with(['class'])
@apply.main
end
@@ -331,9 +343,12 @@ describe Puppet::Application::Apply do
end
describe "with detailed_exitcodes" do
+ before :each do
+ @apply.options[:detailed_exitcodes] = true
+ end
+
it "should exit with report's computed exit status" do
- Puppet.stubs(:[]).with(:noop).returns(false)
- @apply.options.stubs(:[]).with(:detailed_exitcodes).returns(true)
+ Puppet[:noop] = false
Puppet::Transaction::Report.any_instance.stubs(:exit_status).returns(666)
@apply.expects(:exit).with(666)
@@ -341,8 +356,7 @@ describe Puppet::Application::Apply do
end
it "should exit with report's computed exit status, even if --noop is set" do
- Puppet.stubs(:[]).with(:noop).returns(true)
- @apply.options.stubs(:[]).with(:detailed_exitcodes).returns(true)
+ Puppet[:noop] = true
Puppet::Transaction::Report.any_instance.stubs(:exit_status).returns(666)
@apply.expects(:exit).with(666)
@@ -350,8 +364,7 @@ describe Puppet::Application::Apply do
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)
+ Puppet[:noop] = false
report = stub 'report', :exit_status => 666
@transaction.stubs(:report).returns(report)
@apply.expects(:exit).with(0)
@@ -360,8 +373,7 @@ describe Puppet::Application::Apply do
end
it "should always exit with 0 if --noop" do
- Puppet.stubs(:[]).with(:noop).returns(true)
- @apply.options.stubs(:[]).with(:detailed_exitcodes).returns(true)
+ Puppet[:noop] = true
report = stub 'report', :exit_status => 666
@transaction.stubs(:report).returns(report)
@apply.expects(:exit).with(0)