use Test::More;
use Test::MockModule;
+use Socket qw(AF_INET AF_INET6);
use PVE::Tools qw(file_get_contents file_set_contents run_command);
+use PVE::INotify;
+use PVE::SysFSTools;
+
use PVE::QemuConfig;
use PVE::QemuServer;
+use PVE::QemuServer::Monitor;
+use PVE::QemuServer::Machine;
my $base_env = {
storage_config => {
real_qemu_version => PVE::QemuServer::kvm_user_version(), # not yet mocked
};
+my $pci_devs = [
+ "0000:00:43.1",
+ "0000:00:f4.0",
+ "0000:00:ff.1",
+ "0000:0f:f2.0",
+ "0000:d0:13.0",
+ "0000:d0:15.1",
+ "0000:d0:17.0",
+ "0000:f0:42.0",
+ "0000:f0:43.0",
+ "0000:f0:43.1",
+ "1234:f0:43.1",
+];
+
my $current_test; # = {
# description => 'Test description', # if available
# qemu_version => '2.12',
}
}
+sub get_test_qemu_version {
+ $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12';
+}
+
my $qemu_server_module;
$qemu_server_module = Test::MockModule->new('PVE::QemuServer');
$qemu_server_module->mock(
kvm_user_version => sub {
- return $current_test->{qemu_version} // $base_env->{real_qemu_version};
+ return get_test_qemu_version();
},
kvm_version => sub {
- return $current_test->{qemu_version} // $base_env->{real_qemu_version};
+ return get_test_qemu_version();
},
kernel_has_vhost_net => sub {
return 1; # TODO: make this per-test configurable?
},
);
+my $pve_common_tools;
+$pve_common_tools = Test::MockModule->new('PVE::Tools');
+$pve_common_tools->mock(
+ next_vnc_port => sub {
+ my ($family, $address) = @_;
+
+ return '5900';
+ },
+ next_spice_port => sub {
+ my ($family, $address) = @_;
+
+ return '61000';
+ },
+ getaddrinfo_all => sub {
+ my ($hostname, @opts) = @_;
+ die "need stable hostname" if $hostname ne 'localhost';
+ return (
+ {
+ addr => Socket::pack_sockaddr_in(0, Socket::INADDR_LOOPBACK),
+ family => AF_INET, # IPv4
+ protocol => 6,
+ socktype => 1,
+ },
+ );
+ },
+);
+
+my $pve_common_inotify;
+$pve_common_inotify = Test::MockModule->new('PVE::INotify');
+$pve_common_inotify->mock(
+ nodename => sub {
+ return 'localhost';
+ },
+);
+
+my $pve_common_sysfstools;
+$pve_common_sysfstools = Test::MockModule->new('PVE::SysFSTools');
+$pve_common_sysfstools->mock(
+ lspci => sub {
+ my ($filter, $verbose) = @_;
+
+ return [
+ map { { id => $_ } }
+ grep {
+ !defined($filter)
+ || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
+ || (ref($filter) eq 'CODE' && $filter->({ id => $_ }))
+ } sort @$pci_devs
+ ];
+ },
+);
+
+my $qemu_monitor_module;
+$qemu_monitor_module = Test::MockModule->new('PVE::QemuServer::Monitor');
+$qemu_monitor_module->mock(
+ mon_cmd => sub {
+ my ($vmid, $cmd) = @_;
+
+ die "invalid vmid: $vmid (expected: $base_env->{vmid})"
+ if $vmid != $base_env->{vmid};
+
+ if ($cmd eq 'query-version') {
+ my $ver = get_test_qemu_version();
+ $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
+ return {
+ qemu => {
+ major => $1,
+ minor => $2,
+ micro => $3
+ }
+ }
+ }
+
+ die "unexpected QMP command: '$cmd'";
+ },
+);
+$qemu_monitor_module->mock('qmp_cmd', \&qmp_cmd);
+
sub diff($$) {
my ($a, $b) = @_;
return if $a eq $b;
my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $vmid);
+ # check if QEMU version set correctly and test version_cmp
+ (my $qemu_major = get_test_qemu_version()) =~ s/\..*$//;
+ die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
+ if !PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, $qemu_major);
+
$cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
$cmdline .= "\n";
if (-f $cmd_fn) {
my $cmdline_expected = file_get_contents($cmd_fn);
- my $cmd_expected = [ sort split /\s*\\?\n\s*/, $cmdline_expected ];
- my $cmd = [ sort split /\s*\\?\n\s*/, $cmdline ];
+ my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
+ my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
- # comment out for easier debugging
+ # uncomment for easier debugging
#file_set_contents("$cmd_fn.tmp", $cmdline);
my $exp = join("\n", @$cmd_expected);