diff --git a/lib/puppet/util/log.rb b/lib/puppet/util/log.rb
index 36a765c61..a5aacc265 100644
--- a/lib/puppet/util/log.rb
+++ b/lib/puppet/util/log.rb
@@ -1,257 +1,258 @@
require 'puppet/util/tagging'
require 'puppet/util/classgen'
# Pass feedback to the user. Log levels are modeled after syslog's, and it is
# expected that that will be the most common log destination. Supports
# multiple destinations, one of which is a remote server.
class Puppet::Util::Log
include Puppet::Util
extend Puppet::Util::ClassGen
include Puppet::Util::Tagging
@levels = [:debug,:info,:notice,:warning,:err,:alert,:emerg,:crit]
@loglevel = 2
@desttypes = {}
# Create a new destination type.
def self.newdesttype(name, options = {}, &block)
dest = genclass(
name, :parent => Puppet::Util::Log::Destination, :prefix => "Dest",
:block => block,
:hash => @desttypes,
:attributes => options
)
dest.match(dest.name)
dest
end
require 'puppet/util/log/destination'
require 'puppet/util/log/destinations'
@destinations = {}
@queued = []
class << self
include Puppet::Util
include Puppet::Util::ClassGen
attr_reader :desttypes
end
# Reset log to basics. Basically just flushes and closes files and
# undefs other objects.
def Log.close(destination)
if @destinations.include?(destination)
@destinations[destination].flush if @destinations[destination].respond_to?(:flush)
@destinations[destination].close if @destinations[destination].respond_to?(:close)
@destinations.delete(destination)
end
end
def self.close_all
destinations.keys.each { |dest|
close(dest)
}
+ raise Puppet::DevError.new("Log.close_all failed to close #{@destinations.keys.inspect}") if !@destinations.empty?
end
# Flush any log destinations that support such operations.
def Log.flush
@destinations.each { |type, dest|
dest.flush if dest.respond_to?(:flush)
}
end
# Create a new log message. The primary role of this method is to
# avoid creating log messages below the loglevel.
def Log.create(hash)
raise Puppet::DevError, "Logs require a level" unless hash.include?(:level)
raise Puppet::DevError, "Invalid log level #{hash[:level]}" unless @levels.index(hash[:level])
@levels.index(hash[:level]) >= @loglevel ? Puppet::Util::Log.new(hash) : nil
end
def Log.destinations
@destinations
end
# Yield each valid level in turn
def Log.eachlevel
@levels.each { |level| yield level }
end
# Return the current log level.
def Log.level
@levels[@loglevel]
end
# Set the current log level.
def Log.level=(level)
level = level.intern unless level.is_a?(Symbol)
raise Puppet::DevError, "Invalid loglevel #{level}" unless @levels.include?(level)
@loglevel = @levels.index(level)
end
def Log.levels
@levels.dup
end
# Create a new log destination.
def Log.newdestination(dest)
# Each destination can only occur once.
if @destinations.find { |name, obj| obj.name == dest }
return
end
name, type = @desttypes.find do |name, klass|
klass.match?(dest)
end
raise Puppet::DevError, "Unknown destination type #{dest}" unless type
begin
if type.instance_method(:initialize).arity == 1
@destinations[dest] = type.new(dest)
else
@destinations[dest] = type.new
end
flushqueue
@destinations[dest]
rescue => detail
puts detail.backtrace if Puppet[:debug]
# If this was our only destination, then add the console back in.
newdestination(:console) if @destinations.empty? and (dest != :console and dest != "console")
end
end
# Route the actual message. FIXME There are lots of things this method
# should do, like caching and a bit more. It's worth noting that there's
# a potential for a loop here, if the machine somehow gets the destination set as
# itself.
def Log.newmessage(msg)
return if @levels.index(msg.level) < @loglevel
queuemessage(msg) if @destinations.length == 0
@destinations.each do |name, dest|
threadlock(dest) do
dest.handle(msg)
end
end
end
def Log.queuemessage(msg)
@queued.push(msg)
end
def Log.flushqueue
return unless @destinations.size >= 1
@queued.each do |msg|
Log.newmessage(msg)
end
@queued.clear
end
def Log.sendlevel?(level)
@levels.index(level) >= @loglevel
end
# Reopen all of our logs.
def Log.reopen
Puppet.notice "Reopening log files"
types = @destinations.keys
@destinations.each { |type, dest|
dest.close if dest.respond_to?(:close)
}
@destinations.clear
# We need to make sure we always end up with some kind of destination
begin
types.each { |type|
Log.newdestination(type)
}
rescue => detail
if @destinations.empty?
Log.newdestination(:syslog)
Puppet.err detail.to_s
end
end
end
# Is the passed level a valid log level?
def self.validlevel?(level)
@levels.include?(level)
end
attr_accessor :time, :remote, :file, :line, :version, :source
attr_reader :level, :message
def initialize(args)
self.level = args[:level]
self.message = args[:message]
self.source = args[:source] || "Puppet"
@time = Time.now
if tags = args[:tags]
tags.each { |t| self.tag(t) }
end
[:file, :line, :version].each do |attr|
next unless value = args[attr]
send(attr.to_s + "=", value)
end
Log.newmessage(self)
end
def message=(msg)
raise ArgumentError, "Puppet::Util::Log requires a message" unless msg
@message = msg.to_s
end
def level=(level)
raise ArgumentError, "Puppet::Util::Log requires a log level" unless level
@level = level.to_sym
raise ArgumentError, "Invalid log level #{@level}" unless self.class.validlevel?(@level)
# Tag myself with my log level
tag(level)
end
# If they pass a source in to us, we make sure it is a string, and
# we retrieve any tags we can.
def source=(source)
if source.respond_to?(:source_descriptors)
descriptors = source.source_descriptors
@source = descriptors[:path]
descriptors[:tags].each { |t| tag(t) }
[:file, :line, :version].each do |param|
next unless descriptors[param]
send(param.to_s + "=", descriptors[param])
end
else
@source = source.to_s
end
end
def to_report
"#{time} #{source} (#{level}): #{to_s}"
end
def to_s
message
end
end
# This is for backward compatibility from when we changed the constant to Puppet::Util::Log
# because the reports include the constant name. Apparently the alias was created in
# March 2007, should could probably be removed soon.
Puppet::Log = Puppet::Util::Log
diff --git a/lib/puppet/util/log/destinations.rb b/lib/puppet/util/log/destinations.rb
index 22b3dedb2..2e2f9a5b7 100644
--- a/lib/puppet/util/log/destinations.rb
+++ b/lib/puppet/util/log/destinations.rb
@@ -1,217 +1,229 @@
Puppet::Util::Log.newdesttype :syslog do
def close
Syslog.close
end
def initialize
Syslog.close if Syslog.opened?
name = Puppet[:name]
name = "puppet-#{name}" unless name =~ /puppet/
options = Syslog::LOG_PID | Syslog::LOG_NDELAY
# XXX This should really be configurable.
str = Puppet[:syslogfacility]
begin
facility = Syslog.const_get("LOG_#{str.upcase}")
rescue NameError
raise Puppet::Error, "Invalid syslog facility #{str}"
end
@syslog = Syslog.open(name, options, facility)
end
def handle(msg)
# XXX Syslog currently has a bug that makes it so you
# cannot log a message with a '%' in it. So, we get rid
# of them.
if msg.source == "Puppet"
@syslog.send(msg.level, msg.to_s.gsub("%", '%%'))
else
@syslog.send(msg.level, "(%s) %s" % [msg.source.to_s.gsub("%", ""),
msg.to_s.gsub("%", '%%')
]
)
end
end
end
Puppet::Util::Log.newdesttype :file do
match(/^\//)
def close
if defined?(@file)
@file.close
@file = nil
end
end
def flush
@file.flush if defined?(@file)
end
def initialize(path)
@name = path
# first make sure the directory exists
# We can't just use 'Config.use' here, because they've
# specified a "special" destination.
unless FileTest.exist?(File.dirname(path))
Puppet.recmkdir(File.dirname(path))
Puppet.info "Creating log directory #{File.dirname(path)}"
end
# create the log file, if it doesn't already exist
file = File.open(path, File::WRONLY|File::CREAT|File::APPEND)
@file = file
@autoflush = Puppet[:autoflush]
end
def handle(msg)
@file.puts("#{msg.time} #{msg.source} (#{msg.level}): #{msg}")
@file.flush if @autoflush
end
end
Puppet::Util::Log.newdesttype :console do
RED = {:console => "[0;31m", :html => "FFA0A0"}
GREEN = {:console => "[0;32m", :html => "00CD00"}
YELLOW = {:console => "[0;33m", :html => "FFFF60"}
BLUE = {:console => "[0;34m", :html => "80A0FF"}
PURPLE = {:console => "[0;35m", :html => "FFA500"}
CYAN = {:console => "[0;36m", :html => "40FFFF"}
WHITE = {:console => "[0;37m", :html => "FFFFFF"}
HRED = {:console => "[1;31m", :html => "FFA0A0"}
HGREEN = {:console => "[1;32m", :html => "00CD00"}
HYELLOW = {:console => "[1;33m", :html => "FFFF60"}
HBLUE = {:console => "[1;34m", :html => "80A0FF"}
HPURPLE = {:console => "[1;35m", :html => "FFA500"}
HCYAN = {:console => "[1;36m", :html => "40FFFF"}
HWHITE = {:console => "[1;37m", :html => "FFFFFF"}
RESET = {:console => "[0m", :html => "" }
@@colormap = {
:debug => WHITE,
:info => GREEN,
:notice => CYAN,
:warning => YELLOW,
:err => HPURPLE,
:alert => RED,
:emerg => HRED,
:crit => HRED
}
def colorize(level, str)
case Puppet[:color]
when true, :ansi, "ansi", "yes"; console_color(level, str)
when :html, "html"; html_color(level, str)
else
str
end
end
def console_color(level, str)
@@colormap[level][:console] + str + RESET[:console]
end
def html_color(level, str)
%{%s} % [@@colormap[level][:html], str]
end
def initialize
# Flush output immediately.
$stdout.sync = true
end
def handle(msg)
if msg.source == "Puppet"
puts colorize(msg.level, "#{msg.level}: #{msg}")
else
puts colorize(msg.level, "#{msg.level}: #{msg.source}: #{msg}")
end
end
end
Puppet::Util::Log.newdesttype :host do
def initialize(host)
Puppet.info "Treating #{host} as a hostname"
args = {}
if host =~ /:(\d+)/
args[:Port] = $1
args[:Server] = host.sub(/:\d+/, '')
else
args[:Server] = host
end
@name = host
@driver = Puppet::Network::Client::LogClient.new(args)
end
def handle(msg)
unless msg.is_a?(String) or msg.remote
@hostname ||= Facter["hostname"].value
unless defined?(@domain)
@domain = Facter["domain"].value
@hostname += ".#{@domain}" if @domain
end
if msg.source =~ /^\//
msg.source = @hostname + ":#{msg.source}"
elsif msg.source == "Puppet"
msg.source = @hostname + " #{msg.source}"
else
msg.source = @hostname + " #{msg.source}"
end
begin
#puts "would have sent #{msg}"
#puts "would have sent %s" %
# CGI.escape(YAML.dump(msg))
begin
tmp = CGI.escape(YAML.dump(msg))
rescue => detail
puts "Could not dump: #{detail}"
return
end
# Add the hostname to the source
@driver.addlog(tmp)
rescue => detail
puts detail.backtrace if Puppet[:trace]
Puppet.err detail
Puppet::Util::Log.close(self)
end
end
end
end
# Log to a transaction report.
Puppet::Util::Log.newdesttype :report do
attr_reader :report
match "Puppet::Transaction::Report"
def initialize(report)
@report = report
end
def handle(msg)
@report << msg
end
end
# Log to an array, just for testing.
+module Puppet::Test
+ class LogCollector
+ def initialize(logs)
+ @logs = logs
+ end
+
+ def <<(value)
+ @logs << value
+ end
+ end
+end
+
Puppet::Util::Log.newdesttype :array do
- match "Array"
+ match "Puppet::Test::LogCollector"
def initialize(messages)
@messages = messages
end
def handle(msg)
@messages << msg
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
@@ -1,79 +1,79 @@
unless defined?(SPEC_HELPER_IS_LOADED)
SPEC_HELPER_IS_LOADED = 1
dir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift("#{dir}/")
$LOAD_PATH.unshift("#{dir}/lib") # a spec-specific test lib dir
$LOAD_PATH.unshift("#{dir}/../lib")
# Don't want puppet getting the command line arguments for rake or autotest
ARGV.clear
require 'puppet'
require 'mocha'
gem 'rspec', '>=1.2.9'
require 'spec/autorun'
# So everyone else doesn't have to include this base constant.
module PuppetSpec
FIXTURE_DIR = File.join(dir = File.expand_path(File.dirname(__FILE__)), "fixtures") unless defined?(FIXTURE_DIR)
end
require 'lib/puppet_spec/files'
require 'monkey_patches/alias_should_to_must'
require 'monkey_patches/add_confine_and_runnable_to_rspec_dsl'
require 'monkey_patches/publicize_methods'
Spec::Runner.configure do |config|
config.mock_with :mocha
config.prepend_after :each do
Puppet.settings.clear
Puppet::Node::Environment.clear
Puppet::Util::Storage.clear
if defined?($tmpfiles)
$tmpfiles.each do |file|
file = File.expand_path(file)
if Puppet.features.posix? and file !~ /^\/tmp/ and file !~ /^\/var\/folders/
puts "Not deleting tmpfile #{file} outside of /tmp or /var/folders"
next
elsif Puppet.features.microsoft_windows?
tempdir = File.expand_path(File.join(Dir::LOCAL_APPDATA, "Temp"))
if file !~ /^#{tempdir}/
puts "Not deleting tmpfile #{file} outside of #{tempdir}"
next
end
end
if FileTest.exist?(file)
system("chmod -R 755 '#{file}'")
system("rm -rf '#{file}'")
end
end
$tmpfiles.clear
end
@logs.clear
Puppet::Util::Log.close_all
end
config.prepend_before :each do
# these globals are set by Application
$puppet_application_mode = nil
$puppet_application_name = nil
# Set the confdir and vardir to gibberish so that tests
# have to be correctly mocked.
Puppet[:confdir] = "/dev/null"
Puppet[:vardir] = "/dev/null"
# Avoid opening ports to the outside world
Puppet.settings[:bindaddress] = "127.0.0.1"
@logs = []
- Puppet::Util::Log.newdestination(@logs)
+ Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(@logs))
end
end
end
diff --git a/spec/unit/configurer_spec.rb b/spec/unit/configurer_spec.rb
index 0c9d06362..2f8e615ff 100755
--- a/spec/unit/configurer_spec.rb
+++ b/spec/unit/configurer_spec.rb
@@ -1,494 +1,491 @@
#!/usr/bin/env ruby
#
# Created by Luke Kanies on 2007-11-12.
# Copyright (c) 2007. All rights reserved.
require File.dirname(__FILE__) + '/../spec_helper'
require 'puppet/configurer'
describe Puppet::Configurer do
before do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Configurer.new
end
it "should include the Plugin Handler module" do
Puppet::Configurer.ancestors.should be_include(Puppet::Configurer::PluginHandler)
end
it "should include the Fact Handler module" do
Puppet::Configurer.ancestors.should be_include(Puppet::Configurer::FactHandler)
end
it "should use the puppetdlockfile as its lockfile path" do
Puppet.settings.expects(:value).with(:puppetdlockfile).returns("/my/lock")
Puppet::Configurer.lockfile_path.should == "/my/lock"
end
describe "when executing a pre-run hook" do
it "should do nothing if the hook is set to an empty string" do
Puppet.settings[:prerun_command] = ""
Puppet::Util.expects(:exec).never
@agent.execute_prerun_command
end
it "should execute any pre-run command provided via the 'prerun_command' setting" do
Puppet.settings[:prerun_command] = "/my/command"
Puppet::Util.expects(:execute).with { |args| args[0] == "/my/command" }
@agent.execute_prerun_command
end
it "should fail if the command fails" do
Puppet.settings[:prerun_command] = "/my/command"
Puppet::Util.expects(:execute).raises Puppet::ExecutionFailure
lambda { @agent.execute_prerun_command }.should raise_error(Puppet::Configurer::CommandHookError)
end
end
describe "when executing a post-run hook" do
it "should do nothing if the hook is set to an empty string" do
Puppet.settings[:postrun_command] = ""
Puppet::Util.expects(:exec).never
@agent.execute_postrun_command
end
it "should execute any post-run command provided via the 'postrun_command' setting" do
Puppet.settings[:postrun_command] = "/my/command"
Puppet::Util.expects(:execute).with { |args| args[0] == "/my/command" }
@agent.execute_postrun_command
end
it "should fail if the command fails" do
Puppet.settings[:postrun_command] = "/my/command"
Puppet::Util.expects(:execute).raises Puppet::ExecutionFailure
lambda { @agent.execute_postrun_command }.should raise_error(Puppet::Configurer::CommandHookError)
end
end
end
describe Puppet::Configurer, "when initializing a report" do
it "should return an instance of a transaction report" do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Configurer.new
@agent.initialize_report.should be_instance_of(Puppet::Transaction::Report)
end
end
describe Puppet::Configurer, "when executing a catalog run" do
before do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Configurer.new
@agent.stubs(:prepare)
@agent.stubs(:facts_for_uploading).returns({})
@catalog = Puppet::Resource::Catalog.new
@catalog.stubs(:apply)
@agent.stubs(:retrieve_catalog).returns @catalog
-
- Puppet::Util::Log.stubs(:newdestination)
- Puppet::Util::Log.stubs(:close)
end
it "should prepare for the run" do
@agent.expects(:prepare)
@agent.run
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}
@agent.run
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}
@agent.run(:report => report)
end
it "should set the report as a log destination" do
report = stub 'report'
@agent.expects(:initialize_report).returns report
Puppet::Util::Log.expects(:newdestination).with(report)
@agent.run
end
it "should retrieve the catalog" do
@agent.expects(:retrieve_catalog)
@agent.run
end
it "should log a failure and do nothing if no catalog can be retrieved" do
@agent.expects(:retrieve_catalog).returns nil
Puppet.expects(:err).with "Could not retrieve catalog; skipping run"
@agent.run
end
it "should apply the catalog with all options to :run" do
@agent.expects(:retrieve_catalog).returns @catalog
@catalog.expects(:apply).with { |args| args[:one] == true }
@agent.run :one => true
end
it "should accept a catalog and use it instead of retrieving a different one" do
@agent.expects(:retrieve_catalog).never
@catalog.expects(:apply)
@agent.run :one => true, :catalog => @catalog
end
it "should benchmark how long it takes to apply the catalog" do
@agent.expects(:benchmark).with(:notice, "Finished catalog run")
@agent.expects(:retrieve_catalog).returns @catalog
@catalog.expects(:apply).never # because we're not yielding
@agent.run
end
it "should execute post-run hooks after the run" do
@agent.expects(:execute_postrun_command)
@agent.run
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 }
@agent.run
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'
@catalog.expects(:apply).returns trans
@agent.expects(:send_report).with { |r, t| t == trans }
@agent.run :catalog => @catalog
end
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)
@agent.run
end
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)
lambda { @agent.run }.should raise_error
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)
end
end
describe Puppet::Configurer, "when sending a report" do
before do
Puppet.settings.stubs(:use).returns(true)
@configurer = Puppet::Configurer.new
@report = stub 'report'
@trans = stub 'transaction'
end
it "should require a report" do
lambda { @configurer.send_report }.should raise_error(ArgumentError)
end
it "should allow specification of a transaction" do
lambda { @configurer.send_report(@report, @trans) }.should_not raise_error(ArgumentError)
end
it "should use any provided transaction to add metrics to the report" do
@trans.expects(:generate_report)
@configurer.send_report(@report, @trans)
end
it "should print a report summary if configured to do so" do
Puppet.settings[:summarize] = true
@report.expects(:summary).returns "stuff"
@configurer.expects(:puts).with("stuff")
@configurer.send_report(@report)
end
it "should not print a report summary if not configured to do so" do
Puppet.settings[:summarize] = false
@configurer.expects(:puts).never
@configurer.send_report(@report)
end
it "should save the report if reporting is enabled" do
Puppet.settings[:report] = true
@report.expects(:save)
@configurer.send_report(@report)
end
it "should not save the report if reporting is disabled" do
Puppet.settings[:report] = false
@report.expects(:save).never
@configurer.send_report(@report)
end
it "should log but not fail if saving the report fails" do
Puppet.settings[:report] = true
@report.expects(:save).raises "whatever"
Puppet.expects(:err)
lambda { @configurer.send_report(@report) }.should_not raise_error
end
end
describe Puppet::Configurer, "when retrieving a catalog" do
before do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Configurer.new
@agent.stubs(:facts_for_uploading).returns({})
@catalog = Puppet::Resource::Catalog.new
# this is the default when using a Configurer instance
Puppet::Resource::Catalog.indirection.stubs(:terminus_class).returns :rest
@agent.stubs(:convert_catalog).returns @catalog
end
describe "and configured to only retrieve a catalog from the cache" do
before do
Puppet.settings[:use_cached_catalog] = true
end
it "should first look in the cache for a catalog" do
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_terminus] == true }.returns @catalog
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_cache] == true }.never
@agent.retrieve_catalog.should == @catalog
end
it "should compile a new catalog if none is found in the cache" do
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_terminus] == true }.returns nil
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_cache] == true }.returns @catalog
@agent.retrieve_catalog.should == @catalog
end
end
describe "when not using a REST terminus for catalogs" do
it "should not pass any facts when retrieving the catalog" do
@agent.expects(:facts_for_uploading).never
Puppet::Resource::Catalog.expects(:find).with { |name, options|
options[:facts].nil?
}.returns @catalog
@agent.retrieve_catalog
end
end
describe "when using a REST terminus for catalogs" do
it "should pass the prepared facts and the facts format as arguments when retrieving the catalog" do
@agent.expects(:facts_for_uploading).returns(:facts => "myfacts", :facts_format => :foo)
Puppet::Resource::Catalog.expects(:find).with { |name, options|
options[:facts] == "myfacts" and options[:facts_format] == :foo
}.returns @catalog
@agent.retrieve_catalog
end
end
it "should use the Catalog class to get its catalog" do
Puppet::Resource::Catalog.expects(:find).returns @catalog
@agent.retrieve_catalog
end
it "should use its certname to retrieve the catalog" do
Facter.stubs(:value).returns "eh"
Puppet.settings[:certname] = "myhost.domain.com"
Puppet::Resource::Catalog.expects(:find).with { |name, options| name == "myhost.domain.com" }.returns @catalog
@agent.retrieve_catalog
end
it "should default to returning a catalog retrieved directly from the server, skipping the cache" do
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_cache] == true }.returns @catalog
@agent.retrieve_catalog.should == @catalog
end
it "should log and return the cached catalog when no catalog can be retrieved from the server" do
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_cache] == true }.returns nil
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_terminus] == true }.returns @catalog
Puppet.expects(:notice)
@agent.retrieve_catalog.should == @catalog
end
it "should not look in the cache for a catalog if one is returned from the server" do
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_cache] == true }.returns @catalog
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_terminus] == true }.never
@agent.retrieve_catalog.should == @catalog
end
it "should return the cached catalog when retrieving the remote catalog throws an exception" do
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_cache] == true }.raises "eh"
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_terminus] == true }.returns @catalog
@agent.retrieve_catalog.should == @catalog
end
it "should log and return nil if no catalog can be retrieved from the server and :usecacheonfailure is disabled" do
Puppet.stubs(:[])
Puppet.expects(:[]).with(:usecacheonfailure).returns false
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_cache] == true }.returns nil
Puppet.expects(:warning)
@agent.retrieve_catalog.should be_nil
end
it "should return nil if no cached catalog is available and no catalog can be retrieved from the server" do
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_cache] == true }.returns nil
Puppet::Resource::Catalog.expects(:find).with { |name, options| options[:ignore_terminus] == true }.returns nil
@agent.retrieve_catalog.should be_nil
end
it "should convert the catalog before returning" do
Puppet::Resource::Catalog.stubs(:find).returns @catalog
@agent.expects(:convert_catalog).with { |cat, dur| cat == @catalog }.returns "converted catalog"
@agent.retrieve_catalog.should == "converted catalog"
end
it "should return nil if there is an error while retrieving the catalog" do
Puppet::Resource::Catalog.expects(:find).raises "eh"
@agent.retrieve_catalog.should be_nil
end
end
describe Puppet::Configurer, "when converting the catalog" do
before do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Configurer.new
@catalog = Puppet::Resource::Catalog.new
@oldcatalog = stub 'old_catalog', :to_ral => @catalog
end
it "should convert the catalog to a RAL-formed catalog" do
@oldcatalog.expects(:to_ral).returns @catalog
@agent.convert_catalog(@oldcatalog, 10).should equal(@catalog)
end
it "should finalize the catalog" do
@catalog.expects(:finalize)
@agent.convert_catalog(@oldcatalog, 10)
end
it "should record the passed retrieval time with the RAL catalog" do
@catalog.expects(:retrieval_duration=).with 10
@agent.convert_catalog(@oldcatalog, 10)
end
it "should write the RAL catalog's class file" do
@catalog.expects(:write_class_file)
@agent.convert_catalog(@oldcatalog, 10)
end
end
describe Puppet::Configurer, "when preparing for a run" do
before do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Configurer.new
@agent.stubs(:dostorage)
@agent.stubs(:download_fact_plugins)
@agent.stubs(:download_plugins)
@agent.stubs(:execute_prerun_command)
@facts = {"one" => "two", "three" => "four"}
end
it "should initialize the metadata store" do
@agent.class.stubs(:facts).returns(@facts)
@agent.expects(:dostorage)
@agent.prepare
end
it "should download fact plugins" do
@agent.expects(:download_fact_plugins)
@agent.prepare
end
it "should download plugins" do
@agent.expects(:download_plugins)
@agent.prepare
end
it "should perform the pre-run commands" do
@agent.expects(:execute_prerun_command)
@agent.prepare
end
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
@@ -1,215 +1,215 @@
#!/usr/bin/env ruby
Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
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"
end
describe Puppet::Util::Log::DestConsole do
before do
@console = Puppet::Util::Log::DestConsole.new
end
it "should colorize if Puppet[:color] is :ansi" do
Puppet[:color] = :ansi
@console.colorize(:alert, "abc").should == "\e[0;31mabc\e[0m"
end
it "should colorize if Puppet[:color] is 'yes'" do
Puppet[:color] = "yes"
@console.colorize(:alert, "abc").should == "\e[0;31mabc\e[0m"
end
it "should htmlize if Puppet[:color] is :html" do
Puppet[:color] = :html
@console.colorize(:alert, "abc").should == "abc"
end
it "should do nothing if Puppet[:color] is false" do
Puppet[:color] = false
@console.colorize(:alert, "abc").should == "abc"
end
it "should do nothing if Puppet[:color] is invalid" do
Puppet[:color] = "invalid option"
@console.colorize(:alert, "abc").should == "abc"
end
end
describe "instances" do
before do
Puppet::Util::Log.stubs(:newmessage)
end
[:level, :message, :time, :remote].each do |attr|
it "should have a #{attr} attribute" do
log = Puppet::Util::Log.new :level => :notice, :message => "A test message"
log.should respond_to(attr)
log.should respond_to(attr.to_s + "=")
end
end
it "should fail if created without a level" do
lambda { Puppet::Util::Log.new(:message => "A test message") }.should raise_error(ArgumentError)
end
it "should fail if created without a message" do
lambda { Puppet::Util::Log.new(:level => :notice) }.should raise_error(ArgumentError)
end
it "should make available the level passed in at initialization" do
Puppet::Util::Log.new(:level => :notice, :message => "A test message").level.should == :notice
end
it "should make available the message passed in at initialization" do
Puppet::Util::Log.new(:level => :notice, :message => "A test message").message.should == "A test message"
end
# LAK:NOTE I don't know why this behavior is here, I'm just testing what's in the code,
# at least at first.
it "should always convert messages to strings" do
Puppet::Util::Log.new(:level => :notice, :message => :foo).message.should == "foo"
end
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
Puppet::Util::Log.new(:level => "notice", :message => :foo).level.should == :notice
end
it "should fail if the level is not a symbol or string" do
lambda { Puppet::Util::Log.new(:level => 50, :message => :foo) }.should raise_error(ArgumentError)
end
it "should fail if the provided level is not valid" do
Puppet::Util::Log.expects(:validlevel?).with(:notice).returns false
lambda { Puppet::Util::Log.new(:level => :notice, :message => :foo) }.should raise_error(ArgumentError)
end
it "should set its time to the initialization time" do
time = mock 'time'
Time.expects(:now).returns time
Puppet::Util::Log.new(:level => "notice", :message => :foo).time.should equal(time)
end
it "should make available any passed-in tags" do
log = Puppet::Util::Log.new(:level => "notice", :message => :foo, :tags => %w{foo bar})
log.tags.should be_include("foo")
log.tags.should be_include("bar")
end
it "should use an passed-in source" do
Puppet::Util::Log.any_instance.expects(:source=).with "foo"
Puppet::Util::Log.new(:level => "notice", :message => :foo, :source => "foo")
end
[:file, :line, :version].each do |attr|
it "should use #{attr} if provided" do
Puppet::Util::Log.any_instance.expects(attr.to_s + "=").with "foo"
Puppet::Util::Log.new(:level => "notice", :message => :foo, attr => "foo")
end
end
it "should default to 'Puppet' as its source" do
Puppet::Util::Log.new(:level => "notice", :message => :foo).source.should == "Puppet"
end
it "should register itself with Log" do
Puppet::Util::Log.expects(:newmessage)
Puppet::Util::Log.new(:level => "notice", :message => :foo)
end
it "should have a method for determining if a tag is present" do
Puppet::Util::Log.new(:level => "notice", :message => :foo).should respond_to(:tagged?)
end
it "should match a tag if any of the tags are equivalent to the passed tag as a string" do
Puppet::Util::Log.new(:level => "notice", :message => :foo, :tags => %w{one two}).should be_tagged(:one)
end
it "should tag itself with its log level" do
Puppet::Util::Log.new(:level => "notice", :message => :foo).should be_tagged(:notice)
end
it "should return its message when converted to a string" do
Puppet::Util::Log.new(:level => "notice", :message => :foo).to_s.should == "foo"
end
it "should include its time, source, level, and message when prepared for reporting" do
log = Puppet::Util::Log.new(:level => "notice", :message => :foo)
report = log.to_report
report.should be_include("notice")
report.should be_include("foo")
report.should be_include(log.source)
report.should be_include(log.time.to_s)
end
describe "when setting the source as a RAL object" do
it "should tag itself with any tags the source has" do
source = Puppet::Type.type(:file).new :path => "/foo/bar"
log = Puppet::Util::Log.new(:level => "notice", :message => :foo, :source => source)
source.tags.each do |tag|
log.tags.should be_include(tag)
end
end
it "should use the source_descriptors" do
source = stub "source"
source.stubs(:source_descriptors).returns(:tags => ["tag","tag2"], :path => "path", :version => 100)
log = Puppet::Util::Log.new(:level => "notice", :message => :foo)
log.expects(:tag).with("tag")
log.expects(:tag).with("tag2")
log.expects(:version=).with(100)
log.source = source
log.source.should == "path"
end
it "should copy over any version information" do
catalog = Puppet::Resource::Catalog.new
catalog.version = 25
source = Puppet::Type.type(:file).new :path => "/foo/bar"
catalog.add_resource source
log = Puppet::Util::Log.new(:level => "notice", :message => :foo, :source => source)
log.version.should == 25
end
it "should copy over any file and line information" do
source = Puppet::Type.type(:file).new :path => "/foo/bar"
source.file = "/my/file"
source.line = 50
log = Puppet::Util::Log.new(:level => "notice", :message => :foo, :source => source)
log.file.should == "/my/file"
log.line.should == 50
end
end
describe "when setting the source as a non-RAL object" do
it "should not try to copy over file, version, line, or tag information" do
source = Puppet::Module.new("foo")
source.expects(:file).never
log = Puppet::Util::Log.new(:level => "notice", :message => :foo, :source => source)
end
end
end
end
diff --git a/test/lib/puppettest.rb b/test/lib/puppettest.rb
index 0b3a89a72..a60092cf7 100755
--- a/test/lib/puppettest.rb
+++ b/test/lib/puppettest.rb
@@ -1,319 +1,319 @@
# Add .../test/lib
testlib = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(testlib) unless $LOAD_PATH.include?(testlib)
# Add .../lib
mainlib = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
$LOAD_PATH.unshift(mainlib) unless $LOAD_PATH.include?(mainlib)
require 'puppet'
require 'mocha'
# Only load the test/unit class if we're not in the spec directory.
# Else we get the bogus 'no tests, no failures' message.
unless Dir.getwd =~ /spec/
require 'test/unit'
end
# Yay; hackish but it works
if ARGV.include?("-d")
ARGV.delete("-d")
$console = true
end
require File.expand_path(File.join(File.dirname(__FILE__), '../../spec/monkey_patches/publicize_methods'))
module PuppetTest
# These need to be here for when rspec tests use these
# support methods.
@@tmpfiles = []
# Munge cli arguments, so we can enable debugging if we want
# and so we can run just specific methods.
def self.munge_argv
require 'getoptlong'
result = GetoptLong.new(
[ "--debug", "-d", GetoptLong::NO_ARGUMENT ],
[ "--resolve", "-r", GetoptLong::REQUIRED_ARGUMENT ],
[ "-n", GetoptLong::REQUIRED_ARGUMENT ],
[ "--help", "-h", GetoptLong::NO_ARGUMENT ]
)
usage = "USAGE: TESTOPTS='[-n -n ...] [-d]' rake [target] [target] ..."
opts = []
dir = method = nil
result.each { |opt,arg|
case opt
when "--resolve"
dir, method = arg.split(",")
when "--debug"
$puppet_debug = true
Puppet::Util::Log.level = :debug
Puppet::Util::Log.newdestination(:console)
when "--help"
puts usage
exit
else
opts << opt << arg
end
}
suites = nil
args = ARGV.dup
# Reset the options, so the test suite can deal with them (this is
# what makes things like '-n' work).
opts.each { |o| ARGV << o }
args
end
# Find the root of the Puppet tree; this is not the test directory, but
# the parent of that dir.
def basedir(*list)
unless defined?(@@basedir)
Dir.chdir(File.dirname(__FILE__)) do
@@basedir = File.dirname(File.dirname(Dir.getwd))
end
end
if list.empty?
@@basedir
else
File.join(@@basedir, *list)
end
end
def datadir(*list)
File.join(basedir, "test", "data", *list)
end
def exampledir(*args)
@@exampledir = File.join(basedir, "examples") unless defined?(@@exampledir)
if args.empty?
return @@exampledir
else
return File.join(@@exampledir, *args)
end
end
module_function :basedir, :datadir, :exampledir
def cleanup(&block)
@@cleaners << block
end
# Rails clobbers RUBYLIB, thanks
def libsetup
curlibs = ENV["RUBYLIB"].split(":")
$LOAD_PATH.reject do |dir| dir =~ /^\/usr/ end.each do |dir|
curlibs << dir unless curlibs.include?(dir)
end
ENV["RUBYLIB"] = curlibs.join(":")
end
def logcollector
collector = []
Puppet::Util::Log.newdestination(collector)
cleanup do
Puppet::Util::Log.close(collector)
end
collector
end
def rake?
$0 =~ /test_loader/
end
# Redirect stdout and stderr
def redirect
@stderr = tempfile
@stdout = tempfile
$stderr = File.open(@stderr, "w")
$stdout = File.open(@stdout, "w")
cleanup do
$stderr = STDERR
$stdout = STDOUT
end
end
def setup
ENV["PATH"] += File::PATH_SEPARATOR + "/usr/sbin" unless ENV["PATH"].split(File::PATH_SEPARATOR).include?("/usr/sbin")
@memoryatstart = Puppet::Util.memory
if defined?(@@testcount)
@@testcount += 1
else
@@testcount = 0
end
@configpath = File.join(
tmpdir,
"configdir" + @@testcount.to_s + "/"
)
unless defined? $user and $group
$user = nonrootuser.uid.to_s
$group = nonrootgroup.gid.to_s
end
Puppet.settings.clear
Puppet[:user] = $user
Puppet[:group] = $group
Puppet[:confdir] = @configpath
Puppet[:vardir] = @configpath
Dir.mkdir(@configpath) unless File.exists?(@configpath)
@@tmpfiles << @configpath << tmpdir
@@tmppids = []
@@cleaners = []
@logs = []
# If we're running under rake, then disable debugging and such.
#if rake? or ! Puppet[:debug]
#if defined?($puppet_debug) or ! rake?
Puppet[:color] = false if textmate?
- Puppet::Util::Log.newdestination(@logs)
+ Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(@logs))
if defined? $console
Puppet.info @method_name
Puppet::Util::Log.newdestination(:console)
Puppet[:trace] = true
end
Puppet::Util::Log.level = :debug
#$VERBOSE = 1
#else
# Puppet::Util::Log.close
# Puppet::Util::Log.newdestination(@logs)
# Puppet[:httplog] = tempfile
#end
Puppet[:ignoreschedules] = true
#@start = Time.now
#Facter.stubs(:value).returns "stubbed_value"
#Facter.stubs(:to_hash).returns({})
end
def tempfile(suffix = '')
if defined?(@@tmpfilenum)
@@tmpfilenum += 1
else
@@tmpfilenum = 1
end
f = File.join(self.tmpdir, "tempfile_" + @@tmpfilenum.to_s + suffix)
@@tmpfiles ||= []
@@tmpfiles << f
f
end
def textmate?
!!ENV["TM_FILENAME"]
end
def tstdir
dir = tempfile
Dir.mkdir(dir)
dir
end
def tmpdir
unless @tmpdir
@tmpdir = case Facter["operatingsystem"].value
when "Darwin"; "/private/tmp"
when "SunOS"; "/var/tmp"
else
"/tmp"
end
@tmpdir = File.join(@tmpdir, "puppettesting#{Process.pid}")
unless File.exists?(@tmpdir)
FileUtils.mkdir_p(@tmpdir)
File.chmod(01777, @tmpdir)
end
end
@tmpdir
end
def remove_tmp_files
@@tmpfiles.each { |file|
unless file =~ /tmp/
puts "Not deleting tmpfile #{file}"
next
end
if FileTest.exists?(file)
system("chmod -R 755 #{file}")
system("rm -rf #{file}")
end
}
@@tmpfiles.clear
end
def teardown
#@stop = Time.now
#File.open("/tmp/test_times.log", ::File::WRONLY|::File::CREAT|::File::APPEND) { |f| f.puts "%0.4f %s %s" % [@stop - @start, @method_name, self.class] }
@@cleaners.each { |cleaner| cleaner.call }
remove_tmp_files
@@tmppids.each { |pid|
%x{kill -INT #{pid} 2>/dev/null}
}
@@tmppids.clear
Puppet::Util::Storage.clear
Puppet.clear
Puppet.settings.clear
Puppet::Util::Cacher.expire
@memoryatend = Puppet::Util.memory
diff = @memoryatend - @memoryatstart
Puppet.info "#{self.class}##{@method_name} memory growth (#{@memoryatstart} to #{@memoryatend}): #{diff}" if diff > 1000
# reset all of the logs
Puppet::Util::Log.close_all
@logs.clear
# Just in case there are processes waiting to die...
require 'timeout'
begin
Timeout::timeout(5) do
Process.waitall
end
rescue Timeout::Error
# just move on
end
end
def logstore
@logs = []
- Puppet::Util::Log.newdestination(@logs)
+ Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(@logs))
end
end
require 'puppettest/support'
require 'puppettest/filetesting'
require 'puppettest/fakes'
require 'puppettest/exetest'
require 'puppettest/parsertesting'
require 'puppettest/servertest'
require 'puppettest/testcase'