-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add checksum testing to Rex::Test #1207
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# | ||
# (c) Jan Gehring <[email protected]> | ||
# | ||
# vim: set ts=2 sw=2 tw=0: | ||
# vim: set expandtab: | ||
|
||
package Rex::Test::Base::has_checksum; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
# VERSION | ||
|
||
use Rex -base; | ||
use Digest::SHA; | ||
use Rex::Commands::MD5; | ||
use base qw(Rex::Test::Base); | ||
|
||
sub new { | ||
my $that = shift; | ||
my $proto = ref($that) || $that; | ||
my $self = {@_}; | ||
|
||
bless( $self, $proto ); | ||
|
||
my ( $pkg, $file ) = caller(0); | ||
|
||
return $self; | ||
} | ||
|
||
sub run_test { | ||
my ( $self, $file, $checksum, $algo, $computed ) = (shift, shift, shift, shift); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not entirely sure about using positional arguments here. Perhaps it might be better with a hash? Also, there are 5 variables on the left hand side, but only 4 |
||
$algo = lc($algo || ''); | ||
my @supported = qw{md5 sha1 sha256}; | ||
|
||
return $self->ok( 0, "has_content: $file not found" ) unless is_file($file); | ||
return $self->ok( 0, "unsupported hash algorithm $algo") | ||
unless grep { $algo eq $_ } @supported; | ||
|
||
$computed = md5( $file ) if ($algo eq 'md5'); | ||
$computed = _sha( $file, $1 ) if ($algo =~ m/^sha(\d+)$/); | ||
|
||
$self->ok( $computed eq $checksum, "Checksum of $file is $checksum." ); | ||
} | ||
|
||
sub _sha { | ||
my ($file, $algo) = (shift, shift); | ||
my $sha = Digest::SHA->new($algo); | ||
$sha->addfile($file); | ||
return $sha->hexdigest; | ||
} | ||
|
||
|
||
1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use strict; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bonus points for the tests! 💯 |
||
use warnings; | ||
|
||
use Test::More; | ||
use Test::Exception; | ||
use Rex::Test::Base; | ||
|
||
sub rex_fails_ok { | ||
my ($t, $code, $test, $desc) = @_; | ||
my $testno = $t->builder->current_test; | ||
my ($output, $failure); | ||
$t->builder->output(\$output); | ||
$t->builder->failure_output(\$failure); | ||
$code->(); | ||
$t->builder->reset_outputs; | ||
$t->builder->current_test($testno); | ||
$t->builder->is_passing(1); | ||
like $failure, qr{Failed\stest\s}, 'test failed'; | ||
return like $failure, $test, $desc; | ||
} | ||
|
||
test { | ||
my $t = shift; | ||
|
||
$t->name('Rex::Test::Base testing'); | ||
|
||
note $t->has_dir('./t'), ' check this directory'; | ||
rex_fails_ok $t, sub { $t->has_dir('/saved-by-the-bell') }, | ||
qr{Found /saved-by-the-bell directory}, 'when dir is not there'; | ||
|
||
note $t->has_file('./t/file-tests.t'), ' check this file'; | ||
|
||
note $t->has_content('./t/cmdb/default.yml', qr{defaultname}), ' content check'; | ||
|
||
note $t->has_checksum('./t/cmdb/foo.yml', '63411aefa9fe64da5e33c762c01e6de4', 'md5'), | ||
' md5 checksum'; | ||
note $t->has_checksum( | ||
'./t/cmdb/foo.yml', 'e9ff883b4b3e6d515de774b7a7cff600aaab3d6c', 'SHA1' | ||
), ' sha1 checksum'; | ||
note $t->has_checksum( | ||
'./t/cmdb/foo.yml', '57a831cda8328d650d98260a376106976a6ba4a5b21b8b2fadb2796e88debcf1', 'Sha256'), | ||
' sha256 checksum'; | ||
|
||
rex_fails_ok $t, sub { | ||
$t->has_checksum('./t/cmdb/foo.yml', '57a831cda8328d650d98260a376106976a6ba4a5b21b8b2fadb2796e88debcf1', 'shal256'); | ||
}, qr/unsupported hash algorithm shal256/, 'bad algorithm'; | ||
|
||
rex_fails_ok $t, sub { | ||
$t->has_checksum('./t/cmdb/foo.yml', '57a831cda8328d650d98260', 'sha256'); | ||
}, qr{Checksum of \./t/cmdb/foo.yml is 57a831cda8328d650d98260}, 'incorrect checksum'; | ||
}; | ||
|
||
|
||
done_testing; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IANAL, but I'm pretty sure in this case the copyright is being held by the author of this file (and not by Jan :)).