2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-20 09:22:17 +00:00

test(kvm): cover the UUID that mkvm, rinv and rscan carry

The plugin runs through process_request with a private SQLite database
and a stub libvirt connection. mkvm gives the domain XML vpd.uuid, and
a UUID that it generates, unchanged; rinv prints the domain UUID; and
rscan -w stores it in vpd.uuid. For a guest on machine type
pc-i440fx-2.1 or pc-i440fx-rhel7.1.0 they swap the first three fields
instead, and guest_uuid swaps only for those two machine types. Against
the old plugin the unchanged cases fail with the fields swapped, and
against a plugin that ignores the machine type the swapped cases fail
unswapped.

(cherry picked from commit 8db182e8c6fdf184ceeccc87ff5e5569070cc26a)
This commit is contained in:
Vinícius Ferrão
2026-10-24 03:53:19 -03:00
committed by Daniel Hilst
parent 76f2008779
commit dbf2ffb076
+219
View File
@@ -0,0 +1,219 @@
#!/usr/bin/env perl
# vpd.uuid holds the system UUID that the guest reports. QEMU stores the libvirt domain UUID as
# SMBIOS 2.6 specifies, so the guest reports the domain UUID unchanged, and the KVM plugin must
# carry vpd.uuid to the domain and back without changing its byte order: mkvm writes it to the
# domain XML, rscan imports the domain UUID into vpd.uuid, and rinv prints it. Machine types
# pc-i440fx-2.1 and pc-i440fx-rhel7.1.0 are the exception: their guest reports the domain UUID with
# the first three fields swapped, so the plugin swaps them for those machine types only.
#
# The plugin runs through process_request, with a private SQLite database and a hypervisor
# connection that serves fixed domain XML in place of libvirt.
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../../perl-xCAT";
use lib "$FindBin::Bin/../../xCAT-server/lib/perl";
use File::Temp qw(tempdir);
use Test::More;
use XML::LibXML;
my $uuid = '12345678-1234-4bcd-9876-1234567890ab';
# A connection to a hypervisor that holds the domains in @XCATTestHypervisor::domains.
BEGIN {
package Sys::Virt;
our $VERSION = '10.0.0';
sub new { return bless {}, 'XCATTestHypervisor'; }
package Sys::Virt::Domain;
use constant {
STATE_NOSTATE => 0,
STATE_RUNNING => 1,
STATE_BLOCKED => 2,
STATE_PAUSED => 3,
STATE_SHUTOFF => 5,
};
$INC{'Sys/Virt.pm'} = __FILE__;
$INC{'Sys/Virt/Domain.pm'} = __FILE__;
# Only rpower reports node status to monitoring.
package xCAT_monitoring::monitorctrl;
$INC{'xCAT_monitoring/monitorctrl.pm'} = __FILE__;
}
{
package XCATTestHypervisor;
our @domains;
sub get_node_info { return { model => 'x86_64' }; }
sub list_all_domains { return map { bless { xml => $_ }, 'XCATTestDomain' } @domains; }
sub get_domain_by_name {
my ( undef, $name ) = @_;
foreach my $xml (@domains) {
return bless { xml => $xml }, 'XCATTestDomain' if $xml =~ m{<name>\Q$name\E</name>};
}
die "no domain $name\n";
}
package XCATTestDomain;
sub get_xml_description { return $_[0]->{xml}; }
sub get_name { return ( $_[0]->{xml} =~ m{<name>([^<]+)</name>} )[0]; }
sub get_info { return { state => Sys::Virt::Domain::STATE_RUNNING() }; }
sub get_block_info { return { capacity => 1073741824, allocation => 0 }; }
}
my $db = tempdir( CLEANUP => 1 );
$ENV{XCATCFG} = "SQLite:$db";
$ENV{XCATROOT} = "$FindBin::Bin/../../xCAT-server";
require xCAT::Table;
sub set_rows {
my ( $table, %rows ) = @_;
my $tab = xCAT::Table->new( $table, -create => 1 ) or die "Cannot open $table\n";
$tab->setNodeAttribs( $_, $rows{$_} ) foreach sort keys %rows;
$tab->close;
}
sub node_attribute {
my ( $table, $node, $column ) = @_;
my $tab = xCAT::Table->new($table) or return undef;
my $ent = $tab->getNodeAttribs( $node, [$column] );
$tab->close;
return $ent ? $ent->{$column} : undef;
}
# cn1 has a vpd.uuid. cn2 has none, so mkvm generates one. cn3 runs on machine type pc-i440fx-2.1.
set_rows( 'nodelist', cn1 => { groups => 'all' }, cn2 => { groups => 'all' }, cn3 => { groups => 'all' },
hyp1 => { groups => 'all' } );
set_rows( 'nodehm', cn1 => { mgt => 'kvm', power => 'kvm' }, cn2 => { mgt => 'kvm', power => 'kvm' },
cn3 => { mgt => 'kvm', power => 'kvm' }, hyp1 => { mgt => 'ipmi' } );
set_rows( 'nodetype', cn1 => { arch => 'x86_64' }, cn2 => { arch => 'x86_64' }, cn3 => { arch => 'x86_64' } );
set_rows( 'vm',
cn1 => { host => 'hyp1', storage => 'phy:/dev/xcattest', nics => 'br0', memory => '1024', cpus => '1' },
cn2 => { host => 'hyp1', storage => 'phy:/dev/xcattest3', nics => 'br0', memory => '1024', cpus => '1' },
cn3 => { host => 'hyp1', storage => 'phy:/dev/xcattest5', nics => 'br0', memory => '1024', cpus => '1',
othersettings => 'machine:pc-i440fx-2.1' } );
set_rows( 'mac', cn1 => { mac => '52:54:00:76:00:21' }, cn2 => { mac => '52:54:00:76:00:23' },
cn3 => { mac => '52:54:00:76:00:27' } );
set_rows( 'vpd', cn1 => { uuid => $uuid }, cn3 => { uuid => $uuid } );
my $plugin = "$FindBin::Bin/../../xCAT-server/lib/xcat/plugins/kvm.pm";
require $plugin;
{
# The plugin probes the SSH port of the hypervisor before it connects.
no warnings qw(once redefine);
*xCAT_plugin::kvm::nodesockopen = sub { return 1; };
}
# The text values of a response, whatever its nesting.
sub texts {
my ($value) = @_;
return map { texts($_) } @$value if ref($value) eq 'ARRAY';
return map { texts($_) } values %$value if ref($value) eq 'HASH';
return defined($value) ? ($value) : ();
}
# Run one command and return the text of every response. The per-hypervisor child calls the
# callback too, so the responses go to a file that outlives the fork.
sub run_command {
my ( $command, $nodes, @args ) = @_;
my $log = "$db/$command.out";
unlink $log;
my $callback = sub {
open( my $fh, '>>', $log ) or die "Cannot write $log: $!";
print {$fh} "$_\n" foreach texts( $_[0] );
close($fh);
};
xCAT_plugin::kvm::process_request( { command => [$command], node => $nodes, arg => \@args }, $callback, sub { return; } );
open( my $fh, '<', $log ) or return ();
chomp( my @output = <$fh> );
close($fh);
return @output;
}
sub domain_uuid {
my ($xml) = @_;
return undef unless $xml;
my ($element) = XML::LibXML->load_xml( string => $xml )->findnodes('/domain/uuid');
return $element ? $element->textContent : undef;
}
# mkvm builds the domain XML of cn1 and keeps it in kvm_nodedata.
my @mkvm = run_command( 'mkvm', ['cn1'] );
my $xml = node_attribute( 'kvm_nodedata', 'cn1', 'xml' );
ok( $xml, 'mkvm stores the domain XML of cn1' ) or diag( join "\n", @mkvm );
is( domain_uuid($xml), $uuid, 'mkvm gives the domain the UUID in vpd.uuid unchanged' );
run_command( 'mkvm', ['cn2'] );
my $generated = node_attribute( 'vpd', 'cn2', 'uuid' );
like( $generated // '', qr/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i,
'mkvm stores a generated UUID in vpd.uuid of cn2' );
is( domain_uuid( node_attribute( 'kvm_nodedata', 'cn2', 'xml' ) ), $generated,
'and gives the domain that UUID unchanged' );
# A domain as libvirt reports it, for the commands that read domains back.
sub domain_xml {
my ( $name, $domain_uuid, $disk, $mac, $machine ) = @_;
$machine //= 'q35';
return <<"XML";
<domain type="kvm" id="7">
<name>$name</name>
<uuid>$domain_uuid</uuid>
<memory unit="KiB">1048576</memory>
<vcpu>1</vcpu>
<os><type arch="x86_64" machine="$machine">hvm</type></os>
<devices>
<disk type="block" device="disk"><source dev="$disk"/><target dev="vda" bus="virtio"/></disk>
<interface type="bridge"><source bridge="br0"/><mac address="$mac"/><model type="virtio"/></interface>
</devices>
</domain>
XML
}
# rinv prints the domain UUID, which is the UUID the guest reports.
@XCATTestHypervisor::domains = ( domain_xml( 'cn1', $uuid, '/dev/xcattest', '52:54:00:76:00:21' ) );
my @rinv = run_command( 'rinv', ['cn1'] );
ok( ( grep { $_ eq $uuid } @rinv ), 'rinv prints the domain UUID unchanged' ) or diag( join "\n", @rinv );
# rscan -w defines a node from a domain that xCAT did not create, and stores its UUID.
my $scanned = '87654321-4321-4dcb-8967-ba0987654321';
@XCATTestHypervisor::domains = ( domain_xml( 'scanned1', $scanned, '/dev/xcattest2', '52:54:00:76:00:22' ) );
my @rscan = run_command( 'rscan', ['hyp1'], '-w' );
is( node_attribute( 'vpd', 'scanned1', 'uuid' ), $scanned, 'rscan -w stores the domain UUID in vpd.uuid unchanged' )
or diag( join "\n", @rscan );
# Measured with -uuid 12345678-1234-4bcd-9876-1234567890ab on QEMU 8.2 and on the qemu-kvm 6.2 of EL8: the
# pc-i440fx-2.1 and pc-i440fx-rhel7.1.0 guests report 78563412-3412-cd4b-9876-1234567890ab, and the other
# machine types report the UUID unchanged.
my $swapped = '78563412-3412-cd4b-9876-1234567890ab';
foreach my $machine ( 'pc-i440fx-2.1', 'pc-i440fx-rhel7.1.0' ) {
is( xCAT::KVMUtils::guest_uuid( $uuid, $machine ), $swapped, "guest_uuid swaps the UUID of a $machine domain" );
is( xCAT::KVMUtils::guest_uuid( $swapped, $machine ), $uuid, 'and swapping again gives the domain UUID back' );
}
foreach my $machine ( undef, 'pc-i440fx-2.0', 'pc-i440fx-2.2', 'pc-i440fx-8.2', 'pc-i440fx-rhel7.0.0', 'pc-i440fx-rhel7.2.0',
'pc-i440fx-rhel10.0.0', 'pc-i440fx-trusty', 'pc-i440fx-wily', 'q35', 'pc-q35-rhel10.2.0' ) {
is( xCAT::KVMUtils::guest_uuid( $uuid, $machine ), $uuid, 'guest_uuid keeps the UUID of machine type ' . ( $machine // 'default' ) );
}
# mkvm, rinv and rscan swap the UUID for a pc-i440fx-2.1 guest, so the guest still reports vpd.uuid.
run_command( 'mkvm', ['cn3'] );
my $legacy_xml = node_attribute( 'kvm_nodedata', 'cn3', 'xml' );
my ($legacy_type) = $legacy_xml ? XML::LibXML->load_xml( string => $legacy_xml )->findnodes('/domain/os/type') : ();
is( $legacy_type ? $legacy_type->getAttribute('machine') : undef, 'pc-i440fx-2.1', 'mkvm gives cn3 the machine type of vm.othersettings' );
is( domain_uuid($legacy_xml), $swapped, 'and a domain UUID that its guest reports as vpd.uuid' );
@XCATTestHypervisor::domains = ( domain_xml( 'cn1', $uuid, '/dev/xcattest', '52:54:00:76:00:21', 'pc-i440fx-2.1' ) );
my @legacy_rinv = run_command( 'rinv', ['cn1'] );
ok( ( grep { $_ eq $swapped } @legacy_rinv ), 'rinv prints the UUID that a pc-i440fx-2.1 guest reports' )
or diag( join "\n", @legacy_rinv );
@XCATTestHypervisor::domains = ( domain_xml( 'scanned2', $uuid, '/dev/xcattest6', '52:54:00:76:00:28', 'pc-i440fx-2.1' ) );
run_command( 'rscan', ['hyp1'], '-w' );
is( node_attribute( 'vpd', 'scanned2', 'uuid' ), $swapped, 'rscan -w stores the UUID that a pc-i440fx-2.1 guest reports' );
@XCATTestHypervisor::domains = ( domain_xml( 'scanned3', $uuid, '/dev/xcattest7', '52:54:00:76:00:29', 'pc-i440fx-rhel7.1.0' ) );
run_command( 'rscan', ['hyp1'], '-w' );
is( node_attribute( 'vpd', 'scanned3', 'uuid' ), $swapped, 'rscan -w stores the UUID that a pc-i440fx-rhel7.1.0 guest reports' );
done_testing();