Skip to content

Commit

Permalink
rmt metadata cleanup after repo updates
Browse files Browse the repository at this point in the history
  • Loading branch information
paragjain0910 committed Dec 17, 2024
1 parent fc422ac commit c410a91
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rmt/mirror/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def cleanup_stale_metadata

def move_files(glob:, destination:)
FileUtils.mkpath(destination) unless Dir.exist?(destination)
FileUtils.rm_rf(Dir.glob(File.join(destination, '*')))
FileUtils.mv(Dir.glob(glob), destination, force: true)
rescue StandardError => e
raise RMT::Mirror::Exception.new(_('Error while moving files %{glob} to %{dest}: %{error}') % {
Expand Down
1 change: 1 addition & 0 deletions package/obs/rmt-server.changes
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Wed Oct 30 09:01:32 UTC 2024 - Natnael Getahun <[email protected]>
* Allow SLE Micro system to access SLES repositories (bsc#1230419)
* Skip system token rotation in read-only APIs
* Enable RMT to handle the new dl.suse.com CDN domain (bsc#1234641)
* Clean up RMT metadata after repo update (bsc#1233308)

-------------------------------------------------------------------
Wed Aug 21 15:28:43 UTC 2024 - Jesús Bermúdez Velázquez <[email protected]>
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/rmt/mirror/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,40 @@

expect { base.move_files(glob: src, destination: dest) }.to raise_exception(/Error while moving files/)
end

it 'removes existing files in the destination directory before moving' do
allow(Dir).to receive(:exist?).with(dest).and_return(true)
allow(Dir).to receive(:glob).with(File.join(dest, '*')).and_return(%w[/destination/path/file1.txt /destination/path/file2.txt])
allow(Dir).to receive(:glob).with(src).and_return(%w[/source/path/newfile1.txt /source/path/newfile2.txt])
expect(FileUtils).not_to receive(:mkpath).with(dest)
expect(FileUtils).to receive(:rm_rf).with(%w[/destination/path/file1.txt /destination/path/file2.txt])
expect(FileUtils).to receive(:mv).with(%w[/source/path/newfile1.txt /source/path/newfile2.txt], dest, force: true)

base.move_files(glob: src, destination: dest)
end

it 'removes existing files when destination directory is empty' do
allow(Dir).to receive(:exist?).with(dest).and_return(false)
allow(Dir).to receive(:glob).with(src).and_return(%w[/source/path/newfile1.txt /source/path/newfile2.txt])
allow(Dir).to receive(:glob).with(File.join(dest, '*')).and_return([])
expect(FileUtils).to receive(:mkpath).with(dest)
expect(FileUtils).to receive(:rm_rf).with([])
expect(FileUtils).to receive(:mv).with(%w[/source/path/newfile1.txt /source/path/newfile2.txt], dest, force: true)

base.move_files(glob: src, destination: dest)
end

it 'handles errors during removal of existing files' do
allow(Dir).to receive(:exist?).with(dest).and_return(true)
existing_files = ['/destination/path/file1.txt']

allow(Dir).to receive(:glob).with(File.join(dest, '*')).and_return(existing_files)
allow(FileUtils).to receive(:rm_rf).with(existing_files).and_raise(StandardError.new('Remove failed'))

expect do
base.move_files(glob: src, destination: dest)
end.to raise_exception(/Error while moving files/)
end
end

describe '#need_to_download?' do
Expand Down

0 comments on commit c410a91

Please sign in to comment.