HealthCheck-Diagnostic-FilePermissions
view release on metacpan or search on metacpan
t/HealthCheck-Diagnostic-FilePermissions.t view on Meta::CPAN
info => qq{Have correct access for '$filename2'},
} ],
}, 'Test that we can override some instance values.';
# Create a method that returns the info and status after running the
# check. If it failed, then this just returns the error.
my $run_check_or_error = sub {
my $result;
local $@;
# We passed in a diagnostic, just run check.
if ( ref $_[0] ) {
$result = eval { $_[0]->check } if ref $_[0] ne 'HASH';
}
# We passed in some check parameters, send them in.
else {
$result = eval {
HealthCheck::Diagnostic::FilePermissions->check( @_ );
};
}
return [ $result->{status}, $result->{info} ] unless $@;
return $@;
};
# Check that we require a list of files.
like $run_check_or_error->(), qr/No files extracted/,
'Cannot run check without files.';
like $run_check_or_error->( files => [] ), qr/No files extracted/,
'Cannot run check without files in ARRAY.';
like $run_check_or_error->( files => sub {} ), qr/No files extracted/,
'Cannot run check without files from CODE.';
# Check for the file path existing on one file.
eq_or_diff( $run_check_or_error->( files => [ $filename ] ), [
'OK', qq{'$filename' exists},
], 'Only check for file that exists when passed file list.' );
eq_or_diff( $run_check_or_error->( files => [ 'file/doesnt/exist' ] ), [
'CRITICAL', qq{'file/doesnt/exist' does not exist},
], 'Show that a file doesn\'t exist when passed in file list.' );
# Check for the file path existing when we have multiple files.
eq_or_diff( $run_check_or_error->( files => [ 'nope', $filename ] ), [
'CRITICAL', qq{'nope' does not exist},
], 'Do not pass when one of the files doesn\'t exist.' );
eq_or_diff( $run_check_or_error->( files => [ $filename, $filename2 ] ), [
'OK', qq{Permissions are correct for '$filename' and '$filename2'},
], 'Pass when both files exist.' );
# Check that we can use a string for one file name.
eq_or_diff( $run_check_or_error->( files => $filename ), [
'OK', qq{'$filename' exists},
], 'Pass when sending in a string for the file list.' );
# Check that we can use a sub to generate the file names.
eq_or_diff( $run_check_or_error->(
files => sub { $filename, $filename2 },
), [
'OK', qq{Permissions are correct for '$filename' and '$filename2'},
], 'Pass when sending in a sub for the file list.' );
SKIP: {
skip "chmod, getpwuid, or getgrgid not supported"
if $^O eq 'MSWin32';
# Check for permissions on the file.
chmod( 0755, $filename );
$diagnostic = HealthCheck::Diagnostic::FilePermissions->new(
files => [ $filename ],
permissions => '493', # 0755 in decimal
);
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'OK', qq{Permissions are 0755 for '$filename'},
], 'Pass when given the right file permissions.' );
chmod( 0700, $filename );
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'CRITICAL',
qq{Permissions should be 0755 but are 0700 for '$filename'},
], 'Do not pass when the permissions are incorrect for a file.' );
# Check for permissions when we have multiple files.
chmod( 0644, $filename );
chmod( 0644, $filename2 );
$diagnostic = HealthCheck::Diagnostic::FilePermissions->new(
files => [ $filename, $filename2 ],
permissions => 0644,
);
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'OK', qq{Permissions are correct for '$filename' and '$filename2'},
], 'Pass when the files have the same permission.' );
chmod( 0700, $filename2 );
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'CRITICAL',
qq{Permissions should be 0644 but are 0700 for '$filename2'},
], 'Do not pass when one of the files has different permissions.' );
# Check for the right result when looking for permissions and one of the
# files doesn't exist.
$diagnostic = HealthCheck::Diagnostic::FilePermissions->new(
files => [ $filename, 'doesnt_exist' ],
permissions => 0644,
);
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'CRITICAL', qq{'doesnt_exist' does not exist},
], 'Do not pass when checking permissions and one file doesn\'t exist.' );
# Make sure it can access a file correctly.
my %access = (
full_name_hash => { read => 1, write => 1, execute => 1 },
short_name_hash => { r => 1, w => 1, x => 1 },
string => 'rwx',
);
foreach ( keys %access ) {
chmod( 0777, $filename );
$diagnostic = HealthCheck::Diagnostic::FilePermissions->new(
files => [ $filename ],
access => $access{$_},
);
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'OK', qq{Have correct access for '$filename'},
], "$_: Pass when it needs to rwx and can rwx." );
chmod( 0000, $filename );
eq_or_diff( $run_check_or_error->( $diagnostic ), [
t/HealthCheck-Diagnostic-FilePermissions.t view on Meta::CPAN
], 'Fail when permissions pass but another file does not exist.' );
chmod( 0666, $filename );
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'CRITICAL',
"Must have permission to execute '$filename'; Permissions ".
"should be 0777 but are 0666 for '$filename'; 'doesnt_exist' ".
"does not exist",
], 'Fail when permissions fail and another file does not exist.' );
# Check that we can ignore the results of some access permissions.
%access = (
full_name_hash => { write => 1 },
short_name_hash => { w => 1 },
string => 'w',
);
foreach ( keys %access ) {
chmod( 0600, $filename );
$diagnostic = HealthCheck::Diagnostic::FilePermissions->new(
files => $filename,
access => $access{$_},
);
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'OK', qq{Have correct access for '$filename'},
], "$_: Pass when it should w and can rw." );
chmod( 0000, $filename );
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'CRITICAL', qq{Must have permission to write '$filename'},
], "$_: Fail when it should w and cannot rwx." );
}
%access = (
full_name_hash => { execute => 0 },
short_name_hash => { x => 0 },
string => '!x',
);
foreach ( keys %access ) {
chmod( 0200, $filename );
$diagnostic = HealthCheck::Diagnostic::FilePermissions->new(
files => [ $filename ],
access => $access{$_},
);
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'OK', qq{Have correct access for '$filename'},
], "$_: Pass when it should not x and can w." );
chmod( 0700, $filename );
eq_or_diff( $run_check_or_error->( $diagnostic ), [
'CRITICAL',
qq{Must not have permission to execute '$filename'},
], "$_: Fail when it should not x and can rwx." );
}
# Test that it dies when we pass invalid access parameters.
my %f = ( files => $filename );
like $run_check_or_error->( %f, access => { write => 1, faker => 1 } ),
qr/Invalid access parameter: faker/,
'Fail when an access parameter hash is invalid.';
like $run_check_or_error->( %f, access => 'wrx!e' ),
qr/Invalid access parameter: e/,
'Fail when an access parameter string is invalid.';
# Test that we check for the group and owner correctly.
my $owner = getpwuid( ( stat $filename )[4] );
my $group = getgrgid( ( stat $filename )[5] );
eq_or_diff( $run_check_or_error->( %f, owner => $owner ), [
'OK', qq{Owner is $owner for '$filename'},
], 'Pass when the owner is correct.' );
eq_or_diff( $run_check_or_error->( %f, group => $group ), [
'OK', qq{Group is $group for '$filename'},
], 'Pass when the group is correct.' );
eq_or_diff(
$run_check_or_error->( %f, owner => $owner, group => $group ), [
'OK', qq{Permissions are correct for '$filename'},
], 'Pass when the owner and group are correct.' );
eq_or_diff( $run_check_or_error->( %f, owner => 'fake-owner' ), [
'CRITICAL',
qq{Owner should be fake-owner but is $owner for '$filename'},
], 'Fail when the owner is incorrect.' );
eq_or_diff( $run_check_or_error->( %f, group => 'fake-group' ), [
'CRITICAL',
qq{Group should be fake-group but is $group for '$filename'},
], 'Fail when the group is incorrect.' );
eq_or_diff( $run_check_or_error->( %f, group => 'fg', owner => 'fo' ), [
'CRITICAL',
"Owner should be fo but is $owner for '$filename'; Group should ".
"be fg but is $group for '$filename'",
], 'Fail when the owner and group are incorrect.' );
}
done_testing;
( run in 0.672 second using v1.01-cache-2.11-cpan-39bf76dae61 )