[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. 2.6.5rc1-120-g2247c80

Paul Berry paul at puppetlabs.com
Mon Feb 7 06:42:13 UTC 2011


The following commit has been merged in the upstream branch:
commit b29f86487581d89016b63f5f7780b15d2ec8573e
Merge: 846012158d08fbfbfb83d66652788a46e4eaf2c6 ce5a2bf3ba66d5ce723a6887580b008e8ba4104b
Author: Paul Berry <paul at puppetlabs.com>
Date:   Tue Feb 1 16:00:17 2011 -0800

    Merge branch 'feature/master/5061' into 2.6.next
    
    * feature/master/5061:
      (#5061) - allow special hostclass/define variables to be evaluated as defaults.

diff --combined lib/puppet/resource/type.rb
index 92e1ef7,c19a28c..34fddf1
--- a/lib/puppet/resource/type.rb
+++ b/lib/puppet/resource/type.rb
@@@ -143,26 -143,18 +143,26 @@@ class Puppet::Resource::Typ
    # classes and nodes.  No parameters are be supplied--if this is a
    # parameterized class, then all parameters take on their default
    # values.
 -  def ensure_in_catalog(scope)
 +  def ensure_in_catalog(scope, parameters=nil)
      type == :definition and raise ArgumentError, "Cannot create resources for defined resource types"
      resource_type = type == :hostclass ? :class : :node
  
      # Do nothing if the resource already exists; this makes sure we don't
      # get multiple copies of the class resource, which helps provide the
      # singleton nature of classes.
 -    if resource = scope.catalog.resource(resource_type, name)
 +    # we should not do this for classes with parameters
 +    # if parameters are passed, we should still try to create the resource
 +    # even if it exists so that we can fail
 +    # this prevents us from being able to combine param classes with include
 +    if resource = scope.catalog.resource(resource_type, name) and !parameters
        return resource
      end
 -
      resource = Puppet::Parser::Resource.new(resource_type, name, :scope => scope, :source => self)
 +    if parameters
 +      parameters.each do |k,v|
 +        resource.set_parameter(k,v)
 +      end
 +    end
      instantiate_resource(scope, resource)
      scope.compiler.add_resource(scope, resource)
      resource
@@@ -230,6 -222,19 +230,19 @@@
        set[param] = true
      end
  
+     if @type == :hostclass
+       scope.setvar("title", resource.title.to_s.downcase) unless set.include? :title
+       scope.setvar("name",  resource.name.to_s.downcase ) unless set.include? :name
+     else
+       scope.setvar("title", resource.title              ) unless set.include? :title
+       scope.setvar("name",  resource.name               ) unless set.include? :name
+     end
+     scope.setvar("module_name", module_name) if module_name and ! set.include? :module_name
+ 
+     if caller_name = scope.parent_module_name and ! set.include?(:caller_module_name)
+       scope.setvar("caller_module_name", caller_name)
+     end
+     scope.class_set(self.name,scope) if hostclass? or node?
      # Verify that all required arguments are either present or
      # have been provided with defaults.
      arguments.each do |param, default|
@@@ -246,19 -251,6 +259,6 @@@
        resource[param] = value
      end
  
-     if @type == :hostclass
-       scope.setvar("title", resource.title.to_s.downcase) unless set.include? :title
-       scope.setvar("name",  resource.name.to_s.downcase ) unless set.include? :name
-     else
-       scope.setvar("title", resource.title              ) unless set.include? :title
-       scope.setvar("name",  resource.name               ) unless set.include? :name
-     end
-     scope.setvar("module_name", module_name) if module_name and ! set.include? :module_name
- 
-     if caller_name = scope.parent_module_name and ! set.include?(:caller_module_name)
-       scope.setvar("caller_module_name", caller_name)
-     end
-     scope.class_set(self.name,scope) if hostclass? or node?
    end
  
    # Create a new subscope in which to evaluate our code.
diff --combined spec/unit/resource/type_spec.rb
index 206616e,dc4d032..87b4ab4
--- a/spec/unit/resource/type_spec.rb
+++ b/spec/unit/resource/type_spec.rb
@@@ -261,6 -261,28 +261,28 @@@ describe Puppet::Resource::Type d
        @type = Puppet::Resource::Type.new(:hostclass, "foo")
      end
  
+     ['module_name', 'name', 'title'].each do |variable|
+       it "should allow #{variable} to be evaluated as param default" do
+         @type.module_name = "bar"
+         var = Puppet::Parser::AST::Variable.new({'value' => variable})
+         @type.set_arguments :foo => var
+         @type.set_resource_parameters(@resource, @scope)
+         @scope.lookupvar('foo').should == 'bar'
+       end
+     end
+ 
+     # this test is to clarify a crazy edge case
+     # if you specify these special names as params, the resource
+     # will override the special variables
+     it "resource should override defaults" do
+       @type.set_arguments :name => nil
+       @resource[:name] = 'foobar'
+       var = Puppet::Parser::AST::Variable.new({'value' => 'name'})
+       @type.set_arguments :foo => var
+       @type.set_resource_parameters(@resource, @scope)
+       @scope.lookupvar('foo').should == 'foobar'
+     end
+ 
      it "should set each of the resource's parameters as variables in the scope" do
        @type.set_arguments :foo => nil, :boo => nil
        @resource[:foo] = "bar"
@@@ -496,7 -518,7 +518,7 @@@
  
        it "should evaluate the parent's resource" do
          @type.parent_type(@scope)
 -        
 +
          @type.evaluate_code(@resource)
  
          @scope.class_scope(@parent_type).should_not be_nil
@@@ -504,7 -526,7 +526,7 @@@
  
        it "should not evaluate the parent's resource if it has already been evaluated" do
          @parent_resource.evaluate
 -        
 +
          @type.parent_type(@scope)
  
          @parent_resource.expects(:evaluate).never
@@@ -545,7 -567,7 +567,7 @@@
  
        it "should not evaluate the parent's resource if it has already been evaluated" do
          @parent_resource.evaluate
 -        
 +
          @type.parent_type(@scope)
  
          @parent_resource.expects(:evaluate).never
@@@ -575,7 -597,7 +597,7 @@@
        @code = Puppet::Resource::TypeCollection.new("env")
        @code.add @top
        @code.add @middle
 -      
 +
        @node.environment.stubs(:known_resource_types).returns(@code)
      end
  
@@@ -601,36 -623,12 +623,36 @@@
        @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource)
      end
  
 +    it "should add specified parameters to the resource" do
 +      @top.ensure_in_catalog(@scope, {'one'=>'1', 'two'=>'2'})
 +      @compiler.catalog.resource(:class, "top")['one'].should == '1'
 +      @compiler.catalog.resource(:class, "top")['two'].should == '2'
 +    end
 +
 +    it "should not require params for a param class" do
 +      @top.ensure_in_catalog(@scope, {})
 +      @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource)
 +    end
 +
      it "should evaluate the parent class if one exists" do
        @middle.ensure_in_catalog(@scope)
  
        @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource)
      end
  
 +    it "should evaluate the parent class if one exists" do
 +      @middle.ensure_in_catalog(@scope, {})
 +
 +      @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource)
 +    end
 +
 +    it "should fail if you try to create duplicate class resources" do
 +      othertop = Puppet::Parser::Resource.new(:class, 'top',:source => @source, :scope => @scope )
 +      # add the same class resource to the catalog
 +      @compiler.catalog.add_resource(othertop)
 +      lambda { @top.ensure_in_catalog(@scope, {}) }.should raise_error(Puppet::Resource::Catalog::DuplicateResourceError)
 +    end
 +
      it "should fail to evaluate if a parent class is defined but cannot be found" do
        othertop = Puppet::Resource::Type.new :hostclass, "something", :parent => "yay"
        @code.add othertop

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list