Skip to content

Commit

Permalink
Rerfactor suma product tree mirroring in repomd and mirror classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ngetahun committed Jan 2, 2024
1 parent 9528d47 commit 2ecb518
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 74 deletions.
6 changes: 5 additions & 1 deletion lib/rmt/cli/mirror.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class RMT::CLI::Mirror < RMT::CLI::Base
def all
RMT::Lockfile.lock('mirror') do
begin
mirror.mirror_suma_product_tree(repository_url: 'https://scc.suse.com/suma/')
suma_product_tree.mirror
rescue RMT::Mirror::Exception => e
errors << _('Mirroring SUMA product tree failed: %{error_message}') % { error_message: e.message }
end
Expand Down Expand Up @@ -78,6 +78,10 @@ def product(*targets)

protected

def suma_product_tree
RMT::Mirror::SumaProductTree.new(logger: logger, mirroring_base_dir: RMT::DEFAULT_MIRROR_DIR)
end

def mirror
config = {
logger: logger,
Expand Down
23 changes: 0 additions & 23 deletions lib/rmt/mirror/repomd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,6 @@ def initialize(logger:, mirroring_base_dir: RMT::DEFAULT_MIRROR_DIR, mirror_src:
@downloader = RMT::Downloader.new(logger: logger, track_files: !airgap_mode)
end

def mirror_suma_product_tree(repository_url:)
# we have an inconsistency in how we mirror in offline mode
# in normal mode we mirror in the following way:
# base_dir/repo/...
# however, in offline mode we mirror in the following way
# base_dir/...
# we need this extra step to ensure that we write to the public directory
base_dir = mirroring_base_dir
base_dir = File.expand_path(File.join(mirroring_base_dir, '/../')) if mirroring_base_dir == RMT::DEFAULT_MIRROR_DIR

repository_dir = File.join(base_dir, '/suma/')
mirroring_paths = {
base_url: URI.join(repository_url),
base_dir: repository_dir,
cache_dir: repository_dir
}

logger.info _('Mirroring SUSE Manager product tree to %{dir}') % { dir: repository_dir }
downloader.download_multi([RMT::Mirror::FileReference.new(relative_path: 'product_tree.json', **mirroring_paths)])
rescue RMT::Downloader::Exception => e
raise RMT::Mirror::Exception.new(_('Could not mirror SUSE Manager product tree with error: %{error}') % { error: e.message })
end

def mirror(repository_url:, local_path:, auth_token: nil, repo_name: nil)
repository_dir = File.join(mirroring_base_dir, local_path)

Expand Down
18 changes: 9 additions & 9 deletions spec/lib/rmt/cli/mirror_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
let(:error_messages) { "Mirroring SUMA product tree failed: #{suma_error}." }

it 'handles the exception and raises an error after mirroring all repos' do
expect_any_instance_of(RMT::Mirror::Repomd)
.to receive(:mirror_suma_product_tree)
expect_any_instance_of(RMT::Mirror::SumaProductTree)
.to receive(:mirror)
.and_raise(RMT::Mirror::Exception, suma_error)
expect_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror)

Expand All @@ -44,7 +44,7 @@
end

it 'raises an error' do
expect_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror_suma_product_tree)
expect_any_instance_of(RMT::Mirror::SumaProductTree).to receive(:mirror)
expect_any_instance_of(RMT::Mirror::Repomd).not_to receive(:mirror)

expect { command }
Expand All @@ -58,7 +58,7 @@
let!(:repository) { create :repository, :with_products, mirroring_enabled: true }

it 'updates repository mirroring timestamp' do
expect_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror_suma_product_tree)
expect_any_instance_of(RMT::Mirror::SumaProductTree).to receive(:mirror)
expect_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror)

Timecop.freeze(Time.utc(2018)) do
Expand All @@ -79,7 +79,7 @@
let(:error_messages) { /Repository '#{repository.name}' \(#{repository.friendly_id}\): #{mirroring_error}\./ }

it 'raises an error' do
expect_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror_suma_product_tree)
expect_any_instance_of(RMT::Mirror::SumaProductTree).to receive(:mirror)

expect { command }
.to raise_error(SystemExit) { |e| expect(e.status).to eq(1) }
Expand Down Expand Up @@ -115,7 +115,7 @@
let(:argv) { ['all', '--do-not-raise-unpublished'] }

it 'log the warning and does not raise an error' do
allow_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror_suma_product_tree)
allow_any_instance_of(RMT::Mirror::SumaProductTree).to receive(:mirror)
expect { command }.to output(error_log).to_stdout
end
end
Expand All @@ -126,7 +126,7 @@
let!(:additional_repository) { create :repository, :with_products, mirroring_enabled: false }

it 'mirrors additional repositories' do
expect_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror_suma_product_tree)
expect_any_instance_of(RMT::Mirror::SumaProductTree).to receive(:mirror)
expect_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror).with(
repository_url: repository.external_url,
local_path: anything,
Expand Down Expand Up @@ -156,7 +156,7 @@
let(:error_messages) { /Repository '#{repository.name}' \(#{repository.friendly_id}\): #{mirroring_error}\./ }

it 'handles exceptions and mirrors additional repositories' do
expect_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror_suma_product_tree)
expect_any_instance_of(RMT::Mirror::SumaProductTree).to receive(:mirror)
expect_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror).with(
repository_url: repository.external_url,
local_path: anything,
Expand Down Expand Up @@ -381,7 +381,7 @@

context 'mirror product using --do-not-raise-unpublished flag' do
it 'log the warning and does not raise an error' do
allow_any_instance_of(RMT::Mirror::Repomd).to receive(:mirror_suma_product_tree)
allow_any_instance_of(RMT::Mirror::SumaProductTree).to receive(:mirror)
expect { command }.to output(error_log).to_stdout
end
end
Expand Down
41 changes: 0 additions & 41 deletions spec/lib/rmt/mirror/repomd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,6 @@

let(:logger) { RMT::Logger.new('/dev/null') }

describe '#mirror_suma_product_tree' do
subject(:command) { rmt_mirror.mirror_suma_product_tree(repository_url: 'https://scc.suse.com/suma/') }

let(:rmt_mirror) do
described_class.new(
mirroring_base_dir: @tmp_dir,
logger: logger,
mirror_src: false
)
end

around do |example|
@tmp_dir = Dir.mktmpdir('rmt')
example.run
FileUtils.remove_entry(@tmp_dir)
end

context 'all is well', vcr: { cassette_name: 'mirroring_suma_product_tree' } do
before do
expect(logger).to receive(:info).with(/Mirroring SUSE Manager product tree to/).once
expect(logger).to receive(:info).with(/↓ product_tree.json/).once
end

it 'downloads the suma product tree' do
command
content = File.read(File.join(@tmp_dir, 'suma/product_tree.json'))
expect(Digest::SHA256.hexdigest(content)).to eq('7486026e9c1181affae5b21c9aa64637aa682fcdeacb099e213f0e8c7e86d85d')
end
end

context 'with download exception' do
before do
expect_any_instance_of(RMT::Downloader).to receive(:download_multi).and_raise(RMT::Downloader::Exception, "418 - I'm a teapot")
end

it 'raises mirroring exception' do
expect { command }.to raise_error(RMT::Mirror::Exception, "Could not mirror SUSE Manager product tree with error: 418 - I'm a teapot")
end
end
end

describe '#mirror' do
around do |example|
@tmp_dir = Dir.mktmpdir('rmt')
Expand Down

0 comments on commit 2ecb518

Please sign in to comment.