diff options
author | Luke Kanies <luke@madstop.com> | 2008-08-28 23:28:22 -0700 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-08-28 23:28:22 -0700 |
commit | 93fc1139550bd97a11529b812e77ac0fc00c6079 (patch) | |
tree | 66ef90dc4dcfa02c012307f80d84a2b194877d9d | |
parent | bd1163a339ff66dbb9a50a1cb13f6320cb056cc3 (diff) | |
download | puppet-93fc1139550bd97a11529b812e77ac0fc00c6079.tar.gz puppet-93fc1139550bd97a11529b812e77ac0fc00c6079.tar.xz puppet-93fc1139550bd97a11529b812e77ac0fc00c6079.zip |
Files now use the Indirector to recurse locally.
I don't yet have integration tests for remote recursion
or link recursion, but we're nearly there.
Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r-- | lib/puppet/type/file.rb | 21 | ||||
-rwxr-xr-x | spec/integration/type/file.rb | 53 | ||||
-rwxr-xr-x | spec/unit/type/file.rb | 29 |
3 files changed, 93 insertions, 10 deletions
diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb index 2ae1e61b7..a59192af3 100644 --- a/lib/puppet/type/file.rb +++ b/lib/puppet/type/file.rb @@ -322,10 +322,14 @@ module Puppet # Create any children via recursion or whatever. def eval_generate - raise(Puppet::DevError, "File recursion cannot happen without a catalog") unless catalog - return nil unless self.recurse? - recurse.reject { |resource| catalog.resource(:file, resource[:path]) }.each do |child| + + raise(Puppet::DevError, "Cannot generate resources for recursion without a catalog") unless catalog + + recurse.reject do |resource| + catalog.resource(:file, resource[:path]) + end.each do |child| + catalog.add_resource child catalog.relationship_graph.add_edge self, child end end @@ -559,11 +563,11 @@ module Puppet full_path = File.join(self[:path], path) # the right-side hash wins in the merge. - options = to_hash.merge(:path => full_path) + options = to_hash.merge(:path => full_path, :implicit => true) options.delete(:parent) if options.include?(:parent) options.delete(:recurse) if options.include?(:recurse) - return catalog.create_implicit_resource(self.class.name, options) + return self.class.create(options) end # Files handle paths specially, because they just lengthen their @@ -641,7 +645,12 @@ module Puppet # Recurse the file itself, returning a Metadata instance for every found file. def recurse_local - perform_recursion(self[:path]).inject({}) { |hash, meta| hash[meta.relative_path] = newchild(meta.relative_path); hash } + perform_recursion(self[:path]).inject({}) do |hash, meta| + next hash if meta.relative_path == "." + + hash[meta.relative_path] = newchild(meta.relative_path) + hash + end end # Recurse against our remote file. diff --git a/spec/integration/type/file.rb b/spec/integration/type/file.rb new file mode 100755 index 000000000..39d0b62dc --- /dev/null +++ b/spec/integration/type/file.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe Puppet::Type.type(:file), "when recursing" do + def mkdir + end + + def build_path(dir) + Dir.mkdir(dir) + File.chmod(0750, dir) + + @dirs = [dir] + @files = [] + + %w{one two}.each do |subdir| + fdir = File.join(dir, subdir) + Dir.mkdir(fdir) + File.chmod(0750, fdir) + @dirs << fdir + + %w{three}.each do |file| + ffile = File.join(fdir, file) + @files << ffile + File.open(ffile, "w") { |f| f.puts "test %s" % file } + File.chmod(0640, ffile) + end + end + end + + it "should be able to recursively set properties on existing files" do + @path = Tempfile.new("file_integration_tests") + @path.close!() + @path = @path.path + + build_path(@path) + + @file = Puppet::Type::File.create(:name => @path, :mode => 0644, :recurse => true) + + @catalog = Puppet::Node::Catalog.new + @catalog.add_resource @file + + @catalog.apply + + @dirs.each do |path| + (File.stat(path).mode & 007777).should == 0755 + end + + @files.each do |path| + (File.stat(path).mode & 007777).should == 0644 + end + end +end diff --git a/spec/unit/type/file.rb b/spec/unit/type/file.rb index 27a077bed..9578a2d17 100755 --- a/spec/unit/type/file.rb +++ b/spec/unit/type/file.rb @@ -143,6 +143,14 @@ describe Puppet::Type.type(:file) do @file.recurse_local end + it "should not create a new child resource for the '.' directory" do + @metadata.stubs(:relative_path).returns "." + + @file.expects(:perform_recursion).returns [@metadata] + @file.expects(:newchild).never + @file.recurse_local + end + it "should return a hash of the created resources with the relative paths as the hash keys" do @file.expects(:perform_recursion).returns [@metadata] @file.expects(:newchild).with("my/file").returns "fiebar" @@ -366,6 +374,19 @@ describe Puppet::Type.type(:file) do @file.eval_generate.should == [foo] end + it "should add each resource to the catalog" do + foo = stub 'foo', :[] => "/foo" + bar = stub 'bar', :[] => "/bar" + bar2 = stub 'bar2', :[] => "/bar" + + @catalog.expects(:add_resource).with(foo) + @catalog.expects(:add_resource).with(bar) + + @file.expects(:recurse).returns [foo, bar] + + @file.eval_generate + end + it "should add a relationshp edge for each returned resource" do foo = stub 'foo', :[] => "/foo" @@ -422,25 +443,25 @@ describe Puppet::Type.type(:file) do describe "and making a new child resource" do it "should create an implicit resource using the provided relative path joined with the file's path" do path = File.join(@file[:path], "my/path") - @catalog.expects(:create_implicit_resource).with { |klass, options| options[:path] == path } + Puppet::Type.type(:file).expects(:create).with { |options| options[:implicit] == true and options[:path] == path } @file.newchild("my/path") end it "should copy most of the parent resource's 'should' values to the new resource" do @file.expects(:to_hash).returns :foo => "bar", :fee => "fum" - @catalog.expects(:create_implicit_resource).with { |klass, options| options[:foo] == "bar" and options[:fee] == "fum" } + Puppet::Type.type(:file).expects(:create).with { |options| options[:foo] == "bar" and options[:fee] == "fum" } @file.newchild("my/path") end it "should not copy the parent resource's parent" do @file.expects(:to_hash).returns :parent => "foo" - @catalog.expects(:create_implicit_resource).with { |klass, options| ! options.include?(:parent) } + Puppet::Type.type(:file).expects(:create).with { |options| ! options.include?(:parent) } @file.newchild("my/path") end it "should not copy the parent resource's recurse value" do @file.expects(:to_hash).returns :recurse => true - @catalog.expects(:create_implicit_resource).with { |klass, options| ! options.include?(:recurse) } + Puppet::Type.type(:file).expects(:create).with { |options| ! options.include?(:recurse) } @file.newchild("my/path") end end |