summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorAdam Jacob <adam@hjksolutions.com>2008-04-08 00:57:17 -0700
committerAdam Jacob <adam@hjksolutions.com>2008-04-08 00:57:17 -0700
commit95eb1375f0647accd1b1a1569a7d0e3acc4a61e4 (patch)
treea58d6fddde4d7818dc33511ff8275c7b9f69f1c6 /spec
parent434f25ba07b5c0c50baa1e15b14a945bba3c3c3b (diff)
downloadchef-95eb1375f0647accd1b1a1569a7d0e3acc4a61e4.tar.gz
Adding chef-solo command, config examples, Chef::Log class, Chef::Log::Formatter, Chef::Compile, and all the tests
Diffstat (limited to 'spec')
-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
21 files changed, 337 insertions, 8 deletions
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