Since mocking imported functions have to be done for each package that imports them, we would have to mock get_host_arch for each package that imports it from PVE::Tools. Instead, introduce one local helper here that uses the function from PVE::Tools. This allows us to only need to override it one time.
Signed-off-by: Dominik Csapak <[email protected]> --- changes from v2: * split out from first patch * use the helper everywhere in the package src/PVE/API2/Qemu/Machine.pm | 3 ++- src/PVE/QemuServer.pm | 5 ++--- src/PVE/QemuServer/CPUConfig.pm | 12 ++++++------ src/PVE/QemuServer/Helpers.pm | 8 +++++++- src/test/run_config2command_tests.pl | 6 +++--- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/PVE/API2/Qemu/Machine.pm b/src/PVE/API2/Qemu/Machine.pm index 26195eae..adc36d49 100644 --- a/src/PVE/API2/Qemu/Machine.pm +++ b/src/PVE/API2/Qemu/Machine.pm @@ -7,9 +7,10 @@ use JSON; use PVE::JSONSchema qw(get_standard_option); use PVE::RESTHandler; -use PVE::Tools qw(extract_param file_get_contents get_host_arch); +use PVE::Tools qw(extract_param file_get_contents); use PVE::QemuServer::Machine; +use PVE::QemuServer::Helpers qw(get_host_arch); use base qw(PVE::RESTHandler); diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm index 5d2dbe03..8bf23945 100644 --- a/src/PVE/QemuServer.pm +++ b/src/PVE/QemuServer.pm @@ -46,8 +46,7 @@ use PVE::SafeSyslog; use PVE::Storage; use PVE::SysFSTools; use PVE::Systemd; -use PVE::Tools - qw(run_command file_read_firstline file_get_contents dir_glob_foreach get_host_arch $IPV6RE); +use PVE::Tools qw(run_command file_read_firstline file_get_contents dir_glob_foreach $IPV6RE); use PVE::QMPClient; use PVE::QemuConfig; @@ -58,7 +57,7 @@ use PVE::QemuServer::Blockdev; use PVE::QemuServer::BlockJob; use PVE::QemuServer::Cfg2Cmd; use PVE::QemuServer::Helpers - qw(config_aware_timeout get_iscsi_initiator_name min_version kvm_user_version windows_version); + qw(config_aware_timeout get_iscsi_initiator_name get_host_arch min_version kvm_user_version windows_version); use PVE::QemuServer::Cloudinit; use PVE::QemuServer::CGroup; use PVE::QemuServer::CPUConfig qw( diff --git a/src/PVE/QemuServer/CPUConfig.pm b/src/PVE/QemuServer/CPUConfig.pm index 32ec4954..0218908e 100644 --- a/src/PVE/QemuServer/CPUConfig.pm +++ b/src/PVE/QemuServer/CPUConfig.pm @@ -11,7 +11,7 @@ use PVE::ProcFSTools; use PVE::RESTEnvironment qw(log_warn); use PVE::Tools qw(run_command); -use PVE::QemuServer::Helpers qw(min_version); +use PVE::QemuServer::Helpers qw(min_version get_host_arch); use base qw(PVE::SectionConfig Exporter); @@ -25,7 +25,7 @@ our @EXPORT_OK = qw( get_cvm_type ); -my $host_arch = PVE::Tools::get_host_arch(); +my $host_arch = get_host_arch(); my $arch_desc = { description => "Virtual processor architecture. Defaults to the host architecture.", @@ -294,7 +294,7 @@ my $supported_cpu_flags_by_arch = { sub get_supported_cpu_flags { my ($arch) = @_; - $arch = $host_arch if !defined($arch); + $arch = get_host_arch() if !defined($arch); return $supported_cpu_flags_by_arch->{$arch}; } @@ -582,7 +582,7 @@ sub add_cpu_json_properties { sub get_cpu_models { my ($include_custom, $arch) = @_; - $arch = $host_arch if !defined($arch); + $arch = get_host_arch() if !defined($arch); my $cpu_vendor_list = $cpu_models_by_arch->{$arch}; my $models = []; @@ -1077,13 +1077,13 @@ sub get_default_cpu_type { sub is_native_arch($) { my ($arch) = @_; - return $host_arch eq $arch; + return get_host_arch() eq $arch; } sub get_cpu_bitness { my ($cpu_prop_str, $arch) = @_; - $arch //= $host_arch; + $arch //= get_host_arch(); my $cputype = get_default_cpu_type($arch, 0); diff --git a/src/PVE/QemuServer/Helpers.pm b/src/PVE/QemuServer/Helpers.pm index 35c00754..e898440c 100644 --- a/src/PVE/QemuServer/Helpers.pm +++ b/src/PVE/QemuServer/Helpers.pm @@ -10,7 +10,7 @@ use JSON; use PVE::Cluster; use PVE::INotify; use PVE::ProcFSTools; -use PVE::Tools qw(get_host_arch); +use PVE::Tools; use base 'Exporter'; our @EXPORT_OK = qw( @@ -20,6 +20,7 @@ our @EXPORT_OK = qw( kvm_user_version parse_number_sets windows_version + get_host_arch ); my $nodename = PVE::INotify::nodename(); @@ -29,6 +30,11 @@ my $arch_to_qemu_binary = { x86_64 => '/usr/bin/qemu-system-x86_64', }; +# wrapper around the Tools helper, having it here makes it easier for testing +sub get_host_arch { + return PVE::Tools::get_host_arch(); +} + sub get_command_for_arch($) { my ($arch) = @_; return '/usr/bin/kvm' if get_host_arch() eq $arch; # i.e. native arch diff --git a/src/test/run_config2command_tests.pl b/src/test/run_config2command_tests.pl index 4c872d1c..c20f2377 100755 --- a/src/test/run_config2command_tests.pl +++ b/src/test/run_config2command_tests.pl @@ -253,9 +253,6 @@ $qemu_server_module->mock( kernel_has_vhost_net => sub { return 1; # TODO: make this per-test configurable? }, - get_host_arch => sub() { - return $current_test->{host_arch} // 'x86_64'; - }, get_iscsi_initiator_name => sub { return 'iqn.1993-08.org.debian:01:aabbccddeeff'; }, @@ -388,6 +385,9 @@ $pve_common_tools->mock( }, ); }, + get_host_arch => sub() { + return $current_test->{host_arch} // 'x86_64'; + }, ); my $pve_cpuconfig; -- 2.47.3
