diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb index b4325d1c24e..64be5446f06 100644 --- a/lib/puppet/type/file.rb +++ b/lib/puppet/type/file.rb @@ -396,6 +396,10 @@ def self.title_patterns end # if the resource is a link, make sure the target is created first req << self[:target] if self[:target] + # if the resource has a source set, make sure it is created first + self[:source]&.each do |src| + req << src.delete_prefix('file://') if src.start_with?('file://') + end req end diff --git a/spec/unit/type/file_spec.rb b/spec/unit/type/file_spec.rb index 6724dca9fb0..c8275965544 100644 --- a/spec/unit/type/file_spec.rb +++ b/spec/unit/type/file_spec.rb @@ -1291,6 +1291,53 @@ end end + describe "source" do + it "should require file resource when specified with the source property" do + file = described_class.new(:path => File.expand_path("/foo"), :ensure => :file, :source => File.expand_path("/bar")) + source = described_class.new(:path => File.expand_path("/bar"), :ensure => :file) + catalog.add_resource file + catalog.add_resource source + reqs = file.autorequire + expect(reqs.size).to eq(1) + expect(reqs[0].source).to eq(source) + expect(reqs[0].target).to eq(file) + end + + it "should require file resource when specified with the source property as file: URI" do + file = described_class.new(:path => File.expand_path("/foo"), :ensure => :file, :source => "file://#{File.expand_path("/bar")}") + source = described_class.new(:path => File.expand_path("/bar"), :ensure => :file) + catalog.add_resource file + catalog.add_resource source + reqs = file.autorequire + expect(reqs.size).to eq(1) + expect(reqs[0].source).to eq(source) + expect(reqs[0].target).to eq(file) + end + + it "should require file resource when specified with the source property as an array" do + file = described_class.new(:path => File.expand_path("/foo"), :ensure => :file, :source => [File.expand_path("/bar")]) + source = described_class.new(:path => File.expand_path("/bar"), :ensure => :file) + catalog.add_resource file + catalog.add_resource source + reqs = file.autorequire + expect(reqs.size).to eq(1) + expect(reqs[0].source).to eq(source) + expect(reqs[0].target).to eq(file) + end + + it "should not require source if source is not local" do + file = described_class.new(:path => File.expand_path('/foo'), :ensure => :file, :source => 'puppet:///modules/nfs/conf') + catalog.add_resource file + expect(file.autorequire.size).to eq(0) + end + + it "should not require source if source is not managed" do + file = described_class.new(:path => File.expand_path('/foo'), :ensure => :file, :source => File.expand_path('/bar')) + catalog.add_resource file + expect(file.autorequire.size).to eq(0) + end + end + describe "directories" do it "should autorequire its parent directory" do dir = described_class.new(:path => File.dirname(path))