Skip to content

Commit

Permalink
[Storage] Delete the shared directory only if the directory exists an…
Browse files Browse the repository at this point in the history
…d it is empty.

We introduce the condition of existence because otherwise, the empty condition may fail when the directory does not exist.

Signed-off-by: Giacomo Marciani <mgiacomo@amazon.com>
  • Loading branch information
gmarciani committed Apr 11, 2024
1 parent 126f161 commit a6f368a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
mode '1777'
recursive false
action :delete
only_if { Dir.empty?(efs_shared_dir.to_s) }
only_if { Dir.exist?(efs_shared_dir.to_s) && Dir.empty?(efs_shared_dir.to_s) }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
mode '1777'
recursive false
action :delete
only_if { Dir.empty?(fsx.shared_dir) }
only_if { Dir.exist?(fsx.shared_dir) && Dir.empty?(fsx.shared_dir) }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
directory shared_dir do
recursive false
action :delete
only_if { Dir.empty?(shared_dir) }
only_if { Dir.exist?(shared_dir) && Dir.empty?(shared_dir) }
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ def mock_already_installed(package, expected_version, installed)
before do
stub_command("mount | grep ' /shared_dir_1 '").and_return(false)
stub_command("mount | grep ' /shared_dir_2 '").and_return(true)
allow(Dir).to receive(:exist?).with("/shared_dir_1").and_return(true)
allow(Dir).to receive(:empty?).with("/shared_dir_1").and_return(true)
allow(Dir).to receive(:exist?).with("/shared_dir_2").and_return(true)
allow(Dir).to receive(:empty?).with("/shared_dir_2").and_return(false)
end

Expand All @@ -382,7 +384,7 @@ def mock_already_installed(package, expected_version, installed)
end
end

it "deletes shared dir only if empty" do
it "deletes shared dir only if it exists and it is empty" do
is_expected.to delete_directory('/shared_dir_1')
.with(recursive: false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@
before do
stub_command("mount | grep ' /shared_dir_1 '").and_return(false)
stub_command("mount | grep ' /shared_dir_2 '").and_return(true)
allow(Dir).to receive(:exist?).with("/shared_dir_1").and_return(true)
allow(Dir).to receive(:empty?).with("/shared_dir_1").and_return(true)
allow(Dir).to receive(:exist?).with("/shared_dir_2").and_return(true)
allow(Dir).to receive(:empty?).with("/shared_dir_2").and_return(false)
end

Expand All @@ -386,7 +388,7 @@
.with(pattern: "lustre_id_2.fsx.REGION.amazonaws.com@tcp:/mount_name_2 *")
end

it 'deletes shared dir only if empty' do
it 'deletes shared dir only if it exists and it is empty' do
is_expected.to delete_directory('/shared_dir_1')
.with(recursive: false)
is_expected.not_to delete_directory('/shared_dir_2')
Expand Down Expand Up @@ -417,7 +419,9 @@
before do
stub_command("mount | grep ' /shared_dir_1 '").and_return(false)
stub_command("mount | grep ' /shared_dir_2 '").and_return(true)
allow(Dir).to receive(:exist?).with("/shared_dir_1").and_return(true)
allow(Dir).to receive(:empty?).with("/shared_dir_1").and_return(true)
allow(Dir).to receive(:exist?).with("/shared_dir_2").and_return(true)
allow(Dir).to receive(:empty?).with("/shared_dir_2").and_return(false)
end

Expand All @@ -441,7 +445,7 @@
.with(pattern: "ontap_id_2.fsx.REGION.amazonaws.com:/junction_path_2 *")
end

it 'deletes shared dir only if empty' do
it 'deletes shared dir only if it exists and it is empty' do
is_expected.to delete_directory('/shared_dir_1')
.with(recursive: false)
is_expected.not_to delete_directory('/shared_dir_2')
Expand Down Expand Up @@ -472,7 +476,9 @@
before do
stub_command("mount | grep ' /filecache_dir_1 '").and_return(false)
stub_command("mount | grep ' /filecache_dir_2 '").and_return(true)
allow(Dir).to receive(:exist?).with("/filecache_dir_1").and_return(true)
allow(Dir).to receive(:empty?).with("/filecache_dir_1").and_return(true)
allow(Dir).to receive(:exist?).with("/filecache_dir_2").and_return(true)
allow(Dir).to receive(:empty?).with("/filecache_dir_2").and_return(false)
end

Expand All @@ -496,7 +502,7 @@
.with(pattern: "filecache_dns_name_2@tcp:/filecache_mount_name_2 *")
end

it 'deletes shared dir only if empty' do
it 'deletes shared dir only if it exists and it is empty' do
is_expected.to delete_directory('/filecache_dir_1')
.with(recursive: false)
is_expected.not_to delete_directory('/filecache_dir_2')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@

before do
stub_command("mount | grep ' /SHARED_DIR '").and_return(false)
allow(Dir).to receive(:exist?).with("/SHARED_DIR").and_return(true)
allow(Dir).to receive(:empty?).with("/SHARED_DIR").and_return(is_dir_empty)
end

Expand All @@ -147,7 +148,7 @@
.with(pattern: " /SHARED_DIR ")
end

it "deletes shared dir only if empty" do
it "deletes shared dir only if it exists and it is empty" do
if is_dir_empty
is_expected.to delete_directory('/SHARED_DIR')
.with(recursive: false)
Expand All @@ -174,6 +175,7 @@

before do
stub_command("mount | grep ' /SHARED_DIR '").and_return(true)
allow(Dir).to receive(:exist?).with("/SHARED_DIR").and_return(true)
allow(Dir).to receive(:empty?).with("/SHARED_DIR").and_return(is_dir_empty)
end

Expand All @@ -189,7 +191,7 @@
.with(pattern: " /SHARED_DIR ")
end

it "deletes shared dir only if empty" do
it "deletes shared dir only if it exists and it is empty" do
if is_dir_empty
is_expected.to delete_directory('/SHARED_DIR')
.with(recursive: false)
Expand Down

0 comments on commit a6f368a

Please sign in to comment.