[libvirt] [PATCH 0/4] tests: Introduce and use TEST_QEMU_CAPS_PATH

Reduce repetition by replacing reutterances with reusable references. Andrea Bolognani (4): tests: Make TEST_QEMU_CAPS_PATH public tests: Drop dirname argument from testQemuGetLatestCapsForArch() tests: Drop dirname argument from testQemuCapsIterate() tests: Use TEST_QEMU_CAPS_PATH wherever possible tests/domaincapstest.c | 4 ++-- tests/qemuargv2xmltest.c | 2 +- tests/qemublocktest.c | 3 +-- tests/qemucapabilitiestest.c | 4 ++-- tests/qemucaps2xmltest.c | 4 ++-- tests/testutilsqemu.c | 25 ++++++++++--------------- tests/testutilsqemu.h | 8 ++++---- tests/testutilsqemuschema.c | 3 +-- 8 files changed, 23 insertions(+), 30 deletions(-) -- 2.20.1

The value (with a slightly different name) is currently private to testutilsqemu, but since we use this path all over the place it makes sense to define it publicly and avoid repetition. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/testutilsqemu.c | 4 +--- tests/testutilsqemu.h | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index 763f511bcf..0f295dc9a9 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -957,8 +957,6 @@ testQemuCapsIterate(const char *dirname, } -#define TEST_CAPS_PATH abs_srcdir "/qemucapabilitiesdata" - int testQemuInfoSetArgs(struct testQemuInfo *info, virHashTablePtr capslatest, ...) @@ -1059,7 +1057,7 @@ testQemuInfoSetArgs(struct testQemuInfo *info, goto cleanup; stripmachinealiases = true; } else if (virAsprintf(&capsfile, "%s/caps_%s.%s.xml", - TEST_CAPS_PATH, capsver, capsarch) < 0) { + TEST_QEMU_CAPS_PATH, capsver, capsarch) < 0) { goto cleanup; } diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h index 9ff34841ff..9ed633f8c0 100644 --- a/tests/testutilsqemu.h +++ b/tests/testutilsqemu.h @@ -25,6 +25,8 @@ # include "qemu/qemu_capabilities.h" # include "qemu/qemu_conf.h" +# define TEST_QEMU_CAPS_PATH abs_srcdir "/qemucapabilitiesdata" + enum { GIC_NONE = 0, GIC_V2, -- 2.20.1

