This is used for AST resources (and fixed the last of the tests I broke in spec/).
Signed-off-by: Luke Kanies <[email protected]> --- lib/puppet/parser/ast/resource.rb | 3 +- lib/puppet/parser/resource.rb | 10 +++- lib/puppet/resource.rb | 18 +++++--- spec/unit/parser/ast/resource.rb | 82 +++++++++++++++---------------------- spec/unit/resource.rb | 14 ++++++ 5 files changed, 68 insertions(+), 59 deletions(-) diff --git a/lib/puppet/parser/ast/resource.rb b/lib/puppet/parser/ast/resource.rb index 5da40b3..c82e0b5 100644 --- a/lib/puppet/parser/ast/resource.rb +++ b/lib/puppet/parser/ast/resource.rb @@ -44,7 +44,8 @@ class Resource < AST::ResourceReference :exported => self.exported, :virtual => virt, :source => scope.source, - :scope => scope + :scope => scope, + :strict => true ) # And then store the resource in the compiler. diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index 3f6d73d..2d71079 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -110,9 +110,6 @@ class Puppet::Parser::Resource < Puppet::Resource def initialize(type, title, options) @scope = options[:scope] - self.relative_type = type - self.title = title - @params = {} # Define all of the parameters if params = options[:params] @@ -136,6 +133,13 @@ class Puppet::Parser::Resource < Puppet::Resource @source ||= scope.source + self.relative_type = type + self.title = title + + if strict? and ! resource_type + raise ArgumentError, "Invalid resource type #{type}" + end + tag(self.type) tag(self.title) if valid_tag?(self.title.to_s) end diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb index b1bd944..df808e1 100644 --- a/lib/puppet/resource.rb +++ b/lib/puppet/resource.rb @@ -12,7 +12,7 @@ class Puppet::Resource extend Puppet::Util::Pson include Enumerable - attr_accessor :file, :line, :catalog, :exported, :virtual, :validate_parameters + attr_accessor :file, :line, :catalog, :exported, :virtual, :validate_parameters, :strict attr_reader :title, :namespaces attr_writer :relative_type @@ -130,7 +130,7 @@ class Puppet::Resource end end - %w{exported virtual}.each do |m| + %w{exported virtual strict}.each do |m| define_method(m+"?") do self.send(m) end @@ -141,10 +141,7 @@ class Puppet::Resource @parameters = {} @namespaces = [""] - (attributes[:parameters] || {}).each do |param, value| - self[param] = value - end - + # Set things like namespaces and strictness first. attributes.each do |attr, value| next if attr == :parameters send(attr.to_s + "=", value) @@ -157,8 +154,17 @@ class Puppet::Resource self.type = tmp_type self.title = tmp_title + (attributes[:parameters] || {}).each do |param, value| + validate_parameter(param) if strict? + self[param] = value + end + tag(self.type) tag(self.title) if valid_tag?(self.title) + + if strict? and ! resource_type + raise ArgumentError, "Invalid resource type #{type}" + end end def ref diff --git a/spec/unit/parser/ast/resource.rb b/spec/unit/parser/ast/resource.rb index ef65f4c..40074c7 100755 --- a/spec/unit/parser/ast/resource.rb +++ b/spec/unit/parser/ast/resource.rb @@ -6,11 +6,11 @@ describe Puppet::Parser::AST::Resource do ast = Puppet::Parser::AST before :each do - @title = stub_everything 'title' - @compiler = stub_everything 'compiler', :environment => Puppet::Node::Environment.new + @title = Puppet::Parser::AST::String.new(:value => "mytitle") + @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("mynode")) @scope = Puppet::Parser::Scope.new(:compiler => @compiler) @scope.stubs(:resource).returns(stub_everything) - @resource = ast::Resource.new(:title => @title, :type => "Resource", :params => ast::ASTArray.new(:children => []) ) + @resource = ast::Resource.new(:title => @title, :type => "file", :params => ast::ASTArray.new(:children => []) ) @resource.stubs(:qualified_type).returns("Resource") end @@ -23,73 +23,57 @@ describe Puppet::Parser::AST::Resource do end it "should evaluate its title" do - - @title.expects(:safeevaluate).with(@scope) - - @resource.evaluate(@scope) + @resource.evaluate(@scope)[0].title.should == "mytitle" end it "should flatten the titles array" do - titles = stub 'titles' - title_array = stub 'title_array', :is_a? => true - - titles.stubs(:safeevaluate).with(@scope).returns(title_array) + titles = [] + %w{one two}.each do |title| + titles << Puppet::Parser::AST::String.new(:value => title) + end - title_array.expects(:flatten).returns([]) + array = Puppet::Parser::AST::ASTArray.new(:children => titles) - @resource.title = titles - @resource.evaluate(@scope) + @resource.title = array + result = @resource.evaluate(@scope).collect { |r| r.title } + result.should be_include("one") + result.should be_include("two") end - it "should create one resource objects per title" do - titles = stub 'titles' - title_array = stub 'title_array', :is_a? => true + it "should create and return one resource objects per title" do + titles = [] + %w{one two}.each do |title| + titles << Puppet::Parser::AST::String.new(:value => title) + end - title_array.stubs(:flatten).returns([...@title]) - titles.stubs(:safeevaluate).with(@scope).returns(title_array) + array = Puppet::Parser::AST::ASTArray.new(:children => titles) - @resource.title = titles - result = @resource.evaluate(@scope) - result[0].should be_instance_of(Puppet::Parser::Resource) - result[0].title.should == @title + @resource.title = array + result = @resource.evaluate(@scope).collect { |r| r.title } + result.should be_include("one") + result.should be_include("two") end it "should handover resources to the compiler" do - resource = stub 'resource' - titles = stub 'titles' - title_array = stub 'title_array', :is_a? => true - - title_array.stubs(:flatten).returns([...@title]) - titles.stubs(:safeevaluate).with(@scope).returns(title_array) - Puppet::Parser::Resource.stubs(:new).returns(resource) - - @compiler.expects(:add_resource).with(@scope, resource) - - @resource.title = titles - @resource.evaluate(@scope) - end - - it "should return the newly created resources" do - resource = stub 'resource' - titles = stub 'titles' - title_array = stub 'title_array', :is_a? => true + titles = [] + %w{one two}.each do |title| + titles << Puppet::Parser::AST::String.new(:value => title) + end - title_array.stubs(:flatten).returns([...@title]) - titles.stubs(:safeevaluate).with(@scope).returns(title_array) + array = Puppet::Parser::AST::ASTArray.new(:children => titles) - @compiler.stubs(:add_resource) + @resource.title = array + result = @resource.evaluate(@scope) - @resource.title = titles - @resource.evaluate(@scope)[0].should be_instance_of(Puppet::Parser::Resource) + result.each do |res| + @compiler.catalog.resource(res.ref).should be_instance_of(Puppet::Parser::Resource) + end end - it "should generate virtual resources if it is virtual" do @resource.virtual = true result = @resource.evaluate(@scope) result[0].should be_virtual - - @resource.evaluate(@scope) end it "should generate virtual and exported resources if it is exported" do diff --git a/spec/unit/resource.rb b/spec/unit/resource.rb index c86ec58..a4952c0 100755 --- a/spec/unit/resource.rb +++ b/spec/unit/resource.rb @@ -250,6 +250,20 @@ describe Puppet::Resource do end end + describe "when running in strict mode" do + it "should be strict" do + Puppet::Resource.new("file", "/path", :strict => true).should be_strict + end + + it "should fail if invalid parameters are used" do + lambda { Puppet::Resource.new("file", "/path", :strict => true, :parameters => {:nosuchparam => "bar"}) }.should raise_error + end + + it "should fail if the resource type cannot be resolved" do + lambda { Puppet::Resource.new("nosuchtype", "/path", :strict => true) }.should raise_error + end + end + describe "when managing parameters" do before do @resource = Puppet::Resource.new("file", "/my/file") -- 1.6.1 -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
