]> xenbits.xensource.com Git - libvirt.git/commitdiff
tests: utils: Add virTestLoadFileJSON helper
authorPeter Krempa <pkrempa@redhat.com>
Tue, 25 Jul 2017 16:40:50 +0000 (18:40 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 27 Jul 2017 07:38:39 +0000 (09:38 +0200)
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.

Reviewed-by: Eric Blake <eblake@redhat.com>
tests/testutils.c
tests/testutils.h

index 53ef351ef7fdbdaae54f69085c286f1292d4231e..5e71f348be342eab3feb74f43de745bccfcfc5af 100644 (file)
@@ -407,6 +407,40 @@ virTestLoadFilePath(const char *p, ...)
 }
 
 
+/**
+ * virTestLoadFileJSON:
+ * @...: name components terminated with a NULL
+ *
+ * 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[],
index 98dfa990e6eb5b88aee6e67b00f2a5a1163dcd21..49649c4f5ad9884a3d6383c0d0bf8aa952152c75 100644 (file)
@@ -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);