Skip to content

Commit

Permalink
Fix repo_upload failure handling
Browse files Browse the repository at this point in the history
- a 404 response previously would output a message to stdout but then
  continue to try to parse JSON, which would fail.

- the `FailedFiles` and `Warnings` code never got executed because it
  was in `rescue` blocks that don't get triggered.
  • Loading branch information
msabramo committed Jul 23, 2016
1 parent 197f518 commit 8a7b026
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
16 changes: 5 additions & 11 deletions lib/aptly_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,20 @@ def repo_upload(repo_options = { name: nil, dir: nil, file: nil,
case response.code
when 404
puts 'repository with such name does not exist'
return response
end

json_response = JSON.parse(response.body)

unless json_response['FailedFiles'].empty?
begin
rescue StandardError => e
puts "Files that failed to upload... #{json_response['FailedFiles']}"
puts e
end
puts "Files that failed to upload... #{json_response['FailedFiles']}"
end

unless json_response['Report']['Warnings'].empty?
begin
rescue StandardError => e
puts "File upload warning message[s]...\
#{json_response['Report']['Warnings']}"
puts e
end
puts "File upload warning message[s]...\
#{json_response['Report']['Warnings']}"
end

return response
end
end
Expand Down
27 changes: 20 additions & 7 deletions test/test_aptly_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,37 @@ def test_repo_upload
'{"FailedFiles"=>[], "Report"=>{"Warnings"=>[], "Added"=>'\
'["geoipupdate_2.0.0_amd64 added"], "Removed"=>[]}}'
end

def test_repo_upload_repo_does_not_exist
file_api.file_post(file_uri: '/testdir',
package: 'testdir/fixtures/test_1.0_amd64.deb',
local_file: 'test/fixtures/test_1.0_amd64.deb')
assert_output /repository with such name does not exist/ do
assert_includes repo_api.repo_upload(
name: 'repodoesnotexist',
dir: 'testdir/',
file: 'test_1.0_amd64.deb').to_s,
'local repo with name repodoesnotexist not found'
end
end
end

describe 'API Upload to Repo, failure scenario' do
config = AptlyCli::AptlyLoad.new.configure_with(nil)
let(:repo_api_fail) { AptlyCli::AptlyRepo.new(config) }
let(:data) do
repo_api_fail.repo_upload(name: 'testrepo',
dir: 'rockpackages',
file: 'test_package_not_here',
noremove: true)
end

def test_repo_upload_fail_response
assert_output /Files that failed to upload.../ do
@data = repo_api_fail.repo_upload(name: 'testrepo',
dir: 'rockpackages',
file: 'test_package_not_here',
noremove: true)
end
assert_equal '["Unable to process /aptly/upload/'\
'rockpackages/test_package_not_here: stat '\
'/aptly/upload/rockpackages/test_package_not_here: '\
'no such file or directory"]',
data['Report']['Warnings'].to_s
@data['Report']['Warnings'].to_s
end
end

Expand Down

0 comments on commit 8a7b026

Please sign in to comment.