diff options
author | Luke Kanies <luke@madstop.com> | 2007-11-19 01:36:48 -0600 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2007-11-19 01:36:48 -0600 |
commit | 2b14f627aca1d5be69cf6606044df4d6e67f6eba (patch) | |
tree | a20db3a608af483f598f482e743868413da8fd8f | |
parent | 9cf477b6cc771eab7bd29d18c49128571e877987 (diff) | |
download | puppet-2b14f627aca1d5be69cf6606044df4d6e67f6eba.tar.gz puppet-2b14f627aca1d5be69cf6606044df4d6e67f6eba.tar.xz puppet-2b14f627aca1d5be69cf6606044df4d6e67f6eba.zip |
Reverting the changes I'd made toward removing the global
resources. These are commits:
c19835ce9f8a5138b30a1a32ca741c996b0916d2
9290cc89a2206fb5204578f8e91208857a48b147
ffb4c2dbc7314b364d25e4f7be599ef05b767b44
41 files changed, 519 insertions, 467 deletions
diff --git a/lib/puppet/metatype/instances.rb b/lib/puppet/metatype/instances.rb index 8fc25d0bb..8cc648e8f 100644 --- a/lib/puppet/metatype/instances.rb +++ b/lib/puppet/metatype/instances.rb @@ -8,13 +8,11 @@ class Puppet::Type # retrieve a named instance of the current type def self.[](name) - raise "DEPRECATED [], yo" @objects[name] || @aliases[name] end # add an instance by name to the class list of instances def self.[]=(name,object) - raise "DEPRECATED []=, yo" newobj = nil if object.is_a?(Puppet::Type) newobj = object @@ -130,6 +128,36 @@ class Puppet::Type #Puppet.debug "Creating %s[%s]" % [self.name, title] + # if the object already exists + if self.isomorphic? and retobj = self[title] + # if only one of our objects is implicit, then it's easy to see + # who wins -- the non-implicit one. + if retobj.implicit? and ! implicit + Puppet.notice "Removing implicit %s" % retobj.title + # Remove all of the objects, but do not remove their subscriptions. + retobj.remove(false) + + # now pass through and create the new object + elsif implicit + Puppet.debug "Ignoring implicit %s[%s]" % [self.name, title] + return nil + else + # If only one of the objects is being managed, then merge them + if retobj.managed? + raise Puppet::Error, "%s '%s' is already being managed" % + [self.name, title] + else + retobj.merge(hash) + return retobj + end + # We will probably want to support merging of some kind in + # the future, but for now, just throw an error. + #retobj.merge(hash) + + #return retobj + end + end + # create it anew # if there's a failure, destroy the object if it got that far, but raise # the error. @@ -139,6 +167,8 @@ class Puppet::Type Puppet.err "Could not create %s: %s" % [title, detail.to_s] if obj obj.remove(true) + elsif obj = self[title] + obj.remove(true) end raise end @@ -147,6 +177,9 @@ class Puppet::Type obj.implicit = true end + # Store the object by title + self[obj.title] = obj + return obj end diff --git a/lib/puppet/metatype/metaparams.rb b/lib/puppet/metatype/metaparams.rb index 8ac2ead9f..eb158a47d 100644 --- a/lib/puppet/metatype/metaparams.rb +++ b/lib/puppet/metatype/metaparams.rb @@ -272,11 +272,16 @@ class Puppet::Type # which resource is applied first and which resource is considered # to be the event generator. def to_edges - raise(Puppet::DevError, "Cannot create dependencies without a configuration") unless resource.configuration - @value.collect do |tname, name| + @value.collect do |value| # we just have a name and a type, and we need to convert it # to an object... - object = resource.configuration.resource(tname, name) + 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 # Either of the two retrieval attempts could have returned # nil. diff --git a/lib/puppet/metatype/relationships.rb b/lib/puppet/metatype/relationships.rb index 2a5341e5e..4fb78ae56 100644 --- a/lib/puppet/metatype/relationships.rb +++ b/lib/puppet/metatype/relationships.rb @@ -18,9 +18,11 @@ class Puppet::Type # Figure out of there are any objects we can automatically add as # dependencies. def autorequire - raise(ArgumentError, "Cannot autorequire resources without a configuration") unless configuration reqs = [] self.class.eachautorequire { |type, block| + # Ignore any types we can't find, although that would be a bit odd. + next unless typeobj = Puppet.type(type) + # Retrieve the list of names from the block. next unless list = self.instance_eval(&block) unless list.is_a?(Array) @@ -29,16 +31,16 @@ class Puppet::Type # Collect the current prereqs list.each { |dep| + obj = nil # Support them passing objects directly, to save some effort. - if dep.is_a?(Puppet::Type) - next unless configuration.resource(type, dep.title) - resource = dep - else + unless dep.is_a? Puppet::Type # Skip autorequires that we aren't managing - next unless resource = configuration.resource(type, dep) + unless dep = typeobj[dep] + next + end end - reqs << Puppet::Relationship.new(resource, self) + reqs << Puppet::Relationship.new(dep, self) } } diff --git a/lib/puppet/metatype/schedules.rb b/lib/puppet/metatype/schedules.rb index 4d4f93764..96ebce0ab 100644 --- a/lib/puppet/metatype/schedules.rb +++ b/lib/puppet/metatype/schedules.rb @@ -4,8 +4,8 @@ class Puppet::Type # file. def schedule unless defined? @schedule - if name = self[:schedule] and self.configuration - if sched = configuration.resource(:schedule, name) + if name = self[:schedule] + if sched = Puppet.type(:schedule)[name] @schedule = sched else self.fail "Could not find schedule %s" % name diff --git a/lib/puppet/network/client/master.rb b/lib/puppet/network/client/master.rb index 691681f9e..ea351ddc3 100644 --- a/lib/puppet/network/client/master.rb +++ b/lib/puppet/network/client/master.rb @@ -69,6 +69,7 @@ class Puppet::Network::Client::Master < Puppet::Network::Client def clear @configuration.clear(true) if @configuration Puppet::Type.allclear + mkdefault_objects @configuration = nil end @@ -189,8 +190,6 @@ class Puppet::Network::Client::Master < Puppet::Network::Client # Keep the state database up to date. @configuration.host_config = true - - add_default_resources(@configuration) end # A simple proxy method, so it's easy to test. @@ -205,6 +204,17 @@ class Puppet::Network::Client::Master < Puppet::Network::Client self.class.instance = self @running = false + + mkdefault_objects + end + + # Make the default objects necessary for function. + def mkdefault_objects + # First create the default scheduling objects + Puppet::Type.type(:schedule).mkdefaultschedules + + # And filebuckets + Puppet::Type.type(:filebucket).mkdefaultbucket end # Mark that we should restart. The Puppet module checks whether we're running, @@ -549,18 +559,6 @@ class Puppet::Network::Client::Master < Puppet::Network::Client private - # Add our default resources to the configuration. - def add_default_resources(configuration) - # These are the only two resource types with default resources. - # We should probably iterate across all of them, but I think that's - # unnecessarily expensive at this point. - [:schedule, :filebucket].each do |resource_type| - Puppet::Type.type(resource_type).create_default_resources.each do |resource| - configuration.add_resource(resource) unless configuration.resource(resource_type, resource.title) - end - end - end - # Use our cached config, optionally specifying whether this is # necessary because of a failure. def use_cached_config(because_of_failure = false) @@ -584,8 +582,6 @@ class Puppet::Network::Client::Master < Puppet::Network::Client clear return false end - - add_default_resources(@configuration) return true end end diff --git a/lib/puppet/network/handler/fileserver.rb b/lib/puppet/network/handler/fileserver.rb index 7a5a1fe9a..dd00450be 100755 --- a/lib/puppet/network/handler/fileserver.rb +++ b/lib/puppet/network/handler/fileserver.rb @@ -551,25 +551,22 @@ class Puppet::Network::Handler @path = nil end - @cache = {} - super() end def fileobj(path, links) obj = nil - if obj = @cache[path] + if obj = Puppet.type(:file)[path] # This can only happen in local fileserving, but it's an # important one. It'd be nice if we didn't just set # the check params every time, but I'm not sure it's worth # the effort. obj[:check] = CHECKPARAMS else - obj = Puppet::Type.type(:file).create( + obj = Puppet.type(:file).create( :name => path, :check => CHECKPARAMS ) - @cache[path] = obj end if links == :manage diff --git a/lib/puppet/network/handler/resource.rb b/lib/puppet/network/handler/resource.rb index c96bdf6a6..0fcd694fb 100755 --- a/lib/puppet/network/handler/resource.rb +++ b/lib/puppet/network/handler/resource.rb @@ -53,8 +53,8 @@ class Puppet::Network::Handler return "success" end - # Describe a given resource. This returns the 'is' values for every property - # available on the resource type. + # Describe a given object. This returns the 'is' values for every property + # available on the object type. def describe(type, name, retrieve = nil, ignore = [], format = "yaml", client = nil, clientip = nil) Puppet.info "Describing %s[%s]" % [type.to_s.capitalize, name] @local = true unless client @@ -63,23 +63,29 @@ class Puppet::Network::Handler raise Puppet::Error, "Puppet type %s is unsupported" % type end + obj = nil + retrieve ||= :all ignore ||= [] - begin - resource = typeklass.create(:name => name, :check => retrieve) - rescue Puppet::Error => detail - raise Puppet::Error, "%s[%s] could not be created: %s" % - [type, name, detail] + if obj = typeklass[name] + obj[:check] = retrieve + else + begin + obj = typeklass.create(:name => name, :check => retrieve) + rescue Puppet::Error => detail + raise Puppet::Error, "%s[%s] could not be created: %s" % + [type, name, detail] + end end - unless resource + unless obj raise XMLRPC::FaultException.new( 1, "Could not create %s[%s]" % [type, name] ) end - trans = resource.to_trans + trans = obj.to_trans # Now get rid of any attributes they specifically don't want ignore.each do |st| @@ -132,10 +138,11 @@ class Puppet::Network::Handler bucket = Puppet::TransBucket.new bucket.type = typeklass.name - typeklass.instances.each do |resource| - next if ignore.include? resource.name + typeklass.instances.each do |obj| + next if ignore.include? obj.name - bucket << resource.to_trans + #object = Puppet::TransObject.new(obj.name, typeklass.name) + bucket << obj.to_trans end unless @local diff --git a/lib/puppet/node/configuration.rb b/lib/puppet/node/configuration.rb index e131839df..804f357d1 100644 --- a/lib/puppet/node/configuration.rb +++ b/lib/puppet/node/configuration.rb @@ -60,22 +60,11 @@ class Puppet::Node::Configuration < Puppet::PGraph end ref = resource.ref - if @resource_table.include?(ref) raise ArgumentError, "Resource %s is already defined" % ref else @resource_table[ref] = resource end - - # If the name and title differ, set up an alias - if ! resource.is_a?(Puppet::Type::Component) and resource.respond_to?(:title) and resource.name != resource.title - if obj = resource(resource.class.name, resource.name) - raise Puppet::Error, "%s already exists with name %s" % [obj.title, self.name] if resource.class.isomorphic? - else - self.alias(resource, resource.name) - end - end - resource.configuration = self unless is_relationship_graph add_vertex!(resource) end @@ -194,11 +183,6 @@ class Puppet::Node::Configuration < Puppet::PGraph unless klass = Puppet::Type.type(type) raise ArgumentError, "Unknown resource type %s" % type end - if options.is_a?(Puppet::TransObject) - options.configuration = self - else - options[:configuration] = self - end return unless resource = klass.create(options) @transient_resources << resource if applying? @@ -386,11 +370,6 @@ class Puppet::Node::Configuration < Puppet::PGraph end end - # Return an array of the currently-defined resources. - def resources - @resource_table.keys - end - # Add a tag. def tag(*names) names.each do |name| diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb index 8f8c32b8d..6a573489c 100644 --- a/lib/puppet/transportable.rb +++ b/lib/puppet/transportable.rb @@ -52,10 +52,13 @@ module Puppet def to_component tmpname = nil - tmpname = "%s[%s]" % [type_capitalized, self.name] - + # 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) - @params.each { |param,value| next unless Puppet::Type::Component.validattr?(param) Puppet.debug "Defining %s on %s of type %s" % [param,@name,@type] @@ -88,7 +91,11 @@ module Puppet def to_ref unless defined? @res_ref - @res_ref = "%s[%s]" % [type_capitalized, self.name] + if self.type and self.name + @res_ref = "%s[%s]" % [type_capitalized, self.name] + else + @res_ref = nil + end end @res_ref end diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index 3f69bc8b1..f5dd0f8dd 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -228,6 +228,18 @@ class Type self.devfail "I was not passed a namevar" end + # If the name and title differ, set up an alias + if self.name != self.title + if obj = self.class[self.name] + if self.class.isomorphic? + raise Puppet::Error, "%s already exists with name %s" % + [obj.title, self.name] + end + else + self.class.alias(self.name, self) + end + end + if hash.include?(:provider) self[:provider] = hash[:provider] hash.delete(:provider) diff --git a/lib/puppet/type/component.rb b/lib/puppet/type/component.rb index 0a7c182cf..7aa24a302 100644 --- a/lib/puppet/type/component.rb +++ b/lib/puppet/type/component.rb @@ -20,7 +20,7 @@ Puppet::Type.newtype(:component) do desc "The type that this component maps to. Generally some kind of class from the language." - defaultto "class" + defaultto "component" end # Remove a child from the component. @@ -97,8 +97,10 @@ Puppet::Type.newtype(:component) do @children = [] super - unless @title.include?("[") - @title = "%s[%s]" % [self[:type].capitalize, @title] + # 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) end end diff --git a/lib/puppet/type/pfile.rb b/lib/puppet/type/pfile.rb index d6dfd86e0..73c60bd14 100644 --- a/lib/puppet/type/pfile.rb +++ b/lib/puppet/type/pfile.rb @@ -73,11 +73,10 @@ module Puppet " defaultto do - if resource.configuration and bucket_resource = resource.configuration.resource(:filebucket, "puppet") - bucket_resource.bucket - else - nil - end + # Make sure the default file bucket exists. + obj = Puppet::Type.type(:filebucket)["puppet"] || + Puppet::Type.type(:filebucket).create(:name => "puppet") + obj.bucket end munge do |value| @@ -96,7 +95,7 @@ module Puppet # We can't depend on looking this up right now, # we have to do it after all of the objects # have been instantiated. - if @resource.configuration and bucketobj = @resource.configuration.resource(:filebucket, value) + if bucketobj = Puppet::Type.type(:filebucket)[value] @resource.bucket = bucketobj.bucket bucketobj.title else @@ -254,6 +253,11 @@ module Puppet end end + def self.[](path) + return nil unless path + super(path.gsub(/\/+/, '/').sub(/\/$/, '')) + end + # List files, but only one level deep. def self.instances(base = "/") unless FileTest.directory?(base) @@ -309,32 +313,31 @@ module Puppet # a couple of these buckets @@filebuckets ||= {} - super - # Look up our bucket, if there is one - return unless bucket_name = self.bucket - - return if bucket_name.is_a?(Puppet::Network::Client.dipper) - - self.fail("Invalid bucket type %s" % bucket_name.class) unless bucket_name.is_a?(String) - - return self.bucket = bucket if bucket = @@filebuckets[bucket_name] - - if configuration and bucket_resource = configuration.resource(:filebucket, bucket_name) - @@filebuckets[bucket_name] = bucket_resource.bucket - self.bucket = bucket - return - end - - if bucket_name == "puppet" - puts "Creating default bucket" - bucket_resource = Puppet::Type.type(:filebucket).create_default_resources - self.bucket = bucket_resource.bucket - configuration.add_resource(bucket_resource) if configuration - @@filebuckets[bucket_name] = bucket - else - self.fail "Could not find filebucket '%s'" % bucket_name + if bucket = self.bucket + case bucket + when String: + if obj = @@filebuckets[bucket] + # This sets the @value on :backup, too + self.bucket = obj + elsif bucket == "puppet" + obj = Puppet::Network::Client.client(:Dipper).new( + :Path => Puppet[:clientbucketdir] + ) + self.bucket = obj + @@filebuckets[bucket] = obj + elsif obj = Puppet::Type.type(:filebucket).bucket(bucket) + @@filebuckets[bucket] = obj + self.bucket = obj + else + self.fail "Could not find filebucket %s" % bucket + end + when Puppet::Network::Client.client(:Dipper): # things are hunky-dorey + else + self.fail "Invalid bucket type %s" % bucket.class + end end + super end # Create any children via recursion or whatever. diff --git a/lib/puppet/type/pfilebucket.rb b/lib/puppet/type/pfilebucket.rb index 27fea6ac8..cf4e5aac3 100755 --- a/lib/puppet/type/pfilebucket.rb +++ b/lib/puppet/type/pfilebucket.rb @@ -53,11 +53,22 @@ module Puppet defaultto { Puppet[:clientbucketdir] } end + + # get the actual filebucket object + def self.bucket(name) + if object = self[name] + return object.bucket + else + return nil + end + end # Create a default filebucket. - def self.create_default_resources - Puppet.debug "Creating default local filebucket" - [self.create(:name => "puppet", :path => Puppet[:clientbucketdir])] + def self.mkdefaultbucket + unless default = self["puppet"] + default = self.create :name => "puppet", :path => Puppet[:clientbucketdir] + end + default end def self.instances diff --git a/lib/puppet/type/schedule.rb b/lib/puppet/type/schedule.rb index 3f4014cd8..46cff10f5 100755 --- a/lib/puppet/type/schedule.rb +++ b/lib/puppet/type/schedule.rb @@ -311,27 +311,30 @@ module Puppet [] end - def self.create_default_resources - Puppet.debug "Creating default schedules" - resources = [] + def self.mkdefaultschedules # Create our default schedule - resources << self.create( - :name => "puppet", - :period => :hourly, - :repeat => "2" - ) + unless self["puppet"] + Puppet.debug "Creating default schedules" + self.create( + :name => "puppet", + :period => :hourly, + :repeat => "2" + ) + end # And then one for every period @parameters.find { |p| p.name == :period }.values.each { |value| - resources << self.create( - :name => value.to_s, - :period => value - ) + unless self[value.to_s] + self.create( + :name => value.to_s, + :period => value + ) + end } - resources end def match?(previous = nil, now = nil) + # If we've got a value, then convert it to a Time instance if previous previous = Time.at(previous) diff --git a/lib/puppet/util/filetype.rb b/lib/puppet/util/filetype.rb index b1b2c1d96..2f1dabe62 100755 --- a/lib/puppet/util/filetype.rb +++ b/lib/puppet/util/filetype.rb @@ -74,7 +74,7 @@ class Puppet::Util::FileType # Pick or create a filebucket to use. def bucket - Puppet::Type.type(:filebucket).create_default_resources[0].bucket + Puppet::Type.type(:filebucket).mkdefaultbucket.bucket end def initialize(path) diff --git a/spec/unit/network/client/master.rb b/spec/unit/network/client/master.rb index fda729cae..dca923994 100755 --- a/spec/unit/network/client/master.rb +++ b/spec/unit/network/client/master.rb @@ -72,7 +72,6 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d @client.stubs(:fresh?).with(@facts).returns true @client.stubs(:use_cached_config).returns(true) @client.class.stubs(:facts).returns(@facts) - @client.stubs(:add_default_resources) @client.getconfig end @@ -98,7 +97,6 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d config.stubs(:to_configuration).returns(config) config.stubs(:host_config=) config.stubs(:from_cache).returns(true) - @client.stubs(:add_default_resources) @client.getconfig end @@ -129,7 +127,6 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d config.stubs(:to_configuration).returns(config) config.stubs(:host_config=) config.stubs(:from_cache).returns(true) - @client.stubs(:add_default_resources) @client.getconfig end @@ -148,9 +145,9 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d yamlconfig.stubs(:classes) yamlconfig.expects(:to_configuration).returns(config) + config.stubs(:host_config=) config.stubs(:from_cache).returns(true) - @client.stubs(:add_default_resources) @client.getconfig end @@ -213,7 +210,6 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d yamlconfig.expects(:to_configuration).returns(config) config.stubs(:host_config=) - @client.stubs(:add_default_resources) config.expects(:from_cache).returns(false) @@ -240,31 +236,6 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d config.stubs(:from_cache).returns(true) config.expects(:host_config=).with(true) - @client.stubs(:add_default_resources) - - @client.getconfig - end - - it "should add the default resources to the configuration" do - @client.stubs(:dostorage) - @client.class.stubs(:facts).returns(@facts) - @master.stubs(:getconfig).returns("myconfig") - - yamlconfig = mock 'yaml config' - YAML.stubs(:load).returns(yamlconfig) - - @client.stubs(:setclasses) - - config = mock 'config' - - yamlconfig.stubs(:classes) - yamlconfig.stubs(:to_configuration).returns(config) - - config.stubs(:from_cache).returns(true) - - config.stubs(:host_config=).with(true) - - @client.expects(:add_default_resources).with(config) @client.getconfig end @@ -336,7 +307,6 @@ describe Puppet::Network::Client::Master, " when using the cached configuration" config.expects(:to_configuration).returns(ral_config) @client.stubs(:retrievecache).returns("whatever") - @client.stubs(:add_default_resources) Puppet::Network::Client::Master.publicize_methods :use_cached_config do @client.use_cached_config().should be_true end @@ -352,7 +322,6 @@ describe Puppet::Network::Client::Master, " when using the cached configuration" config.expects(:to_configuration).returns(ral_config) @client.stubs(:retrievecache).returns("whatever") - @client.stubs(:add_default_resources) Puppet::Network::Client::Master.publicize_methods :use_cached_config do @client.use_cached_config() end @@ -370,7 +339,6 @@ describe Puppet::Network::Client::Master, " when using the cached configuration" config.expects(:to_configuration).returns(ral_config) @client.stubs(:retrievecache).returns("whatever") - @client.stubs(:add_default_resources) Puppet::Network::Client::Master.publicize_methods :use_cached_config do @client.use_cached_config() end @@ -388,89 +356,10 @@ describe Puppet::Network::Client::Master, " when using the cached configuration" config.expects(:to_configuration).returns(ral_config) @client.stubs(:retrievecache).returns("whatever") - @client.stubs(:add_default_resources) Puppet::Network::Client::Master.publicize_methods :use_cached_config do @client.use_cached_config() end @client.configuration.should equal(ral_config) end - - it "should add the default resources to the configuration" do - config = mock 'config' - YAML.stubs(:load).returns(config) - - ral_config = mock 'ral config' - ral_config.expects(:from_cache=).with(true) - ral_config.stubs(:host_config=) - config.stubs(:to_configuration).returns(ral_config) - - @client.stubs(:retrievecache).returns("whatever") - @client.expects(:add_default_resources).with(ral_config) - Puppet::Network::Client::Master.publicize_methods :use_cached_config do - @client.use_cached_config() - end - end -end - -describe Puppet::Network::Client::Master, " when adding default resources" do - before do - @master = mock 'master' - @client = Puppet::Network::Client.master.new( - :Master => @master - ) - @facts = {"one" => "two", "three" => "four"} - end - - it "should add the default schedules" do - config = mock 'config' - one = stub 'one', :title => "one" - two = stub 'two', :title => "two" - Puppet::Type.type(:schedule).expects(:create_default_resources).with().returns([one, two]) - config.expects(:add_resource).with(one) - config.expects(:add_resource).with(two) - config.stubs(:resource).returns(false) - Puppet::Type.type(:filebucket).stubs(:create_default_resources).returns([]) - Puppet::Network::Client::Master.publicize_methods :add_default_resources do - @client.add_default_resources(config) - end - end - - it "should add the default filebucket" do - config = mock 'config' - Puppet::Type.type(:schedule).stubs(:create_default_resources).returns([]) - one = stub 'one', :title => "one" - two = stub 'two', :title => "two" - Puppet::Type.type(:filebucket).expects(:create_default_resources).with().returns([one, two]) - config.expects(:add_resource).with(one) - config.expects(:add_resource).with(two) - config.stubs(:resource).returns(false) - Puppet::Network::Client::Master.publicize_methods :add_default_resources do - @client.add_default_resources(config) - end - end - - it "should only add a default filebucket if no similarly named bucket already exists" do - config = mock 'config' - Puppet::Type.type(:schedule).stubs(:create_default_resources).returns([]) - one = stub 'one', :title => "one" - Puppet::Type.type(:filebucket).expects(:create_default_resources).with().returns([one]) - config.expects(:resource).with(:filebucket, "one").returns(true) - config.expects(:add_resource).with(one).never - Puppet::Network::Client::Master.publicize_methods :add_default_resources do - @client.add_default_resources(config) - end - end - - it "should only add default schedules if no similarly named schedule already exists" do - config = mock 'config' - one = stub 'one', :title => "one" - Puppet::Type.type(:schedule).stubs(:create_default_resources).returns([one]) - Puppet::Type.type(:filebucket).stubs(:create_default_resources).with().returns([]) - config.expects(:resource).with(:schedule, "one").returns(true) - config.expects(:add_resource).with(one).never - Puppet::Network::Client::Master.publicize_methods :add_default_resources do - @client.add_default_resources(config) - end - end end diff --git a/spec/unit/node/configuration.rb b/spec/unit/node/configuration.rb index 44b98dddb..5780d4fbb 100755 --- a/spec/unit/node/configuration.rb +++ b/spec/unit/node/configuration.rb @@ -301,9 +301,9 @@ end describe Puppet::Node::Configuration, " when functioning as a resource container" do before do @config = Puppet::Node::Configuration.new("host") - @one = stub 'resource1', :ref => "Me[one]", :configuration= => nil, :title => "one", :name => "one" - @two = stub 'resource2', :ref => "Me[two]", :configuration= => nil, :title => "two", :name => "two" - @dupe = stub 'resource3', :ref => "Me[one]", :configuration= => nil, :title => "one", :name => "one" + @one = stub 'resource1', :ref => "Me[one]", :configuration= => nil + @two = stub 'resource2', :ref => "Me[two]", :configuration= => nil + @dupe = stub 'resource3', :ref => "Me[one]", :configuration= => nil end it "should provide a method to add one or more resources" do @@ -412,25 +412,6 @@ describe Puppet::Node::Configuration, " when functioning as a resource container @config.remove_resource(@one) @config.resource("me", "other").should be_nil end - - it "should alias resources whose names are not equal to their titles" do - resource = stub("resource", :name => "one", :title => "two", :ref => "Me[two]", :configuration= => nil) - @config.expects(:alias).with(resource, "one") - @config.add_resource resource - end - - it "should fail to add resources whose names conflict with an existing resource even when the title does not conflict" do - conflict = stub("resource", :name => "one", :ref => "Me[one]", :configuration= => nil) - resource = stub("resource", :name => "one", :title => "other", :ref => "Me[other]", :configuration= => nil) - @config.expects(:alias).with(resource, "one") - @config.add_resource resource - end - - it "should not alias components whose names do not match their titles" do - comp = Puppet::Type::Component.create :name => "one", :title => "two" - @config.expects(:alias).never - @config.add_resource comp - end end module ApplyingConfigurations @@ -557,18 +538,18 @@ end describe Puppet::Node::Configuration, " when creating a relationship graph" do before do @config = Puppet::Node::Configuration.new("host") - @compone = @config.create_resource :component, :name => "one" - @comptwo = @config.create_resource :component, :name => "two", :require => ["class", "one"] - + @compone = Puppet::Type::Component.create :name => "one" + @comptwo = Puppet::Type::Component.create :name => "two", :require => ["class", "one"] @file = Puppet::Type.type(:file) - @one = @config.create_resource :file, :path => "/one" - @two = @config.create_resource :file, :path => "/two" + @one = @file.create :path => "/one" + @two = @file.create :path => "/two" @config.add_edge! @compone, @one @config.add_edge! @comptwo, @two - @three = @config.create_resource :file, :path => "/three" - @four = @config.create_resource :file, :path => "/four", :require => ["file", "/three"] - @five = @config.create_resource :file, :path => "/five" + @three = @file.create :path => "/three" + @four = @file.create :path => "/four", :require => ["file", "/three"] + @five = @file.create :path => "/five" + @config.add_resource @compone, @comptwo, @one, @two, @three, @four, @five @relationships = @config.relationship_graph end diff --git a/test/language/snippets.rb b/test/language/snippets.rb index 05597cdec..0806a40b4 100755 --- a/test/language/snippets.rb +++ b/test/language/snippets.rb @@ -22,14 +22,14 @@ class TestSnippets < Test::Unit::TestCase end def assert_file(path, msg = nil) - unless file = @configuration.resource(:file, path) + unless file = @file[path] msg ||= "Could not find file %s" % path raise msg end end def assert_mode_equal(mode, path) - unless file = @configuration.resource(:file, path) + unless file = @file[path] raise "Could not find file %s" % path end @@ -211,8 +211,8 @@ class TestSnippets < Test::Unit::TestCase path1 = "/tmp/argumenttest1" path2 = "/tmp/argumenttest2" - file1 = @configuration.resource(:file, path1) - file2 = @configuration.resource(:file, path2) + file1 = @file[path1] + file2 = @file[path2] assert_file(path1) assert_mode_equal(0755, path1) @@ -231,7 +231,7 @@ class TestSnippets < Test::Unit::TestCase } paths.each { |path| - file = @configuration.resource(:file, path) + file = @file[path] assert(file, "File %s is missing" % path) assert_mode_equal(0755, path) } @@ -241,7 +241,7 @@ class TestSnippets < Test::Unit::TestCase paths = %w{a b c d e f g h}.collect { |l| "/tmp/iteration%stest" % l } paths.each { |path| - file = @configuration.resource(:file, path) + file = @file[path] assert_file(path) assert_mode_equal(0755, path) } @@ -262,7 +262,7 @@ class TestSnippets < Test::Unit::TestCase dir = "/tmp/testdirtest" assert_file(file) assert_file(dir) - assert_equal(:directory, @configuration.resource(:file, dir).should(:ensure), "Directory is not set to be a directory") + assert_equal(:directory, @file[dir].should(:ensure), "Directory is not set to be a directory") end def snippet_scopetest @@ -349,7 +349,7 @@ class TestSnippets < Test::Unit::TestCase }.each { |count, str| path = "/tmp/singlequote%s" % count assert_file(path) - assert_equal(str, @configuration.resource(:file, path).should(:content)) + assert_equal(str, @file[path].should(:content)) } end @@ -387,20 +387,21 @@ class TestSnippets < Test::Unit::TestCase end def snippet_emptyexec - assert(@configuration.resource(:exec, "touch /tmp/emptyexectest"), "Did not create exec") + assert(Puppet::Type.type(:exec)["touch /tmp/emptyexectest"], + "Did not create exec") end def snippet_multisubs path = "/tmp/multisubtest" assert_file(path) - file = @configuration.resource(:file, path) + file = @file[path] assert_equal("sub2", file.should(:content), "sub2 did not override content") assert_mode_equal(0755, path) end def snippet_collection assert_file("/tmp/colltest1") - assert_nil(@configuration.resource(:file, "/tmp/colltest2"), "Incorrectly collected file") + assert_nil(@file["/tmp/colltest2"], "Incorrectly collected file") end def snippet_virtualresources diff --git a/test/lib/puppettest/support/utils.rb b/test/lib/puppettest/support/utils.rb index 83509733e..d9bd6b2b6 100644 --- a/test/lib/puppettest/support/utils.rb +++ b/test/lib/puppettest/support/utils.rb @@ -27,7 +27,7 @@ module PuppetTest::Support::Utils if resources[0].is_a?(Puppet::Node::Configuration) config = resources.shift unless resources.empty? - resources.each { |r| config.add_resource(r) unless config.resource(r.class.name, r.title) } + resources.each { |r| config.add_resource r } end elsif resources[0].is_a?(Puppet.type(:component)) raise ArgumentError, "resource2config() no longer accpts components" diff --git a/test/network/client/client.rb b/test/network/client/client.rb index 8d08bd3d7..4a7e9cdb6 100755 --- a/test/network/client/client.rb +++ b/test/network/client/client.rb @@ -140,6 +140,35 @@ class TestClient < Test::Unit::TestCase } end + def test_classfile + Puppet[:code] = "class yaytest {}\n class bootest {}\n include yaytest, bootest" + + Puppet::Node::Facts.indirection.stubs(:save) + + master = client = nil + assert_nothing_raised() { + master = Puppet::Network::Handler.master.new( + :Local => false + ) + } + assert_nothing_raised() { + client = Puppet::Network::Client.master.new( + :Master => master + ) + } + + # Fake that it's local, so it creates the class file + client.local = false + + # We can't guarantee class ordering + client.expects(:setclasses).with do |array| + array.length == 2 and array.include?("yaytest") and array.include?("bootest") + end + assert_nothing_raised { + client.getconfig + } + end + def test_client_loading # Make sure we don't get a failure but that we also get nothing back assert_nothing_raised do diff --git a/test/network/client/master.rb b/test/network/client/master.rb index 0caba4bd2..7216af936 100755 --- a/test/network/client/master.rb +++ b/test/network/client/master.rb @@ -332,6 +332,39 @@ end assert(FileTest.exists?(file), "file was not created on second run") end + def test_default_objects + # Make sure they start out missing + assert_nil(Puppet::Type.type(:filebucket)["puppet"], + "default filebucket already exists") + assert_nil(Puppet::Type.type(:schedule)["daily"], + "default schedules already exists") + + master = mkclient() + + # Now make sure they got created + assert(Puppet::Type.type(:filebucket)["puppet"], + "default filebucket not found") + assert(Puppet::Type.type(:schedule)["daily"], + "default schedules not found") + + # clear everything, and make sure we can recreate them + Puppet::Type.allclear + assert_nil(Puppet::Type.type(:filebucket)["puppet"], + "default filebucket not removed") + assert_nil(Puppet::Type.type(:schedule)["daily"], + "default schedules not removed") + assert_nothing_raised { master.mkdefault_objects } + assert(Puppet::Type.type(:filebucket)["puppet"], + "default filebucket not found") + assert(Puppet::Type.type(:schedule)["daily"], + "default schedules not found") + + + # Make sure we've got schedules + assert(Puppet::Type.type(:schedule)["hourly"], "Could not retrieve hourly schedule") + assert(Puppet::Type.type(:filebucket)["puppet"], "Could not retrieve default bucket") + end + # #540 - make sure downloads aren't affected by noop def test_download_in_noop source = tempfile @@ -517,6 +550,24 @@ end assert_equal(facts["environment"], Puppet[:environment], "Did not add environment to client facts") end + # This is partially to fix #532, but also to save on memory. + def test_remove_objects_after_every_run + client = mkclient + + ftype = Puppet::Type.type(:file) + file = ftype.create :title => "/what/ever", :ensure => :present + config = Puppet::Node::Configuration.new + config.add_resource(file) + + config.expects :apply + + client.configuration = config + client.expects(:getconfig) + client.run + + assert_nil(ftype[@createdfile], "file object was not removed from memory") + end + # #685 def test_http_failures_do_not_kill_puppetd client = mkclient diff --git a/test/network/handler/fileserver.rb b/test/network/handler/fileserver.rb index 8703700ea..3539169dc 100755 --- a/test/network/handler/fileserver.rb +++ b/test/network/handler/fileserver.rb @@ -8,11 +8,6 @@ require 'puppet/network/handler/fileserver' class TestFileServer < Test::Unit::TestCase include PuppetTest - def setup - super - Facter.stubs(:to_hash).returns({}) - end - def mkmount(path = nil) mount = nil name = "yaytest" @@ -151,6 +146,10 @@ class TestFileServer < Test::Unit::TestCase list = server.list(sfile, :ignore, true, false) } + assert_nothing_raised { + file = Puppet.type(:file)[tmpfile] + } + output = "/\tfile" # verify it got listed as a file @@ -944,11 +943,10 @@ allow * end # Now, check that they use Facter info - Facter.stubs(:value).with("hostname").returns("myhost") - Facter.stubs(:value).with("domain").returns("mydomain") - local = "myhost" - domain = "mydomain" + Puppet.notice "The following messages are normal" client = nil + local = Facter["hostname"].value + domain = Facter["domain"].value fqdn = [local, domain].join(".") {"%h" => local, # Short name "%H" => fqdn, # Full name @@ -956,7 +954,6 @@ allow * "%%" => "%", # escape "%o" => "%o" # other }.each do |pat, repl| - Puppet.expects(:notice) check.call(client, pat, repl) end @@ -1135,8 +1132,6 @@ allow * def test_failures # create a server with the file server = nil - Facter.stubs(:[]).with("hostname").returns("myhost") - Facter.stubs(:[]).with("domain").returns("mydomain") config = tempfile [ diff --git a/test/network/handler/resource.rb b/test/network/handler/resource.rb index 247014a47..0d6373160 100755 --- a/test/network/handler/resource.rb +++ b/test/network/handler/resource.rb @@ -3,12 +3,10 @@ require File.dirname(__FILE__) + '/../../lib/puppettest' require 'puppettest' -require 'puppettest/support/utils' require 'base64' require 'cgi' class TestResourceServer < Test::Unit::TestCase - include PuppetTest include PuppetTest::ServerTest def verify_described(type, described) @@ -181,6 +179,9 @@ class TestResourceServer < Test::Unit::TestCase require 'etc' + # Make the example schedules, for testing + Puppet::Type.type(:schedule).mkdefaultschedules + Puppet::Type.eachtype do |type| unless type.respond_to? :instances Puppet.warning "%s does not respond to :instances" % type.name diff --git a/test/other/overrides.rb b/test/other/overrides.rb index 800eab1b7..272f78e30 100755 --- a/test/other/overrides.rb +++ b/test/other/overrides.rb @@ -25,27 +25,28 @@ class TestOverrides < Test::Unit::TestCase baseobj = nil basefile = File.join(basedir, "file") - config = mk_configuration - - baseobj = config.create_resource(:file, - :title => "base", - :path => basedir, - :recurse => true, - :mode => "755" - ) + assert_nothing_raised("Could not create base obj") { + baseobj = Puppet.type(:file).create( + :title => "base", + :path => basedir, + :recurse => true, + :mode => "755" + ) + } subobj = nil subdir = File.join(basedir, "0") subfile = File.join(subdir, "file") + assert_nothing_raised("Could not create sub obj") { + subobj = Puppet.type(:file).create( + :title => "sub", + :path => subdir, + :recurse => true, + :mode => "644" + ) + } - subobj = config.create_resource(:file, - :title => "sub", - :path => subdir, - :recurse => true, - :mode => "644" - ) - - config.apply + assert_apply(baseobj, subobj) assert(File.stat(basefile).mode & 007777 == 0755) assert(File.stat(subfile).mode & 007777 == 0644) diff --git a/test/other/relationships.rb b/test/other/relationships.rb index fe1ae1ac4..0f2a103fe 100755 --- a/test/other/relationships.rb +++ b/test/other/relationships.rb @@ -61,20 +61,19 @@ class TestRelationships < Test::Unit::TestCase :notify => true, :before => true} refreshers = [:subscribe, :notify] [:require, :subscribe, :notify, :before].each do |param| - config = mk_configuration # Create three files to generate our events and three # execs to receive them files = [] execs = [] 3.times do |i| - files << config.create_resource(:file, + files << Puppet::Type.newfile( :title => "file#{i}", :path => tempfile(), :ensure => :file ) path = tempfile() - execs << config.create_resource(:exec, + execs << Puppet::Type.newexec( :title => "notifytest#{i}", :path => "/usr/bin:/bin", :command => "touch #{path}", @@ -166,9 +165,7 @@ class TestRelationships < Test::Unit::TestCase :ensure => :directory) exec = Puppet::Type.newexec(:title => "myexec", :cwd => path, :command => "/bin/echo") - - config = mk_configuration(file, exec) - + reqs = nil assert_nothing_raised do reqs = exec.autorequire @@ -176,10 +173,13 @@ class TestRelationships < Test::Unit::TestCase assert_instance_of(Puppet::Relationship, reqs[0], "Did not return a relationship edge") assert_equal(file, reqs[0].source, "Did not set the autorequire source correctly") assert_equal(exec, reqs[0].target, "Did not set the autorequire target correctly") - + # Now make sure that these relationships are added to the # relationship graph - assert(config.relationship_graph.edge?(file, exec), "autorequire edge was not created") + config = mk_configuration(file, exec) + config.apply do |trans| + assert(config.relationship_graph.edge?(file, exec), "autorequire edge was not created") + end end def test_requires? @@ -210,7 +210,7 @@ class TestRelationships < Test::Unit::TestCase file = Puppet::Type.newfile :path => tempfile, :require => ["file", "/no/such/file"] assert_raise(Puppet::Error) do - config = mk_configuration(file) + file.builddepends end end end diff --git a/test/other/transactions.rb b/test/other/transactions.rb index 2498cc014..8156ba478 100755 --- a/test/other/transactions.rb +++ b/test/other/transactions.rb @@ -6,10 +6,8 @@ require 'puppet' require 'puppettest' require 'mocha' require 'puppettest/support/resources' -require 'puppettest/support/utils' class TestTransactions < Test::Unit::TestCase - include PuppetTest include PuppetTest::FileTesting include PuppetTest::Support::Resources class Fakeprop <Puppet::Property @@ -431,25 +429,26 @@ class TestTransactions < Test::Unit::TestCase # Make sure that unscheduled and untagged objects still respond to events def test_unscheduled_and_untagged_response - config = mk_configuration - config.add_resource(*(Puppet::Type.type(:schedule).create_default_resources)) + Puppet::Type.type(:schedule).mkdefaultschedules Puppet[:ignoreschedules] = false - file = config.create_resource(:file, + file = Puppet.type(:file).create( :name => tempfile(), :ensure => "file", :backup => false ) fname = tempfile() - exec = config.create_resource(:exec, + exec = Puppet.type(:exec).create( :name => "touch %s" % fname, :path => "/usr/bin:/bin", :schedule => "monthly", :subscribe => ["file", file.name] ) + config = mk_configuration(file, exec) + # Run it once - config.apply + assert_apply(config) assert(FileTest.exists?(fname), "File did not get created") assert(!exec.scheduled?, "Exec is somehow scheduled") @@ -605,7 +604,8 @@ class TestTransactions < Test::Unit::TestCase end %w{ya ra y r}.each do |name| - assert(trans.configuration.vertex?(config.resource(:generator, name)), "Generated %s was not a vertex" % name) + assert(trans.configuration.vertex?(Puppet::Type.type(:generator)[name]), + "Generated %s was not a vertex" % name) assert($finished.include?(name), "%s was not finished" % name) end @@ -615,8 +615,10 @@ class TestTransactions < Test::Unit::TestCase end %w{ya ra y r}.each do |name| - assert(!trans.configuration.vertex?(config.resource(:generator, name)), "Generated vertex %s was not removed from graph" % name) - assert_nil(config.resource(:generator, name), "Generated vertex %s was not removed from class" % name) + assert(!trans.configuration.vertex?(Puppet::Type.type(:generator)[name]), + "Generated vertex %s was not removed from graph" % name) + assert_nil(Puppet::Type.type(:generator)[name], + "Generated vertex %s was not removed from class" % name) end end @@ -643,7 +645,7 @@ class TestTransactions < Test::Unit::TestCase assert_nothing_raised("failed to apply yay") do trans.eval_resource(yay) end - ya = config.resource(:generator, "ya") + ya = type["ya"] assert(ya, "Did not generate ya") assert(trans.relationship_graph.vertex?(ya), "Did not add ya to rel_graph") @@ -656,11 +658,11 @@ class TestTransactions < Test::Unit::TestCase # Now make sure it in turn eval_generates appropriately assert_nothing_raised("failed to apply yay") do - trans.eval_resource(config.resource(:generator, "ya")) + trans.eval_resource(type["ya"]) end %w{y}.each do |name| - res = config.resource(:generator, name) + res = type[name] assert(res, "Did not generate %s" % name) assert(trans.relationship_graph.vertex?(res), "Did not add %s to rel_graph" % name) @@ -668,7 +670,7 @@ class TestTransactions < Test::Unit::TestCase end assert_nothing_raised("failed to eval_generate with nil response") do - trans.eval_resource(config.resource(:generator, "y")) + trans.eval_resource(type["y"]) end assert(trans.relationship_graph.edge?(yay, ya), "no edge was created for ya => yay") @@ -676,7 +678,7 @@ class TestTransactions < Test::Unit::TestCase trans.eval_resource(rah) end - ra = config.resource(:generator, "ra") + ra = type["ra"] assert(ra, "Did not generate ra") assert(trans.relationship_graph.vertex?(ra), "Did not add ra to rel_graph" % name) @@ -695,9 +697,9 @@ class TestTransactions < Test::Unit::TestCase end %w{ya ra y r}.each do |name| - assert(!trans.relationship_graph.vertex?(config.resource(:generator, name)), + assert(!trans.relationship_graph.vertex?(type[name]), "Generated vertex %s was not removed from graph" % name) - assert_nil(config.resource(:generator, name), + assert_nil(type[name], "Generated vertex %s was not removed from class" % name) end diff --git a/test/ral/types/basic.rb b/test/ral/types/basic.rb index 862beeadc..4427238bf 100755 --- a/test/ral/types/basic.rb +++ b/test/ral/types/basic.rb @@ -68,6 +68,13 @@ class TestBasic < Test::Unit::TestCase assert_equal("echo", @command.title) end + def test_object_retrieval + [@command, @configfile].each { |obj| + assert_equal(obj.class[obj.name].object_id, obj.object_id, + "%s did not match class version" % obj.ref) + } + end + def test_paths [@configfile, @command, @component].each { |obj| assert_nothing_raised { diff --git a/test/ral/types/cron.rb b/test/ral/types/cron.rb index d6f29efe4..a9a00240c 100755 --- a/test/ral/types/cron.rb +++ b/test/ral/types/cron.rb @@ -150,6 +150,20 @@ class TestCron < Test::Unit::TestCase end end + def test_makeandretrievecron + %w{storeandretrieve a-name another-name more_naming SomeName}.each do |name| + cron = mkcron(name) + comp = mk_configuration(name, cron) + trans = assert_events([:cron_created], comp, name) + + cron.provider.class.prefetch + cron = nil + + assert(cron = Puppet.type(:cron)[name], "Could not retrieve named cron") + assert_instance_of(Puppet.type(:cron), cron) + end + end + # Do input validation testing on all of the parameters. def test_arguments values = { diff --git a/test/ral/types/exec.rb b/test/ral/types/exec.rb index ed0ea9980..11a6d4055 100755 --- a/test/ral/types/exec.rb +++ b/test/ral/types/exec.rb @@ -203,45 +203,42 @@ class TestExec < Test::Unit::TestCase ) comp = mk_configuration("Testing", file, exec) - Puppet::Node::Facts.indirection.stubs(:terminus_class).returns(:memory) assert_events([:file_created, :executed_command], comp) end # Verify that we auto-require any managed scripts. def test_autorequire_files - config = mk_configuration - exe = tempfile() oexe = tempfile() sh = %x{which sh} File.open(exe, "w") { |f| f.puts "#!#{sh}\necho yup" } - file = config.create_resource(:file, + file = Puppet.type(:file).create( :path => oexe, :source => exe, :mode => 755 ) basedir = File.dirname(oexe) - baseobj = config.create_resource(:file, + baseobj = Puppet.type(:file).create( :path => basedir, :source => exe, :mode => 755 ) - ofile = config.create_resource(:file, + ofile = Puppet.type(:file).create( :path => exe, :mode => 755 ) - exec = config.create_resource(:exec, + exec = Puppet.type(:exec).create( :command => oexe, :path => ENV["PATH"], :cwd => basedir ) - cat = config.create_resource(:exec, + cat = Puppet.type(:exec).create( :command => "cat %s %s" % [exe, oexe], :path => ENV["PATH"] ) diff --git a/test/ral/types/file.rb b/test/ral/types/file.rb index 8435855e6..73095a783 100755 --- a/test/ral/types/file.rb +++ b/test/ral/types/file.rb @@ -3,12 +3,10 @@ require File.dirname(__FILE__) + '/../../lib/puppettest' require 'puppettest' -require 'puppettest/support/utils' require 'fileutils' class TestFile < Test::Unit::TestCase include PuppetTest::FileTesting - include PuppetTest::Support::Utils # hmmm # this is complicated, because we store references to the created # objects in a central store @@ -34,8 +32,6 @@ class TestFile < Test::Unit::TestCase @file = Puppet::Type.type(:file) $method = @method_name Puppet[:filetimeout] = -1 - Facter.stubs(:each) - Facter.stubs(:to_hash).returns({}) end def teardown @@ -518,7 +514,7 @@ class TestFile < Test::Unit::TestCase test = File.join(path, "file") File.open(test, "w") { |f| f.puts "yay" } assert_nothing_raised() { ret = dir.localrecurse(true) } - fileobj = config.resource(:file, test) + fileobj = @file[test] assert(fileobj, "child object was not created") assert_equal([fileobj], ret, "child object was not returned") @@ -562,7 +558,7 @@ class TestFile < Test::Unit::TestCase assert_nothing_raised() { ret = dir.localrecurse(true) } assert_equal([bad], ret.collect { |f| f.title }, "purge failed") - badobj = config.resource(:file, bad) + badobj = @file[bad] assert(badobj, "did not create bad object") end @@ -758,6 +754,43 @@ class TestFile < Test::Unit::TestCase assert(file.insync?(currentvalues)) end + def test_remove + basedir = tempfile() + subdir = File.join(basedir, "this") + FileUtils.mkdir_p(subdir) + + dir = nil + assert_nothing_raised { + dir = Puppet.type(:file).create( + :path => basedir, + :recurse => true, + :check => %w{owner mode group} + ) + } + mk_configuration dir + + assert_nothing_raised { + dir.eval_generate + } + + obj = nil + assert_nothing_raised { + obj = Puppet.type(:file)[subdir] + } + + assert(obj, "Could not retrieve subdir object") + + assert_nothing_raised { + obj.remove(true) + } + + assert_nothing_raised { + obj = Puppet.type(:file)[subdir] + } + + assert_nil(obj, "Retrieved removed object") + end + def test_path dir = tempfile() @@ -777,14 +810,14 @@ class TestFile < Test::Unit::TestCase :check => %w{mode owner group} ) } - config = mk_configuration dirobj + mk_configuration dirobj assert_nothing_raised { dirobj.eval_generate } assert_nothing_raised { - file = config.resource(:file, path) + file = dirobj.class[path] } assert(file, "Could not retrieve file object") @@ -796,14 +829,12 @@ class TestFile < Test::Unit::TestCase basedir = tempfile() subfile = File.join(basedir, "subfile") - config = Puppet::Node::Configuration.new - - baseobj = config.create_resource(:file, + baseobj = Puppet.type(:file).create( :name => basedir, :ensure => "directory" ) - subobj = config.create_resource(:file, + subobj = Puppet.type(:file).create( :name => subfile, :ensure => "file" ) @@ -1098,17 +1129,18 @@ class TestFile < Test::Unit::TestCase file = tempfile() newfile = tempfile() - config = mk_configuration - File.open(file, "w", 0411) { |f| f.puts "yayness" } - resource = config.create_resource(:file, - :path => file, :content => "rahness\n", :backup => ".puppet-bak" - ) + obj = nil + assert_nothing_raised { + obj = Puppet::Type.type(:file).create( + :path => file, :content => "rahness\n", :backup => ".puppet-bak" + ) + } - assert_apply(config) + assert_apply(obj) - backupfile = file + resource[:backup] + backupfile = file + obj[:backup] @@tmpfiles << backupfile assert(FileTest.exists?(backupfile), "Backup file %s does not exist" % backupfile) @@ -1119,15 +1151,13 @@ class TestFile < Test::Unit::TestCase bucket = "bucket" bpath = tempfile() Dir.mkdir(bpath) - config.create_resource(:filebucket, + Puppet::Type.type(:filebucket).create( :title => bucket, :path => bpath ) - resource[:backup] = bucket - resource[:content] = "New content" - - resource.finish - assert_apply(config) + obj[:backup] = bucket + obj[:content] = "New content" + assert_apply(obj) md5 = "18cc17fa3047fcc691fdf49c0a7f539a" dir, file, pathfile = Puppet::Network::Handler.filebucket.paths(bpath, md5) @@ -1256,20 +1286,19 @@ class TestFile < Test::Unit::TestCase # this file should get removed File.open(purgee, "w") { |f| f.puts "footest" } - config = mk_configuration - - lfobj = config.create_resource(:file, + lfobj = Puppet::Type.newfile( :title => "localfile", :path => localfile, :content => "rahtest", :backup => false ) - destobj = config.create_resource(:file, :title => "destdir", :path => destdir, + destobj = Puppet::Type.newfile(:title => "destdir", :path => destdir, :source => sourcedir, :backup => false, :recurse => true) + config = mk_configuration(lfobj, destobj) config.apply assert(FileTest.exists?(dsourcefile), "File did not get copied") @@ -1402,11 +1431,10 @@ class TestFile < Test::Unit::TestCase :link => proc { File.symlink(linkdest, path) } } - config = mk_configuration - - bucket = config.create_resource :filebucket, :name => "main", :path => tempfile() + bucket = Puppet::Type.newfilebucket :name => "main", :path => tempfile() - obj = config.create_resource :file, :path => path, :force => true, :links => :manage + obj = Puppet::Type.newfile :path => path, :force => true, + :links => :manage Puppet[:trace] = true ["main", false].each do |backup| @@ -1529,17 +1557,15 @@ class TestFile < Test::Unit::TestCase dfiles = [File.join(dest, "1"), File.join(dest, "dir", "2")] bpath = tempfile - - config = mk_configuration - bucket = config.create_resource :filebucket, :name => "rtest", :path => bpath + bucket = Puppet::Type.type(:filebucket).create :name => "rtest", :path => bpath dipper = bucket.bucket dipper = Puppet::Network::Handler.filebucket.new( :Path => bpath ) assert(dipper, "did not receive bucket client") - file = config.create_resource :file, :path => dest, :source => source, :recurse => true, :backup => "rtest" + file = Puppet::Type.newfile :path => dest, :source => source, :recurse => true, :backup => "rtest" - config.apply + assert_apply(file) dfiles.each do |f| assert(FileTest.exists?(f), "destfile %s was not created" % f) end @@ -1549,7 +1575,7 @@ class TestFile < Test::Unit::TestCase f.puts "boo: %s" % File.basename(sf) } } - config.apply + assert_apply(file) dfiles.each do |f| assert_equal("boo: %s\n" % File.basename(f), File.read(f), "file was not copied correctly") @@ -1574,8 +1600,7 @@ class TestFile < Test::Unit::TestCase def test_backup path = tempfile() - config = Puppet::Node::Configuration.new - file = config.create_resource :file, :path => path, :content => "yay" + file = Puppet::Type.newfile :path => path, :content => "yay" [false, :false, "false"].each do |val| assert_nothing_raised do @@ -1604,7 +1629,7 @@ class TestFile < Test::Unit::TestCase assert_equal("main", file.bucket, "file's bucket was not set") # And then an existing bucket - obj = config.create_resource :filebucket, :name => "testing" + obj = Puppet::Type.type(:filebucket).create :name => "testing" bucket = obj.bucket assert_nothing_raised do @@ -1620,12 +1645,12 @@ class TestFile < Test::Unit::TestCase file = File.join(dir, "file") File.open(file, "w") { |f| f.puts "" } obj = Puppet::Type.newfile :path => dir, :recurse => true, :mode => 0755 - config = mk_configuration obj + mk_configuration obj assert_equal("/%s" % obj.ref, obj.path) list = obj.eval_generate - fileobj = config.resource(:file, file) + fileobj = obj.class[file] assert(fileobj, "did not generate file object") assert_equal("/%s" % fileobj.ref, fileobj.path, "did not generate correct subfile path") end @@ -1642,8 +1667,7 @@ class TestFile < Test::Unit::TestCase # Testing #434 def test_stripping_extra_slashes_during_lookup - config = mk_configuration - file = config.create_resource(:file, :path => "/one/two") + file = Puppet::Type.newfile(:path => "/one/two") %w{/one/two/ /one/two /one//two //one//two//}.each do |path| assert(Puppet::Type.type(:file)[path], "could not look up file via path %s" % path) end @@ -1738,14 +1762,14 @@ class TestFile < Test::Unit::TestCase assert_nothing_raised("Failure when recursing") do children = obj.eval_generate end - assert(config.resource(:file, subdir), "did not create subdir object") + assert(obj.class[subdir], "did not create subdir object") children.each do |c| assert_nothing_raised("Failure when recursing on %s" % c) do c.configuration = config others = c.eval_generate end end - oobj = config.resource(:file, other) + oobj = obj.class[other] assert(oobj, "did not create other object") assert_nothing_raised do @@ -1756,14 +1780,12 @@ class TestFile < Test::Unit::TestCase # Make sure we default to the "puppet" filebucket, rather than a string def test_backup_defaults_to_bucket path = tempfile - config = Puppet::Node::Configuration.new - config.add_resource(Puppet::Type.type(:filebucket).create_default_resources) - file = config.create_resource :file, :path => path, :content => 'some content' + file = Puppet::Type.newfile(:path => path, :content => 'some content') file.finish - assert_instance_of(Puppet::Network::Client.dipper, file.bucket, + assert_instance_of(Puppet::Network::Client::Dipper, file.bucket, "did not default to a filebucket for backups") - assert_equal(config.resource(:filebucket, "puppet").bucket, file.bucket, + assert_equal(Puppet::Type.type(:filebucket)["puppet"].bucket, file.bucket, "did not default to the 'puppet' filebucket") end @@ -1782,7 +1804,7 @@ class TestFile < Test::Unit::TestCase assert_nothing_raised do children = obj.eval_generate end - fobj = config.resource(:file,file) + fobj = obj.class[file] assert(fobj, "did not create file object") assert(fobj.should(:ensure) != :directory, "ensure was passed to child") end diff --git a/test/ral/types/file/target.rb b/test/ral/types/file/target.rb index cc53e2645..f5cbe4a45 100755 --- a/test/ral/types/file/target.rb +++ b/test/ral/types/file/target.rb @@ -3,11 +3,9 @@ require File.dirname(__FILE__) + '/../../../lib/puppettest' require 'puppettest' -require 'puppettest/support/assertions' require 'fileutils' class TestFileTarget < Test::Unit::TestCase - include PuppetTest include PuppetTest::FileTesting def setup @@ -46,7 +44,7 @@ class TestFileTarget < Test::Unit::TestCase def test_linkrecurse dest = tempfile() link = @file.create :path => tempfile(), :recurse => true, :ensure => dest - config = mk_configuration link + mk_configuration link ret = nil @@ -64,8 +62,7 @@ class TestFileTarget < Test::Unit::TestCase assert_equal(:directory, link.should(:ensure), "ensure was not set to directory") assert_equal([File.join(link.title, "one")], ret.collect { |f| f.title }, "Did not get linked file") - oneobj = config.resource(:file, File.join(link.title, "one")) - + oneobj = @file[File.join(link.title, "one")] assert_equal(one, oneobj.should(:target), "target was not set correctly") oneobj.remove @@ -84,7 +81,7 @@ class TestFileTarget < Test::Unit::TestCase "Did not get links back") returns.each do |path| - obj = config.resource(:file, path) + obj = @file[path] assert(path, "did not get obj for %s" % path) sdest = File.join(dest, File.basename(path)) assert_equal(sdest, obj.should(:target), @@ -102,14 +99,15 @@ class TestFileTarget < Test::Unit::TestCase system("touch %s" % file) link = nil - config = mk_configuration - link = config.create_resource(:file, - :ensure => source, - :path => path, - :recurse => true - ) + assert_nothing_raised { + link = Puppet.type(:file).create( + :ensure => source, + :path => path, + :recurse => true + ) + } - config.apply + assert_apply(link) sublink = File.join(path, "subdir") linkpath = File.join(sublink, "file") @@ -118,7 +116,7 @@ class TestFileTarget < Test::Unit::TestCase assert(File.symlink?(linkpath), "path is not a link") assert_equal(file, File.readlink(linkpath)) - assert_nil(config.resource(:file, sublink), "objects were not removed") + assert_nil(@file[sublink], "objects were not removed") assert_equal([], link.evaluate, "Link is not in sync") end diff --git a/test/ral/types/filebucket.rb b/test/ral/types/filebucket.rb index 3681febf4..ecfe0e167 100755 --- a/test/ral/types/filebucket.rb +++ b/test/ral/types/filebucket.rb @@ -3,12 +3,10 @@ require File.dirname(__FILE__) + '/../../lib/puppettest' require 'puppettest' -require 'puppettest/support/utils' require 'fileutils' class TestFileBucket < Test::Unit::TestCase include PuppetTest::FileTesting - include PuppetTest::Support::Utils # hmmm # this is complicated, because we store references to the created # objects in a central store @@ -65,9 +63,14 @@ class TestFileBucket < Test::Unit::TestCase def test_simplebucket name = "yayness" bucketpath = tempfile() - bucket_resource = mkbucket(name, bucketpath) + mkbucket(name, bucketpath) - bucket = bucket_resource.bucket + bucket = nil + assert_nothing_raised { + bucket = Puppet.type(:filebucket).bucket(name) + } + + assert_instance_of(Puppet::Network::Client.dipper, bucket) md5 = nil newpath = tempfile() @@ -106,16 +109,15 @@ class TestFileBucket < Test::Unit::TestCase end def test_fileswithbuckets - Facter.stubs(:to_hash).returns({}) name = "yayness" - resource = mkbucket(name, tempfile()) + mkbucket(name, tempfile()) - config = mk_configuration resource - - bucket = resource.bucket + bucket = nil + assert_nothing_raised { + bucket = Puppet.type(:filebucket).bucket(name) + } file = mktestfile() - config.add_resource(file) assert_nothing_raised { file[:backup] = name } @@ -131,7 +133,7 @@ class TestFileBucket < Test::Unit::TestCase # file[:backup] = true #} - config.apply + assert_apply(file) # so, we've now replaced the file with the opath file assert_equal( diff --git a/test/ral/types/fileignoresource.rb b/test/ral/types/fileignoresource.rb index 501672ab7..5c7536453 100755 --- a/test/ral/types/fileignoresource.rb +++ b/test/ral/types/fileignoresource.rb @@ -3,13 +3,11 @@ require File.dirname(__FILE__) + '/../../lib/puppettest' require 'puppettest' -require 'puppettest/support/utils' require 'cgi' require 'fileutils' class TestFileIgnoreSources < Test::Unit::TestCase include PuppetTest::FileTesting - include PuppetTest::Support::Utils def setup super @@ -20,7 +18,7 @@ class TestFileIgnoreSources < Test::Unit::TestCase end end - #This is not needed unless using md5 (correct me if I'm wrong) +#This is not needed unless using md5 (correct me if I'm wrong) def initstorage Puppet::Util::Storage.init Puppet::Util::Storage.load @@ -32,7 +30,6 @@ class TestFileIgnoreSources < Test::Unit::TestCase end def test_ignore_simple_source - Facter.stubs(:to_hash).returns({}) #Temp directory to run tests in path = tempfile() @@ -92,7 +89,6 @@ class TestFileIgnoreSources < Test::Unit::TestCase end def test_ignore_with_wildcard - Facter.stubs(:to_hash).returns({}) #Temp directory to run tests in path = tempfile() @@tmpfiles.push path @@ -162,7 +158,6 @@ class TestFileIgnoreSources < Test::Unit::TestCase end def test_ignore_array - Facter.stubs(:to_hash).returns({}) #Temp directory to run tests in path = tempfile() @@tmpfiles.push path diff --git a/test/ral/types/filesources.rb b/test/ral/types/filesources.rb index bfbc185e3..1f22fc9e0 100755 --- a/test/ral/types/filesources.rb +++ b/test/ral/types/filesources.rb @@ -3,13 +3,11 @@ require File.dirname(__FILE__) + '/../../lib/puppettest' require 'puppettest' -require 'puppettest/support/assertions' require 'cgi' require 'fileutils' require 'mocha' class TestFileSources < Test::Unit::TestCase - include PuppetTest include PuppetTest::FileTesting def setup super @@ -287,7 +285,7 @@ class TestFileSources < Test::Unit::TestCase end assert_equal([destfile], sourced, "Did not get correct list of sourced objects") - dfileobj = config.resource(:file, destfile) + dfileobj = @file[destfile] assert(dfileobj, "Did not create destfile object") assert_equal([dfileobj], result) @@ -303,7 +301,7 @@ class TestFileSources < Test::Unit::TestCase result, sourced = obj.sourcerecurse(true) end assert_equal([destfile], sourced, "Did not get correct list of sourced objects") - dfileobj = config.resource(:file, destfile) + dfileobj = @file[destfile] assert(dfileobj, "Did not create destfile object with a missing source") assert_equal([dfileobj], result) dfileobj.remove @@ -417,6 +415,9 @@ class TestFileSources < Test::Unit::TestCase def test_sources_with_deleted_destfiles fromdir, todir, one, two = run_complex_sources assert(FileTest.exists?(todir)) + + # We shouldn't have a 'two' file object in memory + assert_nil(@file[two], "object for 'two' is still in memory") # then delete a file File.unlink(two) @@ -938,6 +939,7 @@ class TestFileSources < Test::Unit::TestCase end File.unlink(file1) File.unlink(file3) + Puppet.err :yay assert_apply(obj) assert(FileTest.exists?(file1), "File from source 1 was not copied") diff --git a/test/ral/types/host.rb b/test/ral/types/host.rb index 5e91a8c58..1013651c5 100755 --- a/test/ral/types/host.rb +++ b/test/ral/types/host.rb @@ -154,13 +154,11 @@ class TestHost < Test::Unit::TestCase def test_puppetalias host = mkhost() - config = mk_configuration(host) - assert_nothing_raised { host[:alias] = "testing" } - same = config.resource(:host, "testing") + same = host.class["testing"] assert(same, "Could not retrieve by alias") end end diff --git a/test/ral/types/package.rb b/test/ral/types/package.rb index d09f7d9b7..7d0caa0ba 100755 --- a/test/ral/types/package.rb +++ b/test/ral/types/package.rb @@ -9,7 +9,6 @@ require 'mocha' $platform = Facter["operatingsystem"].value class TestPackages < Test::Unit::TestCase - include PuppetTest include PuppetTest::FileTesting def setup super diff --git a/test/ral/types/parameter.rb b/test/ral/types/parameter.rb index 7eda05bb5..1d402cb85 100755 --- a/test/ral/types/parameter.rb +++ b/test/ral/types/parameter.rb @@ -113,7 +113,8 @@ class TestParameter < Test::Unit::TestCase inst = type.create(:name => "test") - config = mk_configuration(inst) + config = mk_configuration + inst.configuration = config assert_nothing_raised("Could not create shadowed param") { inst[:alias] = "foo" @@ -130,7 +131,7 @@ class TestParameter < Test::Unit::TestCase assert_instance_of(param, obj, "alias is an instance of the wrong class") # Make sure the alias got created - assert(config.resource(inst.class.name, "foo"), "Did not retrieve object by its alias") + assert(type["foo"], "Did not retrieve object by its alias") # Now try it during initialization other = nil diff --git a/test/ral/types/schedule.rb b/test/ral/types/schedule.rb index 06698f288..2870b8db6 100755 --- a/test/ral/types/schedule.rb +++ b/test/ral/types/schedule.rb @@ -242,23 +242,24 @@ class TestSchedule < Test::Unit::TestCase s = mksched s[:period] = :hourly + f = nil path = tempfile() - f = Puppet.type(:file).create( - :name => path, - :schedule => s.name, - :ensure => "file" - ) - - config = mk_configuration(s, f) + assert_nothing_raised { + f = Puppet.type(:file).create( + :name => path, + :schedule => s.name, + :ensure => "file" + ) + } assert(f.scheduled?, "File is not scheduled to run") - config.apply + assert_apply(f) assert(! f.scheduled?, "File is scheduled to run already") File.unlink(path) - config.apply + assert_apply(f) assert(! FileTest.exists?(path), "File was created when not scheduled") end @@ -286,17 +287,25 @@ class TestSchedule < Test::Unit::TestCase # Verify that each of our default schedules exist def test_defaultschedules - defaults = nil - assert_nothing_raised("Could not create default schedules") do - defaults = Puppet.type(:schedule).create_default_resources + assert_nothing_raised do + Puppet.type(:schedule).mkdefaultschedules end s = {} %w{puppet hourly daily weekly monthly}.each { |period| - schedule = defaults.find { |r| r.title == period } - assert(schedule, "Could not find %s schedule" % + obj = Puppet.type(:schedule)[period] + assert(obj, "Could not find %s schedule" % period) - s[period] = schedule + s[period] = obj } + assert_nothing_raised("Could not rerun mkdefaultschedules") do + Puppet.type(:schedule).mkdefaultschedules + end + s.each do |period, obj| + newobj = Puppet.type(:schedule)[period] + assert(newobj, "somehow lost schedule for %s" % period) + assert_equal(obj.object_id, newobj.object_id, + "created a new schedule instead of reusing existing one") + end end def test_period_with_repeat diff --git a/test/ral/types/sshkey.rb b/test/ral/types/sshkey.rb index a2daf6cfb..c99f7562a 100755 --- a/test/ral/types/sshkey.rb +++ b/test/ral/types/sshkey.rb @@ -102,8 +102,6 @@ class TestSSHKey < Test::Unit::TestCase def test_moddingkey key = mkkey() - config = mk_configuration(key) - assert_events([:sshkey_created], key) key.retrieve @@ -115,7 +113,7 @@ class TestSSHKey < Test::Unit::TestCase assert_events([:sshkey_changed], key) aliases.each do |name| - assert_equal(key, config.resource(:sshkey, name), + assert_equal(key, key.class[name], "alias was not set") end end @@ -133,13 +131,12 @@ class TestSSHKey < Test::Unit::TestCase def test_puppetalias key = mkkey() - config = mk_configuration(key) assert_nothing_raised { key[:alias] = "testing" } - same = config.resource(:sshkey, "testing") + same = key.class["testing"] assert(same, "Could not retrieve by alias") end @@ -171,14 +168,13 @@ class TestSSHKey < Test::Unit::TestCase keys << k names << k.name } - config = mk_configuration(*keys) - config.apply - + assert_apply(*keys) + keys.clear + Puppet.type(:sshkey).clear newkey = mkkey() - config = mk_configuration(newkey) + #newkey[:ensure] = :present names << newkey.name - - config.apply + assert_apply(newkey) # Verify we can retrieve that info assert_nothing_raised("Could not retrieve after second write") { diff --git a/test/ral/types/tidy.rb b/test/ral/types/tidy.rb index 6cdf0f050..e95f26b95 100755 --- a/test/ral/types/tidy.rb +++ b/test/ral/types/tidy.rb @@ -3,10 +3,8 @@ require File.dirname(__FILE__) + '/../../lib/puppettest' require 'puppettest' -require 'puppettest/support/utils' class TestTidy < Test::Unit::TestCase - include PuppetTest include PuppetTest::FileTesting def mktmpfile # because luke's home directory is on nfs, it can't be used for testing @@ -203,10 +201,16 @@ class TestTidy < Test::Unit::TestCase path = tempfile() File.open(path, "w") { |f| 10.times { f.puts "yayness " } } tidy = Puppet::Type.type(:tidy).create :path => path, :size => "1b" - config = mk_configuration(tidy) - - config.apply + + assert_apply(tidy) + assert(! FileTest.exists?(path), "file did not get tidied") + + # Now try one with just an age attribute. + File.open(path, "w") { |f| 10.times { f.puts "yayness " } } + tidy = Puppet::Type.type(:tidy).create :path => path, :age => "5s" + + assert_apply(tidy) assert(! FileTest.exists?(path), "file did not get tidied") end diff --git a/test/util/filetype.rb b/test/util/filetype.rb index a53fb385f..6c7ede07b 100755 --- a/test/util/filetype.rb +++ b/test/util/filetype.rb @@ -85,7 +85,8 @@ class TestFileType < Test::Unit::TestCase assert_nothing_raised("Could not call backup with no buckets") do obj.backup end - puppet = Puppet::Type.type(:filebucket).create_default_resources + puppet = type["puppet"] + assert(puppet, "Did not create default filebucket") assert_equal("one", puppet.bucket.getfile(Digest::MD5.hexdigest(File.read(path))), "Could not get file from backup") |