diff options
| author | Luke Kanies <luke@madstop.com> | 2007-11-28 15:20:52 -0600 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-11-28 15:20:52 -0600 |
| commit | 11ae473e3852adcc382a3efea2329586d2e4bcb3 (patch) | |
| tree | 7a7ff70f7d9f7bc9af0635c56690fb9fc810b9f5 | |
| parent | 8127397e1efafc13975b79eabf7ce951c1e90114 (diff) | |
| download | puppet-11ae473e3852adcc382a3efea2329586d2e4bcb3.tar.gz puppet-11ae473e3852adcc382a3efea2329586d2e4bcb3.tar.xz puppet-11ae473e3852adcc382a3efea2329586d2e4bcb3.zip | |
Theoretically, this patch is to fix #917 (which it does), but
there were enough problems fixing it that I decided something
more drastic needed to be done.
This uses the new Puppet::ResourceReference class to canonize
what a resource reference looks like and how to retrieve resources
via their references. Specifically, it guarantees that resource types
are always capitalized, even when they include '::' in them.
While many files are modified in this commit, the majority of changes are
quite small, and most of the changes are fixing the tests to use
capitalized types.
As we look at consolidating some of our resource types, we could consolidate
the ResourceReference stuff at the same time, but at least the
Puppet::Parser::ResourceReference class subclasses the main Puppet::ResourceReference
class.
32 files changed, 214 insertions, 208 deletions
diff --git a/lib/puppet/dsl.rb b/lib/puppet/dsl.rb index 97d06a4ec..c1b86e06d 100644 --- a/lib/puppet/dsl.rb +++ b/lib/puppet/dsl.rb @@ -81,6 +81,7 @@ module Puppet end bucket = Puppet::TransBucket.new(objects) bucket.name = "top" + bucket.type = "class" return bucket end diff --git a/lib/puppet/metatype/attributes.rb b/lib/puppet/metatype/attributes.rb index dc59ce6b6..b83fcdd78 100644 --- a/lib/puppet/metatype/attributes.rb +++ b/lib/puppet/metatype/attributes.rb @@ -428,8 +428,7 @@ class Puppet::Type if defined? @title and @title hash[namevar] = @title else - raise Puppet::Error, - "Was not passed a namevar or title" + raise Puppet::Error, "Was not passed a namevar or title" end end diff --git a/lib/puppet/metatype/manager.rb b/lib/puppet/metatype/manager.rb index e95b6fd07..773f30b1b 100644 --- a/lib/puppet/metatype/manager.rb +++ b/lib/puppet/metatype/manager.rb @@ -111,7 +111,7 @@ module Manager def type(name) @types ||= {} - name = symbolize(name) + name = name.to_s.downcase.to_sym if t = @types[name] return t diff --git a/lib/puppet/metatype/metaparams.rb b/lib/puppet/metatype/metaparams.rb index eb158a47d..349a2c1bb 100644 --- a/lib/puppet/metatype/metaparams.rb +++ b/lib/puppet/metatype/metaparams.rb @@ -276,18 +276,12 @@ class Puppet::Type # we just have a name and a type, and we need to convert it # to an object... tname, name = value - object = nil - if type = Puppet::Type.type(tname) - object = type[name] - else # try to treat it as a component - object = Puppet::Type::Component["#{tname}[#{name}]"] - end + reference = Puppet::ResourceReference.new(tname, name) # Either of the two retrieval attempts could have returned # nil. - unless object - self.fail "Could not retrieve dependency '%s[%s]' of %s" % - [tname.to_s.capitalize, @resource.ref, name] + unless object = reference.resolve + self.fail "Could not retrieve dependency '%s' of %s" % [reference, @resource.ref] end # Are we requiring them, or vice versa? See the method docs diff --git a/lib/puppet/node/configuration.rb b/lib/puppet/node/configuration.rb index 393f23913..5da539e5c 100644 --- a/lib/puppet/node/configuration.rb +++ b/lib/puppet/node/configuration.rb @@ -103,9 +103,7 @@ class Puppet::Node::Configuration < Puppet::PGraph rescue Puppet::Error => detail Puppet.err "Could not apply complete configuration: %s" % detail rescue => detail - if Puppet[:trace] - puts detail.backtrace - end + puts detail.backtrace if Puppet[:trace] Puppet.err "Got an uncaught exception of type %s: %s" % [detail.class, detail] ensure # Don't try to store state unless we're a host config @@ -113,21 +111,15 @@ class Puppet::Node::Configuration < Puppet::PGraph Puppet::Util::Storage.store if host_config? end - if block_given? - yield transaction - end + yield transaction if block_given? - if host_config and (Puppet[:report] or Puppet[:summarize]) - transaction.send_report - end + transaction.send_report if host_config and (Puppet[:report] or Puppet[:summarize]) return transaction ensure @applying = false cleanup() - if defined? transaction and transaction - transaction.cleanup - end + transaction.cleanup if defined? transaction and transaction end # Are we in the middle of applying the configuration? @@ -213,7 +205,7 @@ class Puppet::Node::Configuration < Puppet::PGraph current = nil buckets = {} - unless main = vertices.find { |res| res.type == "class" and res.title == :main } + unless main = vertices.find { |res| res.type == "Class" and res.title == :main } raise Puppet::DevError, "Could not find 'main' class; cannot generate configuration" end @@ -358,10 +350,15 @@ class Puppet::Node::Configuration < Puppet::PGraph # Look a resource up by its reference (e.g., File[/etc/passwd]). def resource(type, title = nil) + # Always create a resource reference, so that it always canonizes how we + # are referring to them. if title - ref = "%s[%s]" % [type.to_s.capitalize, title] + ref = Puppet::ResourceReference.new(type, title).to_s else - ref = type + # If they didn't provide a title, then we expect the first + # argument to be of the form 'Class[name]', which our + # Reference class canonizes for us. + ref = Puppet::ResourceReference.new(nil, type).to_s end if resource = @resource_table[ref] return resource diff --git a/lib/puppet/parser/ast/resource_defaults.rb b/lib/puppet/parser/ast/resource_defaults.rb index 44ec146b0..8f9c1b8df 100644 --- a/lib/puppet/parser/ast/resource_defaults.rb +++ b/lib/puppet/parser/ast/resource_defaults.rb @@ -10,7 +10,10 @@ class Puppet::Parser::AST # object type. def evaluate(hash) scope = hash[:scope] - type = @type.downcase + + # Use a resource reference to canonize the type + ref = Puppet::ResourceReference.new(@type, "whatever") + type = ref.type params = @params.safeevaluate(:scope => scope) parsewrap do diff --git a/lib/puppet/parser/collector.rb b/lib/puppet/parser/collector.rb index cdde51bee..9dd07f31a 100644 --- a/lib/puppet/parser/collector.rb +++ b/lib/puppet/parser/collector.rb @@ -28,7 +28,9 @@ class Puppet::Parser::Collector def initialize(scope, type, equery, vquery, form) @scope = scope - @type = type + + # Canonize the type + @type = Puppet::ResourceReference.new(type, "whatever").type @equery = equery @vquery = vquery @@ -98,8 +100,7 @@ class Puppet::Parser::Collector end def collect_exported_resources - raise Puppet::ParseError, - "realize() is not yet implemented for exported resources" + raise Puppet::ParseError, "realize() is not yet implemented for exported resources" end # Collect resources directly; this is the result of using 'realize', diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index 2f7fff13f..3f346166e 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -131,8 +131,9 @@ class Puppet::Parser::Resource end @tags = [] - @tags << @ref.type.to_s - @tags << @ref.title.to_s if @ref.title.to_s =~ /^[-\w]+$/ + tag(@ref.type) + tag(@ref.title) if @ref.title.to_s =~ /^[-\w]+$/ + if scope.resource @tags += scope.resource.tags end @@ -225,15 +226,14 @@ class Puppet::Parser::Resource # Add a tag to our current list. These tags will be added to all # of the objects contained in this scope. def tag(*ary) - ary.each { |tag| - tag = tag.to_s + ary.collect { |tag| tag.to_s.downcase }.collect { |tag| tag.split("::") }.flatten.each do |tag| unless tag =~ /^\w[-\w]*$/ fail Puppet::ParseError, "Invalid tag %s" % tag.inspect end unless @tags.include?(tag) @tags << tag end - } + end end def tags diff --git a/lib/puppet/parser/resource/reference.rb b/lib/puppet/parser/resource/reference.rb index 543ee7195..1dd816093 100644 --- a/lib/puppet/parser/resource/reference.rb +++ b/lib/puppet/parser/resource/reference.rb @@ -1,9 +1,11 @@ +require 'puppet/resource_reference' + # A reference to a resource. Mostly just the type and title. -class Puppet::Parser::Resource::Reference +class Puppet::Parser::Resource::Reference < Puppet::ResourceReference include Puppet::Util::MethodHelper include Puppet::Util::Errors - attr_accessor :type, :title, :builtin, :file, :line, :scope + attr_accessor :builtin, :file, :line, :scope # Are we a builtin type? def builtin? @@ -19,7 +21,7 @@ class Puppet::Parser::Resource::Reference end def builtintype - if t = Puppet::Type.type(self.type) and t.name != :component + if t = Puppet::Type.type(self.type.downcase) and t.name != :component t else nil @@ -30,28 +32,24 @@ class Puppet::Parser::Resource::Reference # definitions or nodes. def definedtype unless defined? @definedtype - type = self.type.to_s.downcase - name = self.title - case type - when "class": # look for host classes + case self.type + when "Class": # look for host classes if self.title == :main tmp = @scope.findclass("") else tmp = @scope.findclass(self.title) end - when "node": # look for node definitions + when "Node": # look for node definitions tmp = @scope.parser.nodes[self.title] else # normal definitions # We have to swap these variables around so the errors are right. - name = type - type = "type" tmp = @scope.finddefine(self.type) end if tmp @definedtype = tmp else - fail Puppet::ParseError, "Could not find resource %s '%s'" % [type, name] + fail Puppet::ParseError, "Could not find resource '%s'" % self end end @@ -67,13 +65,6 @@ class Puppet::Parser::Resource::Reference return [type.to_s,title.to_s] end - def to_s - unless defined? @namestring - @namestring = "%s[%s]" % [type.split("::").collect { |s| s.capitalize }.join("::"), title] - end - @namestring - end - def typeclass unless defined? @typeclass if tmp = builtintype || definedtype diff --git a/lib/puppet/rails/host.rb b/lib/puppet/rails/host.rb index e0a26bdd4..9ecacdd6f 100644 --- a/lib/puppet/rails/host.rb +++ b/lib/puppet/rails/host.rb @@ -29,9 +29,7 @@ class Puppet::Rails::Host < ActiveRecord::Base # Store our host in the database. def self.store(node, resources) - unless name = hash[:name] - raise ArgumentError, "You must specify the hostname for storage" - end + raise ArgumentError, "You must specify the hostname for storage" unless name = hash[:name] args = {} diff --git a/lib/puppet/resource_reference.rb b/lib/puppet/resource_reference.rb index 5e4cc649e..479de6127 100644 --- a/lib/puppet/resource_reference.rb +++ b/lib/puppet/resource_reference.rb @@ -11,8 +11,11 @@ class Puppet::ResourceReference attr_accessor :title, :configuration def initialize(type, title) - @title = title - self.type = type + # This will set @type if it looks like a resource reference. + self.title = title + + # Don't override whatever was done by setting the title. + self.type = type if self.type.nil? @builtin_type = nil end @@ -30,9 +33,24 @@ class Puppet::ResourceReference end end + # If the title has square brackets, treat it like a reference and + # set things appropriately; else, just set it. + def title=(value) + if value =~ /^(.+)\[(.+)\]$/ + self.type = $1 + @title = $2 + else + @title = value + end + end + # Canonize the type so we know it's always consistent. def type=(value) - @type = value.to_s.split("::").collect { |s| s.capitalize }.join("::") + if value.nil? or value.to_s.downcase == "component" + @type = "Class" + else + @type = value.to_s.split("::").collect { |s| s.capitalize }.join("::") + end end # Convert to the standard way of referring to resources. diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb index 6a573489c..a448e28a5 100644 --- a/lib/puppet/transportable.rb +++ b/lib/puppet/transportable.rb @@ -1,4 +1,5 @@ require 'puppet' +require 'puppet/resource_reference' require 'yaml' module Puppet @@ -35,13 +36,9 @@ module Puppet def ref unless defined? @ref - if @type == :component - @ref = @name - else - @ref = "%s[%s]" % [type_capitalized, name] - end + @ref = Puppet::ResourceReference.new(@type, @name) end - @ref + @ref.to_s end def tags @@ -50,18 +47,10 @@ module Puppet # Convert a defined type into a component. def to_component - tmpname = nil - - # Nodes have the same name and type - if self.name - tmpname = "%s[%s]" % [type_capitalized, self.name] - else - tmpname = @type - end - trans = TransObject.new(tmpname, :component) + trans = TransObject.new(ref, :component) @params.each { |param,value| next unless Puppet::Type::Component.validattr?(param) - Puppet.debug "Defining %s on %s of type %s" % [param,@name,@type] + Puppet.debug "Defining %s on %s" % [param, ref] trans[param] = value } Puppet::Type::Component.create(trans) @@ -90,14 +79,7 @@ module Puppet end def to_ref - unless defined? @res_ref - if self.type and self.name - @res_ref = "%s[%s]" % [type_capitalized, self.name] - else - @res_ref = nil - end - end - @res_ref + ref end def to_type @@ -110,11 +92,6 @@ module Puppet return retobj end - - # Return the type fully capitalized correctly. - def type_capitalized - type.to_s.split("::").collect { |s| s.capitalize }.join("::") - end end # Just a linear container for objects. Behaves mostly like an array, except @@ -230,32 +207,21 @@ module Puppet end def to_ref + return nil unless self.type and self.name unless defined? @ref - if self.type and self.name - @ref = "%s[%s]" % [self.type, self.name] - else - @ref = nil - end + @ref = Puppet::ResourceReference.new(self.type, self.name) end - @ref + @ref.to_s end def to_type - unless defined? @type - Puppet.debug "TransBucket '%s' has no type" % @name - end + Puppet.debug("TransBucket '%s' has no type" % @name) unless defined? @type # Nodes have the same name and type - if self.name - tmpname = "%s[%s]" % [@type, self.name] - else - tmpname = @type - end - trans = TransObject.new(tmpname, :component) + trans = TransObject.new(to_ref, :component) if defined? @parameters @parameters.each { |param,value| - Puppet.debug "Defining %s on %s of type %s" % - [param,@name,@type] + Puppet.debug "Defining %s on %s" % [param, to_ref] trans[param] = value } end diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index 39a26f1fa..31ffe3bfb 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -10,6 +10,7 @@ require 'puppet/metatype/manager' require 'puppet/util/errors' require 'puppet/util/log_paths' require 'puppet/util/logging' +require 'puppet/resource_reference' # see the bottom of the file for the rest of the inclusions diff --git a/lib/puppet/type/component.rb b/lib/puppet/type/component.rb index 7aa24a302..6dc90596d 100644 --- a/lib/puppet/type/component.rb +++ b/lib/puppet/type/component.rb @@ -48,17 +48,6 @@ Puppet::Type.newtype(:component) do @children.each { |child| yield child } end - # flatten all children, sort them, and evaluate them in order - # this is only called on one component over the whole system - # this also won't work with scheduling, but eh - def evaluate - raise "Component#evaluate is deprecated" - self.finalize unless self.finalized? - transaction = Puppet::Transaction.new(self) - transaction.component = self - return transaction - end - # Do all of the polishing off, mostly doing autorequires and making # dependencies. This will get run once on the top-level component, # and it will do everything necessary. @@ -97,10 +86,10 @@ Puppet::Type.newtype(:component) do @children = [] super - # If the title isn't a full resource reference, assume - # we're a class and make an alias for that. - unless @title.to_s.include?("[") - self.class.alias("class[%s]" % @title, self) + @reference = Puppet::ResourceReference.new(:component, @title) + + unless self.class[@reference.to_s] + self.class.alias(@reference.to_s, self) end end @@ -123,16 +112,16 @@ Puppet::Type.newtype(:component) do # Component paths are special because they function as containers. def pathbuilder - tmp = [] - myname = "" - if self.title =~ /^class\[(.+)\]$/i + if @reference.type == "Class" # 'main' is the top class, so we want to see '//' instead of # its name. - unless $1 == "main" - myname = $1 + if @reference.title == "main" + myname = "" + else + myname = @reference.title end else - myname = self.title + myname = @reference.to_s end if p = self.parent return [p.pathbuilder, myname] @@ -142,21 +131,12 @@ Puppet::Type.newtype(:component) do end def ref - title + @reference.to_s end - # We have a different way of setting the title + # We want our title to just be the whole reference, rather than @title. def title - unless defined? @title - if self[:type] == self[:name] # this is the case for classes - @title = self[:type] - elsif self[:name] =~ /\[.+\]/ # most components already have ref info in the name - @title = self[:name] - else # else, set it up - @title = "%s[%s]" % [self[:type].capitalize, self[:name]] - end - end - return @title + @reference.to_s end def refresh @@ -169,10 +149,6 @@ Puppet::Type.newtype(:component) do end def to_s - if self.title =~ /\[/ - return self.title - else - return "component(%s)" % self.title - end + @reference.to_s end end diff --git a/spec/unit/node/configuration.rb b/spec/unit/node/configuration.rb index 0a8b47fc5..0023e0f2b 100755 --- a/spec/unit/node/configuration.rb +++ b/spec/unit/node/configuration.rb @@ -351,6 +351,18 @@ describe Puppet::Node::Configuration, " when functioning as a resource container @config.vertices.find { |r| r.ref == @one.ref }.should equal(@one) end + it "should canonize how resources are referred to during retrieval when both type and title are provided" do + @config.add_resource(@one) + + @config.resource("me", "one").should equal(@one) + end + + it "should canonize how resources are referred to during retrieval when just the title is provided" do + @config.add_resource(@one) + + @config.resource("me[one]", nil).should equal(@one) + end + it "should not allow two resources with the same resource reference" do @config.add_resource(@one) proc { @config.add_resource(@dupe) }.should raise_error(ArgumentError) diff --git a/spec/unit/other/transbucket.rb b/spec/unit/other/transbucket.rb index 241529ebe..10c551752 100755 --- a/spec/unit/other/transbucket.rb +++ b/spec/unit/other/transbucket.rb @@ -14,7 +14,7 @@ describe Puppet::TransBucket do resource = nil proc { resource = @bucket.to_type }.should_not raise_error resource.should be_instance_of(Puppet::Type::Component) - resource.title.should == "user[luke]" + resource.title.should == "User[luke]" end it "should accept TransObjects into its children list" do @@ -44,7 +44,14 @@ describe Puppet::TransBucket do it "should return the title as its reference" do @bucket.name = "luke" @bucket.type = "user" - @bucket.to_ref.should == "user[luke]" + @bucket.to_ref.should == "User[luke]" + end + + it "should canonize resource references when the type is 'component'" do + @bucket.name = 'something' + @bucket.type = 'foo::bar' + + @bucket.to_ref.should == "Foo::Bar[something]" end end @@ -73,7 +80,7 @@ describe Puppet::TransBucket, " when generating a configuration" do @config = @top.to_configuration @users = %w{top middle bottom} - @fakes = %w{fake[bottom] fake[middle] fake[top]} + @fakes = %w{Fake[bottom] Fake[middle] Fake[top]} end it "should convert all transportable objects to RAL resources" do diff --git a/spec/unit/other/transobject.rb b/spec/unit/other/transobject.rb index eaca855db..830d4923d 100755 --- a/spec/unit/other/transobject.rb +++ b/spec/unit/other/transobject.rb @@ -4,6 +4,13 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/transportable' +describe Puppet::TransObject do + it "should canonize resource references" do + resource = Puppet::TransObject.new("me", "foo::bar") + resource.ref.should == 'Foo::Bar[me]' + end +end + describe Puppet::TransObject, " when serializing" do before do @resource = Puppet::TransObject.new("/my/file", "file") diff --git a/spec/unit/parser/collector.rb b/spec/unit/parser/collector.rb index 72c4c627c..c0e5f2298 100755 --- a/spec/unit/parser/collector.rb +++ b/spec/unit/parser/collector.rb @@ -7,7 +7,7 @@ require 'puppet/parser/collector' describe Puppet::Parser::Collector, "when initializing" do before do @scope = mock 'scope' - @resource_type = mock 'resource_type' + @resource_type = 'resource_type' @form = :exported @vquery = mock 'vquery' @equery = mock 'equery' @@ -20,7 +20,7 @@ describe Puppet::Parser::Collector, "when initializing" do end it "should require a resource type" do - @collector.type.should equal(@resource_type) + @collector.type.should == 'Resource_type' end it "should only accept :virtual or :exported as the collector form" do @@ -34,6 +34,11 @@ describe Puppet::Parser::Collector, "when initializing" do it "should accept an optional exported query" do @collector.equery.should equal(@equery) end + + it "should canonize the type name" do + @collector = Puppet::Parser::Collector.new(@scope, "resource::type", @equery, @vquery, @form) + @collector.type.should == "Resource::Type" + end end describe Puppet::Parser::Collector, "when collecting specific virtual resources" do @@ -95,15 +100,15 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do @scope = mock 'scope' @compile = mock 'compile' @scope.stubs(:compile).returns(@compile) - @resource_type = :mytype + @resource_type = "Mytype" @vquery = proc { |res| true } @collector = Puppet::Parser::Collector.new(@scope, @resource_type, nil, @vquery, :virtual) end it "should find all resources matching the vquery" do - one = stub 'one', :type => :mytype, :virtual? => true - two = stub 'two', :type => :mytype, :virtual? => true + one = stub 'one', :type => "Mytype", :virtual? => true + two = stub 'two', :type => "Mytype", :virtual? => true one.stubs(:virtual=) two.stubs(:virtual=) @@ -114,7 +119,7 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do end it "should mark all matched resources as non-virtual" do - one = stub 'one', :type => :mytype, :virtual? => true + one = stub 'one', :type => "Mytype", :virtual? => true one.expects(:virtual=).with(false) @@ -124,8 +129,8 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do end it "should return matched resources" do - one = stub 'one', :type => :mytype, :virtual? => true - two = stub 'two', :type => :mytype, :virtual? => true + one = stub 'one', :type => "Mytype", :virtual? => true + two = stub 'two', :type => "Mytype", :virtual? => true one.stubs(:virtual=) two.stubs(:virtual=) @@ -136,8 +141,8 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do end it "should return all resources of the correct type if there is no virtual query" do - one = stub 'one', :type => :mytype, :virtual? => true - two = stub 'two', :type => :mytype, :virtual? => true + one = stub 'one', :type => "Mytype", :virtual? => true + two = stub 'two', :type => "Mytype", :virtual? => true one.expects(:virtual=).with(false) two.expects(:virtual=).with(false) @@ -150,7 +155,7 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do end it "should not return or mark resources of a different type" do - one = stub 'one', :type => :mytype, :virtual? => true + one = stub 'one', :type => "Mytype", :virtual? => true two = stub 'two', :type => :other, :virtual? => true one.expects(:virtual=).with(false) @@ -162,7 +167,7 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do end it "should not return or mark non-virtual resources" do - one = stub 'one', :type => :mytype, :virtual? => false + one = stub 'one', :type => "Mytype", :virtual? => false two = stub 'two', :type => :other, :virtual? => false one.expects(:virtual=).never @@ -176,8 +181,8 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do it "should not return or mark non-matching resources" do @collector.vquery = proc { |res| res.name == :one } - one = stub 'one', :name => :one, :type => :mytype, :virtual? => true - two = stub 'two', :name => :two, :type => :mytype, :virtual? => true + one = stub 'one', :name => :one, :type => "Mytype", :virtual? => true + two = stub 'two', :name => :two, :type => "Mytype", :virtual? => true one.expects(:virtual=).with(false) two.expects(:virtual=).never @@ -195,7 +200,7 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do @scope = stub 'scope', :host => "myhost", :debug => nil @compile = mock 'compile' @scope.stubs(:compile).returns(@compile) - @resource_type = :mytype + @resource_type = "Mytype" @equery = "test = true" @vquery = proc { |r| true } @@ -225,8 +230,8 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do it "should return all matching resources from the current compile" do stub_rails(true) - one = stub 'one', :type => :mytype, :virtual? => true, :exported? => true - two = stub 'two', :type => :mytype, :virtual? => true, :exported? => true + one = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true + two = stub 'two', :type => "Mytype", :virtual? => true, :exported? => true one.stubs(:exported=) one.stubs(:virtual=) @@ -241,7 +246,7 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do it "should mark all returned resources as not exported" do stub_rails(true) - one = stub 'one', :type => :mytype, :virtual? => true, :exported? => true + one = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true one.expects(:exported=).with(false) one.stubs(:virtual=) @@ -254,7 +259,7 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do it "should mark all returned resources as not virtual" do stub_rails(true) - one = stub 'one', :type => :mytype, :virtual? => true, :exported? => true + one = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true one.stubs(:exported=) one.expects(:virtual=).with(false) @@ -268,7 +273,7 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do stub_rails() Puppet::Rails::Host.stubs(:find_by_name).returns(nil) - one = stub 'one', :restype => :mytype, :title => "one", :virtual? => true, :exported? => true + one = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true Puppet::Rails::Resource.stubs(:find).returns([one]) resource = mock 'resource' @@ -288,7 +293,7 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do stub_rails() Puppet::Rails::Host.stubs(:find_by_name).returns(nil) - one = stub 'one', :restype => :mytype, :title => "one", :virtual? => true, :exported? => true + one = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true Puppet::Rails::Resource.stubs(:find).returns([one]) resource = mock 'resource' @@ -308,8 +313,8 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do stub_rails() Puppet::Rails::Host.stubs(:find_by_name).returns(nil) - rails = stub 'one', :restype => :mytype, :title => "one", :virtual? => true, :exported? => true, :id => 1, :ref => "yay" - inmemory = stub 'one', :type => :mytype, :virtual? => true, :exported? => true, :rails_id => 2 + rails = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true, :id => 1, :ref => "yay" + inmemory = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true, :rails_id => 2 Puppet::Rails::Resource.stubs(:find).returns([rails]) @@ -327,8 +332,8 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do stub_rails() Puppet::Rails::Host.stubs(:find_by_name).returns(nil) - rails = stub 'one', :restype => :mytype, :title => "one", :virtual? => true, :exported? => true, :id => 1, :ref => "yay" - inmemory = stub 'one', :type => :mytype, :virtual? => true, :exported? => true, :rails_id => 1 + rails = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true, :id => 1, :ref => "yay" + inmemory = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true, :rails_id => 1 Puppet::Rails::Resource.stubs(:find).returns([rails]) @@ -350,7 +355,7 @@ describe Puppet::Parser::Collector, "when building its ActiveRecord query for co @scope = stub 'scope', :host => "myhost", :debug => nil @compile = mock 'compile' @scope.stubs(:compile).returns(@compile) - @resource_type = :mytype + @resource_type = "Mytype" @equery = nil @vquery = proc { |r| true } @@ -390,7 +395,7 @@ describe Puppet::Parser::Collector, "when building its ActiveRecord query for co it "should only search for exported resources with the matching type" do Puppet::Rails::Resource.stubs(:find).with { |*arguments| options = arguments[3] - options[:conditions][0].include?("(exported=? AND restype=?)") and options[:conditions][1] == true and options[:conditions][2] == :mytype + options[:conditions][0].include?("(exported=? AND restype=?)") and options[:conditions][1] == true and options[:conditions][2] == "Mytype" }.returns([]) end diff --git a/spec/unit/parser/resource/reference.rb b/spec/unit/parser/resource/reference.rb index 45af3d938..24b70d088 100755 --- a/spec/unit/parser/resource/reference.rb +++ b/spec/unit/parser/resource/reference.rb @@ -15,7 +15,7 @@ describe Puppet::Parser::Resource::Reference do proc { @type.new(:type => "file") }.should raise_error(Puppet::DevError) end - it "should know when it models a builtin type" do + it "should know when it refers to a builtin type" do ref = @type.new(:type => "file", :title => "/tmp/yay") ref.builtin?.should be_true ref.builtintype.should equal(Puppet::Type.type(:file)) @@ -23,13 +23,18 @@ describe Puppet::Parser::Resource::Reference do it "should return a relationship-style resource reference when asked" do ref = @type.new(:type => "file", :title => "/tmp/yay") - ref.to_ref.should == ["file", "/tmp/yay"] + ref.to_ref.should == ["File", "/tmp/yay"] end it "should return a resource reference string when asked" do ref = @type.new(:type => "file", :title => "/tmp/yay") ref.to_s.should == "File[/tmp/yay]" end + + it "should canonize resource references" do + ref = @type.new(:type => "foo::bar", :title => "/tmp/yay") + ref.to_s.should == "Foo::Bar[/tmp/yay]" + end end describe Puppet::Parser::Resource::Reference, " when modeling defined types" do @@ -45,22 +50,21 @@ describe Puppet::Parser::Resource::Reference, " when modeling defined types" do @compile = Puppet::Parser::Compile.new(@node, @parser) end - it "should be able to model definitions" do + it "should be able to find defined types" do ref = @type.new(:type => "mydefine", :title => "/tmp/yay", :scope => @compile.topscope) ref.builtin?.should be_false ref.definedtype.should equal(@definition) end - it "should be able to model classes" do + it "should be able to find classes" do ref = @type.new(:type => "class", :title => "myclass", :scope => @compile.topscope) ref.builtin?.should be_false ref.definedtype.should equal(@class) end - it "should be able to model nodes" do + it "should be able to find nodes" do ref = @type.new(:type => "node", :title => "mynode", :scope => @compile.topscope) ref.builtin?.should be_false ref.definedtype.object_id.should == @nodedef.object_id end end - diff --git a/spec/unit/ral/type.rb b/spec/unit/ral/type.rb index adb40595e..60b99eeb8 100755 --- a/spec/unit/ral/type.rb +++ b/spec/unit/ral/type.rb @@ -20,7 +20,7 @@ describe Puppet::Type, " when in a configuration" do end it "should set its parent to its in edge" do - @one.parent.ref.should equal(@container.ref) + @one.parent.ref.should == @container.ref end after do diff --git a/spec/unit/resource_reference.rb b/spec/unit/resource_reference.rb index dad33866c..93eeaa5b8 100755 --- a/spec/unit/resource_reference.rb +++ b/spec/unit/resource_reference.rb @@ -16,6 +16,30 @@ describe Puppet::ResourceReference do it "should canonize qualified types so all strings are capitalized" do Puppet::ResourceReference.new("foo::bar", "foo").type.should == "Foo::Bar" end + + it "should set its type to 'Class' and its title to the passed title if the passed type is :component and the title has no square brackets in it" do + ref = Puppet::ResourceReference.new(:component, "foo") + ref.type.should == "Class" + ref.title.should == "foo" + end + + it "should interpret the title as a reference and assign appropriately if the type is :component and the title contains square brackets" do + ref = Puppet::ResourceReference.new(:component, "foo::bar[yay]") + ref.type.should == "Foo::Bar" + ref.title.should == "yay" + end + + it "should set the type to 'Class' if it is nil and the title contains no square brackets" do + ref = Puppet::ResourceReference.new(nil, "yay") + ref.type.should == "Class" + ref.title.should == "yay" + end + + it "should interpret the title as a reference and assign appropriately if the type is nil and the title contains square brackets" do + ref = Puppet::ResourceReference.new(nil, "foo::bar[yay]") + ref.type.should == "Foo::Bar" + ref.title.should == "yay" + end end describe Puppet::ResourceReference, "when resolving resources without a configuration" do diff --git a/test/language/ast.rb b/test/language/ast.rb index 141d27087..b31012d38 100755 --- a/test/language/ast.rb +++ b/test/language/ast.rb @@ -79,7 +79,7 @@ class TestAST < Test::Unit::TestCase hash = nil assert_nothing_raised do - hash = scope.lookupdefaults("file") + hash = scope.lookupdefaults("File") end hash.each do |name, value| diff --git a/test/language/ast/definition.rb b/test/language/ast/definition.rb index b4d58a289..2a71aaa45 100755 --- a/test/language/ast/definition.rb +++ b/test/language/ast/definition.rb @@ -69,7 +69,7 @@ class TestASTDefinition < Test::Unit::TestCase firstobj = config.findresource("File[/tmp/first]") assert(firstobj, "Did not create /tmp/first obj") - assert_equal("file", firstobj.type) + assert_equal("File", firstobj.type) assert_equal("/tmp/first", firstobj.title) assert_equal("nobody", firstobj[:owner]) assert_equal("755", firstobj[:mode]) @@ -99,7 +99,7 @@ class TestASTDefinition < Test::Unit::TestCase secondobj = config.findresource("File[/tmp/second]") assert(secondobj, "Did not create /tmp/second obj") - assert_equal("file", secondobj.type) + assert_equal("File", secondobj.type) assert_equal("/tmp/second", secondobj.title) assert_equal("daemon", secondobj[:owner]) assert_equal("755", secondobj[:mode]) diff --git a/test/language/ast/resource.rb b/test/language/ast/resource.rb index 49c64112d..c99d98eeb 100755 --- a/test/language/ast/resource.rb +++ b/test/language/ast/resource.rb @@ -36,19 +36,19 @@ class TestASTResource< Test::Unit::TestCase title = "title" # First try a qualified type - assert_equal("one::two", newdef("two", title).evaluate(:scope => twoscope)[0].type, + assert_equal("One::Two", newdef("two", title).evaluate(:scope => twoscope)[0].type, "Defined type was not made fully qualified") # Then try a type that does not need to be qualified - assert_equal("one", newdef("one", title).evaluate(:scope => twoscope)[0].type, + assert_equal("One", newdef("one", title).evaluate(:scope => twoscope)[0].type, "Unqualified defined type was not handled correctly") # Then an unqualified type from within the one namespace - assert_equal("three", newdef("three", title).evaluate(:scope => twoscope)[0].type, + assert_equal("Three", newdef("three", title).evaluate(:scope => twoscope)[0].type, "Defined type was not made fully qualified") # Then a builtin type - assert_equal("file", newdef("file", title).evaluate(:scope => twoscope)[0].type, + assert_equal("File", newdef("file", title).evaluate(:scope => twoscope)[0].type, "Builtin type was not handled correctly") # Now try a type that does not exist, which should throw an error. diff --git a/test/language/ast/resource_reference.rb b/test/language/ast/resource_reference.rb index 5a18d3f45..c9fde078f 100755 --- a/test/language/ast/resource_reference.rb +++ b/test/language/ast/resource_reference.rb @@ -26,7 +26,7 @@ class TestASTResourceReference < Test::Unit::TestCase def test_evaluate @parser.newdefine "one::two" @parser.newdefine "one-two" - [%w{file /tmp/yay}, %w{one::two three}, %w{one-two three}].each do |type, title| + [%w{File /tmp/yay}, %w{One::Two three}, %w{One-two three}].each do |type, title| ref = newref(type, title) evaled = nil @@ -47,7 +47,7 @@ class TestASTResourceReference < Test::Unit::TestCase evaled = ref.evaluate(:scope => @scope) end - assert_equal("class", evaled.type, "Did not set type to 'class'") + assert_equal("Class", evaled.type, "Did not set type to 'class'") assert_equal("one", evaled.title, "Did not look up class corectly") end @@ -61,19 +61,19 @@ class TestASTResourceReference < Test::Unit::TestCase title = "title" # First try a qualified type - assert_equal("one::two", newref("two", title).evaluate(:scope => twoscope).type, + assert_equal("One::Two", newref("two", title).evaluate(:scope => twoscope).type, "Defined type was not made fully qualified") # Then try a type that does not need to be qualified - assert_equal("one", newref("one", title).evaluate(:scope => twoscope).type, + assert_equal("One", newref("one", title).evaluate(:scope => twoscope).type, "Unqualified defined type was not handled correctly") # Then an unqualified type from within the one namespace - assert_equal("three", newref("three", title).evaluate(:scope => twoscope).type, + assert_equal("Three", newref("three", title).evaluate(:scope => twoscope).type, "Defined type was not made fully qualified") # Then a builtin type - assert_equal("file", newref("file", title).evaluate(:scope => twoscope).type, + assert_equal("File", newref("file", title).evaluate(:scope => twoscope).type, "Builtin type was not handled correctly") # Now try a type that does not exist, which should throw an error. diff --git a/test/language/resource.rb b/test/language/resource.rb index 8ad3e62f1..d33e78a9c 100755 --- a/test/language/resource.rb +++ b/test/language/resource.rb @@ -41,7 +41,7 @@ class TestResource < PuppetTest::TestCase end ref = res.instance_variable_get("@ref") - assert_equal("resource", ref.type, "did not set resource type") + assert_equal("Resource", ref.type, "did not set resource type") assert_equal("testing", ref.title, "did not set resource title") end @@ -239,9 +239,9 @@ class TestResource < PuppetTest::TestCase assert_equal("nobody", obj["owner"], "Single-value string was not passed correctly") assert_equal(%w{you me}, obj["group"], "Array of strings was not passed correctly") assert_equal("svn", obj["ignore"], "Array with single string was not turned into single value") - assert_equal(["file", refs[0].title], obj["require"], "Resource reference was not passed correctly") - assert_equal([["file", refs[1].title], ["file", refs[2].title]], obj["subscribe"], "Array of resource references was not passed correctly") - assert_equal(["file", refs[3].title], obj["notify"], "Array with single resource reference was not turned into single value") + assert_equal(["File", refs[0].title], obj["require"], "Resource reference was not passed correctly") + assert_equal([["File", refs[1].title], ["File", refs[2].title]], obj["subscribe"], "Array of resource references was not passed correctly") + assert_equal(["File", refs[3].title], obj["notify"], "Array with single resource reference was not turned into single value") end # FIXME This isn't a great test, but I need to move on. @@ -255,7 +255,7 @@ class TestResource < PuppetTest::TestCase result = res.to_trans assert_equal("yay", result.name, "did not set bucket name correctly") - assert_equal("mydefine", result.type, "did not set bucket type correctly") + assert_equal("Mydefine", result.type, "did not set bucket type correctly") end def test_evaluate @@ -312,7 +312,7 @@ class TestResource < PuppetTest::TestCase res = Parser::Resource.new :type => "evaltest", :title => "yay", :source => mock("source"), :scope => mkscope - assert_equal("evaltest", res.type) + assert_equal("Evaltest", res.type) assert_equal("yay", res.title) assert_equal(false, res.builtin?) end @@ -332,7 +332,7 @@ class TestResource < PuppetTest::TestCase end assert_instance_of(Array, trans["require"]) - assert_equal(["file", "/tmp/ref1"], trans["require"]) + assert_equal(["File", "/tmp/ref1"], trans["require"]) # Now try it when using an array of references. two = Parser::Resource::Reference.new(:type => "file", :title => "/tmp/ref2") @@ -348,7 +348,7 @@ class TestResource < PuppetTest::TestCase assert_instance_of(Array, trans["require"][0]) trans["require"].each do |val| assert_instance_of(Array, val) - assert_equal("file", val[0]) + assert_equal("File", val[0]) assert(val[1] =~ /\/tmp\/ref[0-9]/, "Was %s instead of the file name" % val[1]) end @@ -487,7 +487,7 @@ class TestResource < PuppetTest::TestCase # make sure we get each of them. ptags = resource.tags tags.each do |tag| - assert(ptags.include?(tag), "missing #{tag}") + assert(ptags.include?(tag.downcase), "missing #{tag}") end end end diff --git a/test/language/scope.rb b/test/language/scope.rb index 990ca9694..ec11a864e 100755 --- a/test/language/scope.rb +++ b/test/language/scope.rb @@ -384,7 +384,7 @@ class TestScope < Test::Unit::TestCase # And run the loop. config.send(:evaluate_generators) - %w{file}.each do |type| + %w{File}.each do |type| objects = config.resources.find_all { |r| r.type == type and r.exported } assert(!objects.empty?, "Did not get an exported %s" % type) diff --git a/test/network/handler/resource.rb b/test/network/handler/resource.rb index b1f743082..00a88b57f 100755 --- a/test/network/handler/resource.rb +++ b/test/network/handler/resource.rb @@ -255,6 +255,7 @@ class TestResourceServer < Test::Unit::TestCase bucket = Puppet::TransBucket.new bucket.type = "file" + bucket.name = "test" bucket.push filetrans oldbucket = bucket.dup diff --git a/test/other/dsl.rb b/test/other/dsl.rb index 108c9fa31..b4dd0659b 100755 --- a/test/other/dsl.rb +++ b/test/other/dsl.rb @@ -11,6 +11,7 @@ class TestDSL < Test::Unit::TestCase include Puppet::DSL def teardown + super Puppet::Aspect.clear end diff --git a/test/rails/ast.rb b/test/rails/ast.rb index ac086fde1..e51fa6cf7 100755 --- a/test/rails/ast.rb +++ b/test/rails/ast.rb @@ -62,7 +62,7 @@ class TestRailsAST < PuppetTest::TestCase assert_equal(1, retval.length, "Did not find resource with '#{string}'") res = retval.shift - assert_equal("file", res.restype) + assert_equal("File", res.restype) assert_equal("/tmp/testing", res.title) else assert_equal(0, retval.length, "found a resource with '#{string}'") diff --git a/test/rails/configuration.rb b/test/rails/configuration.rb index ea66bc902..9e2ddfedd 100755 --- a/test/rails/configuration.rb +++ b/test/rails/configuration.rb @@ -36,7 +36,7 @@ class ConfigurationRailsTests < PuppetTest::TestCase Puppet[:storeconfigs] = true Puppet::Rails::Host.expects(:store).with do |node, resources| - if res = resources.find { |r| r.type == "file" and r.title == "/tmp/yay" } + if res = resources.find { |r| r.type == "File" and r.title == "/tmp/yay" } assert_equal("root", res["owner"], "Did not set default on resource") true else diff --git a/test/rails/host.rb b/test/rails/host.rb index dd1bb2a42..5853e8219 100755 --- a/test/rails/host.rb +++ b/test/rails/host.rb @@ -89,10 +89,10 @@ class TestRailsHost < PuppetTest::TestCase end assert(resource[:restype] != "", "Did not get a type from the resource") case resource["restype"] - when "file": + when "File": assert_equal("user#{i}", resource.parameter("owner"), "got no owner for %s" % resource.ref) - when "exec": + when "Exec": assert_equal("user#{i}", resource.parameter("user"), "got no user for %s" % resource.ref) else |
