The CPUConfig module sets the local 'host_arch' on loading, so instead
to try to mock all subs that access this variable, provide a setter only
intended for tests (and warn when using this).

Also, we have to override the 'get_host_arch' in the Helpers module too.

Without these changes, the config to command tests fail when building on
non-x86 hosts.

Signed-off-by: Dominik Csapak <[email protected]>
---
not sure about the 'setter hack', which can be partially deleted when
we'll switch to using PVE::Tools::get_host_arch again, but we have to
keep the code changing the cpu_models_by_arch hash.

This patch requires these patches from fiona:
https://lore.proxmox.com/pve-devel/[email protected]/

 src/PVE/QemuServer/CPUConfig.pm      | 12 ++++++++++++
 src/test/run_config2command_tests.pl | 13 +++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/src/PVE/QemuServer/CPUConfig.pm b/src/PVE/QemuServer/CPUConfig.pm
index 32ec4954..686bbdc0 100644
--- a/src/PVE/QemuServer/CPUConfig.pm
+++ b/src/PVE/QemuServer/CPUConfig.pm
@@ -1230,6 +1230,18 @@ sub get_intel_tdx_object {
     return $tdx_object;
 }
 
+# CAUTION: for tests only, DO NOT USE outside of tests!
+# sets the package gloabl $host_arch value, and modifies the 
$cpu_models_by_arch
+# so that the 'host' model is available only in the newly set $arch
+sub set_host_arch($) {
+    my ($arch) = @_;
+    warn "CAUTION: use of 'set_host_arch' only intended in tests!\n";
+
+    delete $cpu_models_by_arch->{$host_arch}->{host};
+    $host_arch = $arch;
+    $cpu_models_by_arch->{$host_arch}->{host} = 'default';
+}
+
 __PACKAGE__->register();
 __PACKAGE__->init();
 
diff --git a/src/test/run_config2command_tests.pl 
b/src/test/run_config2command_tests.pl
index 4c872d1c..b8a2cf7e 100755
--- a/src/test/run_config2command_tests.pl
+++ b/src/test/run_config2command_tests.pl
@@ -235,6 +235,8 @@ sub parse_test($config_fn) {
         $testname = "'$testname' - $desc";
     }
     $current_test->{testname} = $testname;
+
+    PVE::QemuServer::CPUConfig::set_host_arch($current_test->{arch} // 
'x86_64');
 }
 
 sub get_test_qemu_version {
@@ -264,6 +266,14 @@ $qemu_server_module->mock(
     },
 );
 
+my $qemu_server_helpers_module;
+$qemu_server_helpers_module = 
Test::MockModule->new('PVE::QemuServer::Helpers');
+$qemu_server_helpers_module->mock(
+    get_host_arch => sub() {
+        return $current_test->{host_arch} // 'x86_64';
+    },
+);
+
 my $storage_module = Test::MockModule->new("PVE::Storage");
 $storage_module->mock(
     activate_volumes => sub {
@@ -582,6 +592,9 @@ sub diff($a, $b) {
 $SIG{__WARN__} = sub {
     my $warning = shift;
     chomp $warning;
+    if ($warning =~ m/^CAUTION: use of 'set_host/) {
+        return;
+    }
     if (my $warn_expect = $current_test->{expect_warning}) {
         if ($warn_expect ne $warning) {
             fail($current_test->{testname});
-- 
2.47.3




Reply via email to