summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-01-19 13:40:40 -0800
committerLuke Kanies <luke@madstop.com>2008-01-19 13:40:40 -0800
commitf98be4a7198326b26f1072c401b3e337f340db40 (patch)
tree36a3f5637a175bc7da7acb639857bf2c2ca9f0c3
parent2cbab2c6916d6bd7c5ad01b137b3469eeea8d3e4 (diff)
downloadpuppet-f98be4a7198326b26f1072c401b3e337f340db40.tar.gz
puppet-f98be4a7198326b26f1072c401b3e337f340db40.tar.xz
puppet-f98be4a7198326b26f1072c401b3e337f340db40.zip
Fixing #976 -- both the full name of qualified classes and
the class parts are now added as tags. I've also created a Tagging module that we should push throughout the rest of the system that uses tags.
-rw-r--r--CHANGELOG5
-rw-r--r--lib/puppet/parser/resource.rb35
-rw-r--r--lib/puppet/transaction.rb2
-rw-r--r--lib/puppet/util/tagging.rb34
-rwxr-xr-xspec/unit/parser/resource.rb60
-rwxr-xr-xspec/unit/util/tagging.rb79
6 files changed, 189 insertions, 26 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 6f7bc8dd2..f34679fb6 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,8 @@
+ Fixing #976 -- both the full name of qualified classes and
+ the class parts are now added as tags. I've also
+ created a Tagging module that we should push throughout
+ the rest of the system that uses tags.
+
Fixing #995 -- puppetd no longer dies at startup if the server
is not running.
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb
index 3f346166e..7dc42ccec 100644
--- a/lib/puppet/parser/resource.rb
+++ b/lib/puppet/parser/resource.rb
@@ -3,18 +3,18 @@
class Puppet::Parser::Resource
require 'puppet/parser/resource/param'
require 'puppet/parser/resource/reference'
+ require 'puppet/util/tagging'
include Puppet::Util
include Puppet::Util::MethodHelper
include Puppet::Util::Errors
include Puppet::Util::Logging
+ include Puppet::Util::Tagging
attr_accessor :source, :line, :file, :scope, :rails_id
attr_accessor :virtual, :override, :translated
attr_reader :exported, :evaluated, :params
- attr_writer :tags
-
# Determine whether the provided parameter name is a relationship parameter.
def self.relationship_parameter?(name)
unless defined?(@relationship_names)
@@ -86,6 +86,7 @@ class Puppet::Parser::Resource
add_overrides()
add_defaults()
add_metaparams()
+ add_scope_tags()
validate()
end
@@ -130,13 +131,8 @@ class Puppet::Parser::Resource
raise ArgumentError, "Resources do not accept %s" % options.keys.collect { |k| k.to_s }.join(", ")
end
- @tags = []
tag(@ref.type)
- tag(@ref.title) if @ref.title.to_s =~ /^[-\w]+$/
-
- if scope.resource
- @tags += scope.resource.tags
- end
+ tag(@ref.title) if valid_tag?(@ref.title.to_s)
end
# Merge an override resource in. This will throw exceptions if
@@ -223,23 +219,6 @@ class Puppet::Parser::Resource
@ref.to_s
end
- # 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.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
- @tags.dup
- end
-
def to_hash
@params.inject({}) do |hash, ary|
param = ary[1]
@@ -374,6 +353,12 @@ class Puppet::Parser::Resource
end
end
+ def add_scope_tags
+ if scope_resource = scope.resource
+ tag(*scope_resource.tags)
+ end
+ end
+
# Accept a parameter from an override.
def override_parameter(param)
# This can happen if the override is defining a new parameter, rather
diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb
index 6a4981298..f304cadc6 100644
--- a/lib/puppet/transaction.rb
+++ b/lib/puppet/transaction.rb
@@ -424,7 +424,7 @@ class Transaction
# Should we ignore tags?
def ignore_tags?
- ! @catalog.host_config?
+ ! (@catalog.host_config? or Puppet[:name] == "puppet")
end
# this should only be called by a Puppet::Type::Component resource now
diff --git a/lib/puppet/util/tagging.rb b/lib/puppet/util/tagging.rb
new file mode 100644
index 000000000..25d74c420
--- /dev/null
+++ b/lib/puppet/util/tagging.rb
@@ -0,0 +1,34 @@
+# Created on 2008-01-19
+# Copyright Luke Kanies
+
+# A common module to handle tagging.
+module Puppet::Util::Tagging
+ # Add a tag to our current list. These tags will be added to all
+ # of the objects contained in this scope.
+ def tag(*ary)
+ @tags ||= []
+
+ qualified = []
+
+ ary.collect { |tag| tag.to_s.downcase }.each do |tag|
+ fail(Puppet::ParseError, "Invalid tag %s" % tag.inspect) unless valid_tag?(tag)
+ qualified << tag if tag.include?("::")
+ @tags << tag unless @tags.include?(tag)
+ end
+
+ qualified.collect { |name| name.split("::") }.flatten.each { |tag| @tags << tag unless @tags.include?(tag) }
+ end
+
+ # Return a copy of the tag list, so someone can't ask for our tags
+ # and then modify them.
+ def tags
+ @tags ||= []
+ @tags.dup
+ end
+
+ private
+
+ def valid_tag?(tag)
+ tag =~ /^\w[-\w:]*$/
+ end
+end
diff --git a/spec/unit/parser/resource.rb b/spec/unit/parser/resource.rb
index 3d048f7e6..319d8f7d8 100755
--- a/spec/unit/parser/resource.rb
+++ b/spec/unit/parser/resource.rb
@@ -4,6 +4,7 @@ require File.dirname(__FILE__) + '/../../spec_helper'
# LAK: FIXME This is just new tests for resources; I have
# not moved all tests over yet.
+
describe Puppet::Parser::Resource, " when evaluating" do
before do
@type = Puppet::Parser::Resource
@@ -86,4 +87,63 @@ describe Puppet::Parser::Resource, " when finishing" do
@resource["require"].sort.should == %w{container1 container2 resource1 resource2}
end
+
+ it "should add any tags from the scope resource" do
+ scope_resource = stub 'scope_resource', :tags => %w{one two}
+ @scope.stubs(:resource).returns(scope_resource)
+
+ @resource.class.publicize_methods(:add_scope_tags) { @resource.add_scope_tags }
+
+ @resource.tags.should be_include("one")
+ @resource.tags.should be_include("two")
+ end
+end
+
+describe Puppet::Parser::Resource, "when being tagged" do
+ before do
+ @scope_resource = stub 'scope_resource', :tags => %w{srone srtwo}
+ @scope = stub 'scope', :resource => @scope_resource
+ @resource = Puppet::Parser::Resource.new(:type => "file", :title => "yay", :scope => @scope, :source => mock('source'))
+ end
+
+ it "should get tagged with the resource type" do
+ @resource.tags.should be_include("file")
+ end
+
+ it "should get tagged with the title" do
+ @resource.tags.should be_include("yay")
+ end
+
+ it "should get tagged with each name in the title if the title is a qualified class name" do
+ resource = Puppet::Parser::Resource.new(:type => "file", :title => "one::two", :scope => @scope, :source => mock('source'))
+ resource.tags.should be_include("one")
+ resource.tags.should be_include("two")
+ end
+
+ it "should get tagged with each name in the type if the type is a qualified class name" do
+ resource = Puppet::Parser::Resource.new(:type => "one::two", :title => "whatever", :scope => @scope, :source => mock('source'))
+ resource.tags.should be_include("one")
+ resource.tags.should be_include("two")
+ end
+
+ it "should not get tagged with non-alphanumeric titles" do
+ resource = Puppet::Parser::Resource.new(:type => "file", :title => "this is a test", :scope => @scope, :source => mock('source'))
+ resource.tags.should_not be_include("this is a test")
+ end
+
+ it "should fail on tags containing '*' characters" do
+ lambda { @resource.tag("bad*tag") }.should raise_error(Puppet::ParseError)
+ end
+
+ it "should fail on tags starting with '-' characters" do
+ lambda { @resource.tag("-badtag") }.should raise_error(Puppet::ParseError)
+ end
+
+ it "should fail on tags containing ' ' characters" do
+ lambda { @resource.tag("bad tag") }.should raise_error(Puppet::ParseError)
+ end
+
+ it "should allow alpha tags" do
+ lambda { @resource.tag("good_tag") }.should_not raise_error(Puppet::ParseError)
+ end
end
diff --git a/spec/unit/util/tagging.rb b/spec/unit/util/tagging.rb
new file mode 100755
index 000000000..51b69a63c
--- /dev/null
+++ b/spec/unit/util/tagging.rb
@@ -0,0 +1,79 @@
+#!/usr/bin/env ruby
+#
+# Created by Luke Kanies on 2008-01-19.
+# Copyright (c) 2007. All rights reserved.
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/util/tagging'
+
+describe Puppet::Util::Tagging, "when adding tags" do
+ before do
+ @tagger = Object.new
+ @tagger.extend(Puppet::Util::Tagging)
+ end
+
+ it "should have a method for adding tags" do
+ @tagger.should be_respond_to(:tag)
+ end
+
+ it "should have a method for returning all tags" do
+ @tagger.should be_respond_to(:tags)
+ end
+
+ it "should add tags to the returned tag list" do
+ @tagger.tag("one")
+ @tagger.tags.should be_include("one")
+ end
+
+ it "should not add duplicate tags to the returned tag list" do
+ @tagger.tag("one")
+ @tagger.tag("one")
+ @tagger.tags.should == ["one"]
+ end
+
+ it "should return a duplicate of the tag list, rather than the original" do
+ @tagger.tag("one")
+ tags = @tagger.tags
+ tags << "two"
+ @tagger.tags.should_not be_include("two")
+ end
+
+ it "should add all provided tags to the tag list" do
+ @tagger.tag("one", "two")
+ @tagger.tags.should be_include("one")
+ @tagger.tags.should be_include("two")
+ end
+
+ it "should fail on tags containing '*' characters" do
+ lambda { @tagger.tag("bad*tag") }.should raise_error(Puppet::ParseError)
+ end
+
+ it "should fail on tags starting with '-' characters" do
+ lambda { @tagger.tag("-badtag") }.should raise_error(Puppet::ParseError)
+ end
+
+ it "should fail on tags containing ' ' characters" do
+ lambda { @tagger.tag("bad tag") }.should raise_error(Puppet::ParseError)
+ end
+
+ it "should allow alpha tags" do
+ lambda { @tagger.tag("good_tag") }.should_not raise_error(Puppet::ParseError)
+ end
+
+ it "should provide a method for testing tag validity" do
+ @tagger.metaclass.publicize_methods(:valid_tag?) { @tagger.should be_respond_to(:valid_tag?) }
+ end
+
+ it "should add qualified classes as tags" do
+ @tagger.tag("one::two")
+ @tagger.tags.should be_include("one::two")
+ end
+
+ it "should add each part of qualified classes as tags" do
+ @tagger.tag("one::two::three")
+ @tagger.tags.should be_include("one")
+ @tagger.tags.should be_include("two")
+ @tagger.tags.should be_include("three")
+ end
+end