2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-26 20:32:17 +00:00
Files
xcat-core/xCAT-test/native/ubuntu_subiquity_install_disk.t
T
Vinícius Ferrão 75c874ebad test(ubuntu): cover a partitionfile layout on the partitionfile disk
A layout without a match that follows a disk file gets a match on the
disk the file selects, resolved when it is a link, and a use_gap layout
that follows a disk file stops the install. A layout with its own match
and a layout without a disk entry are kept as written.
2026-11-03 17:53:20 -03:00

138 lines
7.7 KiB
Perl

#!/usr/bin/env perl
# The storage config a Subiquity install receives for each linuximage.partitionfile form.
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use lib "$FindBin::Bin/../../perl-xCAT", "$FindBin::Bin/../../xCAT-server/lib/perl";
use File::Path qw(make_path);
use File::Slurper qw(read_text write_text);
use Test::More;
use XCAT::Test::File qw(repo_path);
use XCAT::Test::Sandbox qw(sandbox_root sandbox_run);
use XCAT::Test::Template qw(template_database render_template);
plan skip_all => 'installer execution requires Linux namespaces' unless $^O eq 'linux';
my $database = template_database();
# Subiquity runs its early commands under dash.
my ($shell) = grep { -x $_ } qw(/usr/bin/dash /bin/dash /bin/sh);
my $sequence = 0;
sub render_part {
my ($script, $pattern, @entries) = @_;
my $dir = "$database/case-" . ++$sequence;
make_path($dir);
my @partitionfile;
for my $n (0 .. $#entries) {
my ($prefix, $contents) = @{ $entries[$n] };
write_text("$dir/partition-$n", $contents);
push @partitionfile, "$prefix$dir/partition-$n";
}
render_template($database, repo_path("xCAT-server/share/xcat/install/scripts/$script"), "$dir/node.pre",
'node', '', '', '', join(',', @partitionfile), {xcatmaster => '192.0.2.1'});
my ($part) = read_text("$dir/node.pre") =~ $pattern;
return $part;
}
# getinstdisk has already chosen /dev/sdz.
sub installer_root {
my $root = sandbox_root();
make_path("$root/efi", "$root/by-id");
write_text("$root/bin/logger", "#!/bin/sh\nexit 0\n");
chmod 0755, "$root/bin/logger" or die $!;
write_text("$root/tmp/xcat.install_disk", "/dev/sdz\n");
write_text("$root/nvme7n1", '');
symlink('../../nvme7n1', "$root/by-id/nvme-link") or die $!;
return $root;
}
sub storage_config {
my ($root) = @_;
return -e "$root/tmp/partitionfile" ? read_text("$root/tmp/partitionfile") : '';
}
# Written as PyYAML writes it, so it reads the same before and after the pre-script rewrites it.
my $layout = "storage:\n version: 1\n config:\n - id: custom-disk\n type: disk\n path: /dev/disk/by-id/custom\n";
my $writer = "#!/bin/sh\ncat >/tmp/partitionfile <<'EOF'\n${layout}EOF\n";
my $diskscript = "#!/bin/sh\necho /dev/disk/by-id/scripted >/tmp/xcat.install_disk\n";
# Each case: label, the partitionfile entries as [prefix, file contents], and the disk the storage
# config must name, or undef when the install must stop before a storage config exists. A fourth
# value is the whole storage config of a layout.
my @cases = (
['no partitionfile', [], '/dev/sdz'],
['a disk file', [['d:', "/dev/disk/by-id/nvme-pinned /dev/sdb\n"]], '/dev/disk/by-id/nvme-pinned'],
['a disk file naming a link', [['d:', "/dev/disk/by-id/nvme-link\n"]], '/dev/nvme7n1'],
['a disk script', [['s:d:', $diskscript]], '/dev/disk/by-id/scripted'],
['a layout file', [['', $layout]], '/dev/disk/by-id/custom'],
['a layout script', [['s:', $writer]], '/dev/disk/by-id/custom'],
['a layout file after a layout script', [['s:', "#!/bin/sh\nexit 0\n"], ['', $layout]], '/dev/disk/by-id/custom'],
['a disk file after a disk script', [['s:d:', "#!/bin/sh\nexit 0\n"], ['d:', "/dev/disk/by-id/nvme-pinned\n"]],
'/dev/disk/by-id/nvme-pinned'],
['a layout file before a disk script', [['', $layout], ['s:d:', $diskscript]], '/dev/disk/by-id/custom'],
['a layout script before a disk file', [['s:', $writer], ['d:', "/dev/disk/by-id/nvme-pinned\n"]], '/dev/disk/by-id/custom'],
['a disk script that fails', [['s:d:', "#!/bin/sh\nexit 42\n"]], undef],
['a disk script that selects no disk', [['s:d:', "#!/bin/sh\nexit 0\n"]], undef],
['a layout script that fails', [['s:', "#!/bin/sh\nexit 42\n"]], undef],
['a layout script that writes no layout', [['s:', "#!/bin/sh\nexit 0\n"]], undef],
['a layout file with only blank lines', [['', "\n \n"]], undef],
['a layout file with only comments', [['', "# no layout yet\n"]], undef],
['a disk file with only blank lines', [['d:', " \n\n"]], undef],
['a layout file with an empty storage section', [['', "storage:\n"]], undef],
['a layout file with a null storage section', [['', "storage: null\n"]], undef],
['a layout file that starts a YAML document', [['', "---\n$layout"]], '/dev/disk/by-id/custom'],
['a layout without a match after a disk file', [['', "storage:\n layout:\n name: direct\n"],
['d:', "/dev/disk/by-id/nvme-link\n"]], undef,
"storage:\n layout:\n name: direct\n match:\n path: /dev/nvme7n1\n"],
['a layout with its own match after a disk file', [['', "storage:\n layout:\n name: direct\n match:\n ssd: true\n"],
['d:', "/dev/disk/by-id/nvme-link\n"]], undef,
"storage:\n layout:\n name: direct\n match:\n ssd: true\n"],
['a layout without a match and no disk entry', [['', "storage:\n layout:\n name: direct\n"]], undef,
"storage:\n layout:\n name: direct\n"],
['a use_gap layout after a disk file', [['', "storage:\n layout:\n name: direct\n mode: use_gap\n"],
['d:', "/dev/disk/by-id/nvme-link\n"]], undef],
);
for my $case (@cases) {
my ($label, $entries, $expected, $expected_layout) = @$case;
subtest $label => sub {
my $part = render_part('pre.ubuntu.subiquity', qr/(^base64decode\(\)\n.*^exit 0$)/ms, @$entries);
ok($part, 'the rendered script defines base64decode before the partitioning') or return;
my $root = installer_root();
write_text("$root/part.sh", $part);
my ($rc, $output) = sandbox_run($root, {read_only => {"$root/efi" => '/sys/firmware/efi',
"$root/by-id" => '/dev/disk/by-id', "$root/nvme7n1" => '/dev/nvme7n1'}}, $shell, '/fixture/part.sh');
if (defined $expected_layout) {
is($rc, 0, "the partitioning runs under $shell") or diag($output);
is(storage_config($root), $expected_layout, 'the storage config is the expected layout');
return;
}
if (!defined $expected) {
isnt($rc, 0, "the pre-install script fails under $shell");
ok(!-e "$root/tmp/partitionfile", 'and leaves no storage config to inject');
return;
}
is($rc, 0, "the partitioning runs under $shell") or diag($output);
my $config = storage_config($root);
my ($disk) = $config =~ /^\s+type: disk\n(?:\s+\S+:.*\n)*?\s+path: (\S+)$/m;
is($disk, $expected, "the storage config partitions $expected");
is($config, $layout, 'the administrator layout replaces the default one') if $expected eq '/dev/disk/by-id/custom';
# The early command appends the storage config to Subiquity's autoinstall.yaml.
write_text("$root/autoinstall.yaml", "version: 1\nidentity:\n hostname: node\n$config");
is(system('/usr/bin/python3', '-I', '-c', 'import sys, yaml; sys.exit("storage" not in yaml.safe_load(open(sys.argv[1])))',
"$root/autoinstall.yaml"), 0, 'autoinstall.yaml stays one YAML document with a storage section');
};
}
# The debian-installer path renders the same entries into pre.ubuntu, under a shell with read -n.
subtest 'a debian-installer recipe file after a recipe script' => sub {
my $part = render_part('pre.ubuntu', qr/(^base64decode\(\)\n.*?^exit 0$)/ms,
['s:', "#!/bin/sh\nexit 0\n"], ['', "recipe ::\n .\n"]);
ok($part, 'the rendered script defines base64decode before the recipe') or return;
my $root = installer_root();
write_text("$root/part.sh", $part);
my ($rc, $output) = sandbox_run($root, '/bin/bash', '/fixture/part.sh');
is($rc, 0, 'the recipe step runs') or diag($output);
is(storage_config($root), "recipe ::\n .\n", 'the later recipe file replaces the default recipe');
};
done_testing();