2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-12-04 08:07:04 +00:00

test(packages): reuse binary fixture I/O

Replace duplicated readers and writers with File::Slurper. Keep package command records, generated runners and existing missing-file policies.
This commit is contained in:
Vinícius Ferrão
2026-11-21 02:56:28 -03:00
parent 826f52ddac
commit f3a1b487fc
4 changed files with 37 additions and 75 deletions
@@ -1,6 +1,7 @@
#!/usr/bin/env perl
use strict;
use warnings;
use File::Slurper qw(read_binary write_binary);
use FindBin;
use File::Copy qw(copy);
use File::Path qw(make_path);
@@ -22,7 +23,7 @@ unless (-x "$postscripts/otherpkgs") {
my $dir = tempdir(CLEANUP => 1);
make_path("$dir/bin");
write_text("$dir/bin/dnf", <<'SH');
write_binary("$dir/bin/dnf", <<'SH');
#!/bin/sh
printf '%s\n' "$*" >> "$DNF_TRACE"
case " $* " in
@@ -31,7 +32,7 @@ case " $* " in
esac
exit 0
SH
write_text("$dir/bin/rpm", <<'SH');
write_binary("$dir/bin/rpm", <<'SH');
#!/bin/sh
case "$1" in
--version) echo 'RPM version 4.18.2'; exit 0 ;;
@@ -40,10 +41,10 @@ printf '%s\n' "$*" >> "$RPM_TRACE"
exit 1
SH
for my $name (qw(logger mount)) {
write_text("$dir/bin/$name", "#!/bin/sh\nexit 0\n");
write_binary("$dir/bin/$name", "#!/bin/sh\nexit 0\n");
}
write_text("$dir/bin/dpkg", "#!/bin/sh\nexit 1\n");
write_text("$dir/bin/wget", "#!/bin/sh\nprintf 'wget\\n' >> \"\$RPM_TRACE\"\nexit 1\n");
write_binary("$dir/bin/dpkg", "#!/bin/sh\nexit 1\n");
write_binary("$dir/bin/wget", "#!/bin/sh\nprintf 'wget\\n' >> \"\$RPM_TRACE\"\nexit 1\n");
chmod 0755, glob("$dir/bin/*");
my $wrapper = "$dir/run";
copy("$FindBin::Bin/fixtures/postscript-root.sh", $wrapper) or die "copy isolation wrapper: $!";
@@ -123,20 +124,7 @@ sub run_case {
my $output = do { local $/; <$pipe> } // '';
close($pipe);
my $rc = (($? & 127) ? 128 + ($? & 127) : $? >> 8);
my $repos = join('', map { read_text($_) } sort glob("$run/repos/*.repo"));
return ($rc, $output, $repos, -f "$run/dnf" ? read_text("$run/dnf") : '',
-f "$run/rpm" ? read_text("$run/rpm") : '');
}
sub read_text {
my ($path) = @_;
open(my $file, '<', $path) or die "read $path: $!";
return do { local $/; <$file> };
}
sub write_text {
my ($path, $text) = @_;
open(my $file, '>', $path) or die "write $path: $!";
print {$file} $text or die "write $path: $!";
close($file) or die "close $path: $!";
my $repos = join('', map { read_binary($_) } sort glob("$run/repos/*.repo"));
return ($rc, $output, $repos, -f "$run/dnf" ? read_binary("$run/dnf") : '',
-f "$run/rpm" ? read_binary("$run/rpm") : '');
}
@@ -1,6 +1,7 @@
#!/usr/bin/env perl
use strict;
use warnings;
use File::Slurper qw(read_binary write_binary);
use FindBin;
use File::Copy qw(copy);
use File::Path qw(make_path);
@@ -52,7 +53,7 @@ for my $package (
) {
my ($name, $version, $requires, $repo) = @$package;
my $spec = "$dir/$name-$version.spec";
write_text($spec, <<"SPEC");
write_binary($spec, <<"SPEC");
Name: $name
Version: $version
Release: 1
@@ -81,18 +82,18 @@ for my $repo (qw(old os vendor other/native)) {
copy("$dir/key.asc", "$dir/fixtures/$repo/repodata/repomd.xml.key") or die "copy key: $!";
}
copy("$FindBin::Bin/fixtures/dnf.pl", "$dir/bin/dnf") or die "copy DNF adapter: $!";
write_text("$dir/bin/mount", <<'SH');
write_binary("$dir/bin/mount", <<'SH');
#!/bin/sh
printf '%s on %s type nfs\n' "$NFSSERVER" "$INSTALLDIR"
SH
write_text("$dir/bin/rpm", <<'SH');
write_binary("$dir/bin/rpm", <<'SH');
#!/bin/sh
case "$1" in --version) exec /usr/bin/rpm "$@" ;; esac
printf '%s\n' "$*" >> "$RPM_TRACE"
exit 79
SH
for my $name (qw(logger dpkg wget)) {
write_text("$dir/bin/$name", "#!/bin/sh\nexit 1\n");
write_binary("$dir/bin/$name", "#!/bin/sh\nexit 1\n");
}
copy("$FindBin::Bin/fixtures/postscript-root.sh", "$dir/run") or die "copy isolation wrapper: $!";
chmod 0755, glob("$dir/bin/*"), "$dir/run";
@@ -177,12 +178,12 @@ sub run_case {
make_path("$run/repos", "$run/root", "$run/media");
my $key = $options{native_key} ? '/etc/pki/rpm-gpg/RPM-GPG-KEY-openEuler' : "$dir/key.asc";
if ($options{empty_key}) {
write_text("$run/empty-key", '');
write_binary("$run/empty-key", '');
$key = "$run/empty-key";
}
my $vendor = '[publisher]' . "\nname=publisher\nenabled=1\nskip_if_unavailable=False\ngpgcheck=1\ngpgkey=file://$dir/key.asc\nbaseurl=file://"
. ($options{vendor_available} ? "$dir/fixtures/vendor" : "$run/unavailable") . "\n";
write_text("$run/repos/openEuler.repo", $vendor);
write_binary("$run/repos/openEuler.repo", $vendor);
if ($options{seed_old}) {
my ($rc, $output) = capture('/usr/bin/rpm', '--root', "$run/root", '--import', "$dir/key.asc");
die $output if $rc;
@@ -237,13 +238,5 @@ sub capture {
sub read_text {
my ($path) = @_;
return '' unless -f $path;
open(my $file, '<', $path) or die "read $path: $!";
return do { local $/; <$file> };
}
sub write_text {
my ($path, $text) = @_;
open(my $file, '>', $path) or die "write $path: $!";
print {$file} $text or die "write $path: $!";
close($file) or die "close $path: $!";
return read_binary($path);
}
+11 -17
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env perl
use strict;
use warnings;
use File::Slurper qw(read_binary write_binary);
use File::Copy qw(copy);
use File::Path qw(make_path);
use File::Temp qw(tempdir);
@@ -16,7 +17,7 @@ my $wrapper = "$FindBin::Bin/fixtures/buildrpms-namespace.sh";
my $root = tempdir(CLEANUP => 1);
my $driver = "$root/driver.pl";
write_file($driver, <<'PERL');
write_binary($driver, <<'PERL');
BEGIN {
require Parallel::ForkManager;
no warnings 'redefine';
@@ -96,21 +97,21 @@ sub run_builder {
copy(repo_path('build-utils/lib/XCAT/BuildUtils.pm'), "$fixture/build-utils/lib/XCAT/BuildUtils.pm") or die $!;
copy(repo_path('build-utils/lib/XCAT/NFSLock.pm'), "$fixture/build-utils/lib/XCAT/NFSLock.pm") or die $!;
copy($driver, "$fixture/driver.pl") or die $!;
write_file("$fixture/Version", "2.18.0\n");
write_file("$fixture/Gitepoch", "1756000000\n");
write_file("$fixture/native-input/buildinfo.txt", "BUILD_TARGET=openeuler-24.03sp3-x86_64\n");
write_file("$fixture/os-release", "ID=$id\nVERSION=\"$version\"\nVERSION_ID=$version_id\n");
write_binary("$fixture/Version", "2.18.0\n");
write_binary("$fixture/Gitepoch", "1756000000\n");
write_binary("$fixture/native-input/buildinfo.txt", "BUILD_TARGET=openeuler-24.03sp3-x86_64\n");
write_binary("$fixture/os-release", "ID=$id\nVERSION=\"$version\"\nVERSION_ID=$version_id\n");
for my $target ('openeuler-24.03sp3-x86_64', 'openeuler-24.03-ppc64le') {
my $subdir = openeuler_repo_subdir($target);
for my $key (['core', "$fixture/dist/$target/rpms"], ['dep', "$fixture/dep/$subdir"]) {
make_path("$key->[1]/repodata");
write_file("$key->[1]/repodata/repomd.xml.key", 'existing exported key')
write_binary("$key->[1]/repodata/repomd.xml.key", 'existing exported key')
unless ($ENV{BUILD_MISSING_KEY} || '') eq $key->[0];
}
}
write_file("$fixture/bin/dnf", "#!/bin/sh\nprintf '%s\\n' \"\$*\" >> \"\$BUILD_FIXTURE/commands\"\nexit \"\${BUILD_DNF_FAILURE:-0}\"\n");
write_file("$fixture/bin/systemctl", "#!/bin/sh\nexit 0\n");
write_file("$fixture/bin/rpmdev-setuptree", "#!/bin/sh\nexit 0\n");
write_binary("$fixture/bin/dnf", "#!/bin/sh\nprintf '%s\\n' \"\$*\" >> \"\$BUILD_FIXTURE/commands\"\nexit \"\${BUILD_DNF_FAILURE:-0}\"\n");
write_binary("$fixture/bin/systemctl", "#!/bin/sh\nexit 0\n");
write_binary("$fixture/bin/rpmdev-setuptree", "#!/bin/sh\nexit 0\n");
chmod 0755, map {"$fixture/bin/$_"} qw(dnf systemctl rpmdev-setuptree);
local %ENV = (%ENV, HOME => "$fixture/home", BUILD_FIXTURE => $fixture, BUILD_ARCH => $arch, PATH => "$fixture/bin:$ENV{PATH}");
my $pid = fork();
@@ -129,15 +130,8 @@ sub run_builder {
&& !$ENV{BUILD_STOP} && !grep {$_ eq '--merge-core-repos'} @args;
return $result;
}
sub write_file {
my ($path, $contents) = @_;
open(my $fh, '>', $path) or die "$path: $!";
print {$fh} $contents;
close($fh) or die $!;
}
sub read_file {
my ($path) = @_;
return '' unless -f $path;
open(my $fh, '<', $path) or die $!;
return do {local $/; scalar <$fh>} || '';
return read_binary($path) || '';
}
@@ -1,6 +1,7 @@
#!/usr/bin/env perl
use strict;
use warnings;
use File::Slurper qw(read_binary write_binary);
use FindBin;
use File::Path qw(make_path);
use File::Spec;
@@ -13,9 +14,10 @@ plan skip_all => 'requires native Linux DNF and trusted RPM keys'
my $repo = File::Spec->rel2abs("$FindBin::Bin/../..");
my $dir = tempdir(CLEANUP => 1);
my $driver = "$dir/driver.pl";
write_file($driver, <<'PERL');
write_binary($driver, <<'PERL');
use strict;
use warnings;
use File::Slurper qw(read_binary);
use Storable qw(nstore);
use Text::ParseWords qw(shellwords);
our (@commands, $reached, $config, $restored);
@@ -32,9 +34,7 @@ BEGIN {
my @args = shellwords($command);
my ($index) = grep { $args[$_] eq '-c' } 0 .. $#args - 1;
die 'Package command omitted its configuration' unless defined($index);
open(my $file, '<', $args[$index + 1]) or die $!;
$config = do { local $/; <$file> };
close($file);
$config = read_binary($args[$index + 1]);
my $phase = $command =~ / erase .*pre-remove/ ? 'pre-remove'
: $command =~ / erase .*post-remove/ ? 'post-remove'
: $command =~ / install .*native-extra/ ? 'extra-install'
@@ -55,11 +55,7 @@ BEGIN {
};
}
END {
my $uname = '';
if (open(my $file, '<', "$ENV{GENIMAGE_ROOT}/bin/uname")) {
$uname = do { local $/; <$file> };
close($file);
}
my $uname = eval { read_binary("$ENV{GENIMAGE_ROOT}/bin/uname") } // '';
nstore({ commands => \@commands, reached => $reached, config => $config, uname => $uname },
"$ENV{GENIMAGE_CASE}/result");
unlink("/tmp/genimage.$$.yum.conf");
@@ -110,10 +106,10 @@ sub run_case {
my $case = tempdir(DIR => $dir, CLEANUP => 1);
my $root = "$case/image/rootimg";
make_path("$root/bin", "$root/etc", "$root/dev", "$root/run", "$root/sbin");
write_file("$root/bin/uname", "original uname\n");
write_binary("$root/bin/uname", "original uname\n");
chmod 0755, "$root/bin/uname";
write_file("$case/os.pkglist", "native-os\n");
write_file("$case/extra.pkglist", "-pre-remove\nvendor/native-extra\n--post-remove\n");
write_binary("$case/os.pkglist", "native-os\n");
write_binary("$case/extra.pkglist", "-pre-remove\nvendor/native-extra\n--post-remove\n");
local %ENV = %ENV;
@ENV{qw(GENIMAGE_SCRIPT GENIMAGE_ROOT GENIMAGE_CASE GENIMAGE_FAILURE XCATROOT)} =
("$repo/xCAT-server/share/xcat/netboot/rh/genimage", $root, $case, $failure, "$repo/xCAT-server");
@@ -131,15 +127,6 @@ sub run_case {
}
waitpid($pid, 0);
my $status = (($? & 127) ? 128 + ($? & 127) : $? >> 8);
open(my $file, '<', "$case/output") or die $!;
my $output = do { local $/; <$file> };
close($file);
my $output = read_binary("$case/output");
return ($status, retrieve("$case/result"), $output);
}
sub write_file {
my ($path, $contents) = @_;
open(my $file, '>', $path) or die "write $path: $!";
print {$file} $contents or die $!;
close($file) or die $!;
}