summaryrefslogtreecommitdiffstats
path: root/spec/unit/application
diff options
context:
space:
mode:
authorNick Lewis <nick@puppetlabs.com>2011-06-02 15:47:21 -0700
committerNick Lewis <nick@puppetlabs.com>2011-06-02 16:29:56 -0700
commita00fd25547db6dec8fcd57a004fbec58324ce3dc (patch)
treee3a272176a91984087c998e0d52854537a99d0e8 /spec/unit/application
parent805b2878d0b23d76917f5210abe35489f6f84c74 (diff)
downloadpuppet-a00fd25547db6dec8fcd57a004fbec58324ce3dc.tar.gz
puppet-a00fd25547db6dec8fcd57a004fbec58324ce3dc.tar.xz
puppet-a00fd25547db6dec8fcd57a004fbec58324ce3dc.zip
maint: Refactor specs in preparation for making node name more flexible
These tests were stubbing when it was unnecessary, so replace much of it with actual objects and files. Paired-With: Jacob Helwig <jacob@puppetlabs.com>
Diffstat (limited to 'spec/unit/application')
-rwxr-xr-xspec/unit/application/apply_spec.rb95
1 files changed, 46 insertions, 49 deletions
diff --git a/spec/unit/application/apply_spec.rb b/spec/unit/application/apply_spec.rb
index 67edd4ed7..5bd902a03 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[:certname])
+ @facts.save
- @node = stub_everything 'node'
- Puppet::Node.stubs(:find).returns(@node)
+ @node = Puppet::Node.new(Puppet[:certname])
+ @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,26 +231,28 @@ 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)
- Puppet.expects(:[]=).with(:manifest,"starwarsIV")
+ @apply.command_line.stubs(:args).returns([manifest, 'starwarsI', 'starwarsII'])
+
+ Puppet.expects(:[]=).with(:manifest,manifest)
Puppet.expects(:warning).with('Only one file can be applied per run. Skipping starwarsI, starwarsII')
@apply.main
@@ -260,18 +264,12 @@ describe Puppet::Application::Apply do
@apply.main
end
- it "should raise an error if we can't find the node" do
+ it "should raise an error if we can't find the facts" do
Puppet::Node::Facts.expects(:find).returns(nil)
lambda { @apply.main }.should raise_error
end
- it "should look for 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)
@@ -279,21 +277,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 +328,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 +341,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 +349,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 +358,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)