From 75d9d835c8310a512de83154c0044a5fc32e342d Mon Sep 17 00:00:00 2001 From: Likhitha Priya Date: Mon, 8 Jan 2024 10:07:40 +0000 Subject: [PATCH] Adding tests to improve coverage --- lib/rmt/mirror/base.rb | 1 + spec/lib/rmt/mirror/base_spec.rb | 61 +++++++++++++++++++++++++++++- spec/lib/rmt/mirror/debian_spec.rb | 30 +++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/lib/rmt/mirror/base.rb b/lib/rmt/mirror/base.rb index 792f59377..c08b0f0cf 100644 --- a/lib/rmt/mirror/base.rb +++ b/lib/rmt/mirror/base.rb @@ -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( diff --git a/spec/lib/rmt/mirror/base_spec.rb b/spec/lib/rmt/mirror/base_spec.rb index 42bb58da6..02a9b4189 100644 --- a/spec/lib/rmt/mirror/base_spec.rb +++ b/spec/lib/rmt/mirror/base_spec.rb @@ -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 } @@ -21,6 +21,8 @@ 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) @@ -28,6 +30,28 @@ 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' } @@ -128,7 +152,7 @@ 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 @@ -136,6 +160,28 @@ 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 @@ -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/') diff --git a/spec/lib/rmt/mirror/debian_spec.rb b/spec/lib/rmt/mirror/debian_spec.rb index 60fc47d52..5825641f9 100644 --- a/spec/lib/rmt/mirror/debian_spec.rb +++ b/spec/lib/rmt/mirror/debian_spec.rb @@ -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 {