summaryrefslogtreecommitdiff
path: root/spec/unit
diff options
context:
space:
mode:
authorAdam Jacob <adam@hjksolutions.com>2008-04-07 18:14:25 -0700
committerAdam Jacob <adam@hjksolutions.com>2008-04-07 18:14:25 -0700
commit434f25ba07b5c0c50baa1e15b14a945bba3c3c3b (patch)
tree03085c689549a62d546d87e6484dde8756da2d23 /spec/unit
parentf543b509ba61dd347512e8a9e3153a49a2a8cb6b (diff)
downloadchef-434f25ba07b5c0c50baa1e15b14a945bba3c3c3b.tar.gz
Adding the Params::Validate mixin, refactored Chef::Config to be a singleton, Implemented require_recipe
Diffstat (limited to 'spec/unit')
-rw-r--r--spec/unit/chef_spec.rb2
-rw-r--r--spec/unit/config_spec.rb51
-rw-r--r--spec/unit/cookbook_loader_spec.rb9
-rw-r--r--spec/unit/cookbook_spec.rb2
-rw-r--r--spec/unit/mixin/params_validate_spec.rb298
-rw-r--r--spec/unit/node_spec.rb14
-rw-r--r--spec/unit/provider/file_spec.rb2
-rw-r--r--spec/unit/provider_spec.rb2
-rw-r--r--spec/unit/recipe_spec.rb22
-rw-r--r--spec/unit/resource/file_spec.rb2
-rw-r--r--spec/unit/resource_collection_spec.rb2
-rw-r--r--spec/unit/resource_definition_spec.rb2
-rw-r--r--spec/unit/resource_spec.rb2
13 files changed, 376 insertions, 34 deletions
diff --git a/spec/unit/chef_spec.rb b/spec/unit/chef_spec.rb
index a75e3ad548..cfb6cd4dac 100644
--- a/spec/unit/chef_spec.rb
+++ b/spec/unit/chef_spec.rb
@@ -18,7 +18,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef do
it "should have a version defined" do
diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb
index dec94796e0..4148336078 100644
--- a/spec/unit/config_spec.rb
+++ b/spec/unit/config_spec.rb
@@ -18,55 +18,70 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::Config do
- before(:each) do
- @config = Chef::Config.new
- end
it "should load a .rb file in context" do
lambda {
- Chef::Config.load_file(File.join(File.dirname(__FILE__), "..", "data", "config.rb"))
+ Chef::Config.from_file(File.join(File.dirname(__FILE__), "..", "data", "config.rb"))
}.should_not raise_error
end
- it "should raise a NoMethodError with an explanation if you have a bad config file" do
+ it "should raise an ArgumentError with an explanation if you try and set a non-existent variable" do
lambda {
- Chef::Config.load_file(File.join(File.dirname(__FILE__), "..", "data", "bad-config.rb"))
- }.should raise_error(NoMethodError)
+ Chef::Config.from_file(File.join(File.dirname(__FILE__), "..", "data", "bad-config.rb"))
+ }.should raise_error(ArgumentError)
end
it "should raise an IOError if it can't find the file" do
lambda {
- Chef::Config.load_file("/tmp/timmytimmytimmy")
+ Chef::Config.from_file("/tmp/timmytimmytimmy")
}.should raise_error(IOError)
end
it "should have a default cookbook_path" do
- @config.cookbook_path.should be_kind_of(Array)
+ Chef::Config.cookbook_path.should be_kind_of(Array)
end
it "should allow you to set a cookbook_path with a string" do
- @config.cookbook_path("/etc/chef/cookbook")
- @config.cookbook_path.should eql(["/etc/chef/cookbook"])
+ Chef::Config.cookbook_path("/etc/chef/cookbook")
+ Chef::Config.cookbook_path.should eql("/etc/chef/cookbook")
end
it "should allow you to set a cookbook_path with multiple strings" do
- @config.cookbook_path("/etc/chef/cookbook", "/etc/chef/upstream-cookbooks")
- @config.cookbook_path.should eql([
+ Chef::Config.cookbook_path("/etc/chef/cookbook", "/etc/chef/upstream-cookbooks")
+ Chef::Config.cookbook_path.should eql([
"/etc/chef/cookbook",
"/etc/chef/upstream-cookbooks"
])
end
it "should allow you to set a cookbook_path with an array" do
- @config.cookbook_path ["one", "two"]
- @config.cookbook_path.should eql(["one", "two"])
+ Chef::Config.cookbook_path ["one", "two"]
+ Chef::Config.cookbook_path.should eql(["one", "two"])
+ end
+
+ it "should allow you to reference a value by index" do
+ Chef::Config[:cookbook_path].should be_kind_of(Array)
+ end
+
+ it "should allow you to set a value by index" do
+ Chef::Config[:cookbook_path] = "one"
+ Chef::Config[:cookbook_path].should == "one"
+ end
+
+ it "should allow you to set config values with a block" do
+ Chef::Config.configure do |c|
+ c[:cookbook_path] = "monkey_rabbit"
+ c[:otherthing] = "boo"
+ end
+ Chef::Config.cookbook_path.should == "monkey_rabbit"
+ Chef::Config.otherthing.should == "boo"
end
- it "should not allow you to set a cookbook_path with anything else" do
- lambda { @config.cookbook_path :symbol }.should raise_error(ArgumentError)
+ it "should raise an ArgumentError if you access a config option that does not exist" do
+ lambda { Chef::Config[:snob_hobbery] }.should raise_error(ArgumentError)
end
end \ No newline at end of file
diff --git a/spec/unit/cookbook_loader_spec.rb b/spec/unit/cookbook_loader_spec.rb
index 3da3e644ec..d66034ce7d 100644
--- a/spec/unit/cookbook_loader_spec.rb
+++ b/spec/unit/cookbook_loader_spec.rb
@@ -18,16 +18,15 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::CookbookLoader do
before(:each) do
- config = Chef::Config.new
- config.cookbook_path [
+ Chef::Config.cookbook_path [
File.join(File.dirname(__FILE__), "..", "data", "cookbooks"),
File.join(File.dirname(__FILE__), "..", "data", "kitchen")
]
- @cl = Chef::CookbookLoader.new(config)
+ @cl = Chef::CookbookLoader.new()
end
it "should be a Chef::CookbookLoader object" do
@@ -56,7 +55,7 @@ describe Chef::CookbookLoader do
end
it "should find all the cookbooks in the cookbook path" do
- @cl.config.cookbook_path << File.join(File.dirname(__FILE__), "..", "data", "hidden-cookbooks")
+ Chef::Config.cookbook_path << File.join(File.dirname(__FILE__), "..", "data", "hidden-cookbooks")
@cl.load_cookbooks
@cl.detect { |cb| cb.name == :openldap }.should_not eql(nil)
@cl.detect { |cb| cb.name == :apache2 }.should_not eql(nil)
diff --git a/spec/unit/cookbook_spec.rb b/spec/unit/cookbook_spec.rb
index 1e44f519f7..f702fe32c1 100644
--- a/spec/unit/cookbook_spec.rb
+++ b/spec/unit/cookbook_spec.rb
@@ -18,7 +18,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::Cookbook do
COOKBOOK_PATH = File.join(File.dirname(__FILE__), "..", "data", "cookbooks", "openldap")
diff --git a/spec/unit/mixin/params_validate_spec.rb b/spec/unit/mixin/params_validate_spec.rb
new file mode 100644
index 0000000000..3ade2b3c2d
--- /dev/null
+++ b/spec/unit/mixin/params_validate_spec.rb
@@ -0,0 +1,298 @@
+# 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"))
+
+class TinyClass
+ include Chef::Mixin::ParamsValidate
+
+ def music(is_good=true)
+ is_good
+ end
+end
+
+describe Chef::Mixin::ParamsValidate do
+ before(:each) do
+ @vo = TinyClass.new()
+ end
+
+ it "should allow a hash and a hash as arguments to validate" do
+ lambda { @vo.validate({:one => "two"}, {}) }.should_not raise_error(ArgumentError)
+ end
+
+ it "should raise an argument error if validate is called incorrectly" do
+ lambda { @vo.validate("one", "two") }.should raise_error(ArgumentError)
+ end
+
+ it "should require validation map keys to be symbols or strings" do
+ lambda { @vo.validate({:one => "two"}, { :one => true }) }.should_not raise_error(ArgumentError)
+ lambda { @vo.validate({:one => "two"}, { "one" => true }) }.should_not raise_error(ArgumentError)
+ lambda { @vo.validate({:one => "two"}, { Hash.new => true }) }.should raise_error(ArgumentError)
+ end
+
+ it "should allow options to be required with true" do
+ lambda { @vo.validate({:one => "two"}, { :one => true }) }.should_not raise_error(ArgumentError)
+ end
+
+ it "should allow options to be optional with false" do
+ lambda { @vo.validate({}, {:one => false})}.should_not raise_error(ArgumentError)
+ end
+
+ it "should allow you to check what kind_of? thing an argument is with kind_of" do
+ lambda {
+ @vo.validate(
+ {:one => "string"},
+ {
+ :one => {
+ :kind_of => String
+ }
+ }
+ )
+ }.should_not raise_error(ArgumentError)
+
+ lambda {
+ @vo.validate(
+ {:one => "string"},
+ {
+ :one => {
+ :kind_of => Array
+ }
+ }
+ )
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should allow you to specify an argument is required with required" do
+ lambda {
+ @vo.validate(
+ {:one => "string"},
+ {
+ :one => {
+ :required => true
+ }
+ }
+ )
+ }.should_not raise_error(ArgumentError)
+
+ lambda {
+ @vo.validate(
+ {:two => "string"},
+ {
+ :one => {
+ :required => true
+ }
+ }
+ )
+ }.should raise_error(ArgumentError)
+
+ lambda {
+ @vo.validate(
+ {:two => "string"},
+ {
+ :one => {
+ :required => false
+ }
+ }
+ )
+ }.should_not raise_error(ArgumentError)
+ end
+
+ it "should allow you to specify whether an object has a method with respond_to" do
+ lambda {
+ @vo.validate(
+ {:one => @vo},
+ {
+ :one => {
+ :respond_to => "validate"
+ }
+ }
+ )
+ }.should_not raise_error(ArgumentError)
+
+ lambda {
+ @vo.validate(
+ {:one => @vo},
+ {
+ :one => {
+ :respond_to => "monkey"
+ }
+ }
+ )
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should allow you to specify whether an object has all the given methods with respond_to and an array" do
+ lambda {
+ @vo.validate(
+ {:one => @vo},
+ {
+ :one => {
+ :respond_to => ["validate", "music"]
+ }
+ }
+ )
+ }.should_not raise_error(ArgumentError)
+
+ lambda {
+ @vo.validate(
+ {:one => @vo},
+ {
+ :one => {
+ :respond_to => ["monkey", "validate"]
+ }
+ }
+ )
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should let you set a default value with default => value" do
+ arguments = Hash.new
+ @vo.validate(arguments, {
+ :one => {
+ :default => "is the loneliest number"
+ }
+ })
+ arguments[:one].should == "is the loneliest number"
+ end
+
+ it "should let you check regular expressions" do
+ lambda {
+ @vo.validate(
+ { :one => "is good" },
+ {
+ :one => {
+ :regex => /^is good$/
+ }
+ }
+ )
+ }.should_not raise_error(ArgumentError)
+
+ lambda {
+ @vo.validate(
+ { :one => "is good" },
+ {
+ :one => {
+ :regex => /^is bad$/
+ }
+ }
+ )
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should let you specify your own callbacks" do
+ lambda {
+ @vo.validate(
+ { :one => "is good" },
+ {
+ :one => {
+ :callbacks => {
+ "should be equal to is good" => lambda { |a|
+ a == "is good"
+ },
+ }
+ }
+ }
+ )
+ }.should_not raise_error(ArgumentError)
+
+ lambda {
+ @vo.validate(
+ { :one => "is bad" },
+ {
+ :one => {
+ :callbacks => {
+ "should be equal to 'is good'" => lambda { |a|
+ a == "is good"
+ },
+ }
+ }
+ }
+ )
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should let you combine checks" do
+ args = { :one => "is good", :two => "is bad" }
+ lambda {
+ @vo.validate(
+ args,
+ {
+ :one => {
+ :kind_of => String,
+ :respond_to => [ :to_s, :upcase ],
+ :regex => /^is good/,
+ :callbacks => {
+ "should be your friend" => lambda { |a|
+ a == "is good"
+ }
+ },
+ :required => true
+ },
+ :two => {
+ :kind_of => String,
+ :required => false
+ },
+ :three => { :default => "neato mosquito" }
+ }
+ )
+ }.should_not raise_error(ArgumentError)
+ args[:three].should == "neato mosquito"
+ lambda {
+ @vo.validate(
+ args,
+ {
+ :one => {
+ :kind_of => String,
+ :respond_to => [ :to_s, :upcase ],
+ :regex => /^is good/,
+ :callbacks => {
+ "should be your friend" => lambda { |a|
+ a == "is good"
+ }
+ },
+ :required => true
+ },
+ :two => {
+ :kind_of => Hash,
+ :required => false
+ },
+ :three => { :default => "neato mosquito" }
+ }
+ )
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should raise an ArgumentError if the validation map has an unknown check" do
+ lambda { @vo.validate(
+ { :one => "two" },
+ {
+ :one => {
+ :busted => "check"
+ }
+ }
+ )
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should accept keys that are strings in the options" do
+ lambda {
+ @vo.validate({ "one" => "two" }, { :one => { :regex => /^two$/ }})
+ }.should_not raise_error(ArgumentError)
+ end
+end \ No newline at end of file
diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb
index e5a92486e5..a8c456e8f5 100644
--- a/spec/unit/node_spec.rb
+++ b/spec/unit/node_spec.rb
@@ -18,7 +18,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::Node do
before(:each) do
@@ -104,5 +104,17 @@ describe Chef::Node do
lambda { @node.from_file("/tmp/monkeydiving") }.should raise_error(IOError)
end
+ it "should allow you to iterate over attributes with each_attribute" do
+ @node.sunshine "is bright"
+ @node.canada "is a nice place"
+ seen_attributes = Hash.new
+ @node.each_attribute do |a,v|
+ seen_attributes[a] = v
+ end
+ seen_attributes.should have_key(:sunshine)
+ seen_attributes.should have_key(:canada)
+ seen_attributes[:sunshine].should == "is bright"
+ seen_attributes[:canada].should == "is a nice place"
+ end
end \ No newline at end of file
diff --git a/spec/unit/provider/file_spec.rb b/spec/unit/provider/file_spec.rb
index 2ee6b1960e..4a98e88df0 100644
--- a/spec/unit/provider/file_spec.rb
+++ b/spec/unit/provider/file_spec.rb
@@ -20,7 +20,7 @@
require 'ostruct'
-require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
describe Chef::Provider::File do
before(:each) do
diff --git a/spec/unit/provider_spec.rb b/spec/unit/provider_spec.rb
index 51980cbe99..71cc1a47e3 100644
--- a/spec/unit/provider_spec.rb
+++ b/spec/unit/provider_spec.rb
@@ -18,7 +18,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::Provider do
before(:each) do
diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb
index e02973d73c..3c33a6d485 100644
--- a/spec/unit/recipe_spec.rb
+++ b/spec/unit/recipe_spec.rb
@@ -18,7 +18,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::Recipe do
before(:each) do
@@ -110,7 +110,7 @@ CODE
@recipe.resources(:zen_master => "lao tzu").something.should eql(true)
end
- it "should load a node from a ruby file" do
+ it "should load a resource from a ruby file" do
@recipe.from_file(File.join(File.dirname(__FILE__), "..", "data", "recipes", "test.rb"))
res = @recipe.resources(:file => "/etc/nsswitch.conf")
res.name.should eql("/etc/nsswitch.conf")
@@ -123,5 +123,23 @@ CODE
it "should raise an exception if the file cannot be found or read" do
lambda { @recipe.from_file("/tmp/monkeydiving") }.should raise_error(IOError)
end
+
+ it "should evaluate another recipe with recipe_require" do
+ Chef::Config.cookbook_path File.join(File.dirname(__FILE__), "..", "data", "cookbooks")
+ @recipe.cookbook_loader.load_cookbooks
+ @recipe.require_recipe "openldap::gigantor"
+ res = @recipe.resources(:cat => "blanket")
+ res.name.should eql("blanket")
+ res.pretty_kitty.should eql(false)
+ end
+
+ it "should load the default recipe for a cookbook if require_recipe is called without a ::" do
+ Chef::Config.cookbook_path File.join(File.dirname(__FILE__), "..", "data", "cookbooks")
+ @recipe.cookbook_loader.load_cookbooks
+ @recipe.require_recipe "openldap"
+ res = @recipe.resources(:cat => "blanket")
+ res.name.should eql("blanket")
+ res.pretty_kitty.should eql(true)
+ end
end \ No newline at end of file
diff --git a/spec/unit/resource/file_spec.rb b/spec/unit/resource/file_spec.rb
index 474c58f0d1..bffb4e43f8 100644
--- a/spec/unit/resource/file_spec.rb
+++ b/spec/unit/resource/file_spec.rb
@@ -17,7 +17,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
describe Chef::Resource::File do
diff --git a/spec/unit/resource_collection_spec.rb b/spec/unit/resource_collection_spec.rb
index 6f6f195513..10f7d43cde 100644
--- a/spec/unit/resource_collection_spec.rb
+++ b/spec/unit/resource_collection_spec.rb
@@ -17,7 +17,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::ResourceCollection do
diff --git a/spec/unit/resource_definition_spec.rb b/spec/unit/resource_definition_spec.rb
index 8bcd9c52f9..8b8ee16f3a 100644
--- a/spec/unit/resource_definition_spec.rb
+++ b/spec/unit/resource_definition_spec.rb
@@ -18,7 +18,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::ResourceDefinition do
before(:each) do
diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb
index f767fe8348..d6db1f2d74 100644
--- a/spec/unit/resource_spec.rb
+++ b/spec/unit/resource_spec.rb
@@ -18,7 +18,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-require File.join(File.dirname(__FILE__), "..", "spec_helper")
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Chef::Resource do
before(:each) do