As evidenced by all existing callers, the only directory it makes sense to use is TEST_QEMU_CAPS_PATH, so let's just bake that into the function. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/qemublocktest.c | 3 +-- tests/testutilsqemu.c | 14 ++++++-------- tests/testutilsqemu.h | 3 +-- tests/testutilsqemuschema.c | 3 +-- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c index 1a365a411c..c221b8af30 100644 --- a/tests/qemublocktest.c +++ b/tests/qemublocktest.c @@ -327,8 +327,7 @@ mymain(void) diskxmljsondata.driver = &driver; - if (!(capslatest_x86_64 = testQemuGetLatestCapsForArch(abs_srcdir "/qemucapabilitiesdata", - "x86_64", "xml"))) + if (!(capslatest_x86_64 = testQemuGetLatestCapsForArch("x86_64", "xml"))) return EXIT_FAILURE; VIR_TEST_VERBOSE("\nlatest caps x86_64: %s\n", capslatest_x86_64); diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index 0f295dc9a9..ccc05233db 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -805,8 +805,7 @@ testQemuCapsSetGIC(virQEMUCapsPtr qemuCaps, char * -testQemuGetLatestCapsForArch(const char *dirname, - const char *arch, +testQemuGetLatestCapsForArch(const char *arch, const char *suffix) { struct dirent *ent; @@ -822,10 +821,10 @@ testQemuGetLatestCapsForArch(const char *dirname, if (virAsprintf(&fullsuffix, "%s.%s", arch, suffix) < 0) goto cleanup; - if (virDirOpen(&dir, dirname) < 0) + if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0) goto cleanup; - while ((rc = virDirRead(dir, &ent, dirname)) > 0) { + while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH)) > 0) { VIR_FREE(tmp); if ((rc = VIR_STRDUP(tmp, STRSKIP(ent->d_name, "caps_"))) < 0) @@ -853,11 +852,11 @@ testQemuGetLatestCapsForArch(const char *dirname, if (!maxname) { VIR_TEST_VERBOSE("failed to find capabilities for '%s' in '%s'\n", - arch, dirname); + arch, TEST_QEMU_CAPS_PATH); goto cleanup; } - ignore_value(virAsprintf(&ret, "%s/%s", dirname, maxname)); + ignore_value(virAsprintf(&ret, "%s/%s", TEST_QEMU_CAPS_PATH, maxname)); cleanup: VIR_FREE(tmp); @@ -886,8 +885,7 @@ testQemuGetLatestCaps(void) VIR_TEST_VERBOSE("\n"); for (i = 0; i < ARRAY_CARDINALITY(archs); ++i) { - char *cap = testQemuGetLatestCapsForArch(abs_srcdir "/qemucapabilitiesdata", - archs[i], "xml"); + char *cap = testQemuGetLatestCapsForArch(archs[i], "xml"); if (!cap || virHashAddEntry(capslatest, archs[i], cap) < 0) goto error; diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h index 9ed633f8c0..949e98f591 100644 --- a/tests/testutilsqemu.h +++ b/tests/testutilsqemu.h @@ -92,8 +92,7 @@ int qemuTestCapsCacheInsert(virFileCachePtr cache, int testQemuCapsSetGIC(virQEMUCapsPtr qemuCaps, int gic); -char *testQemuGetLatestCapsForArch(const char *dirname, - const char *arch, +char *testQemuGetLatestCapsForArch(const char *arch, const char *suffix); virHashTablePtr testQemuGetLatestCaps(void); diff --git a/tests/testutilsqemuschema.c b/tests/testutilsqemuschema.c index aa846e1e79..f1365e8846 100644 --- a/tests/testutilsqemuschema.c +++ b/tests/testutilsqemuschema.c @@ -533,8 +533,7 @@ testQEMUSchemaGetLatest(void) virJSONValuePtr reply = NULL; virJSONValuePtr schema = NULL; - if (!(capsLatestFile = testQemuGetLatestCapsForArch(abs_srcdir "/qemucapabilitiesdata", - "x86_64", "replies"))) { + if (!(capsLatestFile = testQemuGetLatestCapsForArch("x86_64", "replies"))) { VIR_TEST_VERBOSE("failed to find latest caps replies\n"); return NULL; } -- 2.20.1

As evidenced by all existing callers, the only directory it makes sense to use is TEST_QEMU_CAPS_PATH, so let's just bake that into the function. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/qemucapabilitiestest.c | 2 +- tests/qemucaps2xmltest.c | 2 +- tests/testutilsqemu.c | 7 +++---- tests/testutilsqemu.h | 3 +-- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c index 4c9b7c5322..5477740339 100644 --- a/tests/qemucapabilitiestest.c +++ b/tests/qemucapabilitiestest.c @@ -221,7 +221,7 @@ mymain(void) if (testQemuDataInit(&data) < 0) return EXIT_FAILURE; - if (testQemuCapsIterate(data.dataDir, ".replies", doCapsTest, &data) < 0) + if (testQemuCapsIterate(".replies", doCapsTest, &data) < 0) return EXIT_FAILURE; /* diff --git a/tests/qemucaps2xmltest.c b/tests/qemucaps2xmltest.c index 7d3c768bfd..59f676ede1 100644 --- a/tests/qemucaps2xmltest.c +++ b/tests/qemucaps2xmltest.c @@ -212,7 +212,7 @@ mymain(void) if (testQemuDataInit(&data) < 0) return EXIT_FAILURE; - if (testQemuCapsIterate(data.inputDir, ".xml", doCapsTest, &data) < 0) + if (testQemuCapsIterate(".xml", doCapsTest, &data) < 0) return EXIT_FAILURE; return (data.ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index ccc05233db..bce847ce5e 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -903,8 +903,7 @@ testQemuGetLatestCaps(void) int -testQemuCapsIterate(const char *dirname, - const char *suffix, +testQemuCapsIterate(const char *suffix, testQemuCapsIterateCallback callback, void *opaque) { @@ -916,10 +915,10 @@ testQemuCapsIterate(const char *dirname, if (!callback) return 0; - if (virDirOpen(&dir, dirname) < 0) + if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0) goto cleanup; - while ((rc = virDirRead(dir, &ent, dirname) > 0)) { + while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH) > 0)) { char *tmp = ent->d_name; char *base = NULL; char *archName = NULL; diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h index 949e98f591..25dedb5edd 100644 --- a/tests/testutilsqemu.h +++ b/tests/testutilsqemu.h @@ -99,8 +99,7 @@ virHashTablePtr testQemuGetLatestCaps(void); typedef int (*testQemuCapsIterateCallback)(const char *base, const char *archName, void *opaque); -int testQemuCapsIterate(const char *dirname, - const char *suffix, +int testQemuCapsIterate(const char *suffix, testQemuCapsIterateCallback callback, void *opaque); -- 2.20.1

