Skip to content

Commit

Permalink
Add fallback to Ruby Digest if command isn't found
Browse files Browse the repository at this point in the history
Signed-off-by: Jerry Aldrich <[email protected]>
  • Loading branch information
jerryaldrichiii committed Apr 1, 2018
1 parent 0c7324a commit 37b6108
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
23 changes: 17 additions & 6 deletions lib/train/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def perform_checksum(method)
res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
perform_ruby_checksum(method)
end

def perform_checksum_windows(method)
Expand All @@ -187,13 +187,24 @@ def perform_checksum_windows(method)
res = @backend.run_command(cmd)
return res.stdout.split("\r\n")[1].tr(' ', '') if res.exit_status == 0

raise_checksum_error(cmd, res)
perform_ruby_checksum(method)
end

def raise_checksum_error(cmd, res)
fail "Failed to get checksum with `#{cmd}`.\n" \
"STDOUT: #{res.stdout}\n" \
"STDERR: #{res.stderr}\n"
# This pulls the content of the file to the machine running Train and uses
# Digest to perform the checksum. This is less efficient than using remote
# system binaries and can lead to incorrect results due to encoding.
def perform_ruby_checksum(method)
case method
when :md5
res = Digest::MD5.new
when :sha256
res = Digest::SHA256.new
end

res.update(content)
res.hexdigest
rescue TypeError => _
nil
end
end
end
21 changes: 13 additions & 8 deletions test/unit/file/remote/qnx_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,28 @@
end

describe '#md5sum' do
it 'raises an error matching /command not found/' do
it 'performs a checksum via Ruby if the `md5sum` command fails' do
stderr = 'md5sum: command not found'
backend.mock_command('md5sum /tmp/testfile', nil, stderr, 1)
proc { cls.new(backend, '/tmp/testfile').md5sum }
.must_raise RuntimeError, /md5sum: command not found/

cls.new(backend, '/tmp/testfile').stub :content, 'mock' do |i|
i.md5sum.must_equal '17404a596cbd0d1e6c7d23fcd845ab82'
end
end
end

describe '#sha256sum' do
let(:sha256_checksum) {
'491260aaa6638d4a64c714a17828c3d82bad6ca600c9149b3b3350e91bcd283d'
'ec864fe99b539704b8872ac591067ef22d836a8d942087f2dba274b301ebe6e5'
}

it 'calculates the correct sha256sum on the `qnx` platform family' do
output = "#{sha256_checksum} /tmp/testfile"
backend.mock_command('shasum -a 256 /tmp/testfile', output)
cls.new(backend, '/tmp/testfile').sha256sum.must_equal sha256_checksum
it 'performs a checksum via Ruby if the `sha256sum` command fails' do
stderr = 'sha256sum: command not found'
backend.mock_command('sha256sum /tmp/testfile', nil, stderr, 1)

cls.new(backend, '/tmp/testfile').stub :content, 'mock' do |i|
i.sha256sum.must_equal sha256_checksum
end
end
end
end
1 change: 0 additions & 1 deletion test/unit/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def mockup(stubs)
new_cls.file_version.must_be_nil
end


describe 'type' do
it 'recognized type == file' do
fc = mockup(type: :file)
Expand Down

0 comments on commit 37b6108

Please sign in to comment.