libvirt/SOURCES/libvirt-util-virhostcpu-Fai...

103 lines
3.5 KiB
Diff

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