-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring: Moved suma product tree mirroring into a separate class
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |