diff --git a/xCAT-test/lib/XCAT/Test/Sandbox.pm b/xCAT-test/lib/XCAT/Test/Sandbox.pm index 9bcc0734e2..fb26382aa4 100644 --- a/xCAT-test/lib/XCAT/Test/Sandbox.pm +++ b/xCAT-test/lib/XCAT/Test/Sandbox.pm @@ -7,7 +7,7 @@ use File::Path qw(make_path); use File::Temp qw(tempdir); use XCAT::Test::Process qw(run_command); -our @EXPORT_OK = qw(sandbox_root sandbox_run); +our @EXPORT_OK = qw(sandbox_root sandbox_command sandbox_run); sub sandbox_root { my $root = tempdir(CLEANUP => 1); @@ -15,9 +15,10 @@ sub sandbox_root { return $root; } -sub sandbox_run { - my ($root, @command) = @_; - my $options = ref($command[0]) eq 'HASH' ? shift @command : {}; +# Return arguments so raw-status callers can keep their own capture policy. +sub sandbox_command { + my ($root, $options) = @_; + $options ||= {}; my $bwrap = -x '/usr/bin/bwrap' ? '/usr/bin/bwrap' : '/bin/bwrap'; die 'Install bubblewrap to run this test' unless -x $bwrap; my @sandbox = ($bwrap, '--unshare-all', '--die-with-parent', '--new-session', @@ -25,12 +26,17 @@ sub sandbox_run { '--setenv', 'PATH', '/fixture/bin:/usr/bin:/bin', '--setenv', 'LC_ALL', 'C', '--bind', $root, '/fixture', '--chdir', '/fixture'); for my $directory (qw(/usr /bin /sbin /lib /lib64)) { - push @sandbox, '--ro-bind', $directory, $directory if -d $directory; + if ($options->{preserve_links} && -l $directory) { + push @sandbox, '--symlink', readlink($directory), $directory; + } elsif (-d $directory) { + push @sandbox, '--ro-bind', $directory, $directory; + } } - for my $mount (qw(boot etc target tmp)) { + for my $mount (@{$options->{directories} // [qw(boot etc target tmp)]}) { push @sandbox, '--bind', "$root/$mount", "/$mount"; } push @sandbox, '--bind', "$root/log", '/var/log'; + push @sandbox, map { ('--tmpfs', $_) } @{$options->{tmpfs} // []}; push @sandbox, '--ro-bind', '/etc/alternatives', '/etc/alternatives' if -d '/etc/alternatives'; my @mounts; for my $kind (qw(read_only writable)) { @@ -41,9 +47,16 @@ sub sandbox_run { for my $mount (sort { length($a->[2]) <=> length($b->[2]) || $a->[2] cmp $b->[2] } @mounts) { push @sandbox, @$mount; } - for my $name (keys %{$options->{env} // {}}) { + for my $name (sort keys %{$options->{env} // {}}) { push @sandbox, '--setenv', $name, $options->{env}{$name}; } + return @sandbox; +} + +sub sandbox_run { + my ($root, @command) = @_; + my $options = ref($command[0]) eq 'HASH' ? shift @command : {}; + my @sandbox = sandbox_command($root, $options); local %ENV; return run_command(@sandbox, '/usr/bin/timeout', '--kill-after=5', '45', @command); } diff --git a/xCAT-test/native/postinstall_bootstrap.t b/xCAT-test/native/postinstall_bootstrap.t index 28f9a10f60..0027a88579 100644 --- a/xCAT-test/native/postinstall_bootstrap.t +++ b/xCAT-test/native/postinstall_bootstrap.t @@ -5,11 +5,11 @@ use Capture::Tiny qw(capture); use File::Copy qw(copy); use File::Path qw(make_path); use File::Slurper qw(read_binary write_binary); -use File::Temp qw(tempdir); use FindBin; use lib "$FindBin::Bin/../lib"; use Test::More; use XCAT::Test::File qw(repo_path); +use XCAT::Test::Sandbox qw(sandbox_root sandbox_command); plan skip_all => 'installer execution requires Linux' unless $^O eq 'linux'; die 'Install bubblewrap to run this test' unless -x '/usr/bin/bwrap'; @@ -22,7 +22,7 @@ sub executable { } sub fixture { - my $root = tempdir(CLEANUP => 1); + my $root = sandbox_root(); make_path(map { "$root/$_" } qw(bin db opt/xcat etc/systemd/system/multi-user.target.wants etc/init.d etc/rc2.d etc/rc.d/rc3.d etc/rc.d/rc4.d etc/rc.d/rc5.d xcatpost log/xcat boot)); @@ -66,26 +66,14 @@ SH sub isolated { my ($root, $stage, $environment, @command) = @_; - my @args = ('/usr/bin/bwrap', '--unshare-all', '--die-with-parent', '--new-session', - '--tmpfs', '/', '--proc', '/proc', '--dev', '/dev', '--tmpfs', '/tmp', - '--tmpfs', '/run', '--bind', $root, '/fixture', '--chdir', '/fixture', - '--setenv', 'PATH', '/fixture/bin:/usr/bin:/bin:/usr/sbin:/sbin', - '--setenv', 'LC_ALL', 'C'); - if ($stage eq 'render') { - push @args, '--ro-bind', repo_path('xCAT/postscripts'), '/install/postscripts', - '--ro-bind', repo_path('.'), repo_path('.'); - } - for my $directory (qw(/usr /bin /sbin /lib /lib64)) { - if (-l $directory) { - push @args, '--symlink', readlink($directory), $directory; - } elsif (-d $directory) { - push @args, '--ro-bind', $directory, $directory; - } - } - push @args, '--bind', "$root/$_", "/$_" for qw(opt etc xcatpost boot); - push @args, '--bind', "$root/log", '/var/log'; - push @args, '--ro-bind', '/etc/alternatives', '/etc/alternatives' if -d '/etc/alternatives'; - push @args, '--setenv', $_, $environment->{$_} for sort keys %$environment; + my %read_only = $stage eq 'render' + ? (repo_path('xCAT/postscripts') => '/install/postscripts', + repo_path('.') => repo_path('.')) : (); + my @args = sandbox_command($root, { + preserve_links => 1, directories => [qw(opt etc xcatpost boot)], + tmpfs => ['/tmp'], read_only => \%read_only, + env => {PATH => '/fixture/bin:/usr/bin:/bin:/usr/sbin:/sbin', %$environment}, + }); local %ENV = (PATH => '/usr/bin:/bin', LC_ALL => 'C'); my ($out, $err, $status) = capture { system(@args, @command) }; return ($status, $out . $err); @@ -110,6 +98,24 @@ my @cases = ( {name => 'missing postboot file', no_post => 1, disable => 1}, ); +{ + my $root = fixture(); + for my $stage (qw(render client)) { + my ($status, $output) = isolated($root, $stage, {}, '/bin/sh', '-c', + 'test ! -e /etc/os-release && test ! -e /tmp/fixture-marker && ' + . 'printf private > /tmp/fixture-marker && ' + . 'if test -r /install/postscripts/xcatlib.sh; then printf visible; else printf hidden; fi'); + is($status, 0, "$stage has private configuration and temporary files"); + is($output, $stage eq 'render' ? 'visible' : 'hidden', "$stage retains its postscripts visibility"); + ($status, $output) = isolated($root, $stage, {}, '/bin/sh', '-c', + 'test -r "$1/Version"', 'visibility', repo_path('.')); + is($status, $stage eq 'render' ? 0 : 1 << 8, "$stage retains its repository visibility"); + } + my ($status, $output) = isolated($root, 'client', {}, '/bin/sh', '-c', + 'printf error >&2; printf output; exit 7'); + is_deeply([$status, $output], [7 << 8, 'outputerror'], 'retains raw status and stdout-before-stderr capture'); +} + for my $owner (qw(post.xcat post.xcat.ng post.xcat.rhels10)) { my $build = fixture(); my ($status, $output) = isolated($build, 'render', {}, $^X,