After the recent changes, there are only a few places left where we use the explicit path instead of taking advantage of the publicly available define; let's get rid of those too. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/domaincapstest.c | 4 ++-- tests/qemuargv2xmltest.c | 2 +- tests/qemucapabilitiestest.c | 2 +- tests/qemucaps2xmltest.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c index a9922e1edb..f87a1d63fb 100644 --- a/tests/domaincapstest.c +++ b/tests/domaincapstest.c @@ -88,8 +88,8 @@ fillQemuCaps(virDomainCapsPtr domCaps, fakeHostCPU(caps, domCaps->arch) < 0) goto cleanup; - if (virAsprintf(&path, "%s/qemucapabilitiesdata/%s.%s.xml", - abs_srcdir, name, arch) < 0 || + if (virAsprintf(&path, "%s/%s.%s.xml", + TEST_QEMU_CAPS_PATH, name, arch) < 0 || !(qemuCaps = qemuTestParseCapabilities(caps, path))) goto cleanup; diff --git a/tests/qemuargv2xmltest.c b/tests/qemuargv2xmltest.c index 29c80a0471..2a51e22318 100644 --- a/tests/qemuargv2xmltest.c +++ b/tests/qemuargv2xmltest.c @@ -156,7 +156,7 @@ mymain(void) do { \ virQEMUCapsPtr qemuCaps; \ qemuCaps = qemuTestParseCapabilitiesArch(VIR_ARCH_X86_64, \ - abs_srcdir "/qemucapabilitiesdata/caps_2.12.0." arch ".xml"); \ + TEST_QEMU_CAPS_PATH "/caps_2.12.0." arch ".xml"); \ if (virFileCacheInsertData(driver.qemuCapsCache, \ "/usr/bin/qemu-system-" arch, \ qemuCaps) < 0) \ diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c index 5477740339..ac9ab6bfce 100644 --- a/tests/qemucapabilitiestest.c +++ b/tests/qemucapabilitiestest.c @@ -48,7 +48,7 @@ testQemuDataInit(testQemuDataPtr data) if (qemuTestDriverInit(&data->driver) < 0) return -1; - data->dataDir = abs_srcdir "/qemucapabilitiesdata"; + data->dataDir = TEST_QEMU_CAPS_PATH; data->ret = 0; diff --git a/tests/qemucaps2xmltest.c b/tests/qemucaps2xmltest.c index 59f676ede1..d107b32221 100644 --- a/tests/qemucaps2xmltest.c +++ b/tests/qemucaps2xmltest.c @@ -39,7 +39,7 @@ struct _testQemuData { static int testQemuDataInit(testQemuDataPtr data) { - data->inputDir = abs_srcdir "/qemucapabilitiesdata"; + data->inputDir = TEST_QEMU_CAPS_PATH; data->outputDir = abs_srcdir "/qemucaps2xmloutdata"; data->ret = 0; -- 2.20.1

On 4/16/19 6:55 AM, Andrea Bolognani wrote:
Reduce repetition by replacing reutterances with reusable references.
Andrea Bolognani (4): tests: Make TEST_QEMU_CAPS_PATH public tests: Drop dirname argument from testQemuGetLatestCapsForArch() tests: Drop dirname argument from testQemuCapsIterate() tests: Use TEST_QEMU_CAPS_PATH wherever possible
tests/domaincapstest.c | 4 ++-- tests/qemuargv2xmltest.c | 2 +- tests/qemublocktest.c | 3 +-- tests/qemucapabilitiestest.c | 4 ++-- tests/qemucaps2xmltest.c | 4 ++-- tests/testutilsqemu.c | 25 ++++++++++--------------- tests/testutilsqemu.h | 8 ++++---- tests/testutilsqemuschema.c | 3 +-- 8 files changed, 23 insertions(+), 30 deletions(-)
Reviewed-by: Cole Robinson <crobinso@redhat.com> - Cole
participants (2)
-
Andrea Bolognani
-
Cole Robinson