Skip to content

Commit

Permalink
Refactoring: Moved suma product tree mirroring into a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
ngetahun committed Jan 2, 2024
1 parent 76522ac commit 9528d47
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/rmt/mirror/suma_product_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class RMT::Mirror::SumaProductTree
FILE_URL = 'https://scc.suse.com/suma/'.freeze

attr_reader :mirroring_base_dir, :url, :logger

def initialize(logger:, mirroring_base_dir:, url: FILE_URL)
@mirroring_base_dir = mirroring_base_dir
@url = url
@logger = logger
end

def downloader
@downloader ||= RMT::Downloader.new(logger: logger, track_files: false)
end

def mirror
# NOTE: Incase we detect the default mirror path which ends with `public/repo`,
# we remove the `repo` sub-path since we expect the file to be stored in `public/`
# FIXME: refactor this into cli/mirror.rb
base_dir = mirroring_base_dir
base_dir = File.expand_path(File.join(mirroring_base_dir, '/../')) if mirroring_base_dir == RMT::DEFAULT_MIRROR_DIR

dest = File.join(base_dir, '/suma/')
ref = RMT::Mirror::FileReference.new(
relative_path: 'product_tree.json',
base_url: url,
base_dir: dest,
cache_dir: nil
)

logger.info _('Mirroring SUSE Manager product tree to %{dir}') % { dir: dest }
downloader.download_multi([ref])
rescue RMT::Downloader::Exception => e
raise RMT::Mirror::Exception.new(_('Could not mirror SUSE Manager product tree with error: %{error}') % { error: e.message })
end

end
51 changes: 51 additions & 0 deletions spec/lib/rmt/mirror/suma_product_tree_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'rails_helper'

RSpec.describe RMT::Mirror::SumaProductTree do
subject(:suma) { described_class.new(**configuration) }

let(:configuration) do
{
logger: RMT::Logger.new('/dev/null'),
mirroring_base_dir: base_dir
}
end
let(:ref_configuration) do
{
relative_path: 'product_tree.json',
base_url: described_class::FILE_URL,
cache_dir: nil,
base_dir: File.join(base_dir, '/suma/')
}
end

let(:base_dir) { '/tmp' }
let(:downloader) { instance_double RMT::Downloader }

describe '#mirror' do
before do
allow(suma).to receive(:downloader).and_return downloader
end

it 'mirrors the product_tree file' do
expect(RMT::Mirror::FileReference).to receive(:new).with(**ref_configuration)
expect(downloader).to receive(:download_multi)
suma.mirror
end

it 'fails to mirror the product file' do
expect(downloader).to receive(:download_multi).and_raise(RMT::Downloader::Exception, 'Network issues')
expect { suma.mirror }.to raise_error(RMT::Mirror::Exception, /Could not mirror SUSE Manager/)
end

context 'with default mirror dir' do
let(:updated_base_dir) { File.expand_path(File.join(RMT::DEFAULT_MIRROR_DIR, '/../')) }
let(:base_dir) { File.join(updated_base_dir, '/suma/') }

it 'alters the default mirroring path' do
expect(RMT::Mirror::FileReference).to receive(:new).with(**ref_configuration)
expect(downloader).to receive(:download_multi)
suma.mirror
end
end
end
end

0 comments on commit 9528d47

Please sign in to comment.