summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser
diff options
context:
space:
mode:
authorBrice Figureau <brice-puppet@daysofwonder.com>2009-07-28 19:37:11 +0200
committerJames Turnbull <james@lovedthanlost.net>2009-08-01 11:15:29 +1000
commitb45ccf8d38a10d3f5226cbabe494240901e4e383 (patch)
tree9e5e8bbbd88ce7f66dc5a42c6aa7c2c6c57e7b4e /spec/unit/parser
parent58a73b5c68485dc5d41a46936c31e5fad5f037b5 (diff)
downloadpuppet-b45ccf8d38a10d3f5226cbabe494240901e4e383.tar.gz
puppet-b45ccf8d38a10d3f5226cbabe494240901e4e383.tar.xz
puppet-b45ccf8d38a10d3f5226cbabe494240901e4e383.zip
Implement node matching with regexes
This patch enhance AST::HostName to support regexes, and modifies the parser to allow regex to be used as node name. Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>]
Diffstat (limited to 'spec/unit/parser')
-rwxr-xr-xspec/unit/parser/ast/definition.rb18
-rwxr-xr-xspec/unit/parser/ast/leaf.rb40
-rwxr-xr-xspec/unit/parser/ast/node.rb20
-rw-r--r--spec/unit/parser/loaded_code.rb55
4 files changed, 121 insertions, 12 deletions
diff --git a/spec/unit/parser/ast/definition.rb b/spec/unit/parser/ast/definition.rb
index a58e4d00e..b8f094180 100755
--- a/spec/unit/parser/ast/definition.rb
+++ b/spec/unit/parser/ast/definition.rb
@@ -29,6 +29,24 @@ describe Puppet::Parser::AST::Definition, "when evaluating" do
@definition.evaluate_code(@resource)
end
+ it "should have a get_classname method" do
+ @definition.should respond_to :get_classname
+ end
+
+ it "should return the current classname with get_classname" do
+ @definition.expects(:classname)
+
+ @definition.get_classname(@scope)
+ end
+
+ describe "when evaluating" do
+ it "should create a resource whose title comes from get_classname" do
+ @definition.expects(:get_classname).returns("classname")
+
+ @definition.evaluate(@scope)
+ end
+ end
+
# it "should copy its namespace to the scope"
#
# it "should mark the scope virtual if the resource is virtual"
diff --git a/spec/unit/parser/ast/leaf.rb b/spec/unit/parser/ast/leaf.rb
index 38b753b28..c9593b967 100755
--- a/spec/unit/parser/ast/leaf.rb
+++ b/spec/unit/parser/ast/leaf.rb
@@ -5,6 +5,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
describe Puppet::Parser::AST::Leaf do
before :each do
@scope = stub 'scope'
+ @value = stub 'value'
+ @leaf = Puppet::Parser::AST::Leaf.new(:value => @value)
end
it "should have a evaluate_match method" do
@@ -12,11 +14,6 @@ describe Puppet::Parser::AST::Leaf do
end
describe "when evaluate_match is called" do
- before :each do
- @value = stub 'value'
- @leaf = Puppet::Parser::AST::Leaf.new(:value => @value)
- end
-
it "should evaluate itself" do
@leaf.expects(:safeevaluate).with(@scope)
@@ -45,6 +42,16 @@ describe Puppet::Parser::AST::Leaf do
Puppet::Parser::AST::Leaf.new( :value => value ).to_s
end
end
+
+ it "should have a match method" do
+ @leaf.should respond_to(:match)
+ end
+
+ it "should delegate match to ==" do
+ @value.expects(:==).with("value")
+
+ @leaf.match("value")
+ end
end
describe Puppet::Parser::AST::FlatString do
@@ -131,6 +138,15 @@ describe Puppet::Parser::AST::Regex do
val.to_s
end
+
+ it "should delegate match to the underlying regexp match method" do
+ regex = Regexp.new("/ab/")
+ val = Puppet::Parser::AST::Regex.new :value => regex
+
+ regex.expects(:match).with("value")
+
+ val.match("value")
+ end
end
describe Puppet::Parser::AST::HostName do
@@ -146,6 +162,10 @@ describe Puppet::Parser::AST::HostName do
lambda { Puppet::Parser::AST::HostName.new( :value => "not an hostname!" ) }.should raise_error
end
+ it "should not raise an error if hostname is a regex" do
+ lambda { Puppet::Parser::AST::HostName.new( :value => Puppet::Parser::AST::Regex.new(:value => "/test/") ) }.should_not raise_error
+ end
+
it "should stringify the value" do
value = stub 'value', :=~ => false
@@ -175,6 +195,11 @@ describe Puppet::Parser::AST::HostName do
host.to_classname.should == "klassname"
end
+ it "should return a string usable as classname when calling to_classname" do
+ host = Puppet::Parser::AST::HostName.new( :value => Puppet::Parser::AST::Regex.new(:value => "/^this-is not@a classname$/") )
+ host.to_classname.should == "this-isnotaclassname"
+ end
+
it "should delegate eql? to the underlying value if it is an HostName" do
@value.expects(:eql?).with("value")
@host.eql?("value")
@@ -190,4 +215,9 @@ describe Puppet::Parser::AST::HostName do
@value.expects(:hash)
@host.hash
end
+
+ it "should return true when regex? is called and value is a Regex" do
+ @value.expects(:is_a?).with(Puppet::Parser::AST::Regex).returns(true)
+ @host.regex?.should be_true
+ end
end
diff --git a/spec/unit/parser/ast/node.rb b/spec/unit/parser/ast/node.rb
index aaba4c2e8..5a4a5efe4 100755
--- a/spec/unit/parser/ast/node.rb
+++ b/spec/unit/parser/ast/node.rb
@@ -12,6 +12,26 @@ describe Puppet::Parser::AST::Node do
@scope = @compiler.topscope
end
+ describe "when calling get_classname" do
+ it "should return current node name if name is a Regex" do
+ name = stub 'name', :regex? => true
+ node = @parser.newnode("node").shift
+ node.stubs(:name).returns(name)
+
+ @scope.expects(:host).returns("testnode")
+
+ node.get_classname(@scope).should == "testnode"
+ end
+
+ it "should return the current node classname if name is not a Regex" do
+ name = stub 'name', :regex? => false
+ node = @parser.newnode("node").shift
+ node.stubs(:name).returns(name)
+
+ node.get_classname(@scope).should == "node"
+ end
+ end
+
describe Puppet::Parser::AST::Node, "when evaluating" do
before do
diff --git a/spec/unit/parser/loaded_code.rb b/spec/unit/parser/loaded_code.rb
index d33bda9a9..4bfd119f5 100644
--- a/spec/unit/parser/loaded_code.rb
+++ b/spec/unit/parser/loaded_code.rb
@@ -129,10 +129,18 @@ describe Puppet::Parser::LoadedCode do
end
describe "when finding nodes" do
+ before :each do
+ @loader = Puppet::Parser::LoadedCode.new
+
+ @nodename1 = stub 'nodename1', :is_a? => true
+ @node1 = stub 'node1'
+ @nodename2 = stub 'nodename2', :is_a? => true
+ @node2 = stub 'node2'
+ end
it "should create an HostName if nodename is a string" do
Puppet::Parser::AST::HostName.expects(:new).with(:value => "foo")
- loader = Puppet::Parser::LoadedCode.new
- loader.node("foo")
+
+ @loader.node("foo")
end
it "should not create an HostName if nodename is an HostName" do
@@ -140,17 +148,50 @@ describe Puppet::Parser::LoadedCode do
Puppet::Parser::AST::HostName.expects(:new).with(:value => "foo").never
- loader = Puppet::Parser::LoadedCode.new
- loader.node(name)
+ @loader.node(name)
end
it "should be able to find nobe by HostName" do
namein = Puppet::Parser::AST::HostName.new(:value => "foo")
nameout = Puppet::Parser::AST::HostName.new(:value => "foo")
- loader = Puppet::Parser::LoadedCode.new
- loader.add_node(namein, "bar")
- loader.node(nameout) == "bar"
+ @loader.add_node(namein, "bar")
+ @loader.node(nameout) == "bar"
+ end
+
+ it "should return the first matching regex nodename" do
+ @nodename1.stubs(:regex?).returns(true)
+ @nodename1.expects(:match).returns(true)
+ @nodename2.stubs(:regex?).returns(false)
+
+ @loader.add_node(@nodename1, @node1)
+ @loader.add_node(@nodename2, @node2)
+
+ @loader.node("test").should == @node1
+ end
+
+ it "should not scan non-regex node" do
+ @nodename1.stubs(:regex?).returns(true)
+ @nodename1.stubs(:match).returns(false)
+ @nodename2.stubs(:regex?).returns(false)
+ @nodename2.expects(:match).never
+
+ @loader.add_node(@nodename1,@node1)
+ @loader.add_node(@nodename2,@node2)
+
+ @loader.node("test")
+ end
+
+ it "should prefer non-regex nodes to regex nodes" do
+ @nodename1.stubs(:regex?).returns(false)
+ @nodename1.expects(:match).never
+ @nodename2.stubs(:regex?).returns(true)
+ @nodename2.expects(:match).never
+
+ @loader.add_node(@nodename1,@node1)
+ @loader.add_node(@nodename2,@node2)
+
+ @loader.node(@nodename1)
end
end
end