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