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