[PATCH v2 4/7] tests: rewrite host CPU mocking

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

 



Move all the host CPU data into a separate file and rewrite qemucpumock
to not use passed @caps.  This is preparation for following patch which
will replace virCaps argument with virArch.

Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx>
---

Notes:
    New in v2

 tests/domaincapstest.c    |  55 +----------------
 tests/qemucpumock.c       |  13 ++--
 tests/testutilshostcpus.h | 148 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/testutilsqemu.c     |  93 +++--------------------------
 tests/testutilsxen.c      |  29 +--------
 5 files changed, 167 insertions(+), 171 deletions(-)
 create mode 100644 tests/testutilshostcpus.h

diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c
index 5a36fcf29d..e17aa8ade3 100644
--- a/tests/domaincapstest.c
+++ b/tests/domaincapstest.c
@@ -117,38 +117,7 @@ fillAllCaps(virDomainCapsPtr domCaps)
 
 #if WITH_QEMU
 # include "testutilsqemu.h"
-
-static virCPUDef aarch64Cpu = {
-    .sockets = 1,
-    .cores = 1,
-    .threads = 1,
-};
-
-static virCPUDef ppc64leCpu = {
-    .type = VIR_CPU_TYPE_HOST,
-    .arch = VIR_ARCH_PPC64LE,
-    .model = (char *) "POWER8",
-    .sockets = 1,
-    .cores = 1,
-    .threads = 1,
-};
-
-static virCPUDef x86Cpu = {
-    .type = VIR_CPU_TYPE_HOST,
-    .arch = VIR_ARCH_X86_64,
-    .model = (char *) "Broadwell",
-    .sockets = 1,
-    .cores = 1,
-    .threads = 1,
-};
-
-static virCPUDef s390Cpu = {
-    .type = VIR_CPU_TYPE_HOST,
-    .arch = VIR_ARCH_S390X,
-    .sockets = 1,
-    .cores = 1,
-    .threads = 1,
-};
+# include "testutilshostcpus.h"
 
 static int
 fakeHostCPU(virCapsPtr caps,
@@ -156,32 +125,14 @@ fakeHostCPU(virCapsPtr caps,
 {
     virCPUDefPtr cpu;
 
-    switch (arch) {
-    case VIR_ARCH_AARCH64:
-        cpu = &aarch64Cpu;
-        break;
-
-    case VIR_ARCH_PPC64LE:
-        cpu = &ppc64leCpu;
-        break;
-
-    case VIR_ARCH_X86_64:
-        cpu = &x86Cpu;
-        break;
-
-    case VIR_ARCH_S390X:
-        cpu = &s390Cpu;
-        break;
-
-    default:
+    if (!(cpu = testUtilsHostCpusGetDefForArch(arch))) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        "cannot fake host CPU for arch %s",
                        virArchToString(arch));
         return -1;
     }
 
-    if (!(caps->host.cpu = virCPUDefCopy(cpu)))
-        return -1;
+    qemuTestSetHostCPU(caps, cpu);
 
     return 0;
 }
diff --git a/tests/qemucpumock.c b/tests/qemucpumock.c
index f0a113ffce..38827584ea 100644
--- a/tests/qemucpumock.c
+++ b/tests/qemucpumock.c
@@ -16,20 +16,23 @@
 
 #include <config.h>
 
-#include "internal.h"
+#include <stdlib.h>
+
+#include "conf/cpu_conf.h"
+#include "cpu/cpu.h"
 #include "qemu/qemu_capabilities.h"
 #define __QEMU_CAPSPRIV_H_ALLOW__
 #include "qemu/qemu_capspriv.h"
 #undef __QEMU_CAPSPRIV_H_ALLOW__
+#include "testutilshostcpus.h"
 
 
 virCPUDefPtr
-virQEMUCapsProbeHostCPUForEmulator(virCapsPtr caps,
+virQEMUCapsProbeHostCPUForEmulator(virCapsPtr caps ATTRIBUTE_UNUSED,
                                    virQEMUCapsPtr qemuCaps ATTRIBUTE_UNUSED,
                                    virDomainVirtType type ATTRIBUTE_UNUSED)
 {
-    if (!caps || !caps->host.cpu || !caps->host.cpu->model)
-        return NULL;
+    const char *model = getenv("VIR_TEST_MOCK_FAKE_HOST_CPU");
 
-    return virCPUDefCopy(caps->host.cpu);
+    return testUtilsHostCpusGetDefForModel(model);
 }
diff --git a/tests/testutilshostcpus.h b/tests/testutilshostcpus.h
new file mode 100644
index 0000000000..f0ab23b962
--- /dev/null
+++ b/tests/testutilshostcpus.h
@@ -0,0 +1,148 @@
+#include "conf/cpu_conf.h"
+#include "internal.h"
+#include "util/virarch.h"
+
+static virCPUFeatureDef cpuDefaultFeatures[] = {
+    { (char *) "ds",        -1 },
+    { (char *) "acpi",      -1 },
+    { (char *) "ss",        -1 },
+    { (char *) "ht",        -1 },
+    { (char *) "tm",        -1 },
+    { (char *) "pbe",       -1 },
+    { (char *) "ds_cpl",    -1 },
+    { (char *) "vmx",       -1 },
+    { (char *) "est",       -1 },
+    { (char *) "tm2",       -1 },
+    { (char *) "cx16",      -1 },
+    { (char *) "xtpr",      -1 },
+    { (char *) "lahf_lm",   -1 },
+};
+static virCPUDef cpuDefaultData = {
+    .type = VIR_CPU_TYPE_HOST,
+    .arch = VIR_ARCH_X86_64,
+    .model = (char *) "core2duo",
+    .vendor = (char *) "Intel",
+    .sockets = 1,
+    .cores = 2,
+    .threads = 1,
+    .nfeatures = ARRAY_CARDINALITY(cpuDefaultFeatures),
+    .nfeatures_max = ARRAY_CARDINALITY(cpuDefaultFeatures),
+    .features = cpuDefaultFeatures,
+};
+
+static virCPUFeatureDef cpuHaswellFeatures[] = {
+    { (char *) "vme",       -1 },
+    { (char *) "ds",        -1 },
+    { (char *) "acpi",      -1 },
+    { (char *) "ss",        -1 },
+    { (char *) "ht",        -1 },
+    { (char *) "tm",        -1 },
+    { (char *) "pbe",       -1 },
+    { (char *) "dtes64",    -1 },
+    { (char *) "monitor",   -1 },
+    { (char *) "ds_cpl",    -1 },
+    { (char *) "vmx",       -1 },
+    { (char *) "smx",       -1 },
+    { (char *) "est",       -1 },
+    { (char *) "tm2",       -1 },
+    { (char *) "xtpr",      -1 },
+    { (char *) "pdcm",      -1 },
+    { (char *) "osxsave",   -1 },
+    { (char *) "f16c",      -1 },
+    { (char *) "rdrand",    -1 },
+    { (char *) "cmt",       -1 },
+    { (char *) "pdpe1gb",   -1 },
+    { (char *) "abm",       -1 },
+    { (char *) "invtsc",    -1 },
+    { (char *) "lahf_lm",   -1 },
+};
+static virCPUDef cpuHaswellData = {
+    .type = VIR_CPU_TYPE_HOST,
+    .arch = VIR_ARCH_X86_64,
+    .model = (char *) "Haswell",
+    .vendor = (char *) "Intel",
+    .sockets = 1,
+    .cores = 2,
+    .threads = 2,
+    .nfeatures = ARRAY_CARDINALITY(cpuHaswellFeatures),
+    .nfeatures_max = ARRAY_CARDINALITY(cpuHaswellFeatures),
+    .features = cpuHaswellFeatures,
+};
+
+static virCPUDef cpuBroadwellData = {
+    .type = VIR_CPU_TYPE_HOST,
+    .arch = VIR_ARCH_X86_64,
+    .model = (char *) "Broadwell",
+    .sockets = 1,
+    .cores = 2,
+    .threads = 2,
+};
+
+static virCPUDef cpuPower8Data = {
+    .type = VIR_CPU_TYPE_HOST,
+    .arch = VIR_ARCH_PPC64,
+    .model = (char *) "POWER8",
+    .sockets = 1,
+    .cores = 8,
+    .threads = 8,
+};
+
+static virCPUDef cpuPower9Data = {
+    .type = VIR_CPU_TYPE_HOST,
+    .arch = VIR_ARCH_PPC64,
+    .model = (char *) "POWER9",
+    .sockets = 1,
+    .cores = 16,
+    .threads = 1,
+};
+
+static virCPUDef cpuAarch64Data = {
+    .type = VIR_CPU_TYPE_HOST,
+    .arch = VIR_ARCH_AARCH64,
+    .sockets = 1,
+    .cores = 4,
+    .threads = 1,
+};
+
+static virCPUDef cpuS390Data = {
+    .type = VIR_CPU_TYPE_HOST,
+    .arch = VIR_ARCH_S390X,
+    .sockets = 2,
+    .cores = 1,
+    .threads = 1,
+};
+
+static inline virCPUDefPtr
+testUtilsHostCpusGetDefForModel(const char *model)
+{
+    if (!model)
+        return NULL;
+
+    if (STREQ(model, "core2duo"))
+        return virCPUDefCopy(&cpuDefaultData);
+    else if (STREQ(model, "Haswell"))
+        return virCPUDefCopy(&cpuHaswellData);
+    else if (STREQ(model, "Broadwell"))
+        return virCPUDefCopy(&cpuBroadwellData);
+    else if (STREQ(model, "POWER8"))
+        return virCPUDefCopy(&cpuPower8Data);
+    else if (STREQ(model, "POWER9"))
+        return virCPUDefCopy(&cpuPower9Data);
+
+    return NULL;
+}
+
+static inline virCPUDefPtr
+testUtilsHostCpusGetDefForArch(virArch arch)
+{
+    if (ARCH_IS_X86(arch))
+        return virCPUDefCopy(&cpuBroadwellData);
+    else if (ARCH_IS_PPC64(arch))
+        return virCPUDefCopy(&cpuPower8Data);
+    else if (ARCH_IS_S390(arch))
+        return virCPUDefCopy(&cpuS390Data);
+    else if (arch == VIR_ARCH_AARCH64)
+        return virCPUDefCopy(&cpuAarch64Data);
+
+    return NULL;
+}
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index 88e11ba14a..67b21c9893 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -3,6 +3,7 @@
 # include <stdlib.h>
 
 # include "testutilsqemu.h"
+# include "testutilshostcpus.h"
 # include "testutils.h"
 # include "viralloc.h"
 # include "cpu_conf.h"
@@ -19,91 +20,6 @@ virCPUDefPtr cpuHaswell;
 virCPUDefPtr cpuPower8;
 virCPUDefPtr cpuPower9;
 
-static virCPUFeatureDef cpuDefaultFeatures[] = {
-    { (char *) "ds",        -1 },
-    { (char *) "acpi",      -1 },
-    { (char *) "ss",        -1 },
-    { (char *) "ht",        -1 },
-    { (char *) "tm",        -1 },
-    { (char *) "pbe",       -1 },
-    { (char *) "ds_cpl",    -1 },
-    { (char *) "vmx",       -1 },
-    { (char *) "est",       -1 },
-    { (char *) "tm2",       -1 },
-    { (char *) "cx16",      -1 },
-    { (char *) "xtpr",      -1 },
-    { (char *) "lahf_lm",   -1 },
-};
-static virCPUDef cpuDefaultData = {
-    .type = VIR_CPU_TYPE_HOST,
-    .arch = VIR_ARCH_X86_64,
-    .model = (char *) "core2duo",
-    .vendor = (char *) "Intel",
-    .sockets = 1,
-    .cores = 2,
-    .threads = 1,
-    .nfeatures = ARRAY_CARDINALITY(cpuDefaultFeatures),
-    .nfeatures_max = ARRAY_CARDINALITY(cpuDefaultFeatures),
-    .features = cpuDefaultFeatures,
-};
-
-static virCPUFeatureDef cpuHaswellFeatures[] = {
-    { (char *) "vme",       -1 },
-    { (char *) "ds",        -1 },
-    { (char *) "acpi",      -1 },
-    { (char *) "ss",        -1 },
-    { (char *) "ht",        -1 },
-    { (char *) "tm",        -1 },
-    { (char *) "pbe",       -1 },
-    { (char *) "dtes64",    -1 },
-    { (char *) "monitor",   -1 },
-    { (char *) "ds_cpl",    -1 },
-    { (char *) "vmx",       -1 },
-    { (char *) "smx",       -1 },
-    { (char *) "est",       -1 },
-    { (char *) "tm2",       -1 },
-    { (char *) "xtpr",      -1 },
-    { (char *) "pdcm",      -1 },
-    { (char *) "osxsave",   -1 },
-    { (char *) "f16c",      -1 },
-    { (char *) "rdrand",    -1 },
-    { (char *) "cmt",       -1 },
-    { (char *) "pdpe1gb",   -1 },
-    { (char *) "abm",       -1 },
-    { (char *) "invtsc",    -1 },
-    { (char *) "lahf_lm",   -1 },
-};
-static virCPUDef cpuHaswellData = {
-    .type = VIR_CPU_TYPE_HOST,
-    .arch = VIR_ARCH_X86_64,
-    .model = (char *) "Haswell",
-    .vendor = (char *) "Intel",
-    .sockets = 1,
-    .cores = 2,
-    .threads = 2,
-    .nfeatures = ARRAY_CARDINALITY(cpuHaswellFeatures),
-    .nfeatures_max = ARRAY_CARDINALITY(cpuHaswellFeatures),
-    .features = cpuHaswellFeatures,
-};
-
-static virCPUDef cpuPower8Data = {
-    .type = VIR_CPU_TYPE_HOST,
-    .arch = VIR_ARCH_PPC64,
-    .model = (char *) "POWER8",
-    .sockets = 1,
-    .cores = 8,
-    .threads = 8,
-};
-
-static virCPUDef cpuPower9Data = {
-    .type = VIR_CPU_TYPE_HOST,
-    .arch = VIR_ARCH_PPC64,
-    .model = (char *) "POWER9",
-    .sockets = 1,
-    .cores = 16,
-    .threads = 1,
-};
-
 typedef enum {
     TEST_UTILS_QEMU_BIN_I686,
     TEST_UTILS_QEMU_BIN_X86_64,
@@ -557,8 +473,13 @@ qemuTestSetHostCPU(virCapsPtr caps,
             cpu = cpuPower8;
     }
 
-    if (cpu)
+    if (cpu) {
         caps->host.arch = cpu->arch;
+        if (cpu->model)
+            setenv("VIR_TEST_MOCK_FAKE_HOST_CPU", cpu->model, 1);
+        else
+            unsetenv("VIR_TEST_MOCK_FAKE_HOST_CPU");
+    }
     caps->host.cpu = cpu;
 }
 
diff --git a/tests/testutilsxen.c b/tests/testutilsxen.c
index c4fc1465e7..4328441b79 100644
--- a/tests/testutilsxen.c
+++ b/tests/testutilsxen.c
@@ -4,36 +4,9 @@
 #include <stdlib.h>
 
 #include "testutilsxen.h"
+#include "testutilshostcpus.h"
 #include "domain_conf.h"
 
-static virCPUFeatureDef cpuDefaultFeatures[] = {
-    { (char *) "ds",        -1 },
-    { (char *) "acpi",      -1 },
-    { (char *) "ss",        -1 },
-    { (char *) "ht",        -1 },
-    { (char *) "tm",        -1 },
-    { (char *) "pbe",       -1 },
-    { (char *) "ds_cpl",    -1 },
-    { (char *) "vmx",       -1 },
-    { (char *) "est",       -1 },
-    { (char *) "tm2",       -1 },
-    { (char *) "cx16",      -1 },
-    { (char *) "xtpr",      -1 },
-    { (char *) "lahf_lm",   -1 },
-};
-static virCPUDef cpuDefaultData = {
-    .type = VIR_CPU_TYPE_HOST,
-    .arch = VIR_ARCH_X86_64,
-    .model = (char *) "core2duo",
-    .vendor = (char *) "Intel",
-    .sockets = 1,
-    .cores = 2,
-    .threads = 1,
-    .nfeatures = ARRAY_CARDINALITY(cpuDefaultFeatures),
-    .nfeatures_max = ARRAY_CARDINALITY(cpuDefaultFeatures),
-    .features = cpuDefaultFeatures,
-};
-
 virCapsPtr testXenCapsInit(void)
 {
     struct utsname utsname;
-- 
2.13.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux