mirror of
https://github.com/xcat2/xcat-core.git
synced 2026-12-04 08:07:04 +00:00
test(images): share namespace construction
Reuse fixture allocation and file access through ImageSandbox. Keep the prerequisite checks, XCATROOT, process status and output capture contracts of each adapter. Exercise those contracts in private namespaces.
This commit is contained in:
@@ -35,19 +35,25 @@ sub read {
|
||||
return read_binary("$self->{root}/$path");
|
||||
}
|
||||
|
||||
sub run {
|
||||
my ($self, @command) = @_;
|
||||
sub sandbox_command {
|
||||
my ($self, $xcatroot) = @_;
|
||||
my @sandbox = ('bwrap', '--unshare-all', '--die-with-parent', '--new-session', '--tmpfs', '/',
|
||||
(map { ('--ro-bind', $_, $_) } grep { -d $_ } qw(/usr /bin /sbin /lib /lib64)),
|
||||
'--proc', '/proc', '--dev', '/dev', '--dir', '/run', '--dir', '/sys', '--dir', '/opt',
|
||||
(map { ('--bind', "$self->{root}/$_", "/$_") } qw(etc install tmp work)),
|
||||
'--ro-bind', repo_path('.'), '/repo', '--chdir', '/work',
|
||||
'--setenv', 'PATH', '/work/bin:/usr/bin:/bin:/usr/sbin:/sbin',
|
||||
'--setenv', 'LC_ALL', 'C', '--setenv', 'XCATROOT', '/repo/xCAT-server',
|
||||
'--setenv', 'LC_ALL', 'C', '--setenv', 'XCATROOT', $xcatroot,
|
||||
'--setenv', 'XCATCFG', 'SQLite:/work/db',
|
||||
'--setenv', 'PERL5LIB', '/repo/perl-xCAT:/repo/xCAT-server/lib/perl');
|
||||
push @sandbox, '--ro-bind', '/etc/alternatives', '/etc/alternatives' if -d '/etc/alternatives';
|
||||
push @sandbox, @{ $self->{mounts} || [] };
|
||||
return @sandbox;
|
||||
}
|
||||
|
||||
sub run {
|
||||
my ($self, @command) = @_;
|
||||
my @sandbox = $self->sandbox_command('/repo/xCAT-server');
|
||||
local %ENV = (PATH => '/usr/bin:/bin:/usr/sbin:/sbin');
|
||||
return XCAT::Test::Lifecycle::command(@sandbox, 'timeout', '45', @command);
|
||||
}
|
||||
|
||||
@@ -4,12 +4,9 @@ use strict;
|
||||
use warnings;
|
||||
|
||||
use Capture::Tiny qw(capture_merged);
|
||||
use File::Basename qw(dirname);
|
||||
use File::Path qw(make_path);
|
||||
use File::Slurper qw(read_binary write_binary);
|
||||
use File::Temp qw(tempdir);
|
||||
use JSON::PP qw(encode_json);
|
||||
use XCAT::Test::File qw(repo_path);
|
||||
use parent 'XCAT::Test::ImageSandbox';
|
||||
|
||||
sub new {
|
||||
my ($class) = @_;
|
||||
@@ -17,43 +14,12 @@ sub new {
|
||||
die "Ubuntu image tests require $tool\n"
|
||||
unless grep { -x "$_/$tool" } qw(/usr/bin /bin /usr/sbin /sbin);
|
||||
}
|
||||
my $self = bless { root => tempdir(CLEANUP => 1) }, $class;
|
||||
make_path(map { "$self->{root}/$_" } qw(etc install tmp work/bin work/db));
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub write {
|
||||
my ($self, $file, $content) = @_;
|
||||
my $path = "$self->{root}/$file";
|
||||
make_path(dirname($path));
|
||||
write_binary($path, $content);
|
||||
return $path;
|
||||
}
|
||||
|
||||
sub read {
|
||||
my ($self, $file) = @_;
|
||||
return read_binary("$self->{root}/$file");
|
||||
return $class->SUPER::new();
|
||||
}
|
||||
|
||||
sub run {
|
||||
my ($self, @command) = @_;
|
||||
my $root = $self->{root};
|
||||
my @sandbox = (
|
||||
'bwrap', '--unshare-all', '--die-with-parent', '--new-session',
|
||||
'--tmpfs', '/',
|
||||
(map { ('--ro-bind', $_, $_) } grep { -d $_ } qw(/usr /bin /sbin /lib /lib64)),
|
||||
'--dev', '/dev', '--proc', '/proc', '--dir', '/run', '--dir', '/sys',
|
||||
'--dir', '/opt',
|
||||
'--bind', "$root/etc", '/etc', '--bind', "$root/install", '/install',
|
||||
'--bind', "$root/tmp", '/tmp', '--bind', "$root/work", '/work',
|
||||
'--ro-bind', repo_path('.'), '/repo', '--chdir', '/work',
|
||||
'--setenv', 'PATH', '/work/bin:/usr/bin:/bin:/usr/sbin:/sbin',
|
||||
'--setenv', 'LC_ALL', 'C', '--setenv', 'XCATROOT', '/repo',
|
||||
'--setenv', 'XCATCFG', 'SQLite:/work/db',
|
||||
'--setenv', 'PERL5LIB', '/repo/perl-xCAT:/repo/xCAT-server/lib/perl',
|
||||
);
|
||||
push @sandbox, '--ro-bind', '/etc/alternatives', '/etc/alternatives'
|
||||
if -d '/etc/alternatives';
|
||||
my @sandbox = $self->sandbox_command('/repo');
|
||||
my ($output, $status) = capture_merged {
|
||||
local %ENV = (PATH => '/usr/bin:/bin:/usr/sbin:/sbin');
|
||||
system(@sandbox, 'timeout', '45', @command);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
use FindBin;
|
||||
use lib "$FindBin::Bin/../lib";
|
||||
use Test::More;
|
||||
use XCAT::Test::ImageSandbox;
|
||||
use XCAT::Test::UbuntuImage;
|
||||
|
||||
plan skip_all => 'image namespaces require Linux' unless $^O eq 'linux';
|
||||
|
||||
for my $class ('XCAT::Test::ImageSandbox', 'XCAT::Test::UbuntuImage') {
|
||||
subtest $class => sub {
|
||||
my $image = $class->new();
|
||||
for my $bytes ('', '0', "\0\xff\r\n") {
|
||||
$image->write('work/nested/bytes', $bytes);
|
||||
is($image->read('work/nested/bytes'), $bytes, 'fixture I/O preserves bytes');
|
||||
}
|
||||
my $read = eval { $image->read('work/missing'); 1 };
|
||||
ok(!$read && $@, 'a missing required file fails');
|
||||
my @result = $image->run('/bin/sh', '-c',
|
||||
'printf "%s" "$XCATROOT"; printf error >&2; exit 7');
|
||||
if ($class eq 'XCAT::Test::UbuntuImage') {
|
||||
is_deeply(\@result, [7 << 8, '/repoerror'], 'retains raw status and merged output');
|
||||
} else {
|
||||
is_deeply(\@result, [7, '/repo/xCAT-server', 'error'], 'retains decoded status and separate streams');
|
||||
}
|
||||
@result = $image->run('/bin/sh', '-c',
|
||||
'test ! -e /etc/os-release && test -r /repo/Version && '
|
||||
. '! test -w /repo && printf private > /etc/fixture-marker');
|
||||
is($result[0], 0, 'host configuration is hidden and the repository is read-only');
|
||||
is($image->read('etc/fixture-marker'), 'private', 'writes reach only the fixture');
|
||||
};
|
||||
}
|
||||
|
||||
my $image = XCAT::Test::ImageSandbox->new();
|
||||
$image->write('work/mounted', 'extra mount');
|
||||
$image->{mounts} = ['--ro-bind', "$image->{root}/work/mounted", '/opt/input'];
|
||||
my ($status, $output) = $image->run('/bin/cat', '/opt/input');
|
||||
is($status, 0, 'the image adapter retains extra mounts');
|
||||
is($output, 'extra mount', 'the command reads the supplied mount');
|
||||
done_testing();
|
||||
Reference in New Issue
Block a user