[libvirt] [PATCH 09/24] tests: utils: Add virTestLoadFileJSON helper

Peter Krempa posted 24 patches 7 years, 9 months ago
[libvirt] [PATCH 09/24] tests: utils: Add virTestLoadFileJSON helper
Posted by Peter Krempa 7 years, 9 months ago
This new helper loads, parses and returns a JSON file from 'abs_srcdir'
By using variable arguments for the function, it's not necessary to
format the path separately in the test cases.
---
 tests/testutils.c | 34 ++++++++++++++++++++++++++++++++++
 tests/testutils.h |  4 ++++
 2 files changed, 38 insertions(+)

diff --git a/tests/testutils.c b/tests/testutils.c
index f193cdf8b..75f69e1c3 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -407,6 +407,40 @@ virTestLoadFilePath(const char *p, ...)
 }


+/**
+ * virTestLoadFileJSON:
+ * @...: name components
+ *
+ * Constructs the test file path from variable arguments and loads and parses
+ * the JSON file. 'abs_srcdir' is automatically prepended to the path.
+ */
+virJSONValuePtr
+virTestLoadFileJSON(const char *p, ...)
+{
+    virJSONValuePtr ret = NULL;
+    char *jsonstr = NULL;
+    char *path = NULL;
+    va_list ap;
+
+    va_start(ap, p);
+
+    if (!(path = virTestLoadFileGetPath(p, ap)))
+        goto cleanup;
+
+    if (virTestLoadFile(path, &jsonstr) < 0)
+        goto cleanup;
+
+    if (!(ret = virJSONValueFromString(jsonstr)))
+        VIR_TEST_VERBOSE("failed to parse json from file '%s'", path);
+
+ cleanup:
+    va_end(ap);
+    VIR_FREE(jsonstr);
+    VIR_FREE(path);
+    return ret;
+}
+
+
 #ifndef WIN32
 static
 void virTestCaptureProgramExecChild(const char *const argv[],
diff --git a/tests/testutils.h b/tests/testutils.h
index 98dfa990e..49649c4f5 100644
--- a/tests/testutils.h
+++ b/tests/testutils.h
@@ -27,6 +27,7 @@
 # include "viralloc.h"
 # include "virfile.h"
 # include "virstring.h"
+# include "virjson.h"
 # include "capabilities.h"
 # include "domain_conf.h"

@@ -54,6 +55,9 @@ int virTestRun(const char *title,
 int virTestLoadFile(const char *file, char **buf);
 char *virTestLoadFilePath(const char *p, ...)
     ATTRIBUTE_SENTINEL;
+virJSONValuePtr virTestLoadFileJSON(const char *p, ...)
+    ATTRIBUTE_SENTINEL;
+
 int virTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen);

 void virTestClearCommandPath(char *cmdset);
-- 
2.13.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 09/24] tests: utils: Add virTestLoadFileJSON helper
Posted by Eric Blake 7 years, 9 months ago
On 07/26/2017 05:00 AM, Peter Krempa wrote:
> This new helper loads, parses and returns a JSON file from 'abs_srcdir'
> By using variable arguments for the function, it's not necessary to
> format the path separately in the test cases.
> ---
>  tests/testutils.c | 34 ++++++++++++++++++++++++++++++++++
>  tests/testutils.h |  4 ++++
>  2 files changed, 38 insertions(+)
> 
> diff --git a/tests/testutils.c b/tests/testutils.c
> index f193cdf8b..75f69e1c3 100644
> --- a/tests/testutils.c
> +++ b/tests/testutils.c
> @@ -407,6 +407,40 @@ virTestLoadFilePath(const char *p, ...)
>  }
> 
> 
> +/**
> + * virTestLoadFileJSON:
> + * @...: name components

Same comment as in 7/24: document that it must end in NULL.  With that,

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list