[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 1/2] tests: Add mocking for qemuMonitorJSONGetSEVCapabilities



Commit d4005609 added "altered" capabilities replies output
in order to fake a 'query-sev-capabilities' reply from QEMU.
This worked fine for the 2.12 processing for qemuxml2argvtest
until the next capabilities was generated and the output wasn't
doctored. Thus commit 6c50cef8 used DO_TEST_CAPS_VER against
2.12.0 noting that the 2.12.0 capabilities were hand edited
to add AMD specific output into an Intel capabilities reply.

Instead of "altering" the output or running against a specific
reply that we know was altered, let's instead use the mocking
capabilities to check the return from a real call and mock up
return data if we determine the returned real call doesn't
support compiled-in SEV. This way the qemuxml2argvtest can
use the DO_TEST_CAPS_LATEST which runs only for x86_64 to
determine that noting in "latest" changes with respect to
SEV and effectively fake things out to generate expected
output ensuring that other changes to libvirt/qemu don't
somehow affect SEV support.

Signed-off-by: John Ferlan <jferlan redhat com>
---
 tests/qemucapsprobemock.c                     | 50 +++++++++++++++++++
 ...=> launch-security-sev.x86_64-latest.args} |  0
 tests/qemuxml2argvtest.c                      |  2 +-
 3 files changed, 51 insertions(+), 1 deletion(-)
 rename tests/qemuxml2argvdata/{launch-security-sev.x86_64-2.12.0.args => launch-security-sev.x86_64-latest.args} (100%)

diff --git a/tests/qemucapsprobemock.c b/tests/qemucapsprobemock.c
index f3f17f2116..0351b946b2 100644
--- a/tests/qemucapsprobemock.c
+++ b/tests/qemucapsprobemock.c
@@ -22,9 +22,15 @@
 #include "internal.h"
 #include "viralloc.h"
 #include "virjson.h"
+#include "virlog.h"
+#include "virstring.h"
 #include "qemu/qemu_monitor.h"
 #include "qemu/qemu_monitor_json.h"
 
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("tests.qemucapsprobemock");
+
 #define REAL_SYM(realFunc) \
     do { \
         if (!realFunc && !(realFunc = dlsym(RTLD_NEXT, __FUNCTION__))) { \
@@ -120,3 +126,47 @@ qemuMonitorJSONIOProcessLine(qemuMonitorPtr mon,
     virJSONValueFree(value);
     return ret;
 }
+
+
+static int (*realQemuMonitorJSONGetSEVCapabilities)(qemuMonitorPtr mon,
+                                                    virSEVCapability **capabilities);
+
+int
+qemuMonitorJSONGetSEVCapabilities(qemuMonitorPtr mon,
+                                  virSEVCapability **capabilities)
+{
+    int ret = -1;
+    VIR_AUTOPTR(virSEVCapability) capability = NULL;
+
+    VIR_DEBUG("mocked qemuMonitorJSONGetSEVCapabilities");
+
+    REAL_SYM(realQemuMonitorJSONGetSEVCapabilities);
+
+    ret = realQemuMonitorJSONGetSEVCapabilities(mon, capabilities);
+
+    if (ret == 0) {
+        /* QEMU has only compiled-in support of SEV in which case we
+         * can mock up a response instead since generation of SEV output
+         * is only possible on AMD hardware. Since the qemuxml2argvtest
+         * doesn't currently distinguish between AMD and Intel for x86_64
+         * if we "alter" the pseudo failure we can at least allow the
+         * test to succeed using the latest replies rather than a specific
+         * version with altered reply data */
+        if (VIR_ALLOC(capability) < 0)
+            return -1;
+
+        if (VIR_STRDUP(capability->pdh, "Unchecked, but mocked pdh") < 0)
+            return -1;
+
+        if (VIR_STRDUP(capability->cert_chain, "Mocked cert_chain too") < 0)
+            return -1;
+
+        capability->cbitpos = 47;
+        capability->reduced_phys_bits = 1;
+        VIR_STEAL_PTR(*capabilities, capability);
+
+        return 1;
+    }
+
+    return ret;
+}
diff --git a/tests/qemuxml2argvdata/launch-security-sev.x86_64-2.12.0.args b/tests/qemuxml2argvdata/launch-security-sev.x86_64-latest.args
similarity index 100%
rename from tests/qemuxml2argvdata/launch-security-sev.x86_64-2.12.0.args
rename to tests/qemuxml2argvdata/launch-security-sev.x86_64-latest.args
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index ba6fd4db35..c65f6f91ee 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -3068,7 +3068,7 @@ mymain(void)
     DO_TEST_CAPS_ARCH_LATEST("vhost-vsock-ccw", "s390x");
     DO_TEST_CAPS_ARCH_LATEST("vhost-vsock-ccw-auto", "s390x");
 
-    DO_TEST_CAPS_VER("launch-security-sev", "2.12.0");
+    DO_TEST_CAPS_LATEST("launch-security-sev");
 
     DO_TEST("riscv64-virt",
             QEMU_CAPS_DEVICE_VIRTIO_MMIO);
-- 
2.20.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]