render / rpms / libvirt

Forked from rpms/libvirt 11 months ago
Clone
397dc2
From ff54ea3d2a61a25079339d38caa6c509cf697ce3 Mon Sep 17 00:00:00 2001
397dc2
Message-Id: <ff54ea3d2a61a25079339d38caa6c509cf697ce3@dist-git>
397dc2
From: "Mauro S. M. Rodrigues" <maurosr@linux.vnet.ibm.com>
397dc2
Date: Tue, 19 Jan 2021 21:04:08 -0300
397dc2
Subject: [PATCH] util: virhostcpu: Fail when fetching CPU Stats for invalid
397dc2
 cpu
397dc2
397dc2
virHostCPUGetStatsLinux walks through every cpu in /proc/stat until it
397dc2
finds cpu%cpuNum that matches with the requested cpu.
397dc2
If none is found it logs the error but it should return -1, instead of 0.
397dc2
Otherwise virsh nodecpustats --cpu <invalid cpu number> and API bindings
397dc2
don't fail properly, printing a blank line instead of an error message.
397dc2
397dc2
This patch also includes an additional test for virhostcputest to avoid
397dc2
this regression to happen again in the future.
397dc2
397dc2
Fixes: 93af79fba3fd75a8df6b7ca608719dd97f9511a0
397dc2
Reported-by: Satheesh Rajendran <satheera@in.ibm.com>
397dc2
Signed-off-by: Mauro S. M. Rodrigues <maurosr@linux.vnet.ibm.com>
397dc2
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
397dc2
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
397dc2
Reviewed-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
397dc2
Tested-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
397dc2
(cherry picked from commit 75a4ec42f70b5324f95d7ffbbfbf7457620735e4)
397dc2
397dc2
https://bugzilla.redhat.com/1915183
397dc2
397dc2
Signed-off-by: Daniel Henrique Barboza <dbarboza@redhat.com>
397dc2
Message-Id: <20210120000408.106596-1-dbarboza@redhat.com>
397dc2
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
397dc2
---
397dc2
 src/util/virhostcpu.c  |  2 +-
397dc2
 tests/virhostcputest.c | 21 ++++++++++++++++++---
397dc2
 2 files changed, 19 insertions(+), 4 deletions(-)
397dc2
397dc2
diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
397dc2
index 218272d7ec..37cc45e3a6 100644
397dc2
--- a/src/util/virhostcpu.c
397dc2
+++ b/src/util/virhostcpu.c
397dc2
@@ -855,7 +855,7 @@ virHostCPUGetStatsLinux(FILE *procstat,
397dc2
                         _("Invalid cpuNum in %s"),
397dc2
                         __FUNCTION__);
397dc2
 
397dc2
-    return 0;
397dc2
+    return -1;
397dc2
 }
397dc2
 
397dc2
 
397dc2
diff --git a/tests/virhostcputest.c b/tests/virhostcputest.c
397dc2
index 7865b61578..70a723098b 100644
397dc2
--- a/tests/virhostcputest.c
397dc2
+++ b/tests/virhostcputest.c
397dc2
@@ -196,6 +196,7 @@ linuxTestHostCPU(const void *opaque)
397dc2
 struct nodeCPUStatsData {
397dc2
     const char *name;
397dc2
     int ncpus;
397dc2
+    bool shouldFail;
397dc2
 };
397dc2
 
397dc2
 static int
397dc2
@@ -214,6 +215,19 @@ linuxTestNodeCPUStats(const void *data)
397dc2
     result = linuxCPUStatsCompareFiles(cpustatfile,
397dc2
                                        testData->ncpus,
397dc2
                                        outfile);
397dc2
+    if (result < 0) {
397dc2
+        if (testData->shouldFail) {
397dc2
+            /* Expected error */
397dc2
+            result = 0;
397dc2
+        }
397dc2
+    } else {
397dc2
+        if (testData->shouldFail) {
397dc2
+            fprintf(stderr, "Expected a failure, got success");
397dc2
+            result = -1;
397dc2
+        }
397dc2
+    }
397dc2
+
397dc2
+
397dc2
     VIR_FREE(cpustatfile);
397dc2
     VIR_FREE(outfile);
397dc2
     return result;
397dc2
@@ -258,14 +272,15 @@ mymain(void)
397dc2
         if (virTestRun(nodeData[i].testName, linuxTestHostCPU, &nodeData[i]) != 0)
397dc2
             ret = -1;
397dc2
 
397dc2
-# define DO_TEST_CPU_STATS(name, ncpus) \
397dc2
+# define DO_TEST_CPU_STATS(name, ncpus, shouldFail) \
397dc2
     do { \
397dc2
-        static struct nodeCPUStatsData data = { name, ncpus }; \
397dc2
+        static struct nodeCPUStatsData data = { name, ncpus, shouldFail}; \
397dc2
         if (virTestRun("CPU stats " name, linuxTestNodeCPUStats, &data) < 0) \
397dc2
             ret = -1; \
397dc2
     } while (0)
397dc2
 
397dc2
-    DO_TEST_CPU_STATS("24cpu", 24);
397dc2
+    DO_TEST_CPU_STATS("24cpu", 24, false);
397dc2
+    DO_TEST_CPU_STATS("24cpu", 25, true);
397dc2
 
397dc2
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
397dc2
 }
397dc2
-- 
397dc2
2.30.0
397dc2