Test that validate_cpu_conf denies custom CPU models with
a 'reported-model' that does not exist on the specified (or
default) 'arch'.

Test that get_cpu_options denies configurations with custom
CPU model arch not matching the VM arch.

Signed-off-by: Arthur Bied-Charreton <[email protected]>
---
Changes since v2:
* fix expected error message in custom-cpu-model-wrong-arch.conf,
  got out of sync with actual error message before submitting v2

 src/test/Makefile                             |  5 +-
 .../cfg2cmd/custom-cpu-model-wrong-arch.conf  |  9 +++
 src/test/run_config2command_tests.pl          |  4 ++
 src/test/run_cpu_config_tests.pl              | 57 +++++++++++++++++++
 4 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 src/test/cfg2cmd/custom-cpu-model-wrong-arch.conf
 create mode 100755 src/test/run_cpu_config_tests.pl

diff --git a/src/test/Makefile b/src/test/Makefile
index 2ef9073a..dcfab6d6 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -1,6 +1,6 @@
 all: test
 
-test: test_snapshot test_cfg_to_cmd test_pci_addr_conflicts 
test_pci_reservation test_qemu_img_convert test_migration test_restore_config 
test_parse_config
+test: test_snapshot test_cfg_to_cmd test_pci_addr_conflicts 
test_pci_reservation test_qemu_img_convert test_migration test_restore_config 
test_parse_config test_cpu_config
 
 test_snapshot: run_snapshot_tests.pl
        ./run_snapshot_tests.pl
@@ -31,6 +31,9 @@ test_restore_config: run_qemu_restore_config_tests.pl
 test_parse_config: run_parse_config_tests.pl
        ./run_parse_config_tests.pl
 
+test_cpu_config: run_cpu_config_tests.pl
+       perl -I../ ./run_cpu_config_tests.pl
+
 .PHONY: clean
 clean:
        rm -rf MigrationTest/run parse-config-output
diff --git a/src/test/cfg2cmd/custom-cpu-model-wrong-arch.conf 
b/src/test/cfg2cmd/custom-cpu-model-wrong-arch.conf
new file mode 100644
index 00000000..d1b9b08a
--- /dev/null
+++ b/src/test/cfg2cmd/custom-cpu-model-wrong-arch.conf
@@ -0,0 +1,9 @@
+# TEST: custom CPU model with wrong arch for VM is rejected
+# EXPECT_ERROR: custom CPU model has architecture 'aarch64', but VM has 
'x86_64'
+cores: 2
+cpu: custom-aarch64cpu
+name: wrong-arch-cpu
+numa: 0
+ostype: l26
+smbios1: uuid=2ea3f676-dfa5-11e9-ae82-c721e12f3fcd
+sockets: 1
diff --git a/src/test/run_config2command_tests.pl 
b/src/test/run_config2command_tests.pl
index 4c872d1c..dec07bda 100755
--- a/src/test/run_config2command_tests.pl
+++ b/src/test/run_config2command_tests.pl
@@ -408,6 +408,10 @@ cpu-model: qemu64
 
 cpu-model: alldefault
 
+cpu-model: aarch64cpu
+    reported-model cortex-a76
+    arch aarch64
+
 EOF
         );
     },
diff --git a/src/test/run_cpu_config_tests.pl b/src/test/run_cpu_config_tests.pl
new file mode 100755
index 00000000..d3e587cc
--- /dev/null
+++ b/src/test/run_cpu_config_tests.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use lib qw(..);
+
+use Test::More;
+
+use PVE::JSONSchema;
+use PVE::QemuServer::CPUConfig;
+
+# Test that validate_cpu_conf rejects a custom CPU model whose reported-model
+# doesn't match the specified arch.
+
+my @tests = (
+    {
+        name => 'x86_64 reported-model with aarch64 arch is rejected',
+        input => 'custom-mycpu,reported-model=qemu64,arch=aarch64',
+        expected_error => qr/reported model 'qemu64' is not valid for 
architecture 'aarch64'/,
+    },
+    {
+        name => 'aarch64 reported-model with x86_64 arch is rejected',
+        input => 'custom-mycpu,reported-model=cortex-a76,arch=x86_64',
+        expected_error =>
+            qr/reported model 'cortex-a76' is not valid for architecture 
'x86_64'/,
+    },
+    {
+        name => 'aarch64 reported-model with default (x86_64) arch is 
rejected',
+        input => 'custom-mycpu,reported-model=cortex-a76',
+        expected_error =>
+            qr/reported model 'cortex-a76' is not valid for architecture 
'x86_64'/,
+    },
+    {
+        name => 'x86_64 reported-model with matching arch is accepted',
+        input => 'custom-mycpu,reported-model=qemu64,arch=x86_64',
+        expected_error => undef,
+    },
+    {
+        name => 'aarch64 reported-model with matching arch is accepted',
+        input => 'custom-mycpu,reported-model=cortex-a76,arch=aarch64',
+        expected_error => undef,
+    },
+);
+
+for my $test (@tests) {
+    eval { PVE::JSONSchema::parse_property_string('pve-cpu-conf', 
$test->{input}); };
+    my $err = $@;
+
+    if ($test->{expected_error}) {
+        like($err, $test->{expected_error}, $test->{name});
+    } else {
+        is($err, '', $test->{name});
+    }
+}
+
+done_testing();
-- 
2.47.3



Reply via email to