mirror of
https://github.com/xcat2/xcat-core.git
synced 2026-12-04 08:07:04 +00:00
test(uftp): cover write failures and temporary file ownership
This commit is contained in:
@@ -3,17 +3,19 @@ use strict;
|
||||
use warnings;
|
||||
|
||||
use Digest::SHA qw(sha256_hex);
|
||||
use Errno qw(EIO EPERM);
|
||||
use Errno qw(EIO EPERM ENOSPC EACCES);
|
||||
use File::Slurper qw(read_binary write_binary);
|
||||
use File::Temp qw(tempdir);
|
||||
use FindBin;
|
||||
use lib "$FindBin::Bin/../../perl-xCAT", "$FindBin::Bin/../../xCAT-server/lib/perl";
|
||||
use Test::More;
|
||||
use Symbol qw(qualify_to_ref);
|
||||
|
||||
our ($FAIL, $OWNER);
|
||||
BEGIN {
|
||||
*CORE::GLOBAL::close = sub (*) {
|
||||
my $result = CORE::close($_[0]);
|
||||
my $handle = ref($_[0]) ? $_[0] : qualify_to_ref($_[0], scalar caller);
|
||||
my $result = CORE::close($handle);
|
||||
if (($FAIL // '') eq 'close' && caller eq $OWNER) {
|
||||
$! = EIO;
|
||||
return 0;
|
||||
@@ -34,8 +36,25 @@ BEGIN {
|
||||
}
|
||||
return CORE::rename($_[0], $_[1]);
|
||||
};
|
||||
*CORE::GLOBAL::syswrite = sub (*$;$$) {
|
||||
if (($FAIL // '') eq 'write' && caller eq $OWNER) {
|
||||
$! = ENOSPC;
|
||||
return undef;
|
||||
}
|
||||
my $length = $_[2] // length($_[1]);
|
||||
$length-- if ($FAIL // '') eq 'short-write' && caller eq $OWNER;
|
||||
my $written = CORE::syswrite($_[0], $_[1], $length, $_[3] // 0);
|
||||
$! = ENOSPC if ($FAIL // '') eq 'short-write' && caller eq $OWNER;
|
||||
return $written;
|
||||
};
|
||||
}
|
||||
require xCAT::SvrUtils;
|
||||
require xCAT::UFTP;
|
||||
|
||||
sub errno_text {
|
||||
local $! = shift;
|
||||
return "$!";
|
||||
}
|
||||
|
||||
sub entries {
|
||||
my ($dir) = @_;
|
||||
@@ -59,7 +78,8 @@ subtest 'checksum publication' => sub {
|
||||
write_binary($target, 'previous checksum');
|
||||
chmod(0640, $target) or die $!;
|
||||
local ($FAIL, $OWNER) = ($fault, 'xCAT::SvrUtils');
|
||||
like(xCAT::SvrUtils->write_rootimg_checksum($image), qr/^Cannot write \Q$target\E: /,
|
||||
my $reason = errno_text($fault eq 'chmod' ? EPERM : EIO);
|
||||
like(xCAT::SvrUtils->write_rootimg_checksum($image), qr/^Cannot write \Q$target\E: \Q$reason\E$/,
|
||||
"$fault failure is returned");
|
||||
is(scalar(read_binary($target)), 'previous checksum', "$fault failure keeps previous bytes");
|
||||
is((stat($target))[2] & 07777, 0640, "$fault failure keeps previous permissions");
|
||||
@@ -70,7 +90,8 @@ subtest 'checksum publication' => sub {
|
||||
chmod(0500, $dir) or die $!;
|
||||
my $error = xCAT::SvrUtils->write_rootimg_checksum($image);
|
||||
chmod(0700, $dir) or die $!;
|
||||
like($error, qr/^Cannot write \Q$target\E: /, 'creation failure is returned, not thrown');
|
||||
my $reason = errno_text(EACCES);
|
||||
like($error, qr/^Cannot write \Q$target\E: \Q$reason\E$/, 'creation failure is returned, not thrown');
|
||||
is(scalar(read_binary($target)), 'previous checksum', 'creation failure preserves published bytes');
|
||||
is_deeply(entries($dir), [qw(rootimg.gz rootimg.gz.sha256)], 'creation failure leaves no staging file');
|
||||
}
|
||||
@@ -94,4 +115,63 @@ subtest 'checksum publication' => sub {
|
||||
}
|
||||
};
|
||||
|
||||
subtest 'UFTP publication' => sub {
|
||||
my $dir = tempdir(CLEANUP => 1);
|
||||
my $target = "$dir/hosts";
|
||||
for my $bytes ('', '0', "binary\0hostlist\xff\n") {
|
||||
for my $mode (0600, 0644) {
|
||||
is(xCAT::UFTP::write_file_atomic($target, $bytes, $mode), undef, 'file publication succeeds');
|
||||
is(scalar(read_binary($target)), $bytes, 'published bytes are unchanged');
|
||||
is((stat($target))[2] & 07777, $mode, 'requested permissions are applied');
|
||||
}
|
||||
}
|
||||
for my $fault (qw(write short-write sync close chmod rename)) {
|
||||
write_binary($target, 'previous hosts');
|
||||
chmod(0640, $target) or die $!;
|
||||
local ($FAIL, $OWNER) = ($fault, 'xCAT::UFTP');
|
||||
my $sync = IO::Handle->can('sync');
|
||||
no warnings 'redefine';
|
||||
local *IO::Handle::sync = sub {
|
||||
if ($fault eq 'sync') { $! = EIO; return 0 }
|
||||
return $sync->(@_);
|
||||
};
|
||||
my $step = $fault =~ /write/ ? 'write' : $fault eq 'sync' ? 'flush'
|
||||
: $fault eq 'close' ? 'close' : 'publish';
|
||||
my $reason = errno_text($fault =~ /write/ ? ENOSPC : $fault eq 'chmod' ? EPERM : EIO);
|
||||
like(xCAT::UFTP::write_file_atomic($target, 'new hosts', 0600), qr/^Cannot $step .+: \Q$reason\E$/,
|
||||
"$fault failure is returned");
|
||||
is(scalar(read_binary($target)), 'previous hosts', "$fault failure keeps previous bytes");
|
||||
is((stat($target))[2] & 07777, 0640, "$fault failure keeps previous permissions");
|
||||
is_deeply(entries($dir), ['hosts'], "$fault failure leaves no staging file");
|
||||
}
|
||||
SKIP: {
|
||||
skip 'permission denial requires an unprivileged user', 3 unless $>;
|
||||
chmod(0500, $dir) or die $!;
|
||||
my $error = xCAT::UFTP::write_file_atomic($target, 'new hosts', 0600);
|
||||
chmod(0700, $dir) or die $!;
|
||||
my $reason = errno_text(EACCES);
|
||||
like($error, qr/^Cannot create .+: \Q$reason\E$/, 'creation failure is returned, not thrown');
|
||||
is(scalar(read_binary($target)), 'previous hosts', 'creation failure preserves published bytes');
|
||||
is_deeply(entries($dir), ['hosts'], 'creation failure leaves no staging file');
|
||||
}
|
||||
for my $kind (qw(file symlink)) {
|
||||
my $old_temp = "$dir/.hosts.tmp$$";
|
||||
write_binary("$dir/unrelated", 'leave this alone');
|
||||
if ($kind eq 'symlink') {
|
||||
symlink('unrelated', $old_temp) or die $!;
|
||||
} else {
|
||||
write_binary($old_temp, 'other writer');
|
||||
}
|
||||
is(xCAT::UFTP::write_file_atomic($target, 'new hosts', 0600), undef,
|
||||
"$kind at an old staging name does not block publication");
|
||||
is(scalar(read_binary("$dir/unrelated")), 'leave this alone', "$kind does not redirect the write");
|
||||
ok(-e $old_temp, "$kind at the old staging name is retained");
|
||||
is(-e $old_temp ? scalar(read_binary($old_temp)) : undef,
|
||||
$kind eq 'symlink' ? 'leave this alone' : 'other writer', "$kind retains its contents");
|
||||
is_deeply(entries($dir), [".hosts.tmp$$", 'hosts', 'unrelated'], 'only owned staging files are removed');
|
||||
unlink($old_temp) if -e $old_temp || -l $old_temp;
|
||||
unlink("$dir/unrelated") or die $!;
|
||||
}
|
||||
};
|
||||
|
||||
done_testing();
|
||||
|
||||
@@ -129,6 +129,16 @@ write_binary("$tmp/server.fingerprint", "$fp\n");
|
||||
is($error, undef, 'the runtime check passes');
|
||||
is($fingerprint, $fp, 'the runtime check returns the fingerprint of the server key');
|
||||
is(scalar(() = glob("$tmp/log/.write-check*")), 0, 'the write check leaves no file in log_dir');
|
||||
{
|
||||
my $old_probe = "$tmp/log/.write-check.$$";
|
||||
write_binary($old_probe, 'another process owns this file');
|
||||
is((runtime($checked))[1], undef, 'an occupied old probe name does not block the runtime check');
|
||||
ok(-f $old_probe, 'the runtime check keeps the unrelated probe file');
|
||||
is(-f $old_probe ? scalar(read_binary($old_probe)) : undef, 'another process owns this file',
|
||||
'the runtime check keeps unrelated probe bytes');
|
||||
is_deeply([glob("$tmp/log/.write-check*")], [$old_probe], 'the runtime check removes only its own probe');
|
||||
unlink($old_probe) if -e $old_probe;
|
||||
}
|
||||
(undef, $error) = runtime($checked, sub { (undef, "$tmp/uftp_keymgt failed for $tmp/server.key") });
|
||||
is($error, "$tmp/uftp_keymgt failed for $tmp/server.key", 'a key that uftp_keymgt cannot read is refused');
|
||||
chmod(0640, "$tmp/server.key");
|
||||
|
||||
Reference in New Issue
Block a user