summaryrefslogtreecommitdiffstats
path: root/spec/unit
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-03-07 12:28:58 -0600
committerLuke Kanies <luke@madstop.com>2008-03-07 12:28:58 -0600
commit9f8bb7e42aeea4670d9292f935529f8db5d57bc5 (patch)
treeb3b746e516d62d10368d19a197749dd25a54307e /spec/unit
parentc2b33acd5c1e6fe4021d7609f806b7bd8af834f8 (diff)
parente8029cc61a1956263935a0df469cc77b8e80a102 (diff)
downloadpuppet-9f8bb7e42aeea4670d9292f935529f8db5d57bc5.tar.gz
puppet-9f8bb7e42aeea4670d9292f935529f8db5d57bc5.tar.xz
puppet-9f8bb7e42aeea4670d9292f935529f8db5d57bc5.zip
Merge branch '0.24.x'
Diffstat (limited to 'spec/unit')
-rwxr-xr-xspec/unit/indirector/node/exec.rb6
-rwxr-xr-xspec/unit/indirector/node/ldap.rb195
-rwxr-xr-xspec/unit/node/catalog.rb2
-rwxr-xr-xspec/unit/parser/resource.rb161
4 files changed, 276 insertions, 88 deletions
diff --git a/spec/unit/indirector/node/exec.rb b/spec/unit/indirector/node/exec.rb
index 744a32b0a..5e6a9b1da 100755
--- a/spec/unit/indirector/node/exec.rb
+++ b/spec/unit/indirector/node/exec.rb
@@ -59,4 +59,10 @@ describe Puppet::Node::Exec, " when handling the results of the command" do
@node.expects(:fact_merge)
@searcher.find(@name)
end
+
+ it "should set the node's environment if one is provided" do
+ @result[:environment] = "yay"
+ @node.expects(:environment=).with "yay"
+ @searcher.find(@name)
+ end
end
diff --git a/spec/unit/indirector/node/ldap.rb b/spec/unit/indirector/node/ldap.rb
index 95b73de60..2a4f240ed 100755
--- a/spec/unit/indirector/node/ldap.rb
+++ b/spec/unit/indirector/node/ldap.rb
@@ -5,27 +5,26 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
require 'puppet/indirector/node/ldap'
describe Puppet::Node::Ldap do
- before :each do
- @searcher = Puppet::Node::Ldap.new
- @entries = {}
- entries = @entries
-
- @connection = mock 'connection'
- @entry = mock 'entry'
- @connection.stubs(:search).yields(@entry)
- @searcher.stubs(:connection).returns(@connection)
- @searcher.stubs(:class_attributes).returns([])
- @searcher.stubs(:parent_attribute).returns(nil)
- @searcher.stubs(:search_base).returns(:yay)
- @searcher.stubs(:search_filter).returns(:filter)
-
- @node = mock 'node'
- @node.stubs(:fact_merge)
- @name = "mynode"
- Puppet::Node.stubs(:new).with(@name).returns(@node)
- end
-
- describe Puppet::Node::Ldap, " when searching for nodes" do
+ describe "when searching for nodes" do
+ before :each do
+ @searcher = Puppet::Node::Ldap.new
+ @entries = {}
+ entries = @entries
+
+ @connection = mock 'connection'
+ @entry = mock 'entry'
+ @connection.stubs(:search).yields(@entry)
+ @searcher.stubs(:connection).returns(@connection)
+ @searcher.stubs(:class_attributes).returns([])
+ @searcher.stubs(:parent_attribute).returns(nil)
+ @searcher.stubs(:search_base).returns(:yay)
+ @searcher.stubs(:search_filter).returns(:filter)
+
+ @node = mock 'node'
+ @node.stubs(:fact_merge)
+ @name = "mynode"
+ Puppet::Node.stubs(:new).with(@name).returns(@node)
+ end
it "should return nil if no results are found in ldap" do
@connection.stubs(:search)
@@ -61,6 +60,13 @@ describe Puppet::Node::Ldap do
@searcher.find("mynode")
end
+ it "should set the node's environment to the environment of the results" do
+ @entry.stubs(:to_hash).returns("environment" => ["test"])
+ @node.stubs(:parameters=)
+ @node.expects(:environment=).with("test")
+ @searcher.find("mynode")
+ end
+
it "should retain false parameter values" do
@entry.stubs(:to_hash).returns("one" => [false])
@node.expects(:parameters=).with("one" => false)
@@ -78,91 +84,114 @@ describe Puppet::Node::Ldap do
@node.expects(:parameters=).with("one" => ["a", "b"])
@searcher.find("mynode")
end
- end
- describe Puppet::Node::Ldap, " when a parent node is specified" do
+ describe "and a parent node is specified" do
+ before do
+ @parent = mock 'parent'
+ @parent_parent = mock 'parent_parent'
- before do
- @parent = mock 'parent'
- @parent_parent = mock 'parent_parent'
+ @searcher.meta_def(:search_filter) do |name|
+ return name
+ end
+ @connection.stubs(:search).with { |*args| args[2] == @name }.yields(@entry)
+ @connection.stubs(:search).with { |*args| args[2] == 'parent' }.yields(@parent)
+ @connection.stubs(:search).with { |*args| args[2] == 'parent_parent' }.yields(@parent_parent)
- @searcher.meta_def(:search_filter) do |name|
- return name
+ @searcher.stubs(:parent_attribute).returns(:parent)
end
- @connection.stubs(:search).with { |*args| args[2] == @name }.yields(@entry)
- @connection.stubs(:search).with { |*args| args[2] == 'parent' }.yields(@parent)
- @connection.stubs(:search).with { |*args| args[2] == 'parent_parent' }.yields(@parent_parent)
- @searcher.stubs(:parent_attribute).returns(:parent)
- end
+ it "should fail if the parent cannot be found" do
+ @connection.stubs(:search).with { |*args| args[2] == 'parent' }.returns("whatever")
- it "should fail if the parent cannot be found" do
- @connection.stubs(:search).with { |*args| args[2] == 'parent' }.returns("whatever")
+ @entry.stubs(:to_hash).returns({})
+ @entry.stubs(:vals).with(:parent).returns(%w{parent})
- @entry.stubs(:to_hash).returns({})
- @entry.stubs(:vals).with(:parent).returns(%w{parent})
+ proc { @searcher.find("mynode") }.should raise_error(Puppet::Error)
+ end
- proc { @searcher.find("mynode") }.should raise_error(Puppet::Error)
- end
+ it "should add any parent classes to the node's classes" do
+ @entry.stubs(:to_hash).returns({})
+ @entry.stubs(:vals).with(:parent).returns(%w{parent})
+ @entry.stubs(:vals).with("classes").returns(%w{a b})
- it "should add any parent classes to the node's classes" do
- @entry.stubs(:to_hash).returns({})
- @entry.stubs(:vals).with(:parent).returns(%w{parent})
- @entry.stubs(:vals).with("classes").returns(%w{a b})
+ @parent.stubs(:to_hash).returns({})
+ @parent.stubs(:vals).with("classes").returns(%w{c d})
+ @parent.stubs(:vals).with(:parent).returns(nil)
- @parent.stubs(:to_hash).returns({})
- @parent.stubs(:vals).with("classes").returns(%w{c d})
- @parent.stubs(:vals).with(:parent).returns(nil)
+ @searcher.stubs(:class_attributes).returns(%w{classes})
+ @node.expects(:classes=).with(%w{a b c d})
+ @searcher.find("mynode")
+ end
- @searcher.stubs(:class_attributes).returns(%w{classes})
- @node.expects(:classes=).with(%w{a b c d})
- @searcher.find("mynode")
- end
+ it "should add any parent parameters to the node's parameters" do
+ @entry.stubs(:to_hash).returns("one" => "two")
+ @entry.stubs(:vals).with(:parent).returns(%w{parent})
- it "should add any parent parameters to the node's parameters" do
- @entry.stubs(:to_hash).returns("one" => "two")
- @entry.stubs(:vals).with(:parent).returns(%w{parent})
+ @parent.stubs(:to_hash).returns("three" => "four")
+ @parent.stubs(:vals).with(:parent).returns(nil)
- @parent.stubs(:to_hash).returns("three" => "four")
- @parent.stubs(:vals).with(:parent).returns(nil)
+ @node.expects(:parameters=).with("one" => "two", "three" => "four")
+ @searcher.find("mynode")
+ end
- @node.expects(:parameters=).with("one" => "two", "three" => "four")
- @searcher.find("mynode")
- end
+ it "should prefer node parameters over parent parameters" do
+ @entry.stubs(:to_hash).returns("one" => "two")
+ @entry.stubs(:vals).with(:parent).returns(%w{parent})
- it "should prefer node parameters over parent parameters" do
- @entry.stubs(:to_hash).returns("one" => "two")
- @entry.stubs(:vals).with(:parent).returns(%w{parent})
+ @parent.stubs(:to_hash).returns("one" => "three")
+ @parent.stubs(:vals).with(:parent).returns(nil)
- @parent.stubs(:to_hash).returns("one" => "three")
- @parent.stubs(:vals).with(:parent).returns(nil)
+ @node.expects(:parameters=).with("one" => "two")
+ @searcher.find("mynode")
+ end
- @node.expects(:parameters=).with("one" => "two")
- @searcher.find("mynode")
- end
+ it "should use the parent's environment if the node has none" do
+ @entry.stubs(:to_hash).returns({})
+ @entry.stubs(:vals).with(:parent).returns(%w{parent})
- it "should recursively look up parent information" do
- @entry.stubs(:to_hash).returns("one" => "two")
- @entry.stubs(:vals).with(:parent).returns(%w{parent})
+ @parent.stubs(:to_hash).returns("environment" => ["parent"])
+ @parent.stubs(:vals).with(:parent).returns(nil)
- @parent.stubs(:to_hash).returns("three" => "four")
- @parent.stubs(:vals).with(:parent).returns(['parent_parent'])
+ @node.stubs(:parameters=)
+ @node.expects(:environment=).with("parent")
+ @searcher.find("mynode")
+ end
- @parent_parent.stubs(:to_hash).returns("five" => "six")
- @parent_parent.stubs(:vals).with(:parent).returns(nil)
- @parent_parent.stubs(:vals).with(:parent).returns(nil)
+ it "should prefer the node's environment to the parent's" do
+ @entry.stubs(:to_hash).returns("environment" => %w{child})
+ @entry.stubs(:vals).with(:parent).returns(%w{parent})
- @node.expects(:parameters=).with("one" => "two", "three" => "four", "five" => "six")
- @searcher.find("mynode")
- end
+ @parent.stubs(:to_hash).returns("environment" => ["parent"])
+ @parent.stubs(:vals).with(:parent).returns(nil)
+
+ @node.stubs(:parameters=)
+ @node.expects(:environment=).with("child")
+ @searcher.find("mynode")
+ end
+
+ it "should recursively look up parent information" do
+ @entry.stubs(:to_hash).returns("one" => "two")
+ @entry.stubs(:vals).with(:parent).returns(%w{parent})
+
+ @parent.stubs(:to_hash).returns("three" => "four")
+ @parent.stubs(:vals).with(:parent).returns(['parent_parent'])
- it "should not allow loops in parent declarations" do
- @entry.stubs(:to_hash).returns("one" => "two")
- @entry.stubs(:vals).with(:parent).returns(%w{parent})
+ @parent_parent.stubs(:to_hash).returns("five" => "six")
+ @parent_parent.stubs(:vals).with(:parent).returns(nil)
+ @parent_parent.stubs(:vals).with(:parent).returns(nil)
- @parent.stubs(:to_hash).returns("three" => "four")
- @parent.stubs(:vals).with(:parent).returns([@name])
- proc { @searcher.find("mynode") }.should raise_error(ArgumentError)
+ @node.expects(:parameters=).with("one" => "two", "three" => "four", "five" => "six")
+ @searcher.find("mynode")
+ end
+
+ it "should not allow loops in parent declarations" do
+ @entry.stubs(:to_hash).returns("one" => "two")
+ @entry.stubs(:vals).with(:parent).returns(%w{parent})
+
+ @parent.stubs(:to_hash).returns("three" => "four")
+ @parent.stubs(:vals).with(:parent).returns([@name])
+ proc { @searcher.find("mynode") }.should raise_error(ArgumentError)
+ end
end
end
end
diff --git a/spec/unit/node/catalog.rb b/spec/unit/node/catalog.rb
index 604dabbb6..360dd87f2 100755
--- a/spec/unit/node/catalog.rb
+++ b/spec/unit/node/catalog.rb
@@ -304,6 +304,8 @@ describe Puppet::Node::Catalog, " when converting to a RAL catalog" do
resource = stub 'resource', :name => "changer2", :title => "changer2", :ref => "Test[changer2]", :catalog= => nil, :remove => nil
+ #changer is going to get duplicated as part of a fix for aliases 1094
+ changer.expects(:dup).returns(changer)
changer.expects(:to_type).returns(resource)
newconfig = nil
diff --git a/spec/unit/parser/resource.rb b/spec/unit/parser/resource.rb
index 9ce7b391b..1948a3c07 100755
--- a/spec/unit/parser/resource.rb
+++ b/spec/unit/parser/resource.rb
@@ -14,6 +14,36 @@ describe Puppet::Parser::Resource do
@scope = @compiler.topscope
end
+ def mkresource(args = {})
+ args[:source] ||= "source"
+ args[:scope] ||= stub('scope', :source => mock('source'))
+
+ {:type => "resource", :title => "testing", :source => "source", :scope => "scope"}.each do |param, value|
+ args[param] ||= value
+ end
+
+ params = args[:params] || {:one => "yay", :three => "rah"}
+ if args[:params] == :none
+ args.delete(:params)
+ else
+ args[:params] = paramify(args[:source], params)
+ end
+
+ Puppet::Parser::Resource.new(args)
+ end
+
+ def param(name, value, source)
+ Puppet::Parser::Resource::Param.new(:name => name, :value => value, :source => source)
+ end
+
+ def paramify(source, hash)
+ hash.collect do |name, value|
+ Puppet::Parser::Resource::Param.new(
+ :name => name, :value => value, :source => source
+ )
+ end
+ end
+
it "should be isomorphic if it is builtin and models an isomorphic type" do
Puppet::Type.type(:file).expects(:isomorphic?).returns(true)
@resource = Puppet::Parser::Resource.new(:type => "file", :title => "whatever", :scope => @scope, :source => @source).isomorphic?.should be_true
@@ -29,6 +59,30 @@ describe Puppet::Parser::Resource do
@resource = Puppet::Parser::Resource.new(:type => "whatever", :title => "whatever", :scope => @scope, :source => @source).isomorphic?.should be_true
end
+ it "should have a array-indexing method for retrieving parameter values" do
+ @resource = mkresource
+ @resource[:one].should == "yay"
+ end
+
+ describe "when initializing" do
+ before do
+ @arguments = {:type => "resource", :title => "testing", :scope => stub('scope', :source => mock('source'))}
+ end
+
+ [:type, :title, :scope].each do |name|
+ it "should fail unless #{name.to_s} is specified" do
+ try = @arguments.dup
+ try.delete(name)
+ lambda { Puppet::Parser::Resource.new(try) }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "should set the reference correctly" do
+ res = Puppet::Parser::Resource.new(@arguments)
+ res.ref.should == "Resource[testing]"
+ end
+ end
+
describe "when evaluating" do
before do
@type = Puppet::Parser::Resource
@@ -60,11 +114,10 @@ describe Puppet::Parser::Resource do
describe "when finishing" do
before do
- @definition = @parser.newdefine "mydefine"
@class = @parser.newclass "myclass"
@nodedef = @parser.newnode("mynode")[0]
- @resource = Puppet::Parser::Resource.new(:type => "mydefine", :title => "whatever", :scope => @scope, :source => @source)
+ @resource = Puppet::Parser::Resource.new(:type => "file", :title => "whatever", :scope => @scope, :source => @source)
end
it "should do nothing if it has already been finished" do
@@ -73,6 +126,32 @@ describe Puppet::Parser::Resource do
@resource.finish
end
+ it "should add all defaults available from the scope" do
+ @resource.scope.expects(:lookupdefaults).with(@resource.type).returns(:owner => param(:owner, "default", @resource.source))
+ @resource.finish
+
+ @resource[:owner].should == "default"
+ end
+
+ it "should not replace existing parameters with defaults" do
+ @resource.set_parameter :owner, "oldvalue"
+ @resource.scope.expects(:lookupdefaults).with(@resource.type).returns(:owner => :replaced)
+ @resource.finish
+
+ @resource[:owner].should == "oldvalue"
+ end
+
+ it "should add a copy of each default, rather than the actual default parameter instance" do
+ newparam = param(:owner, "default", @resource.source)
+ other = newparam.dup
+ other.value = "other"
+ newparam.expects(:dup).returns(other)
+ @resource.scope.expects(:lookupdefaults).with(@resource.type).returns(:owner => newparam)
+ @resource.finish
+
+ @resource[:owner].should == "other"
+ end
+
it "should copy metaparams from its scope" do
@scope.setvar("noop", "true")
@@ -82,7 +161,7 @@ describe Puppet::Parser::Resource do
end
it "should not copy metaparams that it already has" do
- @resource.class.publicize_methods(:set_parameter) { @resource.set_parameter("noop", "false") }
+ @resource.set_parameter("noop", "false")
@scope.setvar("noop", "true")
@resource.class.publicize_methods(:add_metaparams) { @resource.add_metaparams }
@@ -91,7 +170,7 @@ describe Puppet::Parser::Resource do
end
it "should stack relationship metaparams from its container if it already has them" do
- @resource.class.publicize_methods(:set_parameter) { @resource.set_parameter("require", "resource") }
+ @resource.set_parameter("require", "resource")
@scope.setvar("require", "container")
@resource.class.publicize_methods(:add_metaparams) { @resource.add_metaparams }
@@ -100,7 +179,7 @@ describe Puppet::Parser::Resource do
end
it "should flatten the array resulting from stacking relationship metaparams" do
- @resource.class.publicize_methods(:set_parameter) { @resource.set_parameter("require", ["resource1", "resource2"]) }
+ @resource.set_parameter("require", ["resource1", "resource2"])
@scope.setvar("require", %w{container1 container2})
@resource.class.publicize_methods(:add_metaparams) { @resource.add_metaparams }
@@ -167,4 +246,76 @@ describe Puppet::Parser::Resource do
lambda { @resource.tag("good_tag") }.should_not raise_error(Puppet::ParseError)
end
end
+
+ describe "when merging overrides" do
+ before do
+ @source = "source1"
+ @resource = mkresource :source => @source
+ @override = mkresource :source => @source
+ end
+
+ it "should fail when the override was not created by a parent class" do
+ @override.source = "source2"
+ @override.source.expects(:child_of?).with("source1").returns(false)
+ assert_raise(Puppet::ParseError, "Allowed unrelated resources to override") do
+ @resource.merge(@override)
+ end
+ end
+
+ it "should succeed when the override was created in the current scope" do
+ @resource.source = "source3"
+ @override.source = @resource.source
+ @override.source.expects(:child_of?).with("source3").never
+ params = {:a => :b, :c => :d}
+ @override.expects(:params).returns(params)
+ @resource.expects(:override_parameter).with(:b)
+ @resource.expects(:override_parameter).with(:d)
+ @resource.merge(@override)
+ end
+
+ it "should succeed when a parent class created the override" do
+ @resource.source = "source3"
+ @override.source = "source4"
+ @override.source.expects(:child_of?).with("source3").returns(true)
+ params = {:a => :b, :c => :d}
+ @override.expects(:params).returns(params)
+ @resource.expects(:override_parameter).with(:b)
+ @resource.expects(:override_parameter).with(:d)
+ @resource.merge(@override)
+ end
+
+ it "should add new parameters when the parameter is not set" do
+ @source.stubs(:child_of?).returns true
+ @override.set_parameter(:testing, "value")
+ @resource.merge(@override)
+
+ @resource[:testing].should == "value"
+ end
+
+ it "should replace existing parameter values" do
+ @source.stubs(:child_of?).returns true
+ @resource.set_parameter(:testing, "old")
+ @override.set_parameter(:testing, "value")
+
+ @resource.merge(@override)
+
+ @resource[:testing].should == "value"
+ end
+
+ it "should add values to the parameter when the override was created with the '+>' syntax" do
+ @source.stubs(:child_of?).returns true
+ param = Puppet::Parser::Resource::Param.new(:name => :testing, :value => "testing", :source => @resource.source)
+ param.add = true
+
+ @override.set_parameter(param)
+
+ @resource.set_parameter(:testing, "other")
+
+ @resource.merge(@override)
+
+ @resource[:testing].should == %w{other testing}
+ end
+
+
+ end
end