diff --git a/lib/puppet/functions/archive/go_md5.rb b/lib/puppet/functions/archive/go_md5.rb index 2dc8042..017e421 100644 --- a/lib/puppet/functions/archive/go_md5.rb +++ b/lib/puppet/functions/archive/go_md5.rb @@ -1,36 +1,36 @@ require_relative '../../../puppet_x/bodeco/util.rb' # @summary # Retrieves and returns specific file's md5 from GoCD server md5 checksum file # @api private # @see http://www.thoughtworks.com/products/docs/go/12.4/help/Artifacts_API.html Puppet::Functions.create_function(:'archive::go_md5') do # @param username # GoCD username # @param password # GoCD password # @param file # GoCD filename # @param url # The GoCD MD5 checkum URL # @return [String] # The MD5 string dispatch :default_impl do param 'String', :username param 'String', :password param 'String[1]', :file param 'Stdlib::HTTPUrl', :url return_type 'String' end def default_impl(username, password, file, url) uri = URI(url) response = PuppetX::Bodeco::Util.content(uri, username: username, password: password) checksums = response.split("\n") line = checksums.find { |x| x =~ %r{#{file}=} } - md5 = line.match(%r{\b[0-9a-f]{5,40}\b}) - raise("Could not parse md5 from url#{url} response: #{response.body}") unless md5 + md5 = line.match(%r{\b[0-9a-f]{5,40}\b}) unless line.nil? + raise("Could not parse md5 from url #{url} response: #{response}") unless md5 md5[0] end end diff --git a/spec/functions/archive_go_md5_spec.rb b/spec/functions/archive_go_md5_spec.rb index 3b007ac..d880bdb 100644 --- a/spec/functions/archive_go_md5_spec.rb +++ b/spec/functions/archive_go_md5_spec.rb @@ -1,16 +1,21 @@ require 'spec_helper' describe 'archive::go_md5' do - let(:example_md5) do - File.read(fixtures('checksum', 'gocd.md5')) - end + let(:example_md5) { File.read(fixtures('checksum', 'gocd.md5')) } + let(:url) { 'https://gocd.lan/path/file.md5' } + let(:uri) { URI(url) } it { is_expected.not_to eq(nil) } it 'retreives file md5' do - url = 'https://gocd.lan/path/file.md5' - uri = URI(url) PuppetX::Bodeco::Util.stubs(:content).with(uri, username: 'user', password: 'pass').returns(example_md5) is_expected.to run.with_params('user', 'pass', 'filea', url).and_return('283158c7da8c0ada74502794fa8745eb') end + + context 'when file doesn\'t exist' do + it 'raises error' do + PuppetX::Bodeco::Util.stubs(:content).with(uri, username: 'user', password: 'pass').returns(example_md5) + is_expected.to run.with_params('user', 'pass', 'non-existent-file', url).and_raise_error(RuntimeError, "Could not parse md5 from url https://gocd\.lan/path/file\.md5 response: #{example_md5}") + end + end end