summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorNick Lewis <nick@puppetlabs.com>2010-11-23 16:42:06 -0800
committerNick Lewis <nick@puppetlabs.com>2010-11-23 16:42:06 -0800
commit4b35402ba85d8842d757becec5c8a7bf4d6f6654 (patch)
tree5da174c4c1c106b99e8c464e45e89d7b5ba2b5a2 /spec
parenta7bd1630622cace01d0e4c974c76366e4b3b886c (diff)
parent2bc6727b6ac7348dbac98099f1fe0aeb3cd1295f (diff)
downloadpuppet-4b35402ba85d8842d757becec5c8a7bf4d6f6654.tar.gz
puppet-4b35402ba85d8842d757becec5c8a7bf4d6f6654.tar.xz
puppet-4b35402ba85d8842d757becec5c8a7bf4d6f6654.zip
Merge branch 'next'
Diffstat (limited to 'spec')
-rwxr-xr-xspec/integration/configurer_spec.rb43
-rwxr-xr-xspec/integration/defaults_spec.rb1
-rw-r--r--spec/integration/parser/functions_spec.rb21
-rw-r--r--spec/integration/parser/ruby_manifest_spec.rb2
-rwxr-xr-xspec/integration/ssl/certificate_authority_spec.rb4
-rwxr-xr-xspec/integration/util/file_locking_spec.rb42
-rw-r--r--spec/spec_helper.rb2
-rwxr-xr-xspec/unit/application/agent_spec.rb7
-rwxr-xr-xspec/unit/application/apply_spec.rb31
-rw-r--r--spec/unit/application/filebucket_spec.rb1
-rwxr-xr-xspec/unit/application/queue_spec.rb1
-rwxr-xr-xspec/unit/configurer_spec.rb79
-rwxr-xr-xspec/unit/daemon_spec.rb12
-rwxr-xr-xspec/unit/indirector/active_record_spec.rb1
-rwxr-xr-xspec/unit/indirector/catalog/compiler_spec.rb17
-rwxr-xr-xspec/unit/indirector/facts/active_record_spec.rb1
-rw-r--r--spec/unit/indirector/report/yaml_spec.rb38
-rw-r--r--spec/unit/network/handler/fileserver_spec.rb6
-rwxr-xr-xspec/unit/network/http/rack/xmlrpc_spec.rb1
-rwxr-xr-xspec/unit/network/http/rack_spec.rb1
-rwxr-xr-xspec/unit/network/http/webrick_spec.rb1
-rwxr-xr-xspec/unit/network/server_spec.rb13
-rwxr-xr-xspec/unit/parser/ast/collection_spec.rb4
-rwxr-xr-xspec/unit/parser/ast/leaf_spec.rb44
-rwxr-xr-xspec/unit/parser/ast/resource_spec.rb4
-rwxr-xr-xspec/unit/parser/collector_spec.rb1
-rwxr-xr-xspec/unit/parser/parser_spec.rb53
-rwxr-xr-xspec/unit/parser/templatewrapper_spec.rb1
-rwxr-xr-xspec/unit/provider/confine/exists_spec.rb19
-rw-r--r--spec/unit/provider/host/parsed_spec.rb196
-rwxr-xr-xspec/unit/provider/mount_spec.rb9
-rw-r--r--spec/unit/provider/package/yum_spec.rb67
-rw-r--r--spec/unit/provider/service/freebsd_spec.rb50
-rw-r--r--spec/unit/provider/service/upstart.rb3
-rwxr-xr-xspec/unit/provider/ssh_authorized_key/parsed_spec.rb3
-rwxr-xr-xspec/unit/rails/param_value_spec.rb1
-rwxr-xr-xspec/unit/rails/resource_spec.rb1
-rwxr-xr-xspec/unit/rails_spec.rb104
-rwxr-xr-xspec/unit/resource/catalog_spec.rb6
-rwxr-xr-xspec/unit/simple_graph_spec.rb248
-rwxr-xr-xspec/unit/transaction/report_spec.rb15
-rwxr-xr-xspec/unit/type/host_spec.rb83
-rwxr-xr-xspec/unit/type/schedule_spec.rb70
-rw-r--r--spec/unit/util/command_line_spec.rb4
-rwxr-xr-xspec/unit/util/file_locking_spec.rb52
-rwxr-xr-xspec/unit/util/log_spec.rb4
-rw-r--r--spec/unit/util/monkey_patches_spec.rb26
-rwxr-xr-xspec/unit/util/pson_spec.rb15
-rwxr-xr-xspec/unit/util/rdoc_spec.rb10
-rwxr-xr-xspec/unit/util/storage_spec.rb5
-rwxr-xr-xspec/unit/util/zaml_spec.rb1
51 files changed, 1173 insertions, 251 deletions
diff --git a/spec/integration/configurer_spec.rb b/spec/integration/configurer_spec.rb
index 9a8b66fe4..cb7d3d779 100755
--- a/spec/integration/configurer_spec.rb
+++ b/spec/integration/configurer_spec.rb
@@ -5,6 +5,8 @@ require File.dirname(__FILE__) + '/../spec_helper'
require 'puppet/configurer'
describe Puppet::Configurer do
+ include PuppetSpec::Files
+
describe "when downloading plugins" do
it "should use the :pluginsignore setting, split on whitespace, for ignoring remote files" do
resource = Puppet::Type.type(:notify).new :name => "yay"
@@ -17,19 +19,50 @@ describe Puppet::Configurer do
end
describe "when running" do
- it "should send a transaction report with valid data" do
- catalog = Puppet::Resource::Catalog.new
- catalog.add_resource(Puppet::Type.type(:notify).new(:title => "testing"))
+ before(:each) do
+ @catalog = Puppet::Resource::Catalog.new
+ @catalog.add_resource(Puppet::Type.type(:notify).new(:title => "testing"))
- configurer = Puppet::Configurer.new
+ # Make sure we don't try to persist the local state after the transaction ran,
+ # because it will fail during test (the state file is in an not existing directory)
+ # and we need the transaction to be successful to be able to produce a summary report
+ @catalog.host_config = false
+
+ @configurer = Puppet::Configurer.new
+ end
+
+ it "should send a transaction report with valid data" do
+ @configurer.stubs(:save_last_run_summary)
Puppet::Transaction::Report.indirection.expects(:save).with do |x, report|
report.time.class == Time and report.logs.length > 0
end
Puppet[:report] = true
- configurer.run :catalog => catalog
+ @configurer.run :catalog => @catalog
+ end
+
+ it "should save a correct last run summary" do
+ report = Puppet::Transaction::Report.new
+ report.stubs(:save)
+
+ Puppet[:lastrunfile] = tmpfile("lastrunfile")
+ Puppet[:report] = true
+
+ @configurer.run :catalog => @catalog, :report => report
+
+ summary = nil
+ File.open(Puppet[:lastrunfile], "r") do |fd|
+ summary = YAML.load(fd.read)
+ end
+
+ summary.should be_a(Hash)
+ %w{time changes events resources}.each do |key|
+ summary.should be_key(key)
+ end
+ summary["time"].should be_key("notify")
+ summary["time"]["last_run"].should >= Time.now.tv_sec
end
end
end
diff --git a/spec/integration/defaults_spec.rb b/spec/integration/defaults_spec.rb
index 1f90c7cbc..2f30014e8 100755
--- a/spec/integration/defaults_spec.rb
+++ b/spec/integration/defaults_spec.rb
@@ -3,6 +3,7 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'puppet/defaults'
+require 'puppet/rails'
describe "Puppet defaults" do
include Puppet::Util::Execution
diff --git a/spec/integration/parser/functions_spec.rb b/spec/integration/parser/functions_spec.rb
new file mode 100644
index 000000000..cbfb4ac88
--- /dev/null
+++ b/spec/integration/parser/functions_spec.rb
@@ -0,0 +1,21 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe Puppet::Parser::Functions do
+ before :each do
+ Puppet::Parser::Functions.rmfunction("template") if Puppet::Parser::Functions.function("template")
+ end
+
+ it "should support multiple threads autoloading the same function" do
+ threads = []
+ lambda {
+ 10.times { |a|
+ threads << Thread.new {
+ Puppet::Parser::Functions.function("template")
+ }
+ }
+ }.should_not raise_error
+ threads.each { |t| t.join }
+ end
+end \ No newline at end of file
diff --git a/spec/integration/parser/ruby_manifest_spec.rb b/spec/integration/parser/ruby_manifest_spec.rb
index af110d3b3..de6f4628c 100644
--- a/spec/integration/parser/ruby_manifest_spec.rb
+++ b/spec/integration/parser/ruby_manifest_spec.rb
@@ -69,7 +69,7 @@ describe "Pure ruby manifests" do
write_file('foo.rb', "hostclass 'bar' do file 'test_file', :owner => 'root', :mode => '644' end")
catalog = compile("import 'foo'\ninclude bar")
file = catalog.resource("File[test_file]")
- file.should be_a Puppet::Resource
+ file.should be_a(Puppet::Resource)
file.type.should == 'File'
file.title.should == 'test_file'
file.exported.should_not be
diff --git a/spec/integration/ssl/certificate_authority_spec.rb b/spec/integration/ssl/certificate_authority_spec.rb
index fca17b405..67ff6f215 100755
--- a/spec/integration/ssl/certificate_authority_spec.rb
+++ b/spec/integration/ssl/certificate_authority_spec.rb
@@ -121,9 +121,7 @@ describe Puppet::SSL::CertificateAuthority do
it "should save valid certificates" do
@ca.sign("luke.madstop.com")
- ssl = %x{which openssl}
-
- unless ssl
+ unless ssl = Puppet::Util::which('openssl')
pending "No ssl available"
else
ca_cert = Puppet[:cacert]
diff --git a/spec/integration/util/file_locking_spec.rb b/spec/integration/util/file_locking_spec.rb
index 20c61d3d5..50613448b 100755
--- a/spec/integration/util/file_locking_spec.rb
+++ b/spec/integration/util/file_locking_spec.rb
@@ -5,28 +5,30 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f
require 'puppet/util/file_locking'
describe Puppet::Util::FileLocking do
- it "should be able to keep file corruption from happening when there are multiple writers" do
- file = Tempfile.new("puppetspec")
- filepath = file.path
- file.close!()
- file = filepath
- data = {:a => :b, :c => "A string", :d => "another string", :e => %w{an array of strings}}
- File.open(file, "w") { |f| f.puts YAML.dump(data) }
+ before :each do
+ @file = Tempfile.new("puppetspec")
+ filepath = @file.path
+ @file.close!()
+ @file = filepath
+ @data = {:a => :b, :c => "A string", :d => "another string", :e => %w{an array of strings}}
+ File.open(@file, "w") { |f| f.puts YAML.dump(@data) }
+ end
+ it "should be able to keep file corruption from happening when there are multiple writers threads" do
threads = []
sync = Sync.new
9.times { |a|
threads << Thread.new {
9.times { |b|
sync.synchronize(Sync::SH) {
- Puppet::Util::FileLocking.readlock(file) { |f|
- YAML.load(f.read).should == data
+ Puppet::Util::FileLocking.readlock(@file) { |f|
+ YAML.load(f.read).should == @data
}
}
sleep 0.01
sync.synchronize(Sync::EX) {
- Puppet::Util::FileLocking.writelock(file) { |f|
- f.puts YAML.dump(data)
+ Puppet::Util::FileLocking.writelock(@file) { |f|
+ f.puts YAML.dump(@data)
}
}
}
@@ -34,4 +36,22 @@ describe Puppet::Util::FileLocking do
}
threads.each { |th| th.join }
end
+
+ it "should be able to keep file corruption from happening when there are multiple writers processes" do
+ unless Process.fork
+ 50.times { |b|
+ Puppet::Util::FileLocking.writelock(@file) { |f|
+ f.puts YAML.dump(@data)
+ }
+ sleep 0.01
+ }
+ Kernel.exit!
+ end
+
+ 50.times { |c|
+ Puppet::Util::FileLocking.readlock(@file) { |f|
+ YAML.load(f.read).should == @data
+ }
+ }
+ end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index ed4e2c2fb..0c4b076f4 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -72,7 +72,7 @@ Spec::Runner.configure do |config|
Puppet.settings[:bindaddress] = "127.0.0.1"
@logs = []
- Puppet::Util::Log.newdestination(@logs)
+ Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(@logs))
end
end
diff --git a/spec/unit/application/agent_spec.rb b/spec/unit/application/agent_spec.rb
index 8fc98b8c1..50ef00c57 100755
--- a/spec/unit/application/agent_spec.rb
+++ b/spec/unit/application/agent_spec.rb
@@ -180,6 +180,7 @@ describe Puppet::Application::Agent do
Puppet[:libdir] = "/dev/null/lib"
Puppet::SSL::Host.stubs(:ca_location=)
Puppet::Transaction::Report.stubs(:terminus_class=)
+ Puppet::Transaction::Report.stubs(:cache_class=)
Puppet::Resource::Catalog.stubs(:terminus_class=)
Puppet::Resource::Catalog.stubs(:cache_class=)
Puppet::Node::Facts.stubs(:terminus_class=)
@@ -311,6 +312,12 @@ describe Puppet::Application::Agent do
@puppetd.setup
end
+ it "should tell the report handler to cache locally as yaml" do
+ Puppet::Transaction::Report.expects(:cache_class=).with(:yaml)
+
+ @puppetd.setup
+ end
+
it "should change the catalog_terminus setting to 'rest'" do
Puppet[:catalog_terminus] = :foo
@puppetd.setup
diff --git a/spec/unit/application/apply_spec.rb b/spec/unit/application/apply_spec.rb
index edb41b5c3..f07416378 100755
--- a/spec/unit/application/apply_spec.rb
+++ b/spec/unit/application/apply_spec.rb
@@ -4,6 +4,7 @@ require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/application/apply'
require 'puppet/file_bucket/dipper'
+require 'puppet/configurer'
describe Puppet::Application::Apply do
before :each do
@@ -56,6 +57,7 @@ describe Puppet::Application::Apply do
Puppet.stubs(:parse_config)
Puppet::FileBucket::Dipper.stubs(:new)
STDIN.stubs(:read)
+ Puppet::Transaction::Report.stubs(:cache_class=)
@apply.options.stubs(:[]).with(any_parameters)
end
@@ -113,6 +115,11 @@ describe Puppet::Application::Apply do
lambda { @apply.setup }.should raise_error(SystemExit)
end
+ it "should tell the report handler to cache locally as yaml" do
+ Puppet::Transaction::Report.expects(:cache_class=).with(:yaml)
+
+ @apply.setup
+ end
end
describe "when executing" do
@@ -193,6 +200,9 @@ describe Puppet::Application::Apply do
@catalog.stubs(:apply).returns(@transaction)
@apply.stubs(:exit)
+
+ 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
@@ -301,27 +311,32 @@ describe Puppet::Application::Apply do
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)
+ Puppet::Configurer.any_instance.expects(:execute_prerun_command)
+ Puppet::Configurer.any_instance.expects(:execute_postrun_command)
@apply.main
end
it "should apply the catalog" do
- @catalog.expects(:apply).returns(stub_everything 'transaction')
+ @catalog.expects(:apply).returns(stub_everything('transaction'))
@apply.main
end
+ it "should save the last run summary" do
+ Puppet.stubs(:[]).with(:noop).returns(false)
+ report = stub 'report'
+ Puppet::Configurer.any_instance.stubs(:initialize_report).returns(report)
+
+ Puppet::Configurer.any_instance.expects(:save_last_run_summary).with(report)
+ @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)
+ Puppet::Transaction::Report.any_instance.stubs(:exit_status).returns(666)
@apply.expects(:exit).with(666)
@apply.main
diff --git a/spec/unit/application/filebucket_spec.rb b/spec/unit/application/filebucket_spec.rb
index 6e7a7b819..e6272f179 100644
--- a/spec/unit/application/filebucket_spec.rb
+++ b/spec/unit/application/filebucket_spec.rb
@@ -3,6 +3,7 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/application/filebucket'
+require 'puppet/file_bucket/dipper'
describe Puppet::Application::Filebucket do
before :each do
diff --git a/spec/unit/application/queue_spec.rb b/spec/unit/application/queue_spec.rb
index 87c96dfff..bd0d53ab1 100755
--- a/spec/unit/application/queue_spec.rb
+++ b/spec/unit/application/queue_spec.rb
@@ -3,6 +3,7 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/application/queue'
+require 'puppet/indirector/catalog/queue'
describe Puppet::Application::Queue do
before :each do
diff --git a/spec/unit/configurer_spec.rb b/spec/unit/configurer_spec.rb
index 0c9d06362..e34e6fffb 100755
--- a/spec/unit/configurer_spec.rb
+++ b/spec/unit/configurer_spec.rb
@@ -89,9 +89,7 @@ describe Puppet::Configurer, "when executing a catalog run" do
@catalog = Puppet::Resource::Catalog.new
@catalog.stubs(:apply)
@agent.stubs(:retrieve_catalog).returns @catalog
-
- Puppet::Util::Log.stubs(:newdestination)
- Puppet::Util::Log.stubs(:close)
+ @agent.stubs(:save_last_run_summary)
end
it "should prepare for the run" do
@@ -101,14 +99,14 @@ describe Puppet::Configurer, "when executing a catalog run" do
end
it "should initialize a transaction report if one is not provided" do
- report = stub 'report'
+ report = Puppet::Transaction::Report.new
@agent.expects(:initialize_report).returns report
@agent.run
end
it "should pass the new report to the catalog" do
- report = stub 'report'
+ report = Puppet::Transaction::Report.new
@agent.stubs(:initialize_report).returns report
@catalog.expects(:apply).with{|options| options[:report] == report}
@@ -116,7 +114,7 @@ describe Puppet::Configurer, "when executing a catalog run" do
end
it "should use the provided report if it was passed one" do
- report = stub 'report'
+ report = Puppet::Transaction::Report.new
@agent.expects(:initialize_report).never
@catalog.expects(:apply).with{|options| options[:report] == report}
@@ -176,7 +174,7 @@ describe Puppet::Configurer, "when executing a catalog run" do
end
it "should send the report" do
- report = stub 'report'
+ report = Puppet::Transaction::Report.new
@agent.expects(:initialize_report).returns report
@agent.expects(:send_report).with { |r, trans| r == report }
@@ -184,7 +182,7 @@ describe Puppet::Configurer, "when executing a catalog run" do
end
it "should send the transaction report with a reference to the transaction if a run was actually made" do
- report = stub 'report'
+ report = Puppet::Transaction::Report.new
@agent.expects(:initialize_report).returns report
trans = stub 'transaction'
@@ -198,7 +196,7 @@ describe Puppet::Configurer, "when executing a catalog run" do
it "should send the transaction report even if the catalog could not be retrieved" do
@agent.expects(:retrieve_catalog).returns nil
- report = stub 'report'
+ report = Puppet::Transaction::Report.new
@agent.expects(:initialize_report).returns report
@agent.expects(:send_report)
@@ -208,7 +206,7 @@ describe Puppet::Configurer, "when executing a catalog run" do
it "should send the transaction report even if there is a failure" do
@agent.expects(:retrieve_catalog).raises "whatever"
- report = stub 'report'
+ report = Puppet::Transaction::Report.new
@agent.expects(:initialize_report).returns report
@agent.expects(:send_report)
@@ -216,16 +214,16 @@ describe Puppet::Configurer, "when executing a catalog run" do
end
it "should remove the report as a log destination when the run is finished" do
- report = stub 'report'
+ report = Puppet::Transaction::Report.new
@agent.expects(:initialize_report).returns report
-
- Puppet::Util::Log.expects(:close).with(report)
+ report.expects(:<<).at_least_once
@agent.run
+ Puppet::Util::Log.destinations.should_not include(report)
end
it "should return the report as the result of the run" do
- report = stub 'report'
+ report = Puppet::Transaction::Report.new
@agent.expects(:initialize_report).returns report
@agent.run.should equal(report)
@@ -236,6 +234,7 @@ describe Puppet::Configurer, "when sending a report" do
before do
Puppet.settings.stubs(:use).returns(true)
@configurer = Puppet::Configurer.new
+ @configurer.stubs(:save_last_run_summary)
@report = stub 'report'
@trans = stub 'transaction'
@@ -284,6 +283,20 @@ describe Puppet::Configurer, "when sending a report" do
@configurer.send_report(@report)
end
+ it "should save the last run summary if reporting is enabled" do
+ Puppet.settings[:report] = true
+
+ @configurer.expects(:save_last_run_summary).with(@report)
+ @configurer.send_report(@report)
+ end
+
+ it "should not save the last run summary if reporting is disabled" do
+ Puppet.settings[:report] = false
+
+ @configurer.expects(:save_last_run_summary).never
+ @configurer.send_report(@report)
+ end
+
it "should log but not fail if saving the report fails" do
Puppet.settings[:report] = true
@@ -294,6 +307,36 @@ describe Puppet::Configurer, "when sending a report" do
end
end
+describe Puppet::Configurer, "when saving the summary report file" do
+ before do
+ Puppet.settings.stubs(:use).returns(true)
+ @configurer = Puppet::Configurer.new
+
+ @report = stub 'report'
+ @trans = stub 'transaction'
+ @lastrunfd = stub 'lastrunfd'
+ Puppet::Util::FileLocking.stubs(:writelock).yields(@lastrunfd)
+ end
+
+ it "should write the raw summary to the lastrunfile setting value" do
+ Puppet::Util::FileLocking.expects(:writelock).with(Puppet[:lastrunfile], 0660)
+ @configurer.save_last_run_summary(@report)
+ end
+
+ it "should write the raw summary as yaml" do
+ @report.expects(:raw_summary).returns("summary")
+ @lastrunfd.expects(:print).with(YAML.dump("summary"))
+ @configurer.save_last_run_summary(@report)
+ end
+
+ it "should log but not fail if saving the last run summary fails" do
+ Puppet::Util::FileLocking.expects(:writelock).raises "exception"
+ Puppet.expects(:err)
+ lambda { @configurer.save_last_run_summary(@report) }.should_not raise_error
+ end
+
+end
+
describe Puppet::Configurer, "when retrieving a catalog" do
before do
Puppet.settings.stubs(:use).returns(true)
@@ -472,23 +515,23 @@ describe Puppet::Configurer, "when preparing for a run" do
it "should initialize the metadata store" do
@agent.class.stubs(:facts).returns(@facts)
@agent.expects(:dostorage)
- @agent.prepare
+ @agent.prepare({})
end
it "should download fact plugins" do
@agent.expects(:download_fact_plugins)
- @agent.prepare
+ @agent.prepare({})
end
it "should download plugins" do
@agent.expects(:download_plugins)
- @agent.prepare
+ @agent.prepare({})
end
it "should perform the pre-run commands" do
@agent.expects(:execute_prerun_command)
- @agent.prepare
+ @agent.prepare({})
end
end
diff --git a/spec/unit/daemon_spec.rb b/spec/unit/daemon_spec.rb
index 15320736c..e24db7881 100755
--- a/spec/unit/daemon_spec.rb
+++ b/spec/unit/daemon_spec.rb
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby"
+#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../spec_helper'
require 'puppet/daemon'
@@ -142,11 +142,7 @@ describe Puppet::Daemon do
describe "when creating its pidfile" do
it "should use an exclusive mutex" do
Puppet.settings.expects(:value).with(:name).returns "me"
-
- sync = mock 'sync'
- Puppet::Util.expects(:sync).with("me").returns sync
-
- sync.expects(:synchronize).with(Sync::EX)
+ Puppet::Util.expects(:synchronize_on).with("me",Sync::EX)
@daemon.create_pidfile
end
@@ -180,10 +176,8 @@ describe Puppet::Daemon do
it "should use an exclusive mutex" do
Puppet.settings.expects(:value).with(:name).returns "me"
- sync = mock 'sync'
- Puppet::Util.expects(:sync).with("me").returns sync
+ Puppet::Util.expects(:synchronize_on).with("me",Sync::EX)
- sync.expects(:synchronize).with(Sync::EX)
@daemon.remove_pidfile
end
diff --git a/spec/unit/indirector/active_record_spec.rb b/spec/unit/indirector/active_record_spec.rb
index 258c4e793..40af146c8 100755
--- a/spec/unit/indirector/active_record_spec.rb
+++ b/spec/unit/indirector/active_record_spec.rb
@@ -2,6 +2,7 @@
require File.dirname(__FILE__) + '/../../spec_helper'
+require 'puppet/rails'
require 'puppet/indirector/active_record'
describe Puppet::Indirector::ActiveRecord do
diff --git a/spec/unit/indirector/catalog/compiler_spec.rb b/spec/unit/indirector/catalog/compiler_spec.rb
index f9980807a..49b8986eb 100755
--- a/spec/unit/indirector/catalog/compiler_spec.rb
+++ b/spec/unit/indirector/catalog/compiler_spec.rb
@@ -6,6 +6,7 @@
require File.dirname(__FILE__) + '/../../../spec_helper'
require 'puppet/indirector/catalog/compiler'
+require 'puppet/rails'
describe Puppet::Resource::Catalog::Compiler do
before do
@@ -34,8 +35,8 @@ describe Puppet::Resource::Catalog::Compiler do
Puppet::Node.stubs(:find).with('node1').returns(node1)
Puppet::Node.stubs(:find).with('node2').returns(node2)
- compiler.find(stub('request', :node => 'node1', :options => {}))
- compiler.find(stub('node2request', :node => 'node2', :options => {}))
+ compiler.find(stub('request', :key => 'node1', :node => 'node1', :options => {}))
+ compiler.find(stub('node2request', :key => 'node2', :node => 'node2', :options => {}))
end
it "should provide a method for determining if the catalog is networked" do
@@ -71,7 +72,7 @@ describe Puppet::Resource::Catalog::Compiler do
@node = Puppet::Node.new @name
@node.stubs(:merge)
Puppet::Node.stubs(:find).returns @node
- @request = stub 'request', :key => "does not matter", :node => @name, :options => {}
+ @request = stub 'request', :key => @name, :node => @name, :options => {}
end
it "should directly use provided nodes" do
@@ -81,14 +82,14 @@ describe Puppet::Resource::Catalog::Compiler do
@compiler.find(@request)
end
- it "should use the request's node name if no explicit node is provided" do
+ it "should use the authenticated node name if no request key is provided" do
+ @request.stubs(:key).returns(nil)
Puppet::Node.expects(:find).with(@name).returns(@node)
@compiler.expects(:compile).with(@node)
@compiler.find(@request)
end
- it "should use the provided node name if no explicit node is provided and no authenticated node information is available" do
- @request.expects(:node).returns nil
+ it "should use the provided node name by default" do
@request.expects(:key).returns "my_node"
Puppet::Node.expects(:find).with("my_node").returns @node
@@ -205,7 +206,7 @@ describe Puppet::Resource::Catalog::Compiler do
@compiler = Puppet::Resource::Catalog::Compiler.new
@name = "me"
@node = mock 'node'
- @request = stub 'request', :node => @name, :options => {}
+ @request = stub 'request', :key => @name, :options => {}
@compiler.stubs(:compile)
end
@@ -224,7 +225,7 @@ describe Puppet::Resource::Catalog::Compiler do
@compiler = Puppet::Resource::Catalog::Compiler.new
@name = "me"
@node = mock 'node'
- @request = stub 'request', :node => @name, :options => {}
+ @request = stub 'request', :key => @name, :options => {}
@compiler.stubs(:compile)
Puppet::Node.stubs(:find).with(@name).returns(@node)
end
diff --git a/spec/unit/indirector/facts/active_record_spec.rb b/spec/unit/indirector/facts/active_record_spec.rb
index 0cdb70e01..0bdcfcb77 100755
--- a/spec/unit/indirector/facts/active_record_spec.rb
+++ b/spec/unit/indirector/facts/active_record_spec.rb
@@ -2,6 +2,7 @@
require File.dirname(__FILE__) + '/../../../spec_helper'
+require 'puppet/rails'
require 'puppet/node/facts'
describe "Puppet::Node::Facts::ActiveRecord" do
diff --git a/spec/unit/indirector/report/yaml_spec.rb b/spec/unit/indirector/report/yaml_spec.rb
new file mode 100644
index 000000000..610c9ae43
--- /dev/null
+++ b/spec/unit/indirector/report/yaml_spec.rb
@@ -0,0 +1,38 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+require 'puppet/transaction/report'
+require 'puppet/indirector/report/yaml'
+
+describe Puppet::Transaction::Report::Yaml do
+ it "should be a subclass of the Yaml terminus" do
+ Puppet::Transaction::Report::Yaml.superclass.should equal(Puppet::Indirector::Yaml)
+ end
+
+ it "should have documentation" do
+ Puppet::Transaction::Report::Yaml.doc.should_not be_nil
+ end
+
+ it "should be registered with the report indirection" do
+ indirection = Puppet::Indirector::Indirection.instance(:report)
+ Puppet::Transaction::Report::Yaml.indirection.should equal(indirection)
+ end
+
+ it "should have its name set to :yaml" do
+ Puppet::Transaction::Report::Yaml.name.should == :yaml
+ end
+
+ it "should inconditionnally save/load from the --lastrunreport setting" do
+ indirection = stub 'indirection', :name => :my_yaml, :register_terminus_type => nil
+ Puppet::Indirector::Indirection.stubs(:instance).with(:my_yaml).returns(indirection)
+ store_class = Class.new(Puppet::Transaction::Report::Yaml) do
+ def self.to_s
+ "MyYaml::MyType"
+ end
+ end
+ store = store_class.new
+
+ store.path(:me).should == Puppet[:lastrunreport]
+ end
+end
diff --git a/spec/unit/network/handler/fileserver_spec.rb b/spec/unit/network/handler/fileserver_spec.rb
index 40d1e57cd..b37d4f551 100644
--- a/spec/unit/network/handler/fileserver_spec.rb
+++ b/spec/unit/network/handler/fileserver_spec.rb
@@ -4,9 +4,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
require 'puppet/network/handler/fileserver'
-
describe Puppet::Network::Handler::FileServer do
- require 'tmpdir'
+ include PuppetSpec::Files
def create_file(filename)
File.open(filename, "w") { |f| f.puts filename}
@@ -20,8 +19,7 @@ describe Puppet::Network::Handler::FileServer do
end
before do
- @basedir = File.join(Dir.tmpdir, "test_network_handler")
- Dir.mkdir(@basedir)
+ @basedir = tmpdir("test_network_handler")
@file = File.join(@basedir, "aFile")
@link = File.join(@basedir, "aLink")
create_file(@file)
diff --git a/spec/unit/network/http/rack/xmlrpc_spec.rb b/spec/unit/network/http/rack/xmlrpc_spec.rb
index 63ba28bf2..870438f2c 100755
--- a/spec/unit/network/http/rack/xmlrpc_spec.rb
+++ b/spec/unit/network/http/rack/xmlrpc_spec.rb
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../../../spec_helper'
+require 'puppet/network/handler'
require 'puppet/network/http/rack' if Puppet.features.rack?
require 'puppet/network/http/rack/xmlrpc' if Puppet.features.rack?
diff --git a/spec/unit/network/http/rack_spec.rb b/spec/unit/network/http/rack_spec.rb
index df42a1ffa..8be9ccb50 100755
--- a/spec/unit/network/http/rack_spec.rb
+++ b/spec/unit/network/http/rack_spec.rb
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../../spec_helper'
+require 'puppet/network/handler'
require 'puppet/network/http/rack' if Puppet.features.rack?
describe "Puppet::Network::HTTP::Rack" do
diff --git a/spec/unit/network/http/webrick_spec.rb b/spec/unit/network/http/webrick_spec.rb
index 2a6ef2268..8e7c92b71 100755
--- a/spec/unit/network/http/webrick_spec.rb
+++ b/spec/unit/network/http/webrick_spec.rb
@@ -4,6 +4,7 @@
# Copyright (c) 2007. All rights reserved.
require File.dirname(__FILE__) + '/../../../spec_helper'
+require 'puppet/network/handler'
require 'puppet/network/http'
require 'puppet/network/http/webrick'
diff --git a/spec/unit/network/server_spec.rb b/spec/unit/network/server_spec.rb
index ccd9c082e..c2496dcca 100755
--- a/spec/unit/network/server_spec.rb
+++ b/spec/unit/network/server_spec.rb
@@ -5,6 +5,7 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/network/server'
+require 'puppet/network/handler'
describe Puppet::Network::Server do
before do
@@ -161,11 +162,7 @@ describe Puppet::Network::Server do
describe "when creating its pidfile" do
it "should use an exclusive mutex" do
Puppet.settings.expects(:value).with(:name).returns "me"
-
- sync = mock 'sync'
- Puppet::Util.expects(:sync).with("me").returns sync
-
- sync.expects(:synchronize).with(Sync::EX)
+ Puppet::Util.expects(:synchronize_on).with("me",Sync::EX)
@server.create_pidfile
end
@@ -198,11 +195,7 @@ describe Puppet::Network::Server do
describe "when removing its pidfile" do
it "should use an exclusive mutex" do
Puppet.settings.expects(:value).with(:name).returns "me"
-
- sync = mock 'sync'
- Puppet::Util.expects(:sync).with("me").returns sync
-
- sync.expects(:synchronize).with(Sync::EX)
+ Puppet::Util.expects(:synchronize_on).with("me",Sync::EX)
@server.remove_pidfile
end
diff --git a/spec/unit/parser/ast/collection_spec.rb b/spec/unit/parser/ast/collection_spec.rb
index 3f7878a99..392a2c0f0 100755
--- a/spec/unit/parser/ast/collection_spec.rb
+++ b/spec/unit/parser/ast/collection_spec.rb
@@ -5,6 +5,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
describe Puppet::Parser::AST::Collection do
before :each do
@scope = stub_everything 'scope'
+ @mytype = stub_everything('mytype')
+ @scope.stubs(:find_resource_type).returns @mytype
@compiler = stub_everything 'compile'
@scope.stubs(:compiler).returns(@compiler)
@@ -24,6 +26,8 @@ describe Puppet::Parser::AST::Collection do
it "should instantiate a Collector for this type" do
collection = Puppet::Parser::AST::Collection.new :form => :virtual, :type => "test"
+ @test_type = stub 'type', :name => 'test'
+ @scope.expects(:find_resource_type).with('test').returns @test_type
Puppet::Parser::Collector.expects(:new).with(@scope, "test", nil, nil, :virtual)
diff --git a/spec/unit/parser/ast/leaf_spec.rb b/spec/unit/parser/ast/leaf_spec.rb
index eb71f0d37..a19c24115 100755
--- a/spec/unit/parser/ast/leaf_spec.rb
+++ b/spec/unit/parser/ast/leaf_spec.rb
@@ -49,7 +49,7 @@ describe Puppet::Parser::AST::String do
end
it "should return a dup of its value" do
value = ""
- Puppet::Parser::AST::String.new( :value => value ).evaluate(stub 'scope').should_not be_equal(value)
+ Puppet::Parser::AST::String.new( :value => value ).evaluate(stub('scope')).should_not be_equal(value)
end
end
end
@@ -136,6 +136,22 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
access.evaluate(@scope).should == "val2"
end
+ it "should be able to return an array member when index is a stringified number" do
+ @scope.stubs(:lookupvar).with("a").returns(["val1", "val2", "val3"])
+
+ access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "1" )
+
+ access.evaluate(@scope).should == "val2"
+ end
+
+ it "should raise an error when accessing an array with a key" do
+ @scope.stubs(:lookupvar).with("a").returns(["val1", "val2", "val3"])
+
+ access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "get_me_the_second_element_please" )
+
+ lambda { access.evaluate(@scope) }.should raise_error
+ end
+
it "should be able to return an hash value" do
@scope.stubs(:lookupvar).with("a").returns({ "key1" => "val1", "key2" => "val2", "key3" => "val3" })
@@ -144,6 +160,14 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
access.evaluate(@scope).should == "val2"
end
+ it "should be able to return an hash value with a numerical key" do
+ @scope.stubs(:lookupvar).with("a").returns({ "key1" => "val1", "key2" => "val2", "45" => "45", "key3" => "val3" })
+
+ access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "45" )
+
+ access.evaluate(@scope).should == "45"
+ end
+
it "should raise an error if the variable lookup didn't return an hash or an array" do
@scope.stubs(:lookupvar).with("a").returns("I'm a string")
@@ -195,6 +219,24 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
scope.lookupvar("a").should be_include("b")
end
+ it "should raise an error when assigning an array element with a key" do
+ @scope.stubs(:lookupvar).with("a").returns([])
+
+ access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "get_me_the_second_element_please" )
+
+ lambda { access.assign(@scope, "test") }.should raise_error
+ end
+
+ it "should be able to return an array member when index is a stringified number" do
+ scope = Puppet::Parser::Scope.new
+ scope.setvar("a", [])
+
+ access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "0" )
+
+ access.assign(scope, "val2")
+ scope.lookupvar("a").should == ["val2"]
+ end
+
it "should raise an error when trying to overwrite an hash value" do
@scope.stubs(:lookupvar).with("a").returns({ "key" => [ "a" , "b" ]})
access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key")
diff --git a/spec/unit/parser/ast/resource_spec.rb b/spec/unit/parser/ast/resource_spec.rb
index 3ed7b3e08..3f7fa229a 100755
--- a/spec/unit/parser/ast/resource_spec.rb
+++ b/spec/unit/parser/ast/resource_spec.rb
@@ -164,7 +164,7 @@ describe Puppet::Parser::AST::Resource do
result = @resource.evaluate(@scope)
result.length.should == 1
result.first.ref.should == "Class[Classname]"
- @compiler.catalog.resource("Class[Classname]").should equal result.first
+ @compiler.catalog.resource("Class[Classname]").should equal(result.first)
end
it "should cause its parent to be evaluated" do
@@ -175,7 +175,7 @@ describe Puppet::Parser::AST::Resource do
result = @resource.evaluate(@scope)
result.length.should == 1
result.first.ref.should == "Class[Classname]"
- @compiler.catalog.resource("Class[Classname]").should equal result.first
+ @compiler.catalog.resource("Class[Classname]").should equal(result.first)
@compiler.catalog.resource("Class[Parentname]").should be_instance_of(Puppet::Parser::Resource)
end
diff --git a/spec/unit/parser/collector_spec.rb b/spec/unit/parser/collector_spec.rb
index 15808d6ff..908cda63a 100755
--- a/spec/unit/parser/collector_spec.rb
+++ b/spec/unit/parser/collector_spec.rb
@@ -2,6 +2,7 @@
require File.dirname(__FILE__) + '/../../spec_helper'
+require 'puppet/rails'
require 'puppet/parser/collector'
describe Puppet::Parser::Collector, "when initializing" do
diff --git a/spec/unit/parser/parser_spec.rb b/spec/unit/parser/parser_spec.rb
index b4e1518c4..ab43194e9 100755
--- a/spec/unit/parser/parser_spec.rb
+++ b/spec/unit/parser/parser_spec.rb
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe Puppet::Parser do
- ast = Puppet::Parser::AST
+ Puppet::Parser::AST
before :each do
@known_resource_types = Puppet::Resource::TypeCollection.new("development")
@@ -80,21 +80,21 @@ describe Puppet::Parser do
describe "when parsing 'if'" do
it "not, it should create the correct ast objects" do
- ast::Not.expects(:new).with { |h| h[:value].is_a?(ast::Boolean) }
+ Puppet::Parser::AST::Not.expects(:new).with { |h| h[:value].is_a?(Puppet::Parser::AST::Boolean) }
@parser.parse("if ! true { $var = 1 }")
end
it "boolean operation, it should create the correct ast objects" do
- ast::BooleanOperator.expects(:new).with {
- |h| h[:rval].is_a?(ast::Boolean) and h[:lval].is_a?(ast::Boolean) and h[:operator]=="or"
+ Puppet::Parser::AST::BooleanOperator.expects(:new).with {
+ |h| h[:rval].is_a?(Puppet::Parser::AST::Boolean) and h[:lval].is_a?(Puppet::Parser::AST::Boolean) and h[:operator]=="or"
}
@parser.parse("if true or true { $var = 1 }")
end
it "comparison operation, it should create the correct ast objects" do
- ast::ComparisonOperator.expects(:new).with {
- |h| h[:lval].is_a?(ast::Name) and h[:rval].is_a?(ast::Name) and h[:operator]=="<"
+ Puppet::Parser::AST::ComparisonOperator.expects(:new).with {
+ |h| h[:lval].is_a?(Puppet::Parser::AST::Name) and h[:rval].is_a?(Puppet::Parser::AST::Name) and h[:operator]=="<"
}
@parser.parse("if 1 < 2 { $var = 1 }")
@@ -105,13 +105,13 @@ describe Puppet::Parser do
describe "when parsing if complex expressions" do
it "should create a correct ast tree" do
aststub = stub_everything 'ast'
- ast::ComparisonOperator.expects(:new).with {
- |h| h[:rval].is_a?(ast::Name) and h[:lval].is_a?(ast::Name) and h[:operator]==">"
+ Puppet::Parser::AST::ComparisonOperator.expects(:new).with {
+ |h| h[:rval].is_a?(Puppet::Parser::AST::Name) and h[:lval].is_a?(Puppet::Parser::AST::Name) and h[:operator]==">"
}.returns(aststub)
- ast::ComparisonOperator.expects(:new).with {
- |h| h[:rval].is_a?(ast::Name) and h[:lval].is_a?(ast::Name) and h[:operator]=="=="
+ Puppet::Parser::AST::ComparisonOperator.expects(:new).with {
+ |h| h[:rval].is_a?(Puppet::Parser::AST::Name) and h[:lval].is_a?(Puppet::Parser::AST::Name) and h[:operator]=="=="
}.returns(aststub)
- ast::BooleanOperator.expects(:new).with {
+ Puppet::Parser::AST::BooleanOperator.expects(:new).with {
|h| h[:rval]==aststub and h[:lval]==aststub and h[:operator]=="and"
}
@parser.parse("if (1 > 2) and (1 == 2) { $var = 1 }")
@@ -134,8 +134,8 @@ describe Puppet::Parser do
end
it "should create an ast::ResourceReference" do
- ast::ResourceReference.expects(:new).with { |arg|
- arg[:line]==1 and arg[:type]=="File" and arg[:title].is_a?(ast::ASTArray)
+ Puppet::Parser::AST::ResourceReference.expects(:new).with { |arg|
+ arg[:line]==1 and arg[:type]=="File" and arg[:title].is_a?(Puppet::Parser::AST::ASTArray)
}
@parser.parse('exec { test: command => File["a","b"] }')
end
@@ -152,14 +152,14 @@ describe Puppet::Parser do
end
it "should create an ast::ResourceOverride" do
- #ast::ResourceOverride.expects(:new).with { |arg|
- # arg[:line]==1 and arg[:object].is_a?(ast::ResourceReference) and arg[:parameters].is_a?(ast::ResourceParam)
+ #Puppet::Parser::AST::ResourceOverride.expects(:new).with { |arg|
+ # arg[:line]==1 and arg[:object].is_a?(Puppet::Parser::AST::ResourceReference) and arg[:parameters].is_a?(Puppet::Parser::AST::ResourceParam)
#}
ro = @parser.parse('Resource["title1","title2"] { param => value }').code[0]
- ro.should be_a(ast::ResourceOverride)
+ ro.should be_a(Puppet::Parser::AST::ResourceOverride)
ro.line.should == 1
- ro.object.should be_a(ast::ResourceReference)
- ro.parameters[0].should be_a(ast::ResourceParam)
+ ro.object.should be_a(Puppet::Parser::AST::ResourceReference)
+ ro.parameters[0].should be_a(Puppet::Parser::AST::ResourceParam)
end
end
@@ -179,17 +179,17 @@ describe Puppet::Parser do
end
it "should create a nop node for empty branch" do
- ast::Nop.expects(:new)
+ Puppet::Parser::AST::Nop.expects(:new)
@parser.parse("if true { }")
end
it "should create a nop node for empty else branch" do
- ast::Nop.expects(:new)
+ Puppet::Parser::AST::Nop.expects(:new)
@parser.parse("if true { notice('test') } else { }")
end
it "should build a chain of 'ifs' if there's an 'elsif'" do
- ast = @parser.parse(<<-PP)
+ lambda { @parser.parse(<<-PP) }.should_not raise_error
if true { notice('test') } elsif true {} else { }
PP
end
@@ -274,16 +274,23 @@ describe Puppet::Parser do
it "should prefer provided options over AST context" do
@class.expects(:new).with { |opts| opts[:file] == "/bar" }
- @parser.expects(:ast_context).returns :file => "/foo"
+ @lexer.expects(:file).returns "/foo"
@parser.ast(@class, :file => "/bar")
end
it "should include docs when the AST class uses them" do
@class.expects(:use_docs).returns true
@class.stubs(:new)
- @parser.expects(:ast_context).with(true).returns({})
+ @parser.expects(:ast_context).with{ |a| a[0] == true }.returns({})
@parser.ast(@class, :file => "/bar")
end
+
+ it "should get docs from lexer using the correct AST line number" do
+ @class.expects(:use_docs).returns true
+ @class.stubs(:new).with{ |a| a[:doc] == "doc" }
+ @lexer.expects(:getcomment).with(12).returns "doc"
+ @parser.ast(@class, :file => "/bar", :line => 12)
+ end
end
describe "when retrieving a specific node" do
diff --git a/spec/unit/parser/templatewrapper_spec.rb b/spec/unit/parser/templatewrapper_spec.rb
index d4d1d1b8e..68d90a1cc 100755
--- a/spec/unit/parser/templatewrapper_spec.rb
+++ b/spec/unit/parser/templatewrapper_spec.rb
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
+require 'puppet/parser/templatewrapper'
describe Puppet::Parser::TemplateWrapper do
before(:each) do
diff --git a/spec/unit/provider/confine/exists_spec.rb b/spec/unit/provider/confine/exists_spec.rb
index c3958e317..f039208b8 100755
--- a/spec/unit/provider/confine/exists_spec.rb
+++ b/spec/unit/provider/confine/exists_spec.rb
@@ -39,25 +39,18 @@ describe Puppet::Provider::Confine::Exists do
describe "and the confine is for binaries" do
before { @confine.stubs(:for_binary).returns true }
- it "should use its 'binary' method to look up the full path of the file" do
- @confine.expects(:binary).returns nil
+ it "should use its 'which' method to look up the full path of the file" do
+ @confine.expects(:which).returns nil
@confine.pass?("/my/file")
end
- it "should return false if no binary can be found" do
- @confine.expects(:binary).with("/my/file").returns nil
+ it "should return false if no executable can be found" do
+ @confine.expects(:which).with("/my/file").returns nil
@confine.pass?("/my/file").should be_false
end
- it "should return true if the binary can be found and the file exists" do
- @confine.expects(:binary).with("/my/file").returns "/my/file"
- FileTest.expects(:exist?).with("/my/file").returns true
- @confine.pass?("/my/file").should be_true
- end
-
- it "should return false if the binary can be found but the file does not exist" do
- @confine.expects(:binary).with("/my/file").returns "/my/file"
- FileTest.expects(:exist?).with("/my/file").returns true
+ it "should return true if the executable can be found" do
+ @confine.expects(:which).with("/my/file").returns "/my/file"
@confine.pass?("/my/file").should be_true
end
end
diff --git a/spec/unit/provider/host/parsed_spec.rb b/spec/unit/provider/host/parsed_spec.rb
new file mode 100644
index 000000000..239e3bd86
--- /dev/null
+++ b/spec/unit/provider/host/parsed_spec.rb
@@ -0,0 +1,196 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+require 'puppet_spec/files'
+require 'puppettest/support/utils'
+require 'puppettest/fileparsing'
+
+provider_class = Puppet::Type.type(:host).provider(:parsed)
+
+describe provider_class do
+ include PuppetSpec::Files
+ extend PuppetTest::Support::Utils
+ include PuppetTest::FileParsing
+
+ before do
+ @host_class = Puppet::Type.type(:host)
+ @provider = @host_class.provider(:parsed)
+ @hostfile = tmpfile('hosts')
+ @provider.any_instance.stubs(:target).returns @hostfile
+ end
+
+ after :each do
+ @provider.initvars
+ end
+
+ def mkhost(args)
+ hostresource = Puppet::Type::Host.new(:name => args[:name])
+ hostresource.stubs(:should).with(:target).returns @hostfile
+
+ # Using setters of provider
+ host = @provider.new(hostresource)
+ args.each do |property,value|
+ host.send("#{property}=", value)
+ end
+ host
+ end
+
+ def genhost(host)
+ @provider.stubs(:filetype).returns(Puppet::Util::FileType::FileTypeRam)
+ File.stubs(:chown)
+ File.stubs(:chmod)
+ Puppet::Util::SUIDManager.stubs(:asuser).yields
+ host.flush
+ @provider.target_object(@hostfile).read
+ end
+
+ describe "when parsing a line with ip and hostname" do
+
+ it "should parse an ipv4 from the first field" do
+ @provider.parse_line("127.0.0.1 localhost")[:ip].should == "127.0.0.1"
+ end
+
+ it "should parse an ipv6 from the first field" do
+ @provider.parse_line("::1 localhost")[:ip].should == "::1"
+ end
+
+ it "should parse the name from the second field" do
+ @provider.parse_line("::1 localhost")[:name].should == "localhost"
+ end
+
+ it "should set an empty comment" do
+ @provider.parse_line("::1 localhost")[:comment].should == ""
+ end
+
+ end
+
+ describe "when parsing a line with ip, hostname and comment" do
+ before do
+ @testline = "127.0.0.1 localhost # A comment with a #-char"
+ end
+
+ it "should parse the ip from the first field" do
+ @provider.parse_line(@testline)[:ip].should == "127.0.0.1"
+ end
+
+ it "should parse the hostname from the second field" do
+ @provider.parse_line(@testline)[:name].should == "localhost"
+ end
+
+ it "should parse the comment after the first '#' character" do
+ @provider.parse_line(@testline)[:comment].should == 'A comment with a #-char'
+ end
+
+ end
+
+ describe "when parsing a line with ip, hostname and aliases" do
+
+ it "should parse alias from the third field" do
+ @provider.parse_line("127.0.0.1 localhost localhost.localdomain")[:host_aliases].should == ["localhost.localdomain"]
+ end
+
+ it "should parse multiple aliases" do
+ @provider.parse_line("127.0.0.1 host alias1 alias2")[:host_aliases].should == ['alias1', 'alias2']
+ @provider.parse_line("127.0.0.1 host alias1\talias2")[:host_aliases].should == ['alias1', 'alias2']
+ @provider.parse_line("127.0.0.1 host alias1\talias2 alias3")[:host_aliases].should == ['alias1', 'alias2', 'alias3']
+ end
+
+ end
+
+ describe "when parsing a line with ip, hostname, aliases and comment" do
+
+ before do
+ # Just playing with a few different delimiters
+ @testline = "127.0.0.1\t host alias1\talias2 alias3 # A comment with a #-char"
+ end
+
+ it "should parse the ip from the first field" do
+ @provider.parse_line(@testline)[:ip].should == "127.0.0.1"
+ end
+
+ it "should parse the hostname from the second field" do
+ @provider.parse_line(@testline)[:name].should == "host"
+ end
+
+ it "should parse all host_aliases from the third field" do
+ @provider.parse_line(@testline)[:host_aliases].should == ['alias1' ,'alias2', 'alias3']
+ end
+
+ it "should parse the comment after the first '#' character" do
+ @provider.parse_line(@testline)[:comment].should == 'A comment with a #-char'
+ end
+
+ end
+
+ describe "when operating on /etc/hosts like files" do
+ fakedata("data/providers/host/parsed","valid*").each do |file|
+ it "should be able to parse #{file}" do
+ fakedataparse(file)
+ end
+ end
+
+ it "should be able to generate a simple hostfile entry" do
+ host = mkhost(
+ :name => 'localhost',
+ :ip => '127.0.0.1',
+ :ensure => :present
+ )
+ genhost(host).should == "127.0.0.1\tlocalhost\n"
+ end
+
+ it "should be able to generate an entry with one alias" do
+ host = mkhost(
+ :name => 'localhost.localdomain',
+ :ip => '127.0.0.1',
+ :host_aliases => ['localhost'],
+ :ensure => :present
+ )
+ genhost(host).should == "127.0.0.1\tlocalhost.localdomain\tlocalhost\n"
+ end
+
+ it "should be able to generate an entry with more than one alias" do
+ host = mkhost(
+ :name => 'host',
+ :ip => '192.0.0.1',
+ :host_aliases => [ 'a1','a2','a3','a4' ],
+ :ensure => :present
+ )
+ genhost(host).should == "192.0.0.1\thost\ta1\ta2\ta3\ta4\n"
+ end
+
+ it "should be able to generate a simple hostfile entry with comments" do
+ host = mkhost(
+ :name => 'localhost',
+ :ip => '127.0.0.1',
+ :comment => 'Bazinga!',
+ :ensure => :present
+ )
+ genhost(host).should == "127.0.0.1\tlocalhost\t# Bazinga!\n"
+ end
+
+ it "should be able to generate an entry with one alias and a comment" do
+ host = mkhost(
+ :name => 'localhost.localdomain',
+ :ip => '127.0.0.1',
+ :host_aliases => ['localhost'],
+ :comment => 'Bazinga!',
+ :ensure => :present
+ )
+ genhost(host).should == "127.0.0.1\tlocalhost.localdomain\tlocalhost\t# Bazinga!\n"
+ end
+
+ it "should be able to generate an entry with more than one alias and a comment" do
+ host = mkhost(
+ :name => 'host',
+ :ip => '192.0.0.1',
+ :host_aliases => [ 'a1','a2','a3','a4' ],
+ :comment => 'Bazinga!',
+ :ensure => :present
+ )
+ genhost(host).should == "192.0.0.1\thost\ta1\ta2\ta3\ta4\t# Bazinga!\n"
+ end
+
+ end
+
+end
diff --git a/spec/unit/provider/mount_spec.rb b/spec/unit/provider/mount_spec.rb
index 55a52b44a..b034214ee 100755
--- a/spec/unit/provider/mount_spec.rb
+++ b/spec/unit/provider/mount_spec.rb
@@ -113,7 +113,14 @@ describe Puppet::Provider::Mount do
@mounter.should be_mounted
end
- it "should match ' on <name>' if the operating system is not Darwin or Solaris" do
+ it "should match '^<name> on' if the operating system is HP-UX" do
+ Facter.stubs(:value).with("operatingsystem").returns("HP-UX")
+ @mounter.expects(:mountcmd).returns("/ on /dev/dsk/whatever\n/var on /dev/dsk/other")
+
+ @mounter.should be_mounted
+ end
+
+ it "should match ' on <name>' if the operating system is not Darwin, Solaris, or HP-UX" do
Facter.stubs(:value).with("operatingsystem").returns("Debian")
@mounter.expects(:mountcmd).returns("/dev/dsk/whatever on / and stuff\n/dev/other/disk on /var and stuff")
diff --git a/spec/unit/provider/package/yum_spec.rb b/spec/unit/provider/package/yum_spec.rb
new file mode 100644
index 000000000..f6a99aa78
--- /dev/null
+++ b/spec/unit/provider/package/yum_spec.rb
@@ -0,0 +1,67 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+provider = Puppet::Type.type(:package).provider(:yum)
+
+describe provider do
+ before do
+ # Create a mock resource
+ @resource = stub 'resource'
+ @resource.stubs(:[]).with(:name).returns 'mypackage'
+ @provider = provider.new(@resource)
+ @provider.stubs(:resource).returns @resource
+ @provider.stubs(:yum).returns 'yum'
+ @provider.stubs(:rpm).returns 'rpm'
+ @provider.stubs(:get).with(:name).returns 'mypackage'
+ @provider.stubs(:get).with(:version).returns '1'
+ @provider.stubs(:get).with(:release).returns '1'
+ @provider.stubs(:get).with(:arch).returns 'i386'
+ end
+ # provider should repond to the following methods
+ [:install, :latest, :update, :purge].each do |method|
+ it "should have a(n) #{method}" do
+ @provider.should respond_to(method)
+ end
+ end
+
+ describe 'when installing' do
+ it 'should call yum install for :installed' do
+ @resource.stubs(:should).with(:ensure).returns :installed
+ @provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :install, 'mypackage')
+ @provider.install
+ end
+ it 'should use :install to update' do
+ @provider.expects(:install)
+ @provider.update
+ end
+ it 'should be able to set version' do
+ @resource.stubs(:should).with(:ensure).returns '1.2'
+ @provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :install, 'mypackage-1.2')
+ @provider.stubs(:query).returns :ensure => '1.2'
+ @provider.install
+ end
+ it 'should be able to downgrade' do
+ @resource.stubs(:should).with(:ensure).returns '1.0'
+ @provider.expects(:yum).with('-d', '0', '-e', '0', '-y', :downgrade, 'mypackage-1.0')
+ @provider.stubs(:query).returns(:ensure => '1.2').then.returns(:ensure => '1.0')
+ @provider.install
+ end
+ end
+
+ describe 'when uninstalling' do
+ it 'should use erase to purge' do
+ @provider.expects(:yum).with('-y', :erase, 'mypackage')
+ @provider.purge
+ end
+ it 'should use rpm to uninstall' do
+ @provider.expects(:rpm).with('-e', 'mypackage-1-1.i386')
+ @provider.uninstall
+ end
+ end
+
+ it 'should be versionable' do
+ provider.should be_versionable
+ end
+end
+
diff --git a/spec/unit/provider/service/freebsd_spec.rb b/spec/unit/provider/service/freebsd_spec.rb
new file mode 100644
index 000000000..0330adbed
--- /dev/null
+++ b/spec/unit/provider/service/freebsd_spec.rb
@@ -0,0 +1,50 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+provider_class = Puppet::Type.type(:service).provider(:freebsd)
+
+describe provider_class do
+ before :each do
+ @provider = provider_class.new
+ @provider.stubs(:initscript)
+ end
+
+ it "should correctly parse rcvar for FreeBSD < 7" do
+ @provider.stubs(:execute).returns <<OUTPUT
+# ntpd
+$ntpd_enable=YES
+OUTPUT
+ @provider.rcvar.should == ['# ntpd', 'ntpd_enable=YES']
+ end
+
+ it "should correctly parse rcvar for FreeBSD 7 to 8" do
+ @provider.stubs(:execute).returns <<OUTPUT
+# ntpd
+ntpd_enable=YES
+OUTPUT
+ @provider.rcvar.should == ['# ntpd', 'ntpd_enable=YES']
+ end
+
+ it "should correctly parse rcvar for FreeBSD >= 8.1" do
+ @provider.stubs(:execute).returns <<OUTPUT
+# ntpd
+#
+ntpd_enable="YES"
+# (default: "")
+OUTPUT
+ @provider.rcvar.should == ['# ntpd', 'ntpd_enable="YES"', '# (default: "")']
+ end
+
+ it "should find the right rcvar_value for FreeBSD < 7" do
+ @provider.stubs(:rcvar).returns(['# ntpd', 'ntpd_enable=YES'])
+
+ @provider.rcvar_value.should == "YES"
+ end
+
+ it "should find the right rcvar_value for FreeBSD >= 7" do
+ @provider.stubs(:rcvar).returns(['# ntpd', 'ntpd_enable="YES"', '# (default: "")'])
+
+ @provider.rcvar_value.should == "YES"
+ end
+end
diff --git a/spec/unit/provider/service/upstart.rb b/spec/unit/provider/service/upstart.rb
index 439fd2c79..9fde9e67f 100644
--- a/spec/unit/provider/service/upstart.rb
+++ b/spec/unit/provider/service/upstart.rb
@@ -24,8 +24,7 @@ describe provider_class do
resource = Puppet::Type.type(:service).new(:name => "foo", :provider => :upstart, :status => "/bin/foo")
provider = provider_class.new(resource)
- Process::Status.any_instance.stubs(:exitstatus).returns(0)
- provider.expects(:ucommand)
+ provider.expects(:ucommand).with { `true`; true }
provider.status.should == :running
end
diff --git a/spec/unit/provider/ssh_authorized_key/parsed_spec.rb b/spec/unit/provider/ssh_authorized_key/parsed_spec.rb
index 648527924..11e9233e0 100755
--- a/spec/unit/provider/ssh_authorized_key/parsed_spec.rb
+++ b/spec/unit/provider/ssh_authorized_key/parsed_spec.rb
@@ -5,7 +5,6 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
require 'puppet_spec/files'
require 'puppettest/support/utils'
require 'puppettest/fileparsing'
-require 'tmpdir'
require 'puppettest/fakes'
provider_class = Puppet::Type.type(:ssh_authorized_key).provider(:parsed)
@@ -19,7 +18,7 @@ describe provider_class do
before :each do
@sshauthkey_class = Puppet::Type.type(:ssh_authorized_key)
@provider = @sshauthkey_class.provider(:parsed)
- @keyfile = File.join(Dir.tmpdir, 'authorized_keys')
+ @keyfile = tmpfile('authorized_keys')
@provider.any_instance.stubs(:target).returns @keyfile
@user = 'random_bob'
Puppet::Util.stubs(:uid).with(@user).returns 12345
diff --git a/spec/unit/rails/param_value_spec.rb b/spec/unit/rails/param_value_spec.rb
index d6dc7d57b..243456e89 100755
--- a/spec/unit/rails/param_value_spec.rb
+++ b/spec/unit/rails/param_value_spec.rb
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
+require 'puppet/rails'
describe "Puppet::Rails::ParamValue" do
confine "Cannot test without ActiveRecord" => Puppet.features.rails?
diff --git a/spec/unit/rails/resource_spec.rb b/spec/unit/rails/resource_spec.rb
index 73c7f7af4..6e23d2020 100755
--- a/spec/unit/rails/resource_spec.rb
+++ b/spec/unit/rails/resource_spec.rb
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
+require 'puppet/rails'
describe "Puppet::Rails::Resource" do
confine "Cannot test without ActiveRecord" => Puppet.features.rails?
diff --git a/spec/unit/rails_spec.rb b/spec/unit/rails_spec.rb
index 01e822ff3..24248e622 100755
--- a/spec/unit/rails_spec.rb
+++ b/spec/unit/rails_spec.rb
@@ -103,9 +103,9 @@ describe Puppet::Rails, "when initializing a mysql connection" do
Puppet.settings.stubs(:value).with(:dbport).returns("")
Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbconnections).returns((pool_size = 45).to_s)
Puppet.settings.stubs(:value).with(:dbname).returns("testname")
Puppet.settings.stubs(:value).with(:dbsocket).returns("")
- Puppet.settings.stubs(:value).with(:dbconnections).returns(1)
Puppet::Rails.database_arguments.should == {
:adapter => "mysql",
@@ -113,9 +113,9 @@ describe Puppet::Rails, "when initializing a mysql connection" do
:host => "testserver",
:username => "testuser",
:password => "testpassword",
+ :pool => pool_size,
:database => "testname",
- :reconnect => true,
- :pool => 1
+ :reconnect => true
}
end
@@ -126,9 +126,9 @@ describe Puppet::Rails, "when initializing a mysql connection" do
Puppet.settings.stubs(:value).with(:dbport).returns("9999")
Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbconnections).returns((pool_size = 12).to_s)
Puppet.settings.stubs(:value).with(:dbname).returns("testname")
Puppet.settings.stubs(:value).with(:dbsocket).returns("testsocket")
- Puppet.settings.stubs(:value).with(:dbconnections).returns(1)
Puppet::Rails.database_arguments.should == {
:adapter => "mysql",
@@ -137,10 +137,10 @@ describe Puppet::Rails, "when initializing a mysql connection" do
:port => "9999",
:username => "testuser",
:password => "testpassword",
+ :pool => pool_size,
:database => "testname",
:socket => "testsocket",
- :reconnect => true,
- :pool => 1
+ :reconnect => true
}
end
@@ -151,9 +151,9 @@ describe Puppet::Rails, "when initializing a mysql connection" do
Puppet.settings.stubs(:value).with(:dbport).returns("9999")
Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbconnections).returns((pool_size = 23).to_s)
Puppet.settings.stubs(:value).with(:dbname).returns("testname")
Puppet.settings.stubs(:value).with(:dbsocket).returns("testsocket")
- Puppet.settings.stubs(:value).with(:dbconnections).returns(1)
Puppet::Rails.database_arguments.should == {
:adapter => "mysql",
@@ -162,12 +162,32 @@ describe Puppet::Rails, "when initializing a mysql connection" do
:port => "9999",
:username => "testuser",
:password => "testpassword",
+ :pool => pool_size,
:database => "testname",
:socket => "testsocket",
- :reconnect => true,
- :pool => 1
+ :reconnect => true
}
end
+
+ it "should not provide the pool if dbconnections is 0, '0', or ''" do
+ Puppet.settings.stubs(:value).with(:dbadapter).returns("mysql")
+ Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
+ Puppet.settings.stubs(:value).with(:dbserver).returns("testserver")
+ Puppet.settings.stubs(:value).with(:dbport).returns("9999")
+ Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
+ Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbname).returns("testname")
+ Puppet.settings.stubs(:value).with(:dbsocket).returns("testsocket")
+
+ Puppet.settings.stubs(:value).with(:dbconnections).returns(0)
+ Puppet::Rails.database_arguments.should_not be_include(:pool)
+
+ Puppet.settings.stubs(:value).with(:dbconnections).returns('0')
+ Puppet::Rails.database_arguments.should_not be_include(:pool)
+
+ Puppet.settings.stubs(:value).with(:dbconnections).returns('')
+ Puppet::Rails.database_arguments.should_not be_include(:pool)
+ end
end
describe Puppet::Rails, "when initializing a postgresql connection" do
@@ -180,9 +200,9 @@ describe Puppet::Rails, "when initializing a postgresql connection" do
Puppet.settings.stubs(:value).with(:dbport).returns("9999")
Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbconnections).returns((pool_size = 200).to_s)
Puppet.settings.stubs(:value).with(:dbname).returns("testname")
Puppet.settings.stubs(:value).with(:dbsocket).returns("")
- Puppet.settings.stubs(:value).with(:dbconnections).returns(1)
Puppet::Rails.database_arguments.should == {
:adapter => "postgresql",
@@ -191,9 +211,9 @@ describe Puppet::Rails, "when initializing a postgresql connection" do
:port => "9999",
:username => "testuser",
:password => "testpassword",
+ :pool => pool_size,
:database => "testname",
- :reconnect => true,
- :pool => 1
+ :reconnect => true
}
end
@@ -204,9 +224,9 @@ describe Puppet::Rails, "when initializing a postgresql connection" do
Puppet.settings.stubs(:value).with(:dbport).returns("9999")
Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbconnections).returns((pool_size = 122).to_s)
Puppet.settings.stubs(:value).with(:dbname).returns("testname")
Puppet.settings.stubs(:value).with(:dbsocket).returns("testsocket")
- Puppet.settings.stubs(:value).with(:dbconnections).returns(1)
Puppet::Rails.database_arguments.should == {
:adapter => "postgresql",
@@ -215,50 +235,90 @@ describe Puppet::Rails, "when initializing a postgresql connection" do
:port => "9999",
:username => "testuser",
:password => "testpassword",
+ :pool => pool_size,
:database => "testname",
:socket => "testsocket",
- :pool => 1,
:reconnect => true
}
end
+
+ it "should not provide the pool if dbconnections is 0, '0', or ''" do
+ Puppet.settings.stubs(:value).with(:dbadapter).returns("mysql")
+ Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
+ Puppet.settings.stubs(:value).with(:dbserver).returns("testserver")
+ Puppet.settings.stubs(:value).with(:dbport).returns("9999")
+ Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
+ Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbname).returns("testname")
+ Puppet.settings.stubs(:value).with(:dbsocket).returns("testsocket")
+
+ Puppet.settings.stubs(:value).with(:dbconnections).returns(0)
+ Puppet::Rails.database_arguments.should_not be_include(:pool)
+
+ Puppet.settings.stubs(:value).with(:dbconnections).returns('0')
+ Puppet::Rails.database_arguments.should_not be_include(:pool)
+
+ Puppet.settings.stubs(:value).with(:dbconnections).returns('')
+ Puppet::Rails.database_arguments.should_not be_include(:pool)
+ end
end
describe Puppet::Rails, "when initializing an Oracle connection" do
confine "Cannot test without ActiveRecord" => Puppet.features.rails?
- it "should provide the adapter, log_level, and username, password, dbconnections, and database arguments" do
+ it "should provide the adapter, log_level, and username, password, and database arguments" do
Puppet.settings.stubs(:value).with(:dbadapter).returns("oracle_enhanced")
Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbconnections).returns((pool_size = 123).to_s)
Puppet.settings.stubs(:value).with(:dbname).returns("testname")
- Puppet.settings.stubs(:value).with(:dbconnections).returns(1)
Puppet::Rails.database_arguments.should == {
:adapter => "oracle_enhanced",
:log_level => "testlevel",
:username => "testuser",
:password => "testpassword",
- :database => "testname",
- :pool => 1
+ :pool => pool_size,
+ :database => "testname"
}
end
- it "should provide the adapter, log_level, and host, username, password, database, pool, and socket arguments" do
+ it "should provide the adapter, log_level, and host, username, password, database and socket arguments" do
Puppet.settings.stubs(:value).with(:dbadapter).returns("oracle_enhanced")
Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbconnections).returns((pool_size = 124).to_s)
Puppet.settings.stubs(:value).with(:dbname).returns("testname")
- Puppet.settings.stubs(:value).with(:dbconnections).returns(1)
Puppet::Rails.database_arguments.should == {
:adapter => "oracle_enhanced",
:log_level => "testlevel",
:username => "testuser",
:password => "testpassword",
- :database => "testname",
- :pool => 1
+ :pool => pool_size,
+ :database => "testname"
}
end
+
+ it "should not provide the pool if dbconnections is 0, '0', or ''" do
+ Puppet.settings.stubs(:value).with(:dbadapter).returns("mysql")
+ Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
+ Puppet.settings.stubs(:value).with(:dbserver).returns("testserver")
+ Puppet.settings.stubs(:value).with(:dbport).returns("9999")
+ Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
+ Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
+ Puppet.settings.stubs(:value).with(:dbname).returns("testname")
+ Puppet.settings.stubs(:value).with(:dbsocket).returns("testsocket")
+
+ Puppet.settings.stubs(:value).with(:dbconnections).returns(0)
+ Puppet::Rails.database_arguments.should_not be_include(:pool)
+
+ Puppet.settings.stubs(:value).with(:dbconnections).returns('0')
+ Puppet::Rails.database_arguments.should_not be_include(:pool)
+
+ Puppet.settings.stubs(:value).with(:dbconnections).returns('')
+ Puppet::Rails.database_arguments.should_not be_include(:pool)
+ end
end
diff --git a/spec/unit/resource/catalog_spec.rb b/spec/unit/resource/catalog_spec.rb
index 2b6beb5e9..fbfe29ff3 100755
--- a/spec/unit/resource/catalog_spec.rb
+++ b/spec/unit/resource/catalog_spec.rb
@@ -374,7 +374,7 @@ describe Puppet::Resource::Catalog, "when compiling" do
@original.add_edge(@r1,@r2)
@original.filter do |r|
r == @r1
- end.edge(@r1,@r2).should be_empty
+ end.edge?(@r1,@r2).should_not be
end
end
@@ -933,8 +933,8 @@ describe Puppet::Resource::Catalog, "when converting to pson" do
@catalog.add_edge(one, two)
@catalog.add_edge(two, three)
- @catalog.edge(one, two ).expects(:to_pson_data_hash).returns "one_two_pson"
- @catalog.edge(two, three).expects(:to_pson_data_hash).returns "two_three_pson"
+ @catalog.edges_between(one, two )[0].expects(:to_pson_data_hash).returns "one_two_pson"
+ @catalog.edges_between(two, three)[0].expects(:to_pson_data_hash).returns "two_three_pson"
PSON.parse(@catalog.to_pson,:create_additions => false)['data']['edges'].sort.should == %w{one_two_pson two_three_pson}.sort
end
diff --git a/spec/unit/simple_graph_spec.rb b/spec/unit/simple_graph_spec.rb
index 2ca8888c5..fa0bcb06a 100755
--- a/spec/unit/simple_graph_spec.rb
+++ b/spec/unit/simple_graph_spec.rb
@@ -31,12 +31,6 @@ describe Puppet::SimpleGraph do
proc { @graph.to_dot_graph }.should_not raise_error
end
- it "should always put its edges first when printing yaml" do
- @graph = Puppet::SimpleGraph.new
- @graph.add_edge(:one, :two)
- @graph.to_yaml_properties[0].should == "@edges"
- end
-
describe "when managing vertices" do
before do
@graph = Puppet::SimpleGraph.new
@@ -117,16 +111,31 @@ describe Puppet::SimpleGraph do
@graph.edge?(:one, :two).should be_true
end
- it "should provide a method for retrieving an edge label" do
- edge = Puppet::Relationship.new(:one, :two, :callback => :awesome)
- @graph.add_edge(edge)
- @graph.edge_label(:one, :two).should == {:callback => :awesome}
- end
+ describe "when retrieving edges between two nodes" do
+ it "should handle the case of nodes not in the graph" do
+ @graph.edges_between(:one, :two).should == []
+ end
- it "should provide a method for retrieving an edge" do
- edge = Puppet::Relationship.new(:one, :two)
- @graph.add_edge(edge)
- @graph.edge(:one, :two).should equal(edge)
+ it "should handle the case of nodes with no edges between them" do
+ @graph.add_vertex(:one)
+ @graph.add_vertex(:two)
+ @graph.edges_between(:one, :two).should == []
+ end
+
+ it "should handle the case of nodes connected by a single edge" do
+ edge = Puppet::Relationship.new(:one, :two)
+ @graph.add_edge(edge)
+ @graph.edges_between(:one, :two).length.should == 1
+ @graph.edges_between(:one, :two)[0].should equal(edge)
+ end
+
+ it "should handle the case of nodes connected by multiple edges" do
+ edge1 = Puppet::Relationship.new(:one, :two, :callback => :foo)
+ edge2 = Puppet::Relationship.new(:one, :two, :callback => :bar)
+ @graph.add_edge(edge1)
+ @graph.add_edge(edge2)
+ Set.new(@graph.edges_between(:one, :two)).should == Set.new([edge1, edge2])
+ end
end
it "should add the edge source as a vertex if it is not already" do
@@ -253,7 +262,7 @@ describe Puppet::SimpleGraph do
it "should retain labels on edges" do
@graph.add_edge(:one, :two, :callback => :awesome)
- edge = @graph.reversal.edge(:two, :one)
+ edge = @graph.reversal.edges_between(:two, :one)[0]
edge.label.should == {:callback => :awesome}
end
end
@@ -522,14 +531,14 @@ describe Puppet::SimpleGraph do
it "should not add labels to edges that have none" do
@depgraph.add_edge(@two, @three)
splice
- @depgraph.edge_label("c", "i").should == {}
+ @depgraph.edges_between("c", "i")[0].label.should == {}
end
it "should copy labels over edges that have none" do
@depgraph.add_edge("c", @three, {:callback => :refresh})
splice
# And make sure the label got copied.
- @depgraph.edge_label("c", "i").should == {:callback => :refresh}
+ @depgraph.edges_between("c", "i")[0].label.should == {:callback => :refresh}
end
it "should not replace a label with a nil label" do
@@ -537,7 +546,7 @@ describe Puppet::SimpleGraph do
@depgraph.add_edge(@middle, @three)
@depgraph.add_edge("c", @three, {:callback => :refresh})
splice
- @depgraph.edge_label("c", "i").should == {:callback => :refresh}
+ @depgraph.edges_between("c", "i")[0].label.should == {:callback => :refresh}
end
it "should copy labels to all created edges" do
@@ -547,8 +556,207 @@ describe Puppet::SimpleGraph do
@three.each do |child|
edge = Puppet::Relationship.new("c", child)
@depgraph.should be_edge(edge.source, edge.target)
- @depgraph.edge_label(edge.source, edge.target).should == {:callback => :refresh}
+ @depgraph.edges_between(edge.source, edge.target)[0].label.should == {:callback => :refresh}
+ end
+ end
+ end
+
+ it "should serialize to YAML using the old format by default" do
+ Puppet::SimpleGraph.use_new_yaml_format.should == false
+ end
+
+ describe "(yaml tests)" do
+ def empty_graph(graph)
+ end
+
+ def one_vertex_graph(graph)
+ graph.add_vertex(:a)
+ end
+
+ def graph_without_edges(graph)
+ [:a, :b, :c].each { |x| graph.add_vertex(x) }
+ end
+
+ def one_edge_graph(graph)
+ graph.add_edge(:a, :b)
+ end
+
+ def many_edge_graph(graph)
+ graph.add_edge(:a, :b)
+ graph.add_edge(:a, :c)
+ graph.add_edge(:b, :d)
+ graph.add_edge(:c, :d)
+ end
+
+ def labeled_edge_graph(graph)
+ graph.add_edge(:a, :b, :callback => :foo, :event => :bar)
+ end
+
+ def overlapping_edge_graph(graph)
+ graph.add_edge(:a, :b, :callback => :foo, :event => :bar)
+ graph.add_edge(:a, :b, :callback => :biz, :event => :baz)
+ end
+
+ def self.all_test_graphs
+ [:empty_graph, :one_vertex_graph, :graph_without_edges, :one_edge_graph, :many_edge_graph, :labeled_edge_graph,
+ :overlapping_edge_graph]
+ end
+
+ def object_ids(enumerable)
+ # Return a sorted list of the object id's of the elements of an
+ # enumerable.
+ enumerable.collect { |x| x.object_id }.sort
+ end
+
+ def graph_to_yaml(graph, which_format)
+ previous_use_new_yaml_format = Puppet::SimpleGraph.use_new_yaml_format
+ Puppet::SimpleGraph.use_new_yaml_format = (which_format == :new)
+ ZAML.dump(graph)
+ ensure
+ Puppet::SimpleGraph.use_new_yaml_format = previous_use_new_yaml_format
+ end
+
+ # Test serialization of graph to YAML.
+ [:old, :new].each do |which_format|
+ all_test_graphs.each do |graph_to_test|
+ it "should be able to serialize #{graph_to_test} to YAML (#{which_format} format)" do
+ graph = Puppet::SimpleGraph.new
+ send(graph_to_test, graph)
+ yaml_form = graph_to_yaml(graph, which_format)
+
+ # Hack the YAML so that objects in the Puppet namespace get
+ # changed to YAML::DomainType objects. This lets us inspect
+ # the serialized objects easily without invoking any
+ # yaml_initialize hooks.
+ yaml_form.gsub!('!ruby/object:Puppet::', '!hack/object:Puppet::')
+ serialized_object = YAML.load(yaml_form)
+
+ # Check that the object contains instance variables @edges and
+ # @vertices only. @reversal is also permitted, but we don't
+ # check it, because it is going to be phased out.
+ serialized_object.type_id.should == 'object:Puppet::SimpleGraph'
+ serialized_object.value.keys.reject { |x| x == 'reversal' }.sort.should == ['edges', 'vertices']
+
+ # Check edges by forming a set of tuples (source, target,
+ # callback, event) based on the graph and the YAML and make sure
+ # they match.
+ edges = serialized_object.value['edges']
+ edges.should be_a(Array)
+ expected_edge_tuples = graph.edges.collect { |edge| [edge.source, edge.target, edge.callback, edge.event] }
+ actual_edge_tuples = edges.collect do |edge|
+ edge.type_id.should == 'object:Puppet::Relationship'
+ %w{source target}.each { |x| edge.value.keys.should include(x) }
+ edge.value.keys.each { |x| ['source', 'target', 'callback', 'event'].should include(x) }
+ %w{source target callback event}.collect { |x| edge.value[x] }
+ end
+ Set.new(actual_edge_tuples).should == Set.new(expected_edge_tuples)
+ actual_edge_tuples.length.should == expected_edge_tuples.length
+
+ # Check vertices one by one.
+ vertices = serialized_object.value['vertices']
+ if which_format == :old
+ vertices.should be_a(Hash)
+ Set.new(vertices.keys).should == Set.new(graph.vertices)
+ vertices.each do |key, value|
+ value.type_id.should == 'object:Puppet::SimpleGraph::VertexWrapper'
+ value.value.keys.sort.should == %w{adjacencies vertex}
+ value.value['vertex'].should equal(key)
+ adjacencies = value.value['adjacencies']
+ adjacencies.should be_a(Hash)
+ Set.new(adjacencies.keys).should == Set.new([:in, :out])
+ [:in, :out].each do |direction|
+ adjacencies[direction].should be_a(Hash)
+ expected_adjacent_vertices = Set.new(graph.adjacent(key, :direction => direction, :type => :vertices))
+ Set.new(adjacencies[direction].keys).should == expected_adjacent_vertices
+ adjacencies[direction].each do |adj_key, adj_value|
+ # Since we already checked edges, just check consistency
+ # with edges.
+ desired_source = direction == :in ? adj_key : key
+ desired_target = direction == :in ? key : adj_key
+ expected_edges = edges.select do |edge|
+ edge.value['source'] == desired_source && edge.value['target'] == desired_target
+ end
+ adj_value.should be_a(Set)
+ if object_ids(adj_value) != object_ids(expected_edges)
+ raise "For vertex #{key.inspect}, direction #{direction.inspect}: expected adjacencies #{expected_edges.inspect} but got #{adj_value.inspect}"
+ end
+ end
+ end
+ end
+ else
+ vertices.should be_a(Array)
+ Set.new(vertices).should == Set.new(graph.vertices)
+ vertices.length.should == graph.vertices.length
+ end
+ end
+ end
+
+ # Test deserialization of graph from YAML. This presumes the
+ # correctness of serialization to YAML, which has already been
+ # tested.
+ all_test_graphs.each do |graph_to_test|
+ it "should be able to deserialize #{graph_to_test} from YAML (#{which_format} format)" do
+ reference_graph = Puppet::SimpleGraph.new
+ send(graph_to_test, reference_graph)
+ yaml_form = graph_to_yaml(reference_graph, which_format)
+ recovered_graph = YAML.load(yaml_form)
+
+ # Test that the recovered vertices match the vertices in the
+ # reference graph.
+ expected_vertices = reference_graph.vertices.to_a
+ recovered_vertices = recovered_graph.vertices.to_a
+ Set.new(recovered_vertices).should == Set.new(expected_vertices)
+ recovered_vertices.length.should == expected_vertices.length
+
+ # Test that the recovered edges match the edges in the
+ # reference graph.
+ expected_edge_tuples = reference_graph.edges.collect do |edge|
+ [edge.source, edge.target, edge.callback, edge.event]
+ end
+ recovered_edge_tuples = recovered_graph.edges.collect do |edge|
+ [edge.source, edge.target, edge.callback, edge.event]
+ end
+ Set.new(recovered_edge_tuples).should == Set.new(expected_edge_tuples)
+ recovered_edge_tuples.length.should == expected_edge_tuples.length
+
+ # We ought to test that the recovered graph is self-consistent
+ # too. But we're not going to bother with that yet because
+ # the internal representation of the graph is about to change.
+ end
+ end
+
+ it "should be able to serialize a graph where the vertices contain backreferences to the graph (#{which_format} format)" do
+ reference_graph = Puppet::SimpleGraph.new
+ vertex = Object.new
+ vertex.instance_eval { @graph = reference_graph }
+ reference_graph.add_edge(vertex, :other_vertex)
+ yaml_form = graph_to_yaml(reference_graph, which_format)
+ recovered_graph = YAML.load(yaml_form)
+
+ recovered_graph.vertices.length.should == 2
+ recovered_vertex = recovered_graph.vertices.reject { |x| x.is_a?(Symbol) }[0]
+ recovered_vertex.instance_eval { @graph }.should equal(recovered_graph)
+ recovered_graph.edges.length.should == 1
+ recovered_edge = recovered_graph.edges[0]
+ recovered_edge.source.should equal(recovered_vertex)
+ recovered_edge.target.should == :other_vertex
+ end
+ end
+
+ it "should serialize properly when used as a base class" do
+ class Puppet::TestDerivedClass < Puppet::SimpleGraph
+ attr_accessor :foo
end
+ derived = Puppet::TestDerivedClass.new
+ derived.add_edge(:a, :b)
+ derived.foo = 1234
+ recovered_derived = YAML.load(YAML.dump(derived))
+ recovered_derived.class.should equal(Puppet::TestDerivedClass)
+ recovered_derived.edges.length.should == 1
+ recovered_derived.edges[0].source.should == :a
+ recovered_derived.edges[0].target.should == :b
+ recovered_derived.vertices.length.should == 2
+ recovered_derived.foo.should == 1234
end
end
end
diff --git a/spec/unit/transaction/report_spec.rb b/spec/unit/transaction/report_spec.rb
index 7e0b0554b..b310713d2 100755
--- a/spec/unit/transaction/report_spec.rb
+++ b/spec/unit/transaction/report_spec.rb
@@ -225,8 +225,19 @@ describe Puppet::Transaction::Report do
@report.calculate_metrics
end
- %w{Changes Total Resources}.each do |main|
- it "should include information on #{main} in the summary" do
+ %w{changes time resources events}.each do |main|
+ it "should include the key #{main} in the raw summary hash" do
+ @report.raw_summary.should be_key main
+ end
+ end
+
+ it "should include the last run time in the raw summary hash" do
+ Time.stubs(:now).returns(Time.utc(2010,11,10,12,0,24))
+ @report.raw_summary["time"]["last_run"].should == 1289390424
+ end
+
+ %w{Changes Total Resources Time Events}.each do |main|
+ it "should include information on #{main} in the textual summary" do
@report.summary.should be_include(main)
end
end
diff --git a/spec/unit/type/host_spec.rb b/spec/unit/type/host_spec.rb
new file mode 100755
index 000000000..12ae2af08
--- /dev/null
+++ b/spec/unit/type/host_spec.rb
@@ -0,0 +1,83 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+ssh_authorized_key = Puppet::Type.type(:ssh_authorized_key)
+
+describe Puppet::Type.type(:host) do
+ before do
+ @class = Puppet::Type.type(:host)
+ @catalog = Puppet::Resource::Catalog.new
+ end
+
+ it "should have :name be its namevar" do
+ @class.key_attributes.should == [:name]
+ end
+
+ describe "when validating attributes" do
+ [:name, :provider ].each do |param|
+ it "should have a #{param} parameter" do
+ @class.attrtype(param).should == :param
+ end
+ end
+
+ [:ip, :target, :host_aliases, :comment, :ensure].each do |property|
+ it "should have a #{property} property" do
+ @class.attrtype(property).should == :property
+ end
+ end
+ end
+
+ describe "when validating values" do
+ it "should support present as a value for ensure" do
+ proc { @class.new(:name => "foo", :ensure => :present) }.should_not raise_error
+ end
+
+ it "should support absent as a value for ensure" do
+ proc { @class.new(:name => "foo", :ensure => :absent) }.should_not raise_error
+ end
+
+ it "should accept IPv4 addresses" do
+ proc { @class.new(:name => "foo", :ip => '10.96.0.1') }.should_not raise_error
+ end
+
+ it "should accept long IPv6 addresses" do
+ # Taken from wikipedia article about ipv6
+ proc { @class.new(:name => "foo", :ip => '2001:0db8:85a3:08d3:1319:8a2e:0370:7344') }.should_not raise_error
+ end
+
+ it "should accept one host_alias" do
+ proc { @class.new(:name => "foo", :host_aliases => 'alias1') }.should_not raise_error
+ end
+
+ it "should accept multiple host_aliases" do
+ proc { @class.new(:name => "foo", :host_aliases => [ 'alias1', 'alias2' ]) }.should_not raise_error
+ end
+
+ it "should accept shortened IPv6 addresses" do
+ proc { @class.new(:name => "foo", :ip => '2001:db8:0:8d3:0:8a2e:70:7344') }.should_not raise_error
+ proc { @class.new(:name => "foo", :ip => '::ffff:192.0.2.128') }.should_not raise_error
+ proc { @class.new(:name => "foo", :ip => '::1') }.should_not raise_error
+ end
+
+ it "should not accept malformed IPv4 addresses like 192.168.0.300" do
+ proc { @class.new(:name => "foo", :ip => '192.168.0.300') }.should raise_error
+ end
+
+ it "should not accept malformed IP addresses like 2001:0dg8:85a3:08d3:1319:8a2e:0370:7344" do
+ proc { @class.new(:name => "foo", :ip => '2001:0dg8:85a3:08d3:1319:8a2e:0370:7344') }.should raise_error
+ end
+
+ it "should not accept spaces in resourcename" do
+ proc { @class.new(:name => "foo bar") }.should raise_error
+ end
+
+ it "should not accept host_aliases with spaces" do
+ proc { @class.new(:name => "foo", :host_aliases => [ 'well_formed', 'not wellformed' ]) }.should raise_error
+ end
+
+ it "should not accept empty host_aliases" do
+ proc { @class.new(:name => "foo", :host_aliases => ['alias1','']) }.should raise_error
+ end
+ end
+end
diff --git a/spec/unit/type/schedule_spec.rb b/spec/unit/type/schedule_spec.rb
index 420cffd44..8305431a6 100755
--- a/spec/unit/type/schedule_spec.rb
+++ b/spec/unit/type/schedule_spec.rb
@@ -13,14 +13,6 @@ module ScheduleTesting
Time.at(diff)
end
- def month(method, count)
- diff(:hour, 3600 * 24 * 30, method, count)
- end
-
- def week(method, count)
- diff(:hour, 3600 * 24 * 7, method, count)
- end
-
def day(method, count)
diff(:hour, 3600 * 24, method, count)
end
@@ -33,10 +25,6 @@ module ScheduleTesting
diff(:min, 60, method, count)
end
- def sec(method, count)
- diff(:sec, 1, method, count)
- end
-
end
describe Puppet::Type.type(:schedule) do
@@ -194,26 +182,18 @@ describe Puppet::Type.type(:schedule) do
end
it "should match if the times are one minute apart and the current minute is 0" do
- current = Time.now
+ current = Time.utc(2008, 1, 1, 0, 0, 0)
+ previous = Time.utc(2007, 12, 31, 23, 59, 0)
- # Subtract an hour, reset the minute to zero, then add 59 minutes, so we're the previous hour plus 59 minutes.
- previous = (current - 3600 - (current.min * 60) + (59 * 60))
-
- # Now set the "current" time to the zero minute of the current hour.
- now = (current - (current.min * 60))
- Time.stubs(:now).returns(now)
+ Time.stubs(:now).returns(current)
@schedule.match?(previous).should be_true
end
- it "should not match if the times are 58 minutes apart and the current minute is 59" do
- current = Time.now
+ it "should not match if the times are 59 minutes apart and the current minute is 59" do
+ current = Time.utc(2009, 2, 1, 12, 59, 0)
+ previous = Time.utc(2009, 2, 1, 12, 0, 0)
- # reset the minute to zero
- previous = current - (current.min * 60)
-
- # Now set the "current" time to the 59th minute of the current hour.
- now = (current - (current.min * 60) + (59 * 60))
- Time.stubs(:now).returns(now)
+ Time.stubs(:now).returns(current)
@schedule.match?(previous).should be_false
end
end
@@ -227,10 +207,7 @@ describe Puppet::Type.type(:schedule) do
end
it "should match if the times are one minute apart and the current minute and hour are 0" do
- zero = Time.now
-
- # Reset the current time to X:00:00
- current = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec
+ current = Time.utc(2010, "nov", 7, 0, 0, 0)
# Now set the previous time to one minute before that
previous = current - 60
@@ -240,10 +217,9 @@ describe Puppet::Type.type(:schedule) do
end
it "should not match if the times are 23 hours and 58 minutes apart and the current hour is 23 and the current minute is 59" do
- zero = Time.now
# Reset the previous time to 00:00:00
- previous = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec
+ previous = Time.utc(2010, "nov", 7, 0, 0, 0)
# Set the current time to 23:59
now = previous + (23 * 3600) + (59 * 60)
@@ -262,19 +238,17 @@ describe Puppet::Type.type(:schedule) do
end
it "should match if the previous time is prior to the most recent Sunday" do
- now = Time.now
-
- # Subtract the number days we've progressed into the week, plus one because we're zero-indexed.
- previous = now - (3600 * 24 * (now.wday + 1))
+ now = Time.utc(2010, "nov", 11, 0, 0, 0) # Thursday
+ Time.stubs(:now).returns(now)
+ previous = Time.utc(2010, "nov", 6, 23, 59, 59) # Sat
@schedule.match?(previous).should be_true
end
it "should not match if the previous time is after the most recent Saturday" do
- now = Time.now
-
- # Subtract the number days we've progressed into the week
- previous = now - (3600 * 24 * now.wday)
+ now = Time.utc(2010, "nov", 11, 0, 0, 0) # Thursday
+ Time.stubs(:now).returns(now)
+ previous = Time.utc(2010, "nov", 7, 0, 0, 0) # Sunday
@schedule.match?(previous).should be_false
end
@@ -289,19 +263,17 @@ describe Puppet::Type.type(:schedule) do
end
it "should match when the previous time is prior to the first day of this month" do
- now = Time.now
-
- # Subtract the number days we've progressed into the month
- previous = now - (3600 * 24 * now.day)
+ now = Time.utc(2010, "nov", 8, 00, 59, 59)
+ Time.stubs(:now).returns(now)
+ previous = Time.utc(2010, "oct", 31, 23, 59, 59)
@schedule.match?(previous).should be_true
end
it "should not match when the previous time is after the last day of last month" do
- now = Time.now
-
- # Subtract the number days we've progressed into the month, minus one
- previous = now - (3600 * 24 * (now.day - 1))
+ now = Time.utc(2010, "nov", 8, 00, 59, 59)
+ Time.stubs(:now).returns(now)
+ previous = Time.utc(2010, "nov", 1, 0, 0, 0)
@schedule.match?(previous).should be_false
end
diff --git a/spec/unit/util/command_line_spec.rb b/spec/unit/util/command_line_spec.rb
index a83ad968d..7ba965249 100644
--- a/spec/unit/util/command_line_spec.rb
+++ b/spec/unit/util/command_line_spec.rb
@@ -86,7 +86,7 @@ describe Puppet::Util::CommandLine do
describe "when the subcommand is not implemented" do
it "should find and invoke an executable with a hyphenated name" do
commandline = Puppet::Util::CommandLine.new("puppet", ['whatever', 'argument'], @tty)
- Puppet::Util.expects(:binary).with('puppet-whatever').returns('/dev/null/puppet-whatever')
+ Puppet::Util.expects(:which).with('puppet-whatever').returns('/dev/null/puppet-whatever')
commandline.expects(:system).with('/dev/null/puppet-whatever', 'argument')
commandline.execute
@@ -95,7 +95,7 @@ describe Puppet::Util::CommandLine do
describe "and an external implementation cannot be found" do
it "should abort and show the usage message" do
commandline = Puppet::Util::CommandLine.new("puppet", ['whatever', 'argument'], @tty)
- Puppet::Util.expects(:binary).with('puppet-whatever').returns(nil)
+ Puppet::Util.expects(:which).with('puppet-whatever').returns(nil)
commandline.expects(:system).never
commandline.expects(:usage_message).returns("the usage message")
diff --git a/spec/unit/util/file_locking_spec.rb b/spec/unit/util/file_locking_spec.rb
index 10051060c..8fafb1d52 100755
--- a/spec/unit/util/file_locking_spec.rb
+++ b/spec/unit/util/file_locking_spec.rb
@@ -32,17 +32,12 @@ describe Puppet::Util::FileLocking do
end
it "should use a global shared mutex" do
- @sync = mock 'sync'
- @sync.expects(:synchronize).with(Sync::SH).once
- Puppet::Util.expects(:sync).with('/file').returns @sync
-
+ Puppet::Util.expects(:synchronize_on).with('/file',Sync::SH).once
Puppet::Util::FileLocking.readlock '/file'
end
it "should use a shared lock on the file" do
- @sync = mock 'sync'
- @sync.stubs(:synchronize).yields
- Puppet::Util.expects(:sync).with('/file').returns @sync
+ Puppet::Util.expects(:synchronize_on).with('/file',Sync::SH).yields
fh = mock 'filehandle'
File.expects(:open).with("/file").yields fh
@@ -59,9 +54,7 @@ describe Puppet::Util::FileLocking do
end
it "should create missing files" do
- @sync = mock 'sync'
- @sync.stubs(:synchronize).yields
- Puppet::Util.expects(:sync).with('/file').returns @sync
+ Puppet::Util.expects(:synchronize_on).with('/file',Sync::SH).yields
File.expects(:exists?).with('/file').returns false
File.expects(:open).with('/file').once
@@ -72,9 +65,7 @@ describe Puppet::Util::FileLocking do
describe "when acquiring a write lock" do
before do
- @sync = mock 'sync'
- Puppet::Util.stubs(:sync).returns @sync
- @sync.stubs(:synchronize).yields
+ Puppet::Util.stubs(:synchronize_on).yields
File.stubs(:file?).with('/file').returns true
File.stubs(:exists?).with('/file').returns true
end
@@ -88,29 +79,26 @@ describe Puppet::Util::FileLocking do
end
it "should use a global exclusive mutex" do
- sync = mock 'sync'
- sync.expects(:synchronize).with(Sync::EX)
- Puppet::Util.expects(:sync).with("/file").returns sync
-
+ Puppet::Util.expects(:synchronize_on).with("/file",Sync::EX)
Puppet::Util::FileLocking.writelock '/file'
end
it "should use any specified mode when opening the file" do
- File.expects(:open).with("/file", "w", :mymode)
+ File.expects(:open).with("/file", File::Constants::CREAT | File::Constants::WRONLY , :mymode)
Puppet::Util::FileLocking.writelock('/file', :mymode)
end
it "should use the mode of the existing file if no mode is specified" do
File.expects(:stat).with("/file").returns(mock("stat", :mode => 0755))
- File.expects(:open).with("/file", "w", 0755)
+ File.expects(:open).with("/file", File::Constants::CREAT | File::Constants::WRONLY, 0755)
Puppet::Util::FileLocking.writelock('/file')
end
it "should use 0600 as the mode if no mode is specified and the file does not exist" do
File.expects(:stat).raises(Errno::ENOENT)
- File.expects(:open).with("/file", "w", 0600)
+ File.expects(:open).with("/file", File::Constants::CREAT | File::Constants::WRONLY, 0600)
Puppet::Util::FileLocking.writelock('/file')
end
@@ -130,6 +118,8 @@ describe Puppet::Util::FileLocking do
lfh = mock 'locked_filehandle'
fh.expects(:lock_exclusive).yields(lfh)
+ lfh.stubs(:seek)
+ lfh.stubs(:truncate)
lfh.expects(:print).with "foo"
Puppet::Util::FileLocking.writelock('/file') do |f|
@@ -137,18 +127,32 @@ describe Puppet::Util::FileLocking do
end
end
+ it "should truncate the file under an exclusive lock" do
+ fh = mock 'fh'
+ File.expects(:open).yields fh
+
+ lfh = mock 'locked_filehandle'
+ fh.expects(:lock_exclusive).yields(lfh)
+
+ lfh.expects(:seek).with(0, IO::SEEK_SET)
+ lfh.expects(:truncate).with(0)
+ lfh.stubs(:print)
+
+ Puppet::Util::FileLocking.writelock('/file') do |f|
+ f.print "foo"
+ end
+ end
+
it "should only work on regular files" do
File.expects(:file?).with('/file').returns false
proc { Puppet::Util::FileLocking.writelock('/file') }.should raise_error(ArgumentError)
end
it "should create missing files" do
- @sync = mock 'sync'
- @sync.stubs(:synchronize).yields
- Puppet::Util.expects(:sync).with('/file').returns @sync
+ Puppet::Util.expects(:synchronize_on).with('/file',Sync::EX).yields
File.expects(:exists?).with('/file').returns false
- File.expects(:open).with('/file', 'w', 0600).once
+ File.expects(:open).with('/file', File::Constants::CREAT | File::Constants::WRONLY, 0600).once
Puppet::Util::FileLocking.writelock('/file')
end
diff --git a/spec/unit/util/log_spec.rb b/spec/unit/util/log_spec.rb
index 7d96fe190..ea5d59859 100755
--- a/spec/unit/util/log_spec.rb
+++ b/spec/unit/util/log_spec.rb
@@ -7,7 +7,7 @@ require 'puppet/util/log'
describe Puppet::Util::Log do
it "should write a given message to the specified destination" do
arraydest = []
- Puppet::Util::Log.newdestination(arraydest)
+ Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(arraydest))
Puppet::Util::Log.new(:level => :notice, :message => "foo")
message = arraydest.last.message
message.should == "foo"
@@ -87,7 +87,7 @@ describe Puppet::Util::Log do
it "should flush the log queue when the first destination is specified" do
Puppet::Util::Log.close_all
Puppet::Util::Log.expects(:flushqueue)
- Puppet::Util::Log.newdestination([])
+ Puppet::Util::Log.newdestination(:console)
end
it "should convert the level to a symbol if it's passed in as a string" do
diff --git a/spec/unit/util/monkey_patches_spec.rb b/spec/unit/util/monkey_patches_spec.rb
index b0f61c808..049ed1044 100644
--- a/spec/unit/util/monkey_patches_spec.rb
+++ b/spec/unit/util/monkey_patches_spec.rb
@@ -5,3 +5,29 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f
require 'puppet/util/monkey_patches'
+
+describe "yaml deserialization" do
+ it "should call yaml_initialize when deserializing objects that have that method defined" do
+ class Puppet::TestYamlInitializeClass
+ attr_reader :foo
+
+ def yaml_initialize(tag, var)
+ var.should == {'foo' => 100}
+ instance_variables.should == []
+ @foo = 200
+ end
+ end
+
+ obj = YAML.load("--- !ruby/object:Puppet::TestYamlInitializeClass\n foo: 100")
+ obj.foo.should == 200
+ end
+
+ it "should not call yaml_initialize if not defined" do
+ class Puppet::TestYamlNonInitializeClass
+ attr_reader :foo
+ end
+
+ obj = YAML.load("--- !ruby/object:Puppet::TestYamlNonInitializeClass\n foo: 100")
+ obj.foo.should == 100
+ end
+end
diff --git a/spec/unit/util/pson_spec.rb b/spec/unit/util/pson_spec.rb
index d02d28517..474ddafa4 100755
--- a/spec/unit/util/pson_spec.rb
+++ b/spec/unit/util/pson_spec.rb
@@ -35,4 +35,19 @@ describe Puppet::Util::Pson do
bin_string = (1..20000).collect { |i| ((17*i+13*i*i) % 255).chr }.join
PSON.parse(%Q{{ "type": "foo", "data": #{bin_string.to_pson} }})["data"].should == bin_string
end
+
+ it "should be able to handle UTF8 that isn't a real unicode character" do
+ s = ["\355\274\267"]
+ PSON.parse( [s].to_pson ).should == [s]
+ end
+
+ it "should be able to handle UTF8 for \\xFF" do
+ s = ["\xc3\xbf"]
+ PSON.parse( [s].to_pson ).should == [s]
+ end
+
+ it "should be able to handle invalid UTF8 bytes" do
+ s = ["\xc3\xc3"]
+ PSON.parse( [s].to_pson ).should == [s]
+ end
end
diff --git a/spec/unit/util/rdoc_spec.rb b/spec/unit/util/rdoc_spec.rb
index 58c2034ee..41d4b9cd0 100755
--- a/spec/unit/util/rdoc_spec.rb
+++ b/spec/unit/util/rdoc_spec.rb
@@ -43,12 +43,20 @@ describe Puppet::Util::RDoc do
Puppet::Util::RDoc.rdoc("output", [], "utf-8")
end
- it "should tell RDoc to force updates of indices" do
+ it "should tell RDoc to force updates of indices when RDoc supports it" do
+ Options::OptionList.stubs(:options).returns([["--force-update", "-U", 0 ]])
@rdoc.expects(:document).with { |args| args.include?("--force-update") }
Puppet::Util::RDoc.rdoc("output", [])
end
+ it "should not tell RDoc to force updates of indices when RDoc doesn't support it" do
+ Options::OptionList.stubs(:options).returns([])
+ @rdoc.expects(:document).never.with { |args| args.include?("--force-update") }
+
+ Puppet::Util::RDoc.rdoc("output", [])
+ end
+
it "should tell RDoc to use the given outputdir" do
@rdoc.expects(:document).with { |args| args.include?("--op") and args.include?("myoutputdir") }
diff --git a/spec/unit/util/storage_spec.rb b/spec/unit/util/storage_spec.rb
index 6c8baba1f..ae3cbc2ae 100755
--- a/spec/unit/util/storage_spec.rb
+++ b/spec/unit/util/storage_spec.rb
@@ -3,15 +3,14 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require 'yaml'
-require 'tempfile'
-
require 'puppet/util/storage'
describe Puppet::Util::Storage do
+ include PuppetSpec::Files
before(:all) do
@basepath = Puppet.features.posix? ? "/somepath" : "C:/somepath"
- Puppet[:statedir] = Dir.tmpdir
+ Puppet[:statedir] = tmpdir("statedir")
end
after(:all) do
diff --git a/spec/unit/util/zaml_spec.rb b/spec/unit/util/zaml_spec.rb
index b223f89d4..358c6aa11 100755
--- a/spec/unit/util/zaml_spec.rb
+++ b/spec/unit/util/zaml_spec.rb
@@ -61,4 +61,3 @@ describe "Pure ruby yaml implementation" do
x2[2].should equal(x2)
end
end
-