diff --git a/xCAT-test/native/detect_dhcpd_tcpdump_lookup.t b/xCAT-test/native/detect_dhcpd_tcpdump_lookup.t index 5058785445..970656af34 100644 --- a/xCAT-test/native/detect_dhcpd_tcpdump_lookup.t +++ b/xCAT-test/native/detect_dhcpd_tcpdump_lookup.t @@ -5,6 +5,7 @@ use warnings; use Config; use File::Path qw(make_path); use File::Spec; +use File::Slurper qw(read_binary write_binary); use File::Temp qw(tempdir); use FindBin; use Test::More; @@ -47,9 +48,7 @@ delete @ENV{qw(PERL5LIB PERLLIB PERL5OPT)}; sub write_script { my ( $path, $body, $interpreter ) = @_; $interpreter ||= '/bin/sh'; - open( my $fh, '>', $path ) or die "$path: $!"; - print {$fh} "#!$interpreter\n$body"; - close($fh); + write_binary($path, "#!$interpreter\n$body"); chmod 0755, $path; } @@ -87,10 +86,7 @@ sub run_with_path { } sub recorded_invocation { - open( my $fh, '<', $marker ) or return; - chomp( my @lines = <$fh> ); - close($fh); - return @lines; + return slurp_lines($marker); } my $plain = fixture_bin(); @@ -203,9 +199,9 @@ sub run_isolated_status { sub slurp_lines { my ($path) = @_; - open( my $fh, '<', $path ) or return; - chomp( my @lines = <$fh> ); - close($fh); + my $contents = eval { read_binary($path) }; + return if $@; + chomp( my @lines = split /(?<=\n)/, $contents ); return @lines; } diff --git a/xCAT-test/native/ip_forwarding.t b/xCAT-test/native/ip_forwarding.t index ead23c6684..df1c8b0f74 100644 --- a/xCAT-test/native/ip_forwarding.t +++ b/xCAT-test/native/ip_forwarding.t @@ -1,6 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; +use File::Slurper qw(read_binary write_binary); use Cwd qw(abs_path); use File::Copy qw(copy); use File::Path qw(make_path); @@ -20,30 +21,18 @@ my $tmp = tempdir(DIR => '/var/tmp', CLEANUP => !$ENV{XCAT_FORWARD_KEEP}); diag("fixtures: $tmp") if $ENV{XCAT_FORWARD_KEEP}; make_path("$tmp/bin"); -sub write_file { - my ($path, $data) = @_; - open(my $f, '>', $path) or die "$path: $!"; - print {$f} $data; - close($f) or die "$path: $!"; -} -sub read_file { - my ($path) = @_; - open(my $f, '<', $path) or die "$path: $!"; - local $/; - return <$f> // ''; -} -my $host_config = read_file('/etc/sysctl.conf'); -my $host_forwarding = read_file('/proc/sys/net/ipv4/ip_forward'); +my $host_config = read_binary('/etc/sysctl.conf'); +my $host_forwarding = read_binary('/proc/sys/net/ipv4/ip_forward'); my $host_mount = readlink('/proc/self/ns/mnt'); my $host_net = readlink('/proc/self/ns/net'); my $sysctl = `command -v sysctl`; chomp $sysctl; -write_file("$tmp/bin/no", <<'SH'); +write_binary("$tmp/bin/no", <<'SH'); #!/bin/sh printf '%s\n' "$*" >> "$XCAT_FORWARD_FIXTURE/no.log" exit "${XCAT_FORWARD_AIX_RC:-0}" SH -write_file("$tmp/namespace", <<'SH'); +write_binary("$tmp/namespace", <<'SH'); #!/bin/bash set -eu [ "$(readlink /proc/self/ns/mnt)" != "$XCAT_FORWARD_HOST_MOUNT" ] @@ -53,7 +42,7 @@ mount --bind "$XCAT_FORWARD_FIXTURE/etc" /etc "$XCAT_FORWARD_SYSCTL" -qw net.ipv4.ip_forward="$XCAT_FORWARD_INITIAL" exec /usr/bin/perl "$XCAT_FORWARD_RUNNER" SH -write_file("$tmp/call-helper.pl", <<'CHILD'); +write_binary("$tmp/call-helper.pl", <<'CHILD'); use strict; use warnings; use lib "$ENV{XCAT_FORWARD_SOURCE_ROOT}/perl-xCAT"; @@ -105,7 +94,7 @@ for my $case (@cases) { $expected //= "net.ipv4.ip_forward = $enable\n"; my $fixture = "$tmp/case-" . ++$number; make_path("$fixture/etc/sysctl.d"); - write_file("$fixture/etc/sysctl.conf", $input); + write_binary("$fixture/etc/sysctl.conf", $input); symlink '../sysctl.conf', "$fixture/etc/sysctl.d/99-sysctl.conf" or die $!; for my $file (qw(ld.so.cache os-release openEuler-release)) { copy("/etc/$file", "$fixture/etc/$file") or die $! if -f "/etc/$file"; @@ -129,26 +118,26 @@ for my $case (@cases) { waitpid($pid, 0); my $status = (($? & 127) ? 128 + ($? & 127) : $? >> 8); is($status, 0, "pass $pass invokes the full production module in isolation"); - if ($status) { diag(read_file("$fixture/stderr-$pass.log")); next; } - my $result = decode_json(read_file("$fixture/result-$pass.json")); + if ($status) { diag(read_binary("$fixture/stderr-$pass.log")); next; } + my $result = decode_json(read_binary("$fixture/result-$pass.json")); is($result->{rc}, 0, "pass $pass preserves the helper return contract"); isnt($result->{mount_namespace}, $host_mount, "pass $pass uses a separate mount namespace"); isnt($result->{network_namespace}, $host_net, "pass $pass uses a separate network namespace"); - is(read_file("$fixture/etc/sysctl.conf"), $expected, "pass $pass persists only the expected assignment"); - is(read_file("$fixture/etc/sysctl.d/99-sysctl.conf"), $expected, "pass $pass retains the sysctl.d alias"); + is(read_binary("$fixture/etc/sysctl.conf"), $expected, "pass $pass persists only the expected assignment"); + is(read_binary("$fixture/etc/sysctl.d/99-sysctl.conf"), $expected, "pass $pass retains the sysctl.d alias"); is(0 + $result->{forwarding}, $aix ? 1 - $enable : $enable, "pass $pass has the expected real kernel forwarding state"); } if ($aix) { - is(read_file("$fixture/no.log"), "-o ipforwarding=$enable\n" x 2, + is(read_binary("$fixture/no.log"), "-o ipforwarding=$enable\n" x 2, 'AIX keeps its existing native command and arguments'); } else { ok(!-e "$fixture/no.log", 'Linux never invokes the AIX command'); } }; } -is(read_file('/etc/sysctl.conf'), $host_config, 'host sysctl configuration is unchanged'); -is(read_file('/proc/sys/net/ipv4/ip_forward'), $host_forwarding, 'host forwarding state is unchanged'); +is(read_binary('/etc/sysctl.conf'), $host_config, 'host sysctl configuration is unchanged'); +is(read_binary('/proc/sys/net/ipv4/ip_forward'), $host_forwarding, 'host forwarding state is unchanged'); is(readlink('/proc/self/ns/mnt'), $host_mount, 'test process retained its original mount namespace'); is(readlink('/proc/self/ns/net'), $host_net, 'test process retained its original network namespace'); done_testing(); diff --git a/xCAT-test/native/openeuler_install_repositories.t b/xCAT-test/native/openeuler_install_repositories.t index 613dfac78b..56a8d56e8e 100644 --- a/xCAT-test/native/openeuler_install_repositories.t +++ b/xCAT-test/native/openeuler_install_repositories.t @@ -1,6 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; +use File::Slurper qw(read_binary write_binary); use Digest::SHA qw(sha256_hex); use File::Copy qw(copy); use File::Path qw(make_path); @@ -30,11 +31,11 @@ my $error = xCAT::Template->subvars(repo_path('xCAT-server/share/xcat/install/sc "$tmp/post.rhels8", 'node', undef, '/install/media', 'openeuler', undef, {xcatmaster => '192.0.2.1'}); die "Cannot render post.rhels8: $error" if $error; -write_file("$tmp/bin/nmcli", "#!/bin/sh\nexit 0\n"); +write_binary("$tmp/bin/nmcli", "#!/bin/sh\nexit 0\n"); chmod 0755, "$tmp/bin/nmcli" or die $!; is(system('/bin/bash', '-n', "$tmp/post.rhels8"), 0, 'the complete rendered post owner has valid Bash syntax'); -write_file("$tmp/namespace", <<'SH'); +write_binary("$tmp/namespace", <<'SH'); #!/bin/bash set -eu [ "$(readlink /proc/self/ns/mnt)" != "$XCAT_POST_HOST_MOUNT" ] @@ -51,7 +52,7 @@ my $native = "[OS]\nenabled=1\ngpgcheck=1\n[update]\n \tenabled = 1\n[disabled]\ my $native_expected = "[OS]\nenabled=0\ngpgcheck=1\n[update]\nenabled=0\n[disabled]\nenabled=0\n#enabled=1\n"; my $host_mount = readlink('/proc/self/ns/mnt'); my $host_net = readlink('/proc/self/ns/net'); -my %host_repos = map { $_ => sha256_hex(read_file($_)) } +my %host_repos = map { $_ => sha256_hex(read_binary($_)) } glob('/etc/yum.repos.d/*.repo'); for my $case ( @@ -65,10 +66,10 @@ for my $case ( make_path($repos); copy('/etc/ld.so.cache', "$fixture/etc/ld.so.cache") or die $! if -f '/etc/ld.so.cache'; - write_file("$repos/$_", $enabled) for @legacy; - write_file("$repos/openEuler.repo", $input) if defined $input; + write_binary("$repos/$_", $enabled) for @legacy; + write_binary("$repos/openEuler.repo", $input) if defined $input; my @preserved = qw(local-repository-0.repo xCAT-custom.repo administrator.repo); - write_file("$repos/$_", $enabled) for @preserved; + write_binary("$repos/$_", $enabled) for @preserved; subtest $name => sub { local %ENV = (%ENV, XCATDEBUGMODE => '0', MASTER_IP => '192.0.2.1', XCAT_POST_REPOS_FIXTURE => $fixture, XCAT_POST_REPOS_OWNER => "$tmp/post.rhels8", @@ -84,36 +85,20 @@ for my $case ( } waitpid($pid, 0); is($?, 0, "pass $pass runs the whole post owner in isolation") - or diag(read_file("$fixture/stderr-$pass.log")); + or diag(read_binary("$fixture/stderr-$pass.log")); if (defined $expected) { - is(read_file("$repos/openEuler.repo"), $expected, + is(read_binary("$repos/openEuler.repo"), $expected, "pass $pass disables only active native repository assignments"); } else { ok(!-e "$repos/openEuler.repo", "pass $pass does not create an absent vendor file"); } - is(read_file("$repos/$_"), $disabled, "pass $pass preserves policy for $_") for @legacy; - is(read_file("$repos/$_"), $enabled, "pass $pass leaves $_ unchanged") for @preserved; + is(read_binary("$repos/$_"), $disabled, "pass $pass preserves policy for $_") for @legacy; + is(read_binary("$repos/$_"), $enabled, "pass $pass leaves $_ unchanged") for @preserved; } }; } -is_deeply({map { $_ => sha256_hex(read_file($_)) } glob('/etc/yum.repos.d/*.repo')}, +is_deeply({map { $_ => sha256_hex(read_binary($_)) } glob('/etc/yum.repos.d/*.repo')}, \%host_repos, 'host repository files remain unchanged'); is(readlink('/proc/self/ns/mnt'), $host_mount, 'host mount namespace remains unchanged'); is(readlink('/proc/self/ns/net'), $host_net, 'host network namespace remains unchanged'); done_testing(); - -sub write_file { - my ($path, $contents) = @_; - open(my $fh, '>:raw', $path) or die "$path: $!"; - print {$fh} $contents; - close($fh) or die "$path: $!"; -} - -sub read_file { - my ($path) = @_; - open(my $fh, '<:raw', $path) or die "$path: $!"; - local $/; - my $contents = <$fh>; - close($fh) or die "$path: $!"; - return $contents // ''; -} diff --git a/xCAT-test/native/openeuler_networking.t b/xCAT-test/native/openeuler_networking.t index ba9aea2fa5..a9cd5d62a7 100644 --- a/xCAT-test/native/openeuler_networking.t +++ b/xCAT-test/native/openeuler_networking.t @@ -1,6 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; +use File::Slurper qw(read_binary write_binary); use Cwd qw(abs_path); use File::Path qw(make_path); use File::Temp qw(tempdir); @@ -18,22 +19,13 @@ my $tmp = tempdir(CLEANUP => 1); my $bin = "$tmp/bin"; make_path($bin); -sub write_file { - my ($path, $text) = @_; - open(my $fh, '>', $path) or die "$path: $!"; - print {$fh} $text; - close($fh) or die "$path: $!"; -} - sub read_file { my ($path) = @_; return '' unless -f $path; - open(my $fh, '<', $path) or die "$path: $!"; - local $/; - return <$fh> // ''; + return scalar read_binary($path); } -write_file("$bin/double", <<'SH'); +write_binary("$bin/double", <<'SH'); #!/bin/bash command=${0##*/} printf '%s' "$command" >> "$XCAT_NET_LOG" @@ -84,7 +76,7 @@ for my $command (qw(ip nmcli systemctl logger route ifup ifdown modprobe sleep)) symlink 'double', "$bin/$command" or die $!; } -write_file("$tmp/namespace", <<'SH'); +write_binary("$tmp/namespace", <<'SH'); #!/bin/bash set -e mount --make-rprivate / @@ -95,7 +87,7 @@ exec "$@" SH chmod 0755, "$tmp/namespace"; -write_file("$tmp/local-route", <<'PERL'); +write_binary("$tmp/local-route", <<'PERL'); use strict; use warnings; use JSON::PP; @@ -117,10 +109,10 @@ sub run_case { make_path(map { "$fixture/$_" } qw(etc/sysconfig/network-scripts etc/sysconfig/network etc/network/interfaces.d etc/modprobe.d etc/NetworkManager/system-connections etc/systemd/system run install/postscripts db)); - write_file("$fixture/etc/os-release", $case{release} // "ID=openEuler\nVERSION_ID=24.03\nNAME=openEuler\n"); - write_file("$fixture/etc/network/interfaces", "auto eth8\niface eth8 inet static\n"); - write_file("$fixture/etc/systemd/system/NetworkManager.service", ''); - write_file("$fixture/etc/$case{marker}", $case{marker_text} // '') if $case{marker}; + write_binary("$fixture/etc/os-release", $case{release} // "ID=openEuler\nVERSION_ID=24.03\nNAME=openEuler\n"); + write_binary("$fixture/etc/network/interfaces", "auto eth8\niface eth8 inet static\n"); + write_binary("$fixture/etc/systemd/system/NetworkManager.service", ''); + write_binary("$fixture/etc/$case{marker}", $case{marker_text} // '') if $case{marker}; for my $file (qw(routeop xcatlib.sh)) { symlink "$source/xCAT/postscripts/$file", "$fixture/install/postscripts/$file" or die $!; } diff --git a/xCAT-test/native/openeuler_xcatroot.t b/xCAT-test/native/openeuler_xcatroot.t index 478365134a..6c107116e4 100644 --- a/xCAT-test/native/openeuler_xcatroot.t +++ b/xCAT-test/native/openeuler_xcatroot.t @@ -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); @@ -14,7 +15,7 @@ plan skip_all => 'requires Linux root and private mount namespaces' my $netboot = File::Spec->rel2abs("$FindBin::Bin/../../xCAT-server/share/xcat/netboot/rh"); my $dir = tempdir(CLEANUP => 1); my $wrapper = "$dir/run"; -write_file($wrapper, <<'SH'); +write_binary($wrapper, <<'SH'); #!/bin/bash set -e root=$1 @@ -48,16 +49,16 @@ for my $variant (qw(dracut_047 dracut_105/stateless dracut_105/statelite)) { symlink('usr/sbin', "$root/sbin") or die $!; symlink('usr/lib64', "$root/lib64") or die $!; make_path("$payload/etc/init.d", "$payload/etc/sysconfig/network-scripts", "$payload/var/lib/dhclient"); - write_file("$payload/native-marker", "native payload\n"); - write_file("$payload/etc/os-release", "ID=openEuler\nVERSION_ID=24.03\n"); + write_binary("$payload/native-marker", "native payload\n"); + write_binary("$payload/etc/os-release", "ID=openEuler\nVERSION_ID=24.03\n"); if ($localdisk ne 'missing') { - write_file("$payload/etc/init.d/localdisk", "#!/bin/sh\ntouch /sysroot/localdisk-ran\n"); + write_binary("$payload/etc/init.d/localdisk", "#!/bin/sh\ntouch /sysroot/localdisk-ran\n"); chmod($localdisk eq 'executable' ? 0755 : 0644, "$payload/etc/init.d/localdisk"); } - write_file("$root/etc/resolv.conf", "nameserver 192.0.2.1\n"); - write_file("$root/tmp/dhclient.eth0.lease", "fixture lease\n"); - write_file("$root/var/lib/NetworkManager/$_-0f3e5a12-eth0.lease", "$_ lease\n") for qw(dhclient internal); - write_file("$root/lib/dracut-lib.sh", <<'SH'); + write_binary("$root/etc/resolv.conf", "nameserver 192.0.2.1\n"); + write_binary("$root/tmp/dhclient.eth0.lease", "fixture lease\n"); + write_binary("$root/var/lib/NetworkManager/$_-0f3e5a12-eth0.lease", "$_ lease\n") for qw(dhclient internal); + write_binary("$root/lib/dracut-lib.sh", <<'SH'); hookdir=/hooks logger() { :; } getarg() @@ -89,9 +90,7 @@ SH } waitpid($pid, 0); my $rc = (($? & 127) ? 128 + ($? & 127) : $? >> 8); - open(my $file, '<', "$case/output") or die $!; - my $output = do { local $/; <$file> }; - close($file); + my $output = read_binary("$case/output"); my $label = "$variant $method $localdisk localdisk"; is($rc, 0, "$label completes the production xcatroot script") or diag($output); like($output, qr/^PAYLOAD=native payload$/m, "$label extracts the actual image payload"); @@ -110,10 +109,3 @@ SH } } done_testing(); - -sub write_file { - my ($path, $contents) = @_; - open(my $file, '>', $path) or die "write $path: $!"; - print {$file} $contents or die $!; - close($file) or die $!; -} diff --git a/xCAT-test/native/remoteshell_hostkeys_openeuler.t b/xCAT-test/native/remoteshell_hostkeys_openeuler.t index c149e730cd..688db71365 100644 --- a/xCAT-test/native/remoteshell_hostkeys_openeuler.t +++ b/xCAT-test/native/remoteshell_hostkeys_openeuler.t @@ -1,6 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; +use File::Slurper qw(read_binary write_binary); use Cwd qw(abs_path); use Digest::SHA qw(sha256_hex); use File::Copy qw(copy); @@ -17,18 +18,10 @@ my $tmp = tempdir(DIR => '/var/tmp', CLEANUP => 1); chmod 0700, $tmp; make_path("$tmp/keys", "$tmp/bin"); -sub write_file { - my ($path, $contents) = @_; - open(my $fh, '>', $path) or die "$path: $!"; - print {$fh} $contents; - close($fh) or die "$path: $!"; -} sub read_file { my ($path) = @_; return '' unless -f $path; - open(my $fh, '<', $path) or die "$path: $!"; - local $/; - return <$fh> // ''; + return scalar read_binary($path); } sub public_identity { my ($path) = @_; @@ -46,7 +39,7 @@ for my $name (qw(remoteshell xcatlib.sh remoteshell-sshd-config)) { copy("$source/xCAT/postscripts/$name", "$tmp/bin/$name") or die $!; chmod 0755, "$tmp/bin/$name"; } -write_file("$tmp/bin/namespace", <<'SH'); +write_binary("$tmp/bin/namespace", <<'SH'); #!/bin/bash set -e mount --make-rprivate / @@ -55,7 +48,7 @@ mount --bind "$XCAT_KEY_FIXTURE/root" /root mount --bind "$XCAT_KEY_FIXTURE/tmp" /tmp exec "$@" SH -write_file("$tmp/bin/getcredentials.awk", <<'SH'); +write_binary("$tmp/bin/getcredentials.awk", <<'SH'); #!/bin/bash case "$1" in ssh_dsa_hostkey) cat "$XCAT_KEY_INPUT/dsa" ;; @@ -66,12 +59,12 @@ case "$1" in *) printf 'unexpected credential request\n'; exit 1 ;; esac SH -write_file("$tmp/bin/allowcred.awk", "#!/bin/sh\nexec /bin/sleep 60\n"); -write_file("$tmp/bin/logger", "#!/bin/sh\nprintf '%s\\n' \"\$*\" >> \"\$XCAT_KEY_FIXTURE/logger.log\"\n"); -write_file("$tmp/bin/systemctl", "#!/bin/sh\nprintf '%s\\n' \"\$*\" >> \"\$XCAT_KEY_FIXTURE/services.log\"\nexit 0\n"); -write_file("$tmp/bin/sleep", "#!/bin/sh\nexit 0\n"); +write_binary("$tmp/bin/allowcred.awk", "#!/bin/sh\nexec /bin/sleep 60\n"); +write_binary("$tmp/bin/logger", "#!/bin/sh\nprintf '%s\\n' \"\$*\" >> \"\$XCAT_KEY_FIXTURE/logger.log\"\n"); +write_binary("$tmp/bin/systemctl", "#!/bin/sh\nprintf '%s\\n' \"\$*\" >> \"\$XCAT_KEY_FIXTURE/services.log\"\nexit 0\n"); +write_binary("$tmp/bin/sleep", "#!/bin/sh\nexit 0\n"); for my $command (qw(chown chmod)) { - write_file("$tmp/bin/$command", "#!/bin/bash\n" . + write_binary("$tmp/bin/$command", "#!/bin/bash\n" . 'if [ "$XCAT_KEY_FAIL_COMMAND" = "${0##*/}" ] && [[ "${!#}" = "/etc/ssh/ssh_host_${XCAT_KEY_FAIL_TYPE}_key" ]]; then exit 42; fi' . "\n" . 'exec /usr/bin/' . $command . ' "$@"' . "\n"); } @@ -92,12 +85,12 @@ for my $case ( my ($label, $osver, $id, $group, $existing, $fail_command, $fail_type) = @$case; my $fixture = tempdir(DIR => $tmp, CLEANUP => 1); make_path(map { "$fixture/$_" } qw(etc/ssh root tmp)); - write_file("$fixture/etc/passwd", "root:x:0:0:root:/root:/bin/bash\n"); - write_file("$fixture/etc/group", "root:x:0:\n" . ($group ? "ssh_keys:x:4242:\n" : '')); - write_file("$fixture/etc/nsswitch.conf", "passwd: files\ngroup: files\n"); - write_file("$fixture/etc/os-release", "ID=$id\n"); - write_file("$fixture/etc/ssh/sshd_config", "Port 22\n"); - write_file("$fixture/etc/ssh/ssh_config", "Host *\n"); + write_binary("$fixture/etc/passwd", "root:x:0:0:root:/root:/bin/bash\n"); + write_binary("$fixture/etc/group", "root:x:0:\n" . ($group ? "ssh_keys:x:4242:\n" : '')); + write_binary("$fixture/etc/nsswitch.conf", "passwd: files\ngroup: files\n"); + write_binary("$fixture/etc/os-release", "ID=$id\n"); + write_binary("$fixture/etc/ssh/sshd_config", "Port 22\n"); + write_binary("$fixture/etc/ssh/ssh_config", "Host *\n"); for my $type (@types) { next unless $existing; my $key = "$fixture/etc/ssh/ssh_host_${type}_key";