diff options
author | smriti <sgarg@msystechnologies.com> | 2021-05-12 18:30:11 +0530 |
---|---|---|
committer | smriti <sgarg@msystechnologies.com> | 2021-11-30 16:50:58 +0530 |
commit | 5fa2c7ffae1e5b235e2bc72eb3f4e63c531b43c9 (patch) | |
tree | 5f982755075b4a26a07037ad686980a6be9c7f97 | |
parent | 1a3823919167ef6c22848ac2aa5ec0b1b291c0ab (diff) | |
download | chef-5fa2c7ffae1e5b235e2bc72eb3f4e63c531b43c9.tar.gz |
Mount: resource with loop option not idempotent
Signed-off-by: smriti <sgarg@msystechnologies.com>
-rw-r--r-- | cspell.json | 1 | ||||
-rw-r--r-- | lib/chef/provider/mount/linux.rb | 15 | ||||
-rw-r--r-- | lib/chef/provider/mount/mount.rb | 2 | ||||
-rw-r--r-- | spec/unit/provider/mount/linux_spec.rb | 19 |
4 files changed, 31 insertions, 6 deletions
diff --git a/cspell.json b/cspell.json index 72474a7fbc..6f40be96c2 100644 --- a/cspell.json +++ b/cspell.json @@ -612,6 +612,7 @@ "listprop", "ljust", "lltstype", + "losetup", "LMEM", "LMSHARE", "LMSTR", diff --git a/lib/chef/provider/mount/linux.rb b/lib/chef/provider/mount/linux.rb index 382e37d41a..43852e330b 100644 --- a/lib/chef/provider/mount/linux.rb +++ b/lib/chef/provider/mount/linux.rb @@ -29,10 +29,13 @@ class Chef # "findmnt" outputs the mount points with volume. # Convert the mount_point of the resource to a real path in case it # contains symlinks in its parents dirs. + def loop_mount_points + # get loop_mount_points only if not initialized earlier + @loop_mount_points ||= shell_out!("losetup -a").stdout + end def mounted? mounted = false - real_mount_point = if ::File.exists? @new_resource.mount_point ::File.realpath(@new_resource.mount_point) else @@ -45,6 +48,14 @@ class Chef when /\A#{Regexp.escape(real_mount_point)}\s+#{device_mount_regex}\s/ mounted = true logger.trace("Special device #{device_logstring} mounted as #{real_mount_point}") + # Permalink for loop type devices mount points https://rubular.com/r/a0bS4p2RvXsGxx + when %r{\A#{Regexp.escape(real_mount_point)}\s+\/dev\/loop+[0-9]+\s} + loop_mount_points.each_line do |mount_point| + if mount_point.include? device_real + mounted = true + break + end + end # Permalink for multiple devices mounted to the same mount point(i.e. '/proc') https://rubular.com/r/a356yzspU7N9TY when %r{\A#{Regexp.escape(real_mount_point)}\s+([/\w])+\s} mounted = false @@ -64,4 +75,4 @@ class Chef end end end -end +end
\ No newline at end of file diff --git a/lib/chef/provider/mount/mount.rb b/lib/chef/provider/mount/mount.rb index 802ee11c23..2bc9d2c78f 100644 --- a/lib/chef/provider/mount/mount.rb +++ b/lib/chef/provider/mount/mount.rb @@ -279,4 +279,4 @@ class Chef end end end -end +end
\ No newline at end of file diff --git a/spec/unit/provider/mount/linux_spec.rb b/spec/unit/provider/mount/linux_spec.rb index 3e41f895d1..188777a19b 100644 --- a/spec/unit/provider/mount/linux_spec.rb +++ b/spec/unit/provider/mount/linux_spec.rb @@ -10,9 +10,9 @@ describe Chef::Provider::Mount::Linux do let(:new_resource) do new_resource = Chef::Resource::Mount.new("/tmp/foo") - new_resource.device "/dev/sdz1" + new_resource.device "/dev/sdz1" new_resource.device_type :device - new_resource.fstype "ext3" + new_resource.fstype "ext3" new_resource.supports remount: false new_resource end @@ -32,6 +32,7 @@ describe Chef::Provider::Mount::Linux do context "to see if the volume is mounted" do it "should set mounted true if the mount point is found in the mounts list" do + allow(provider).to receive(:shell_out!).with("losetup --list").and_return(double(stdout: "/tmp/foo")) allow(provider).to receive(:shell_out!).and_return(double(stdout: "/tmp/foo /dev/sdz1 type ext3 (rw)\n")) provider.load_current_resource expect(provider.current_resource.mounted).to be_truthy @@ -104,4 +105,16 @@ describe Chef::Provider::Mount::Linux do end end -end + context "to check if loop resource is mounted" do + it "should set mounted true in case of loop resource" do + new_resource.options "loop" + mount = "/tmp/foo /dev/loop16 iso660 cifs ro\n" + losetup = "/dev/loop16 0 0 1 1 /dev/sdz1 \n" + allow(provider).to receive(:shell_out!).with("findmnt -rn").and_return(double(stdout: mount)) + allow(provider).to receive(:shell_out!).with("losetup -a").and_return(double(stdout: losetup)) + provider.load_current_resource + expect(provider.current_resource.mounted).to be_truthy + end + end + +end
\ No newline at end of file |