summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/chef-solo85
-rwxr-xr-xbin/marionette-dotgraph40
-rw-r--r--examples/config/chef-solo.rb7
-rw-r--r--examples/config/cookbooks/fakefile/recipes/default.rb5
-rw-r--r--examples/config/cookbooks/tempfile/recipes/default.rb5
-rw-r--r--examples/config/nodes/latte.rb14
-rw-r--r--lib/chef/compile.rb62
-rw-r--r--lib/chef/config.rb5
-rw-r--r--lib/chef/cookbook.rb6
-rw-r--r--lib/chef/log.rb93
-rw-r--r--lib/chef/log/formatter.rb52
-rw-r--r--lib/chef/node.rb62
-rw-r--r--lib/chef/recipe.rb1
-rw-r--r--lib/chef/resource_collection.rb3
-rw-r--r--spec/data/compile/cookbooks/test/attributes/george.rb1
-rw-r--r--spec/data/compile/cookbooks/test/definitions/new_cat.rb5
-rw-r--r--spec/data/compile/cookbooks/test/recipes/one.rb7
-rw-r--r--spec/data/compile/cookbooks/test/recipes/two.rb7
-rw-r--r--spec/data/compile/nodes/compile.rb5
-rw-r--r--spec/data/cookbooks/openldap/attributes/default.rb2
-rw-r--r--spec/data/cookbooks/openldap/definitions/client.rb2
-rw-r--r--spec/data/cookbooks/openldap/definitions/server.rb2
-rw-r--r--spec/data/cookbooks/openldap/recipes/one.rb15
-rw-r--r--spec/data/nodes/default.rb15
-rw-r--r--spec/data/nodes/test.example.com.rb15
-rw-r--r--spec/data/nodes/test.rb2
-rw-r--r--spec/rcov.opts2
-rw-r--r--spec/spec_helper.rb1
-rw-r--r--spec/unit/compile_spec.rb69
-rw-r--r--spec/unit/cookbook_spec.rb9
-rw-r--r--spec/unit/log/formatter_spec.rb47
-rw-r--r--spec/unit/log_spec.rb64
-rw-r--r--spec/unit/node_spec.rb56
-rw-r--r--spec/unit/recipe_spec.rb2
-rw-r--r--spec/unit/resource_collection_spec.rb17
35 files changed, 735 insertions, 50 deletions
diff --git a/bin/chef-solo b/bin/chef-solo
new file mode 100755
index 0000000000..0d49414f5a
--- /dev/null
+++ b/bin/chef-solo
@@ -0,0 +1,85 @@
+#!/usr/bin/ruby
+#
+# ./chef-solo - Build a meal with chef, sans-server!
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+$: << File.join(File.dirname(__FILE__), "..", "lib")
+
+require 'optparse'
+require 'chef'
+require 'rubygems'
+require 'facter'
+require 'yaml'
+
+config = {
+ :config_file => "/etc/chef/config.rb",
+ :log_level => :info,
+ :noop => false
+}
+opts = OptionParser.new do |opts|
+ opts.banner = "Usage: #{$0} [-d DIR|-r FILE] (options)"
+ opts.on("-c CONFIG", "--config CONFIG", "The Chef Config file to use") do |c|
+ config[:config_file] = c
+ end
+ opts.on("-n", "--noop", "Print what you would do, but don't actually do it.") do
+ config[:noop] = true
+ end
+ opts.on_tail("-l LEVEL", "--loglevel LEVEL", "Set the log level (debug, info, warn, error, fatal)") do |l|
+ config[:log_level] = l.to_sym
+ end
+ opts.on_tail("-h", "--help", "Show this message") do
+ puts opts
+ exit
+ end
+end
+opts.parse!(ARGV)
+
+unless File.exists?(config[:config_file]) && File.readable?(config[:config_file])
+ puts "I cannot find or read the config file: #{config[:config_file]}"
+ puts opts
+ exit
+end
+
+# Load our config file
+Chef::Config.from_file(config[:config_file])
+
+# Find out our own hostname.
+node_name = Facter["fqdn"].value
+node_name ||= Facter["hostname"].value
+
+# Grab a Chef::Compile object
+compile = Chef::Compile.new()
+
+# Load our Node, and then add all the Facter facts as attributes
+compile.load_node(node_name)
+Facter.each do |field, value|
+ compile.node[field.to_sym] = value
+end
+
+puts compile.node.to_yaml
+
+compile.load_definitions
+puts compile.definitions.to_yaml
+
+compile.load_recipes
+puts compile.resource_collection.to_yaml
+
+puts compile.to_yaml
diff --git a/bin/marionette-dotgraph b/bin/marionette-dotgraph
deleted file mode 100755
index 75c33da274..0000000000
--- a/bin/marionette-dotgraph
+++ /dev/null
@@ -1,40 +0,0 @@
-#!/usr/bin/ruby
-#
-# Author:: Adam Jacob (<adam@hjksolutions.com>)
-# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
-# License:: GNU General Public License version 2 or later
-#
-# This program and entire repository is free software; you can
-# redistribute it and/or modify it under the terms of the GNU
-# General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-#
-
-require File.join(File.dirname(__FILE__), "..", "lib", "chef")
-require 'rgl/dot'
-
-mr = Chef::Recipe.new("test", "default", "node")
-raise ArgumentError, "#{ARGV[0]} is not a file!" unless File.exists?(ARGV[0])
-mr.instance_eval(IO.read(ARGV[0]), ARGV[0], 1)
-puts mr.inspect
-puts mr.dg.directed?
-puts mr.dg.acyclic?
-tree = Array.new
-mr.dg.depth_first_visit(:top) do |v|
- tree << v
-end
-maybe = mr.dg.bfs_search_tree_from(:top)
-puts maybe.acyclic?
-puts maybe.directed?
-puts maybe.topsort_iterator.to_a.join("\n")
-maybe.write_to_graphic_file
-
diff --git a/examples/config/chef-solo.rb b/examples/config/chef-solo.rb
new file mode 100644
index 0000000000..1e3bfd342e
--- /dev/null
+++ b/examples/config/chef-solo.rb
@@ -0,0 +1,7 @@
+#
+# Example Chef Solo Config
+
+cookbook_path File.join(File.dirname(__FILE__), "cookbooks")
+node_path File.join(File.dirname(__FILE__), "nodes")
+log_level :debug
+
diff --git a/examples/config/cookbooks/fakefile/recipes/default.rb b/examples/config/cookbooks/fakefile/recipes/default.rb
new file mode 100644
index 0000000000..2f91563bfe
--- /dev/null
+++ b/examples/config/cookbooks/fakefile/recipes/default.rb
@@ -0,0 +1,5 @@
+file "foo" do
+ owner "adam"
+ mode 644
+ action "create"
+end
diff --git a/examples/config/cookbooks/tempfile/recipes/default.rb b/examples/config/cookbooks/tempfile/recipes/default.rb
new file mode 100644
index 0000000000..f0888ca550
--- /dev/null
+++ b/examples/config/cookbooks/tempfile/recipes/default.rb
@@ -0,0 +1,5 @@
+file "glen" do
+ owner "glen"
+ mode 644
+ action "create"
+end
diff --git a/examples/config/nodes/latte.rb b/examples/config/nodes/latte.rb
new file mode 100644
index 0000000000..8d56684a61
--- /dev/null
+++ b/examples/config/nodes/latte.rb
@@ -0,0 +1,14 @@
+##
+# Nodes should have a unique name
+##
+name "latte"
+
+##
+# Nodes can set arbitrary arguments
+##
+owner "Adam Jacob"
+
+##
+# Nodes should have recipes
+##
+recipes "tempfile", "fakefile"
diff --git a/lib/chef/compile.rb b/lib/chef/compile.rb
new file mode 100644
index 0000000000..5b1f9d756e
--- /dev/null
+++ b/lib/chef/compile.rb
@@ -0,0 +1,62 @@
+#
+# Chef::Compile
+#
+# Compile a nodes resources.
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+class Chef
+ class Compile
+
+ attr_accessor :node, :cookbook_loader, :resource_collection, :definitions
+
+ def initialize()
+ @node = nil
+ @cookbook_loader = Chef::CookbookLoader.new
+ @resource_collection = Chef::ResourceCollection.new
+ @definitions = Hash.new
+ end
+
+ def load_node(name)
+ @node = Chef::Node.find(name)
+ end
+
+ def load_definitions()
+ @cookbook_loader.each do |cookbook|
+ hash = cookbook.load_definitions
+ @definitions.merge!(hash)
+ end
+ end
+
+ def load_recipes
+ @node.recipes.each do |recipe|
+ rmatch = recipe.match(/(.+?)::(.+)/)
+ if rmatch
+ cookbook = @cookbook_loader[rmatch[1]]
+ cookbook.load_recipe(rmatch[2], @node, @resource_collection, @definitions, @cookbook_loader)
+ else
+ cookbook = @cookbook_loader[recipe]
+ cookbook.load_recipe("default", @node, @resource_collection, @definitions, @cookbook_loader)
+ end
+ end
+ end
+
+ end
+end \ No newline at end of file
diff --git a/lib/chef/config.rb b/lib/chef/config.rb
index 191de2504c..4793011fb4 100644
--- a/lib/chef/config.rb
+++ b/lib/chef/config.rb
@@ -37,7 +37,10 @@ class Chef
include Chef::Mixin::CheckHelper
@configuration = {
- :cookbook_path => [ "/etc/chef/site-cookbook", "/etc/chef/cookbook" ]
+ :cookbook_path => [ "/etc/chef/site-cookbook", "/etc/chef/cookbook" ],
+ :node_path => "/etc/chef/node",
+ :log_level => :info,
+ :log_location => STDOUT
}
class << self
diff --git a/lib/chef/cookbook.rb b/lib/chef/cookbook.rb
index 218ecb3c10..39013069ca 100644
--- a/lib/chef/cookbook.rb
+++ b/lib/chef/cookbook.rb
@@ -30,6 +30,7 @@ class Chef
@definition_files = Array.new
@recipe_files = Array.new
@recipe_names = Hash.new
+ @loaded_attributes = false
end
def load_attributes(node)
@@ -39,6 +40,7 @@ class Chef
@attribute_files.each do |file|
node.from_file(file)
end
+ @loaded_atributes = true
node
end
@@ -95,6 +97,10 @@ class Chef
unless @recipe_names.has_key?(recipe_name)
raise ArgumentError, "Cannot find a recipe matching #{recipe_name} in cookbook #{@name}"
end
+ Chef::Log.debug("Found recipe #{recipe_name} in cookbook #{cookbook_name}") if Chef::Log.debug?
+ unless @loaded_attributes
+ load_attributes(node)
+ end
recipe = Chef::Recipe.new(cookbook_name, recipe_name, node,
collection, definitions, cookbook_loader)
recipe.from_file(@recipe_files[@recipe_names[recipe_name]])
diff --git a/lib/chef/log.rb b/lib/chef/log.rb
new file mode 100644
index 0000000000..cf8b0b45e9
--- /dev/null
+++ b/lib/chef/log.rb
@@ -0,0 +1,93 @@
+#
+# Chef::Logger
+#
+# A simple wrapper for the standard Ruby Logger.
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+require 'logger'
+
+class Chef
+ class Log
+
+ @logger = nil
+
+ class << self
+ attr_reader :logger #:nodoc
+
+ # Use Chef::Logger.init when you want to set up the logger manually. Arguments to this method
+ # get passed directly to Logger.new, so check out the documentation for the standard Logger class
+ # to understand what to do here.
+ #
+ # If this method is called with no arguments, it will log to STDOUT at the :info level.
+ #
+ # It also configures the Logger instance it creates to use the custom Chef::Log::Formatter class.
+ def init(*opts)
+ if opts.length == 0
+ @logger = Logger.new(STDOUT)
+ else
+ @logger = Logger.new(*opts)
+ end
+ @logger.formatter = Chef::Log::Formatter.new()
+ level(Chef::Config.log_level)
+ end
+
+ # Sets the level for the Logger object by symbol. Valid arguments are:
+ #
+ # :debug
+ # :info
+ # :warn
+ # :error
+ # :fatal
+ #
+ # Throws an ArgumentError if you feed it a bogus log level.
+ def level(loglevel)
+ init() unless @logger
+ case loglevel
+ when :debug
+ @logger.level = Logger::DEBUG
+ when :info
+ @logger.level = Logger::INFO
+ when :warn
+ @logger.level = Logger::WARN
+ when :error
+ @logger.level = Logger::ERROR
+ when :fatal
+ @logger.level = Logger::FATAL
+ else
+ raise ArgumentError, "Log level must be one of :debug, :info, :warn, :error, or :fatal"
+ end
+ end
+
+ # Passes any other method calls on directly to the underlying Logger object created with init. If
+ # this method gets hit before a call to Chef::Logger.init has been made, it will call
+ # Chef::Logger.init() with no arguments.
+ def method_missing(method_symbol, *args)
+ init() unless @logger
+ if args.length > 0
+ @logger.send(method_symbol, *args)
+ else
+ @logger.send(method_symbol)
+ end
+ end
+
+ end # class << self
+ end
+end \ No newline at end of file
diff --git a/lib/chef/log/formatter.rb b/lib/chef/log/formatter.rb
new file mode 100644
index 0000000000..d177060d97
--- /dev/null
+++ b/lib/chef/log/formatter.rb
@@ -0,0 +1,52 @@
+#
+# Chef::Log::Formatter
+#
+# A custom Logger::Formatter implementation for Chef.
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+require 'logger'
+require 'time'
+
+class Chef
+ class Log
+ class Formatter < Logger::Formatter
+ # Prints a log message as '[time] severity: message'
+ def call(severity, time, progname, msg)
+ sprintf("[%s] %s: %s\n", time.rfc2822(), severity, msg2str(msg))
+ end
+
+ # Converts some argument to a Logger.severity() call to a string. Regular strings pass through like
+ # normal, Exceptions get formatted as "message (class)\nbacktrace", and other random stuff gets
+ # put through "object.inspect"
+ def msg2str(msg)
+ case msg
+ when ::String
+ msg
+ when ::Exception
+ "#{ msg.message } (#{ msg.class })\n" <<
+ (msg.backtrace || []).join("\n")
+ else
+ msg.inspect
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/chef/node.rb b/lib/chef/node.rb
index d5896fbd95..7cb42effd0 100644
--- a/lib/chef/node.rb
+++ b/lib/chef/node.rb
@@ -1,7 +1,6 @@
#
# Chef::Node
#
-#
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: GNU General Public License version 2 or later
@@ -24,6 +23,9 @@
require File.join(File.dirname(__FILE__), "mixin", "check_helper")
require File.join(File.dirname(__FILE__), "mixin", "from_file")
+require 'rubygems'
+require 'json'
+
class Chef
class Node
@@ -39,6 +41,42 @@ class Chef
@recipe_list = Array.new
end
+ # Find a Chef::Node by fqdn. Will search first for Chef::Config["node_path"]/fqdn.rb, then
+ # hostname.rb, then default.rb.
+ #
+ # Returns a new Chef::Node object.
+ #
+ # Raises an ArgumentError if it cannot find the node.
+ def self.find(fqdn)
+ node_file = nil
+ host_parts = fqdn.split(".")
+ hostname = host_parts[0]
+
+ if File.exists?(File.join(Chef::Config[:node_path], "#{fqdn}.rb"))
+ node_file = File.join(Chef::Config[:node_path], "#{fqdn}.rb")
+ elsif File.exists?(File.join(Chef::Config[:node_path], "#{hostname}.rb"))
+ node_file = File.join(Chef::Config[:node_path], "#{hostname}.rb")
+ elsif File.exists?(File.join(Chef::Config[:node_path], "default.rb"))
+ node_file = File.join(Chef::Config[:node_path], "default.rb")
+ else
+ raise ArgumentError, "Cannot find a node matching #{fqdn}, not even with default.rb!"
+ end
+ chef_node = Chef::Node.new()
+ chef_node.from_file(node_file)
+ chef_node
+ end
+
+ # Returns an array of nodes available, based on the list of files present.
+ def self.list
+ results = Array.new
+ Dir[File.join(Chef::Config[:node_path], "*.rb")].sort.each do |file|
+ mr = file.match(/^.+\/(.+)\.rb$/)
+ node_name = mr[1]
+ results << node_name
+ end
+ results
+ end
+
# Set the name of this Node, or return the current name.
def name(arg=nil)
set_if_args(@name, arg) do |a|
@@ -62,6 +100,11 @@ class Chef
end
end
+ # Set an attribute of this node
+ def []=(attrib, value)
+ @attribute[attrib] = value
+ end
+
# Iterates over each attribute, passing the attribute and value to the block.
def each_attribute(&block)
@attribute.each do |k,v|
@@ -108,5 +151,22 @@ class Chef
end
end
+ # Serialize this Node as json
+ def to_json()
+ result_object = {
+ "name" => @name,
+ "type" => "Chef::Node",
+ "attributes" => Hash.new,
+ "recipes" => Array.new
+ }
+ each_attribute do |a,v|
+ result_object["attributes"][a] = v
+ end
+ recipes.each do |r|
+ result_object["recipes"] << r
+ end
+ result_object.to_json
+ end
+
end
end \ No newline at end of file
diff --git a/lib/chef/recipe.rb b/lib/chef/recipe.rb
index af1cbd2d77..d7d1435eb4 100644
--- a/lib/chef/recipe.rb
+++ b/lib/chef/recipe.rb
@@ -81,6 +81,7 @@ class Chef
new_def.instance_eval(&block)
new_recipe = Chef::Recipe.new(@cookbook_name, @recipe_name, @node, @collection, @definitions, @cookbook_loader)
new_recipe.params = new_def.params
+ new_recipe.params[:name] = args[0]
new_recipe.instance_eval(&new_def.recipe)
else
method_name = method_symbol.to_s
diff --git a/lib/chef/resource_collection.rb b/lib/chef/resource_collection.rb
index dc93cfb4fa..5728fdd3e9 100644
--- a/lib/chef/resource_collection.rb
+++ b/lib/chef/resource_collection.rb
@@ -33,6 +33,7 @@ class Chef
def []=(index, arg)
is_chef_resource(arg)
+ raise ArgumentError, "Already have a resource named #{arg.to_s}" if @resources_by_name.has_key?(arg.to_s)
@resources[index] = arg
@resources_by_name[arg.to_s] = index
end
@@ -40,6 +41,7 @@ class Chef
def <<(*args)
args.flatten.each do |a|
is_chef_resource(a)
+ raise ArgumentError, "Already have a resource named #{a.to_s}" if @resources_by_name.has_key?(a.to_s)
@resources << a
@resources_by_name[a.to_s] = @resources.length - 1
end
@@ -48,6 +50,7 @@ class Chef
def push(*args)
args.flatten.each do |a|
is_chef_resource(a)
+ raise ArgumentError, "Already have a resource named #{a.to_s}" if @resources_by_name.has_key?(a.to_s)
@resources.push(a)
@resources_by_name[a.to_s] = @resources.length - 1
end
diff --git a/spec/data/compile/cookbooks/test/attributes/george.rb b/spec/data/compile/cookbooks/test/attributes/george.rb
new file mode 100644
index 0000000000..5df9567761
--- /dev/null
+++ b/spec/data/compile/cookbooks/test/attributes/george.rb
@@ -0,0 +1 @@
+george "washington" \ No newline at end of file
diff --git a/spec/data/compile/cookbooks/test/definitions/new_cat.rb b/spec/data/compile/cookbooks/test/definitions/new_cat.rb
new file mode 100644
index 0000000000..a49b53348c
--- /dev/null
+++ b/spec/data/compile/cookbooks/test/definitions/new_cat.rb
@@ -0,0 +1,5 @@
+define :new_cat, :is_pretty => true do
+ cat "#{params[:name]}" do
+ pretty_kitty params[:is_pretty]
+ end
+end
diff --git a/spec/data/compile/cookbooks/test/recipes/one.rb b/spec/data/compile/cookbooks/test/recipes/one.rb
new file mode 100644
index 0000000000..7795cc1d4a
--- /dev/null
+++ b/spec/data/compile/cookbooks/test/recipes/one.rb
@@ -0,0 +1,7 @@
+cat "loulou" do
+ pretty_kitty true
+end
+
+new_cat "birthday" do
+ pretty_kitty false
+end
diff --git a/spec/data/compile/cookbooks/test/recipes/two.rb b/spec/data/compile/cookbooks/test/recipes/two.rb
new file mode 100644
index 0000000000..01def1b2ac
--- /dev/null
+++ b/spec/data/compile/cookbooks/test/recipes/two.rb
@@ -0,0 +1,7 @@
+cat "peanut" do
+ pretty_kitty true
+end
+
+new_cat "fat peanut" do
+ pretty_kitty false
+end
diff --git a/spec/data/compile/nodes/compile.rb b/spec/data/compile/nodes/compile.rb
new file mode 100644
index 0000000000..7f0664d5d6
--- /dev/null
+++ b/spec/data/compile/nodes/compile.rb
@@ -0,0 +1,5 @@
+##
+# Nodes should have a unique name
+##
+name "compile"
+recipes "test::one", "test::two"
diff --git a/spec/data/cookbooks/openldap/attributes/default.rb b/spec/data/cookbooks/openldap/attributes/default.rb
index 8c00028021..204ae9ed77 100644
--- a/spec/data/cookbooks/openldap/attributes/default.rb
+++ b/spec/data/cookbooks/openldap/attributes/default.rb
@@ -1,4 +1,4 @@
-
+chef_env ||= nil
case chef_env
when "prod"
ldap_server "ops1prod"
diff --git a/spec/data/cookbooks/openldap/definitions/client.rb b/spec/data/cookbooks/openldap/definitions/client.rb
index 37364925fb..ac81831d11 100644
--- a/spec/data/cookbooks/openldap/definitions/client.rb
+++ b/spec/data/cookbooks/openldap/definitions/client.rb
@@ -1,5 +1,5 @@
define :openldap_client, :mothra => "a big monster" do
- cat "#{param[:name]}" do
+ cat "#{params[:name]}" do
pretty_kitty true
end
end
diff --git a/spec/data/cookbooks/openldap/definitions/server.rb b/spec/data/cookbooks/openldap/definitions/server.rb
index f189f67ca8..2df437aa84 100644
--- a/spec/data/cookbooks/openldap/definitions/server.rb
+++ b/spec/data/cookbooks/openldap/definitions/server.rb
@@ -1,5 +1,5 @@
define :openldap_server, :mothra => "a big monster" do
- cat "#{param[:name]}" do
+ cat "#{params[:name]}" do
pretty_kitty true
end
end
diff --git a/spec/data/cookbooks/openldap/recipes/one.rb b/spec/data/cookbooks/openldap/recipes/one.rb
new file mode 100644
index 0000000000..e1c3cff92e
--- /dev/null
+++ b/spec/data/cookbooks/openldap/recipes/one.rb
@@ -0,0 +1,15 @@
+##
+# Nodes should have a unique name
+##
+name "test.example.com default"
+
+##
+# Nodes can set arbitrary arguments
+##
+sunshine "in"
+something "else"
+
+##
+# Nodes should have recipes
+##
+recipes "operations-master", "operations-monitoring"
diff --git a/spec/data/nodes/default.rb b/spec/data/nodes/default.rb
new file mode 100644
index 0000000000..e1c3cff92e
--- /dev/null
+++ b/spec/data/nodes/default.rb
@@ -0,0 +1,15 @@
+##
+# Nodes should have a unique name
+##
+name "test.example.com default"
+
+##
+# Nodes can set arbitrary arguments
+##
+sunshine "in"
+something "else"
+
+##
+# Nodes should have recipes
+##
+recipes "operations-master", "operations-monitoring"
diff --git a/spec/data/nodes/test.example.com.rb b/spec/data/nodes/test.example.com.rb
new file mode 100644
index 0000000000..9c374395bf
--- /dev/null
+++ b/spec/data/nodes/test.example.com.rb
@@ -0,0 +1,15 @@
+##
+# Nodes should have a unique name
+##
+name "test.example.com"
+
+##
+# Nodes can set arbitrary arguments
+##
+sunshine "in"
+something "else"
+
+##
+# Nodes should have recipes
+##
+recipes "operations-master", "operations-monitoring"
diff --git a/spec/data/nodes/test.rb b/spec/data/nodes/test.rb
index d1288779f6..d968816d60 100644
--- a/spec/data/nodes/test.rb
+++ b/spec/data/nodes/test.rb
@@ -1,7 +1,7 @@
##
# Nodes should have a unique name
##
-name "ops1prod"
+name "test.example.com short"
##
# Nodes can set arbitrary arguments
diff --git a/spec/rcov.opts b/spec/rcov.opts
index 88fa154776..484626ea9c 100644
--- a/spec/rcov.opts
+++ b/spec/rcov.opts
@@ -1,2 +1,2 @@
--exclude
-spec,bin,/Library/Ruby/Gems/1.8/gems/rcov-0.8.1.2.0/lib/rcov.rb
+spec,bin,/Library/Ruby
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index a57179cdd2..05dbd247ae 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -20,3 +20,4 @@
require File.join(File.dirname(__FILE__), "..", "lib", "chef")
Dir[File.join(File.dirname(__FILE__), 'lib', '**', '*.rb')].sort.each { |lib| require lib }
+Chef::Config.log_level(:fatal)
diff --git a/spec/unit/compile_spec.rb b/spec/unit/compile_spec.rb
new file mode 100644
index 0000000000..400d768398
--- /dev/null
+++ b/spec/unit/compile_spec.rb
@@ -0,0 +1,69 @@
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
+describe Chef::Compile do
+ before(:each) do
+ Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "compile", "nodes"))
+ Chef::Config.cookbook_path(File.join(File.dirname(__FILE__), "..", "data", "compile", "cookbooks"))
+ @compile = Chef::Compile.new
+ end
+
+ it "should create a new Chef::Compile" do
+ @compile.should be_a_kind_of(Chef::Compile)
+ end
+
+ it "should have a Chef::CookbookLoader" do
+ @compile.cookbook_loader.should be_a_kind_of(Chef::CookbookLoader)
+ end
+
+ it "should have a Chef::ResourceCollection" do
+ @compile.resource_collection.should be_a_kind_of(Chef::ResourceCollection)
+ end
+
+ it "should have a hash of Definitions" do
+ @compile.definitions.should be_a_kind_of(Hash)
+ end
+
+ it "should load a node by name" do
+ lambda {
+ @compile.load_node("compile")
+ }.should_not raise_error
+ @compile.node.name.should == "compile"
+ end
+
+ it "should load all the definitions" do
+ lambda { @compile.load_definitions }.should_not raise_error
+ @compile.definitions.should have_key(:new_cat)
+ end
+
+ it "should load all the recipes specified for this node" do
+ @compile.load_node("compile")
+ @compile.load_definitions
+ lambda { @compile.load_recipes }.should_not raise_error
+ puts @compile.resource_collection.inspect
+
+ @compile.resource_collection[0].to_s.should == "cat[loulou]"
+ @compile.resource_collection[1].to_s.should == "cat[birthday]"
+ @compile.resource_collection[2].to_s.should == "cat[peanut]"
+ @compile.resource_collection[3].to_s.should == "cat[fat peanut]"
+ end
+
+end \ No newline at end of file
diff --git a/spec/unit/cookbook_spec.rb b/spec/unit/cookbook_spec.rb
index f702fe32c1..15db9ca3d1 100644
--- a/spec/unit/cookbook_spec.rb
+++ b/spec/unit/cookbook_spec.rb
@@ -132,4 +132,13 @@ describe Chef::Cookbook do
node.name "Julia Child"
lambda { @cookbook.load_recipe("smackdown", node) }.should raise_error(ArgumentError)
end
+
+ it "should load the attributes if it has not already when a recipe is loaded" do
+ @cookbook.attribute_files = Dir[File.join(COOKBOOK_PATH, "attributes", "smokey.rb")]
+ @cookbook.recipe_files = Dir[File.join(COOKBOOK_PATH, "recipes", "**", "*.rb")]
+ node = Chef::Node.new
+ node.name "Julia Child"
+ recipe = @cookbook.load_recipe("openldap::gigantor", node)
+ node.smokey.should == "robinson"
+ end
end \ No newline at end of file
diff --git a/spec/unit/log/formatter_spec.rb b/spec/unit/log/formatter_spec.rb
new file mode 100644
index 0000000000..e7518ead87
--- /dev/null
+++ b/spec/unit/log/formatter_spec.rb
@@ -0,0 +1,47 @@
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+require 'time'
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
+
+describe Chef::Log::Formatter do
+ before(:each) do
+ @formatter = Chef::Log::Formatter.new
+ end
+
+ it "should print raw strings with msg2str(string)" do
+ @formatter.msg2str("nuthin new").should == "nuthin new"
+ end
+
+ it "should format exceptions properly with msg2str(e)" do
+ e = IOError.new("legendary roots crew")
+ @formatter.msg2str(e).should == "legendary roots crew (IOError)\n"
+ end
+
+ it "should format random objects via inspect with msg2str(Object)" do
+ @formatter.msg2str([ "black thought", "?uestlove" ]).should == '["black thought", "?uestlove"]'
+ end
+
+ it "should return a formatted string with call" do
+ time = Time.new
+ @formatter.call("monkey", Time.new, "test", "mos def").should == "[#{time.rfc2822}] monkey: mos def\n"
+ end
+
+end \ No newline at end of file
diff --git a/spec/unit/log_spec.rb b/spec/unit/log_spec.rb
new file mode 100644
index 0000000000..45e0da6dd5
--- /dev/null
+++ b/spec/unit/log_spec.rb
@@ -0,0 +1,64 @@
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# License:: GNU General Public License version 2 or later
+#
+# This program and entire repository is free software; you can
+# redistribute it and/or modify it under the terms of the GNU
+# General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+require 'tempfile'
+require 'logger'
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
+
+describe Chef::Log do
+ it "should accept regular options to Logger.new via init" do
+ tf = Tempfile.new("chef-test-log")
+ tf.open
+ lambda { Chef::Log.init(STDOUT) }.should_not raise_error
+ lambda { Chef::Log.init(tf) }.should_not raise_error
+ end
+
+ it "should set the log level with :debug, :info, :warn, :error, or :fatal" do
+ levels = {
+ :debug => Logger::DEBUG,
+ :info => Logger::INFO,
+ :warn => Logger::WARN,
+ :error => Logger::ERROR,
+ :fatal => Logger::FATAL
+ }
+ levels.each do |symbol, constant|
+ Chef::Log.level(symbol)
+ Chef::Log.logger.level.should == constant
+ end
+ end
+
+ it "should raise an ArgumentError if you try and set the level to something strange" do
+ lambda { Chef::Log.level(:the_roots) }.should raise_error(ArgumentError)
+ end
+
+ it "should pass other method calls directly to logger" do
+ Chef::Log.level(:debug)
+ Chef::Log.should be_debug
+ lambda { Chef::Log.debug("Gimme some sugar!") }.should_not raise_error
+ end
+
+ it "should default to STDOUT if init is called with no arguments" do
+ logger_mock = mock(Logger, :null_object => true)
+ Logger.stub!(:new).and_return(logger_mock)
+ Logger.should_receive(:new).with(STDOUT).and_return(logger_mock)
+ Chef::Log.init
+ end
+
+end \ No newline at end of file
diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb
index a8c456e8f5..c72aea0b7d 100644
--- a/spec/unit/node_spec.rb
+++ b/spec/unit/node_spec.rb
@@ -22,6 +22,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::Node do
before(:each) do
+ Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
@node = Chef::Node.new()
end
@@ -56,6 +57,11 @@ describe Chef::Node do
@node["secret"].should eql(nil)
end
+ it "should allow you to set an attribute via node[]=" do
+ @node["secret"] = "shush"
+ @node["secret"].should eql("shush")
+ end
+
it "should allow you to query whether an attribute exists with attribute?" do
@node.attribute["locust"] = "something"
@node.attribute?("locust").should eql(true)
@@ -94,7 +100,7 @@ describe Chef::Node do
it "should load a node from a ruby file" do
@node.from_file(File.join(File.dirname(__FILE__), "..", "data", "nodes", "test.rb"))
- @node.name.should eql("ops1prod")
+ @node.name.should eql("test.example.com short")
@node.sunshine.should eql("in")
@node.something.should eql("else")
@node.recipes.should eql(["operations-master", "operations-monitoring"])
@@ -116,5 +122,53 @@ describe Chef::Node do
seen_attributes[:sunshine].should == "is bright"
seen_attributes[:canada].should == "is a nice place"
end
+
+ it "should load a node from a file by fqdn" do
+ node = Chef::Node.find("test.example.com")
+ node.name.should == "test.example.com"
+ end
+
+ it "should load a node from a file by hostname" do
+ File.stub!(:exists?).and_return(true)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.example.com.rb")).and_return(false)
+ node = Chef::Node.find("test.example.com")
+ node.name.should == "test.example.com short"
+ end
+
+ it "should load a node from the default file" do
+ File.stub!(:exists?).and_return(true)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.example.com.rb")).and_return(false)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.rb")).and_return(false)
+ node = Chef::Node.find("test.example.com")
+ node.name.should == "test.example.com default"
+ end
+
+ it "should raise an ArgumentError if it cannot find any node file at all" do
+ File.stub!(:exists?).and_return(true)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.example.com.rb")).and_return(false)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.rb")).and_return(false)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "default.rb")).and_return(false)
+ lambda { Chef::Node.find("test.example.com") }.should raise_error(ArgumentError)
+ end
+
+ it "should serialize itself as json" do
+ node = Chef::Node.find("test.example.com")
+ json = node.to_json
+ result = JSON.load(json)
+ result["name"].should == "test.example.com"
+ result["type"].should == "Chef::Node"
+ result["attributes"]["something"].should == "else"
+ result["attributes"]["sunshine"].should == "in"
+ result["recipes"].detect { |r| r == "operations-master" }.should == "operations-master"
+ result["recipes"].detect { |r| r == "operations-monitoring" }.should == "operations-monitoring"
+ end
+
+ it "should return a list of node names based on which files are in the node_path" do
+ list = Chef::Node.list
+ list.should be_a_kind_of(Array)
+ list[0].should == "default"
+ list[1].should == "test.example.com"
+ list[2].should == "test"
+ end
end \ No newline at end of file
diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb
index 3c33a6d485..92434eac3d 100644
--- a/spec/unit/recipe_spec.rb
+++ b/spec/unit/recipe_spec.rb
@@ -22,7 +22,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::Recipe do
before(:each) do
- @recipe = Chef::Recipe.new("hjk", "test", "node")
+ @recipe = Chef::Recipe.new("hjk", "test", Chef::Node.new)
end
it "should load a two word (zen_master) resource" do
diff --git a/spec/unit/resource_collection_spec.rb b/spec/unit/resource_collection_spec.rb
index 10f7d43cde..10f251aaac 100644
--- a/spec/unit/resource_collection_spec.rb
+++ b/spec/unit/resource_collection_spec.rb
@@ -30,16 +30,26 @@ describe Chef::ResourceCollection do
@rc.should be_kind_of(Chef::ResourceCollection)
end
- it "should accept Chef::Resources" do
+ it "should accept Chef::Resources through [index]" do
lambda { @rc[0] = @resource }.should_not raise_error
lambda { @rc[0] = "string" }.should raise_error
end
+ it "should not accept duplicate resources [index]=" do
+ @rc[0] = @resource
+ lambda { @rc[1] = @resource }.should raise_error(ArgumentError)
+ end
+
it "should accept Chef::Resources through pushing" do
lambda { @rc.push(@resource) }.should_not raise_error
lambda { @rc.push("string") }.should raise_error
end
+ it "should not accept duplicate resources through pushing" do
+ lambda { @rc.push(@resource) }.should_not raise_error
+ lambda { @rc.push(@resource) }.should raise_error(ArgumentError)
+ end
+
it "should allow you to fetch Chef::Resources by position" do
@rc[0] = @resource
@rc[0].should eql(@resource)
@@ -49,6 +59,11 @@ describe Chef::ResourceCollection do
lambda { @rc << @resource }.should_not raise_error
end
+ it "should not accept duplicate resources through the << operator" do
+ lambda { @rc << @resource }.should_not raise_error
+ lambda { @rc << @resource }.should raise_error(ArgumentError)
+ end
+
it "should allow you to iterate over every resource in the collection" do
load_up_resources
results = Array.new