Skip to content

Commit

Permalink
Adding tests to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
likhitha77 committed Jan 9, 2024
1 parent e0a0eec commit 75d9d83
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/rmt/mirror/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def mirror
protected

attr_accessor :temp_dirs, :downloader, :deep_verify, :is_airgapped, :mirroring_base_dir
attr_reader :enqueued

def file_reference(relative, to:)
RMT::Mirror::FileReference.new(
Expand Down
61 changes: 59 additions & 2 deletions spec/lib/rmt/mirror/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:configuration) do
{
repository: repository,
logger: RMT::Logger.new('/dev/null'),
logger: logger,
mirroring_base_dir: '/rspec/repository',
mirror_src: enable_source_mirroring
}
Expand All @@ -21,13 +21,37 @@

let(:downloader) { instance_double('downloader') }

let(:logger) { instance_double('RMT::Logger') }

before do
# Make all protected methods public for testing purpose
described_class.send(:public, *described_class.protected_instance_methods)

allow(base).to receive(:downloader).and_return(downloader)
end

# FIXME: rewrite tests for mirror and mirror_implementation.
# This is a placeholder added due to low code coverage issue
describe '#mirror' do
it 'mirrors repositories and licenses' do
expect(base).to receive(:mirror_implementation)
expect(base).to receive(:cleanup_temp_dirs)
base.mirror
end

it 'throws an exception if mirroring fails' do
allow(base).to receive(:mirror_implementation).and_raise(RMT::Mirror::Exception)
expect(base).to receive(:cleanup_temp_dirs)
expect { base.mirror }.to raise_error(RMT::Mirror::Exception)
end
end

describe '#mirror_implementation' do
it 'will implement the main mirroring logic' do
expect { base.mirror_implementation }.to raise_error('Not implemented!')
end
end

describe '#download_cached' do
let(:resource) { 'somedir/somefile.json' }
let(:temp) { '/temp/path' }
Expand Down Expand Up @@ -128,14 +152,36 @@
end
end

context 'has invalid or no signature' do
context 'is unable to download the signature files' do
it 'raises exception' do
expect(downloader).to receive(:download_multi).and_raise(RMT::Downloader::Exception, 'foo')
expect do
base.check_signature(key_file: key_file, signature_file: signature_file, metadata_file: metadata)
end.to raise_error(/foo/)
end
end

context 'the signature file is missing' do
let(:response) { Typhoeus::Response.new(code: 404, body: {}) }

it 'creates a log entry' do
allow(downloader).to receive(:download_multi).and_raise(RMT::Downloader::Exception.new('missing file', response: response))
expect(logger).to receive(:info).with(/metadata signatures are missing/)
base.check_signature(key_file: key_file, signature_file: signature_file, metadata_file: metadata)
end
end
end

describe '#download_enqueued' do
before do
base.enqueue(base.file_reference('enqueued_file', to: '/test/path/'))
end

it 'downloads enqueued contents and clear queue' do
expect(downloader).to receive(:download_multi)
base.download_enqueued
expect(base.enqueued).to be_empty
end
end

describe '#replace_directory' do
Expand Down Expand Up @@ -186,6 +232,17 @@
end
end

describe '#copy_directory_content' do
let(:src) { '/source/path' }
let(:dest) { '/destination/path' }

it 'copies content from source to destination without backup' do
expect(base).to receive(:replace_directory).with(source: src, destination: dest, with_backup: false).and_yield
expect(FileUtils).to receive(:mv).with(Dir.glob(src), dest)
base.copy_directory_content(source: src, destination: dest)
end
end

describe '#need_to_download?' do
let(:source_package) do
ref = base.file_reference('neovim-0.9.4.src.rpm', to: '/test/path/')
Expand Down
30 changes: 30 additions & 0 deletions spec/lib/rmt/mirror/debian_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@
end
let(:packages_ref) { RMT::Mirror::FileReference.new(**config) }

describe '#mirror_implementation' do
before do
allow(debian).to receive(:temp).with(:metadata).and_return('bar')
end

it 'mirrors the metadata' do
allow(debian).to receive(:create_temp_dir).with(:metadata)
expect(debian).to receive(:mirror_metadata)
allow(debian).to receive(:mirror_packages)
allow(debian).to receive(:copy_directory_content)
debian.mirror_implementation
end

it 'mirrors the packages' do
allow(debian).to receive(:create_temp_dir).with(:metadata)
allow(debian).to receive(:mirror_metadata).and_return([])
expect(debian).to receive(:mirror_packages)
allow(debian).to receive(:copy_directory_content)
debian.mirror_implementation
end

it 'moves the files to correct directories' do
allow(debian).to receive(:create_temp_dir).with(:metadata)
allow(debian).to receive(:mirror_metadata).and_return([])
allow(debian).to receive(:mirror_packages)
expect(debian).to receive(:copy_directory_content)
debian.mirror_implementation
end
end

describe '#mirror_metadata' do
let(:config) do
{
Expand Down

0 comments on commit 75d9d83

Please sign in to comment.