From 6198a9aa6ee0d72360cd6693644facd6a17719e3 Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Wed, 4 May 2022 11:20:37 +0200 Subject: [PATCH] (PUP-3399) autorequire local file sources --- lib/puppet/type/file.rb | 7 ++++++ spec/unit/type/file_spec.rb | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb index b4325d1c24e..973b60836cf 100644 --- a/lib/puppet/type/file.rb +++ b/lib/puppet/type/file.rb @@ -396,6 +396,13 @@ 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://') + # the source gets translated to file:///D:/source instead of file://D:/source + # see https://github.com/puppetlabs/puppet/commit/5fea1dc64829e9c8178937764faccd51131b2a77 + req << src.delete_prefix('file:///') if src.start_with?('file:///') && Puppet::Util::Platform.windows? + 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))