Signed-off-by: Miloslav Trmač <mitr@xxxxxxxxxx> --- src/libvirt_private.syms | 4 + tests/Makefile.am | 6 +- tests/errorjsontest.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 tests/errorjsontest.c diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 0b6068d..1f24de2 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -704,6 +704,10 @@ iptablesRemoveUdpInput; # json.h +virJSONStringGeneratorAddProperties; +virJSONStringGeneratorFinishObject; +virJSONStringGeneratorFree; +virJSONStringGeneratorInitObject; virJSONValueArrayAppend; virJSONValueArrayGet; virJSONValueArraySize; diff --git a/tests/Makefile.am b/tests/Makefile.am index 8dbad97..a46502b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -138,7 +138,7 @@ test_programs += object-locking endif if HAVE_YAJL -test_programs += jsontest +test_programs += jsontest errorjsontest endif test_programs += networkxml2xmltest @@ -598,6 +598,10 @@ jsontest_SOURCES = \ jsontest.c testutils.h testutils.c jsontest_LDADD = $(LDADDS) +errorjsontest_SOURCES = \ + errorjsontest.c testutils.h testutils.c +errorjsontest_LDADD = $(LDADDS) + utiltest_SOURCES = \ utiltest.c testutils.h testutils.c utiltest_LDADD = $(LDADDS) diff --git a/tests/errorjsontest.c b/tests/errorjsontest.c new file mode 100644 index 0000000..491306b --- /dev/null +++ b/tests/errorjsontest.c @@ -0,0 +1,211 @@ +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "internal.h" +#include "json.h" +#include "logging.h" +#include "testutils.h" +#include "virterror_internal.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +/* Ideally we should test the syslog/json output, this tests only the inputs to + the logging function. */ + +struct lastMessage +{ + int priority; + char *properties; +}; + +static void +lmReset(struct lastMessage *lm) +{ + lm->priority = -1; + VIR_FREE(lm->properties); +} + +static void +logOutputToLM(const char *category ATTRIBUTE_UNUSED, int priority, + const char *funcname ATTRIBUTE_UNUSED, + long long linenr ATTRIBUTE_UNUSED, + const char *timestamp ATTRIBUTE_UNUSED, + virJSONObjectPtr properties, unsigned int flags, + const char *rawstr ATTRIBUTE_UNUSED, + const char *str ATTRIBUTE_UNUSED, void *data) +{ + struct lastMessage *lm; + virJSONStringGeneratorPtr g = NULL; + const char *json_string; + + virCheckFlags(VIR_LOG_STACK_TRACE,); + + lm = data; + lmReset(lm); + /* category (== file name) is not expected to be stable */ + lm->priority = priority; + /* funcname, linenr, timestamp, rawstr, str are not expected to be stable */ + + g = virJSONStringGeneratorInitObject(); + if (g == NULL) + goto cleanup; + if (properties != NULL + && virJSONStringGeneratorAddProperties(g, properties) != 0) + goto cleanup; + json_string = strdup(virJSONStringGeneratorFinishObject(g)); + if (json_string == NULL) + goto cleanup; + lm->properties = strdup(json_string); + cleanup: + virJSONStringGeneratorFree(g); +} + +static int +testBasicMessage(const void *data) +{ + struct lastMessage *lm; + virConnectPtr conn = NULL; + virDomainPtr dom = NULL; + int ret = -1; + + lm = (struct lastMessage *)data; + conn = virConnectOpen("test:///default"); + if (conn == NULL) { + if (virTestGetVerbose()) + fprintf(stderr, "Could not connect to test URI\n"); + goto cleanup; + } + + dom = virDomainLookupByName(conn, "this_doesnt_exist"); + if (dom != NULL) { + if (virTestGetVerbose()) + fprintf(stderr, + "Should not have connected to nonexistent domain\n"); + goto cleanup; + } + + if (lm->priority != VIR_LOG_ERROR) { + if (virTestGetVerbose()) + fprintf(stderr, "Unexpected priority\n"); + goto cleanup; + } + + ret = 0; + + cleanup: + if (dom != NULL) + virDomainFree(dom); + if (conn != NULL) + virConnectClose(conn); + return ret; +} + +static int +testJSONError(const void *data) +{ + struct lastMessage *lm; + virConnectPtr conn = NULL; + virDomainPtr dom = NULL; + virJSONValuePtr json = NULL; + int ret = -1, val; + + lm = (struct lastMessage *)data; + conn = virConnectOpen("test:///default"); + if (conn == NULL) { + if (virTestGetVerbose()) + fprintf(stderr, "Could not connect to test URI\n"); + goto cleanup; + } + + dom = virDomainLookupByName(conn, "this_doesnt_exist"); + if (dom != NULL) { + if (virTestGetVerbose()) + fprintf(stderr, + "Should not have connected to nonexistent domain\n"); + goto cleanup; + } + + if (lm->properties == NULL) { + if (virTestGetVerbose()) + fprintf(stderr, "Properties missing\n"); + goto cleanup; + } + json = virJSONValueFromString(lm->properties); + if (json == NULL) { + if (virTestGetVerbose()) + fprintf(stderr, "Error parsing properties\n"); + goto cleanup; + } + if (json->type != VIR_JSON_TYPE_OBJECT) { + if (virTestGetVerbose()) + /* logOutputToLM did create an object for us */ + fprintf(stderr, "Didn't get an object?!\n"); + goto cleanup; + } + + if (virJSONValueObjectGetNumberInt(json, "domain", &val) != 0) { + if (virTestGetVerbose()) + fprintf(stderr, "\"domain\" missing\n"); + goto cleanup; + } + if (val != VIR_FROM_TEST) { + if (virTestGetVerbose()) + fprintf(stderr, "Unexpected domain %d (wanted %d)\n", val, + VIR_FROM_TEST); + goto cleanup; + } + + if (virJSONValueObjectGetNumberInt(json, "code", &val) != 0) { + if (virTestGetVerbose()) + fprintf(stderr, "\"code\" missing\n"); + goto cleanup; + } + if (val != VIR_ERR_NO_DOMAIN) { + if (virTestGetVerbose()) + fprintf(stderr, "Unexpected code %d (wanted %d)\n", val, + VIR_ERR_NO_DOMAIN); + goto cleanup; + } + + ret = 0; + + cleanup: + virJSONValueFree(json); + if (dom != NULL) + virDomainFree(dom); + if (conn != NULL) + virConnectClose(conn); + return ret; +} + +static void +testQuietError(void *userData ATTRIBUTE_UNUSED, + virErrorPtr error ATTRIBUTE_UNUSED) +{ + /* nothing */ +} + +static int +mymain(void) +{ + struct lastMessage lm; + int ret = 0; + + memset(&lm, 0, sizeof(lm)); + if (virLogReset() < 0) + return EXIT_FAILURE; + if (virLogDefineOutput(logOutputToLM, NULL, &lm, 0, 0, NULL, 0) < 0) + return EXIT_FAILURE; + virSetErrorFunc(NULL, testQuietError); + if (virtTestRun("Basic message details", 1, testBasicMessage, &lm) != 0) + ret = -1; + if (virtTestRun("JSON error details", 1, testJSONError, &lm) != 0) + ret = -1; + lmReset(&lm); + return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIRT_TEST_MAIN(mymain) -- 1.7.11.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list