[libvirt] [PATCH 0/3] virjsontest: separate some tests into files

Ján Tomko (3): virjsontest: introduce DO_TEST_PARSE_FILE virjsontest: switch DO_TEST_PARSE_FILE to use output files virjsontest: switch AddAndRemove tests to work with files tests/virjsondata/add-remove-failure-in.json | 1 + tests/virjsondata/add-remove-success-in.json | 1 + tests/virjsondata/add-remove-success-out.json | 1 + tests/virjsondata/parse-Harder-in.json | 4 + tests/virjsondata/parse-Harder-out.json | 4 + tests/virjsondata/parse-NotSoSimple-in.json | 3 + tests/virjsondata/parse-NotSoSimple-out.json | 3 + tests/virjsondata/parse-Simple-in.json | 1 + tests/virjsondata/parse-Simple-out.json | 1 + tests/virjsondata/parse-VeryHard-in.json | 24 ++++ tests/virjsondata/parse-VeryHard-out.json | 24 ++++ tests/virjsontest.c | 135 ++++++++++-------- 12 files changed, 145 insertions(+), 57 deletions(-) create mode 100644 tests/virjsondata/add-remove-failure-in.json create mode 100644 tests/virjsondata/add-remove-success-in.json create mode 100644 tests/virjsondata/add-remove-success-out.json create mode 100644 tests/virjsondata/parse-Harder-in.json create mode 100644 tests/virjsondata/parse-Harder-out.json create mode 100644 tests/virjsondata/parse-NotSoSimple-in.json create mode 100644 tests/virjsondata/parse-NotSoSimple-out.json create mode 100644 tests/virjsondata/parse-Simple-in.json create mode 100644 tests/virjsondata/parse-Simple-out.json create mode 100644 tests/virjsondata/parse-VeryHard-in.json create mode 100644 tests/virjsondata/parse-VeryHard-out.json -- 2.19.2

Introduce a new macro DO_TEST_PARSE_FILE which takes the input JSON from a file instead of a C string. This lets us get rid of quote escaping and makes the JSON easier to edit. The output JSON is still taken from a string and will be moved separately. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tests/virjsondata/parse-Harder-in.json | 4 + tests/virjsondata/parse-NotSoSimple-in.json | 3 + tests/virjsondata/parse-Simple-in.json | 1 + tests/virjsondata/parse-VeryHard-in.json | 24 ++++ tests/virjsontest.c | 129 +++++++++++++------- 5 files changed, 118 insertions(+), 43 deletions(-) create mode 100644 tests/virjsondata/parse-Harder-in.json create mode 100644 tests/virjsondata/parse-NotSoSimple-in.json create mode 100644 tests/virjsondata/parse-Simple-in.json create mode 100644 tests/virjsondata/parse-VeryHard-in.json diff --git a/tests/virjsondata/parse-Harder-in.json b/tests/virjsondata/parse-Harder-in.json new file mode 100644 index 0000000000..739d780fb9 --- /dev/null +++ b/tests/virjsondata/parse-Harder-in.json @@ -0,0 +1,4 @@ +{"return": [{"filename": \ +"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server",\ +"label": "charmonitor"}, {"filename": "pty:/dev/pts/158",\ +"label": "charserial0"}], "id": "libvirt-3"} diff --git a/tests/virjsondata/parse-NotSoSimple-in.json b/tests/virjsondata/parse-NotSoSimple-in.json new file mode 100644 index 0000000000..bda5fd1c3f --- /dev/null +++ b/tests/virjsondata/parse-NotSoSimple-in.json @@ -0,0 +1,3 @@ +{"QMP": {"version": {"qemu":\ +{"micro": 91, "minor": 13, "major": 0},\ +"package": " (qemu-kvm-devel)"}, "capabilities": []}} diff --git a/tests/virjsondata/parse-Simple-in.json b/tests/virjsondata/parse-Simple-in.json new file mode 100644 index 0000000000..a40724b322 --- /dev/null +++ b/tests/virjsondata/parse-Simple-in.json @@ -0,0 +1 @@ +{"return": {}, "id": "libvirt-1"} diff --git a/tests/virjsondata/parse-VeryHard-in.json b/tests/virjsondata/parse-VeryHard-in.json new file mode 100644 index 0000000000..e10d605950 --- /dev/null +++ b/tests/virjsondata/parse-VeryHard-in.json @@ -0,0 +1,24 @@ +{"return":[{"name":"quit"},{"name":\ +"eject"},{"name":"change"},{"name":"screendump"},\ +{"name":"stop"},{"name":"cont"},{"name":\ +"system_reset"},{"name":"system_powerdown"},\ +{"name":"device_add"},{"name":"device_del"},\ +{"name":"cpu"},{"name":"memsave"},{"name":\ +"pmemsave"},{"name":"migrate"},{"name":\ +"migrate_cancel"},{"name":"migrate_set_speed"},\ +{"name":"client_migrate_info"},{"name":\ +"migrate_set_downtime"},{"name":"netdev_add"},\ +{"name":"netdev_del"},{"name":"block_resize"},\ +{"name":"balloon"},{"name":"set_link"},{"name":\ +"getfd"},{"name":"closefd"},{"name":"block_passwd"},\ +{"name":"set_password"},{"name":"expire_password"},\ +{"name":"qmp_capabilities"},{"name":\ +"human-monitor-command"},{"name":"query-version"},\ +{"name":"query-commands"},{"name":"query-chardev"},\ +{"name":"query-block"},{"name":"query-blockstats"},\ +{"name":"query-cpus"},{"name":"query-pci"},{"name":\ +"query-kvm"},{"name":"query-status"},{"name":\ +"query-mice"},{"name":"query-vnc"},{"name":\ +"query-spice"},{"name":"query-name"},{"name":\ +"query-uuid"},{"name":"query-migrate"},{"name":\ +"query-balloon"}],"id":"libvirt-2"} diff --git a/tests/virjsontest.c b/tests/virjsontest.c index a4bd4fb07b..1d9fc6ccb6 100644 --- a/tests/virjsontest.c +++ b/tests/virjsontest.c @@ -16,6 +16,51 @@ struct testInfo { }; +static int +testJSONFromFile(const void *data) +{ + const struct testInfo *info = data; + VIR_AUTOPTR(virJSONValue) injson = NULL; + VIR_AUTOFREE(char *) infile = NULL; + VIR_AUTOFREE(char *) indata = NULL; + VIR_AUTOFREE(char *) actual = NULL; + + if (virAsprintf(&infile, "%s/virjsondata/parse-%s-in.json", + abs_srcdir, info->name) < 0) + return -1; + + if (virTestLoadFile(infile, &indata) < 0) + return -1; + + injson = virJSONValueFromString(indata); + + if (!injson) { + if (info->pass) { + VIR_TEST_VERBOSE("Fail to parse %s\n", info->name); + return -1; + } else { + VIR_TEST_DEBUG("Fail to parse %s\n", info->name); + return 0; + } + } + + if (!info->pass) { + VIR_TEST_VERBOSE("Should not have parsed %s\n", info->name); + return -1; + } + + if (!(actual = virJSONValueToString(injson, false))) + return -1; + + if (STRNEQ(info->expect, actual)) { + virTestDifference(stderr, info->expect, actual); + return -1; + } + + return 0; +} + + static int testJSONFromString(const void *data) { @@ -499,49 +544,47 @@ mymain(void) #define DO_TEST_PARSE_FAIL(name, doc) \ DO_TEST_FULL(name, FromString, doc, NULL, false) - - DO_TEST_PARSE("Simple", "{\"return\": {}, \"id\": \"libvirt-1\"}", - "{\"return\":{},\"id\":\"libvirt-1\"}"); - DO_TEST_PARSE("NotSoSimple", "{\"QMP\": {\"version\": {\"qemu\":" - "{\"micro\": 91, \"minor\": 13, \"major\": 0}," - "\"package\": \" (qemu-kvm-devel)\"}, \"capabilities\": []}}", - "{\"QMP\":{\"version\":{\"qemu\":" - "{\"micro\":91,\"minor\":13,\"major\":0}," - "\"package\":\" (qemu-kvm-devel)\"},\"capabilities\":[]}}"); - - DO_TEST_PARSE("Harder", "{\"return\": [{\"filename\": " - "\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\"," - "\"label\": \"charmonitor\"}, {\"filename\": \"pty:/dev/pts/158\"," - "\"label\": \"charserial0\"}], \"id\": \"libvirt-3\"}", - "{\"return\":[{\"filename\":" - "\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\"," - "\"label\":\"charmonitor\"},{\"filename\":\"pty:/dev/pts/158\"," - "\"label\":\"charserial0\"}],\"id\":\"libvirt-3\"}"); - - DO_TEST_PARSE("VeryHard", "{\"return\":[{\"name\":\"quit\"},{\"name\":" - "\"eject\"},{\"name\":\"change\"},{\"name\":\"screendump\"}," - "{\"name\":\"stop\"},{\"name\":\"cont\"},{\"name\":" - "\"system_reset\"},{\"name\":\"system_powerdown\"}," - "{\"name\":\"device_add\"},{\"name\":\"device_del\"}," - "{\"name\":\"cpu\"},{\"name\":\"memsave\"},{\"name\":" - "\"pmemsave\"},{\"name\":\"migrate\"},{\"name\":" - "\"migrate_cancel\"},{\"name\":\"migrate_set_speed\"}," - "{\"name\":\"client_migrate_info\"},{\"name\":" - "\"migrate_set_downtime\"},{\"name\":\"netdev_add\"}," - "{\"name\":\"netdev_del\"},{\"name\":\"block_resize\"}," - "{\"name\":\"balloon\"},{\"name\":\"set_link\"},{\"name\":" - "\"getfd\"},{\"name\":\"closefd\"},{\"name\":\"block_passwd\"}," - "{\"name\":\"set_password\"},{\"name\":\"expire_password\"}," - "{\"name\":\"qmp_capabilities\"},{\"name\":" - "\"human-monitor-command\"},{\"name\":\"query-version\"}," - "{\"name\":\"query-commands\"},{\"name\":\"query-chardev\"}," - "{\"name\":\"query-block\"},{\"name\":\"query-blockstats\"}," - "{\"name\":\"query-cpus\"},{\"name\":\"query-pci\"},{\"name\":" - "\"query-kvm\"},{\"name\":\"query-status\"},{\"name\":" - "\"query-mice\"},{\"name\":\"query-vnc\"},{\"name\":" - "\"query-spice\"},{\"name\":\"query-name\"},{\"name\":" - "\"query-uuid\"},{\"name\":\"query-migrate\"},{\"name\":" - "\"query-balloon\"}],\"id\":\"libvirt-2\"}", NULL); +#define DO_TEST_PARSE_FILE(name, expect) \ + DO_TEST_FULL(name, FromFile, NULL, expect, true) + + + DO_TEST_PARSE_FILE("Simple", + "{\"return\":{},\"id\":\"libvirt-1\"}"); + DO_TEST_PARSE_FILE("NotSoSimple", + "{\"QMP\":{\"version\":{\"qemu\":" + "{\"micro\":91,\"minor\":13,\"major\":0}," + "\"package\":\" (qemu-kvm-devel)\"},\"capabilities\":[]}}"); + + DO_TEST_PARSE_FILE("Harder", + "{\"return\":[{\"filename\":" + "\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\"," + "\"label\":\"charmonitor\"},{\"filename\":\"pty:/dev/pts/158\"," + "\"label\":\"charserial0\"}],\"id\":\"libvirt-3\"}"); + + DO_TEST_PARSE_FILE("VeryHard", "{\"return\":[{\"name\":\"quit\"},{\"name\":" + "\"eject\"},{\"name\":\"change\"},{\"name\":\"screendump\"}," + "{\"name\":\"stop\"},{\"name\":\"cont\"},{\"name\":" + "\"system_reset\"},{\"name\":\"system_powerdown\"}," + "{\"name\":\"device_add\"},{\"name\":\"device_del\"}," + "{\"name\":\"cpu\"},{\"name\":\"memsave\"},{\"name\":" + "\"pmemsave\"},{\"name\":\"migrate\"},{\"name\":" + "\"migrate_cancel\"},{\"name\":\"migrate_set_speed\"}," + "{\"name\":\"client_migrate_info\"},{\"name\":" + "\"migrate_set_downtime\"},{\"name\":\"netdev_add\"}," + "{\"name\":\"netdev_del\"},{\"name\":\"block_resize\"}," + "{\"name\":\"balloon\"},{\"name\":\"set_link\"},{\"name\":" + "\"getfd\"},{\"name\":\"closefd\"},{\"name\":\"block_passwd\"}," + "{\"name\":\"set_password\"},{\"name\":\"expire_password\"}," + "{\"name\":\"qmp_capabilities\"},{\"name\":" + "\"human-monitor-command\"},{\"name\":\"query-version\"}," + "{\"name\":\"query-commands\"},{\"name\":\"query-chardev\"}," + "{\"name\":\"query-block\"},{\"name\":\"query-blockstats\"}," + "{\"name\":\"query-cpus\"},{\"name\":\"query-pci\"},{\"name\":" + "\"query-kvm\"},{\"name\":\"query-status\"},{\"name\":" + "\"query-mice\"},{\"name\":\"query-vnc\"},{\"name\":" + "\"query-spice\"},{\"name\":\"query-name\"},{\"name\":" + "\"query-uuid\"},{\"name\":\"query-migrate\"},{\"name\":" + "\"query-balloon\"}],\"id\":\"libvirt-2\"}"); DO_TEST_FULL("add and remove", AddRemove, "{\"name\": \"sample\", \"value\": true}", -- 2.19.2

On Tue, 2019-02-12 at 16:57 +0100, Ján Tomko wrote: [...]
+{"return": [{"filename": \ +"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server",\ +"label": "charmonitor"}, {"filename": "pty:/dev/pts/158",\ +"label": "charserial0"}], "id": "libvirt-3"}
Are the backslashes at the end of lines necessary? I've tried removing a bunch of them and the test didn't break. Are the files even valid JSON with the backslashes included? Additional question: can't we pretty-print at least the input files now? Unless of course the point of these specific test cases is to prove we can successfully parse certain unusual constructs. [...]
+ if (!injson) { + if (info->pass) { + VIR_TEST_VERBOSE("Fail to parse %s\n", info->name); + return -1; + } else { + VIR_TEST_DEBUG("Fail to parse %s\n", info->name); + return 0; + } + }
The second message should read something like Expected failure while parsing %s or Failed to parse %s (expected)
+ + if (!info->pass) { + VIR_TEST_VERBOSE("Should not have parsed %s\n", info->name); + return -1; + }
Maybe Expected failure while parsing %s, got success instead or something along those lines. I think it would also look more legible if this entire if block was inside the else branch of the previous if block, but if you feel strongly about this version then just leave it as is. Everything else looks good. -- Andrea Bolognani / Red Hat / Virtualization

On Wed, Feb 13, 2019 at 02:18:32PM +0100, Andrea Bolognani wrote:
On Tue, 2019-02-12 at 16:57 +0100, Ján Tomko wrote: [...]
+{"return": [{"filename": \ +"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server",\ +"label": "charmonitor"}, {"filename": "pty:/dev/pts/158",\ +"label": "charserial0"}], "id": "libvirt-3"}
Are the backslashes at the end of lines necessary?
In this patch? Yes. The aim is to preserve the test coverage done before and after.
I've tried removing a bunch of them and the test didn't break. Are the files even valid JSON with the backslashes included?
No, they get stripped by virTestLoadFile
Additional question: can't we pretty-print at least the input files now? Unless of course the point of these specific test cases is to prove we can successfully parse certain unusual constructs.
The whole point of separating these was to allow easier changes and make it more-readable, so introducing more whitespace by removing the backslashes and prettifying it is possible.
[...]
+ if (!injson) { + if (info->pass) { + VIR_TEST_VERBOSE("Fail to parse %s\n", info->name); + return -1; + } else { + VIR_TEST_DEBUG("Fail to parse %s\n", info->name); + return 0; + } + }
The second message should read something like
Expected failure while parsing %s
or
Failed to parse %s (expected)
+ + if (!info->pass) { + VIR_TEST_VERBOSE("Should not have parsed %s\n", info->name); + return -1; + }
Maybe
Expected failure while parsing %s, got success instead
or something along those lines.
All the error messages match the original test. Guess it would make more sense to alter them before copying them.
I think it would also look more legible if this entire if block was inside the else branch of the previous if block, but if you feel strongly about this version then just leave it as is.
Like this? if (!injson) { if (info->pass) { return 0; } else { return -1; } } else { if (!info->pass) return -1; } Jano

On Wed, 2019-02-13 at 15:07 +0100, Ján Tomko wrote:
On Wed, Feb 13, 2019 at 02:18:32PM +0100, Andrea Bolognani wrote:
Are the backslashes at the end of lines necessary?
In this patch? Yes. The aim is to preserve the test coverage done before and after.
I've tried removing a bunch of them and the test didn't break. Are the files even valid JSON with the backslashes included?
No, they get stripped by virTestLoadFile
Oh, I see, they're there to keep the input JSON as a single line as it was originally. Makes sense.
Additional question: can't we pretty-print at least the input files now? Unless of course the point of these specific test cases is to prove we can successfully parse certain unusual constructs.
The whole point of separating these was to allow easier changes and make it more-readable, so introducing more whitespace by removing the backslashes and prettifying it is possible.
Alright, it can be done in separate commits then. [...]
All the error messages match the original test. Guess it would make more sense to alter them before copying them.
Yeah, that way they get improved for FromString tests as well.
I think it would also look more legible if this entire if block was inside the else branch of the previous if block, but if you feel strongly about this version then just leave it as is.
Like this?
if (!injson) { if (info->pass) { return 0; } else { return -1; } } else { if (!info->pass) return -1; }
That's exactly what I had in mind. -- Andrea Bolognani / Red Hat / Virtualization

Also switch the expected output of DO_TEST_PARSE_FILE to be in a file, now that we demonstrated the input files match the expected string representation. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tests/virjsondata/parse-Harder-out.json | 4 ++ tests/virjsondata/parse-NotSoSimple-out.json | 3 ++ tests/virjsondata/parse-Simple-out.json | 1 + tests/virjsondata/parse-VeryHard-out.json | 24 +++++++++ tests/virjsontest.c | 56 +++++--------------- 5 files changed, 44 insertions(+), 44 deletions(-) create mode 100644 tests/virjsondata/parse-Harder-out.json create mode 100644 tests/virjsondata/parse-NotSoSimple-out.json create mode 100644 tests/virjsondata/parse-Simple-out.json create mode 100644 tests/virjsondata/parse-VeryHard-out.json diff --git a/tests/virjsondata/parse-Harder-out.json b/tests/virjsondata/parse-Harder-out.json new file mode 100644 index 0000000000..31b3edd731 --- /dev/null +++ b/tests/virjsondata/parse-Harder-out.json @@ -0,0 +1,4 @@ +{"return":[{"filename":\ +"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server",\ +"label":"charmonitor"},{"filename":"pty:/dev/pts/158",\ +"label":"charserial0"}],"id":"libvirt-3"} diff --git a/tests/virjsondata/parse-NotSoSimple-out.json b/tests/virjsondata/parse-NotSoSimple-out.json new file mode 100644 index 0000000000..e679cd1813 --- /dev/null +++ b/tests/virjsondata/parse-NotSoSimple-out.json @@ -0,0 +1,3 @@ +{"QMP":{"version":{"qemu":\ +{"micro":91,"minor":13,"major":0},\ +"package":" (qemu-kvm-devel)"},"capabilities":[]}} diff --git a/tests/virjsondata/parse-Simple-out.json b/tests/virjsondata/parse-Simple-out.json new file mode 100644 index 0000000000..c6e85e1ceb --- /dev/null +++ b/tests/virjsondata/parse-Simple-out.json @@ -0,0 +1 @@ +{"return":{},"id":"libvirt-1"} diff --git a/tests/virjsondata/parse-VeryHard-out.json b/tests/virjsondata/parse-VeryHard-out.json new file mode 100644 index 0000000000..e10d605950 --- /dev/null +++ b/tests/virjsondata/parse-VeryHard-out.json @@ -0,0 +1,24 @@ +{"return":[{"name":"quit"},{"name":\ +"eject"},{"name":"change"},{"name":"screendump"},\ +{"name":"stop"},{"name":"cont"},{"name":\ +"system_reset"},{"name":"system_powerdown"},\ +{"name":"device_add"},{"name":"device_del"},\ +{"name":"cpu"},{"name":"memsave"},{"name":\ +"pmemsave"},{"name":"migrate"},{"name":\ +"migrate_cancel"},{"name":"migrate_set_speed"},\ +{"name":"client_migrate_info"},{"name":\ +"migrate_set_downtime"},{"name":"netdev_add"},\ +{"name":"netdev_del"},{"name":"block_resize"},\ +{"name":"balloon"},{"name":"set_link"},{"name":\ +"getfd"},{"name":"closefd"},{"name":"block_passwd"},\ +{"name":"set_password"},{"name":"expire_password"},\ +{"name":"qmp_capabilities"},{"name":\ +"human-monitor-command"},{"name":"query-version"},\ +{"name":"query-commands"},{"name":"query-chardev"},\ +{"name":"query-block"},{"name":"query-blockstats"},\ +{"name":"query-cpus"},{"name":"query-pci"},{"name":\ +"query-kvm"},{"name":"query-status"},{"name":\ +"query-mice"},{"name":"query-vnc"},{"name":\ +"query-spice"},{"name":"query-name"},{"name":\ +"query-uuid"},{"name":"query-migrate"},{"name":\ +"query-balloon"}],"id":"libvirt-2"} diff --git a/tests/virjsontest.c b/tests/virjsontest.c index 1d9fc6ccb6..48cf5df5a9 100644 --- a/tests/virjsontest.c +++ b/tests/virjsontest.c @@ -23,9 +23,12 @@ testJSONFromFile(const void *data) VIR_AUTOPTR(virJSONValue) injson = NULL; VIR_AUTOFREE(char *) infile = NULL; VIR_AUTOFREE(char *) indata = NULL; + VIR_AUTOFREE(char *) outfile = NULL; VIR_AUTOFREE(char *) actual = NULL; if (virAsprintf(&infile, "%s/virjsondata/parse-%s-in.json", + abs_srcdir, info->name) < 0 || + virAsprintf(&outfile, "%s/virjsondata/parse-%s-out.json", abs_srcdir, info->name) < 0) return -1; @@ -52,10 +55,8 @@ testJSONFromFile(const void *data) if (!(actual = virJSONValueToString(injson, false))) return -1; - if (STRNEQ(info->expect, actual)) { - virTestDifference(stderr, info->expect, actual); + if (virTestCompareToFile(actual, outfile) < 0) return -1; - } return 0; } @@ -544,47 +545,14 @@ mymain(void) #define DO_TEST_PARSE_FAIL(name, doc) \ DO_TEST_FULL(name, FromString, doc, NULL, false) -#define DO_TEST_PARSE_FILE(name, expect) \ - DO_TEST_FULL(name, FromFile, NULL, expect, true) - - - DO_TEST_PARSE_FILE("Simple", - "{\"return\":{},\"id\":\"libvirt-1\"}"); - DO_TEST_PARSE_FILE("NotSoSimple", - "{\"QMP\":{\"version\":{\"qemu\":" - "{\"micro\":91,\"minor\":13,\"major\":0}," - "\"package\":\" (qemu-kvm-devel)\"},\"capabilities\":[]}}"); - - DO_TEST_PARSE_FILE("Harder", - "{\"return\":[{\"filename\":" - "\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\"," - "\"label\":\"charmonitor\"},{\"filename\":\"pty:/dev/pts/158\"," - "\"label\":\"charserial0\"}],\"id\":\"libvirt-3\"}"); - - DO_TEST_PARSE_FILE("VeryHard", "{\"return\":[{\"name\":\"quit\"},{\"name\":" - "\"eject\"},{\"name\":\"change\"},{\"name\":\"screendump\"}," - "{\"name\":\"stop\"},{\"name\":\"cont\"},{\"name\":" - "\"system_reset\"},{\"name\":\"system_powerdown\"}," - "{\"name\":\"device_add\"},{\"name\":\"device_del\"}," - "{\"name\":\"cpu\"},{\"name\":\"memsave\"},{\"name\":" - "\"pmemsave\"},{\"name\":\"migrate\"},{\"name\":" - "\"migrate_cancel\"},{\"name\":\"migrate_set_speed\"}," - "{\"name\":\"client_migrate_info\"},{\"name\":" - "\"migrate_set_downtime\"},{\"name\":\"netdev_add\"}," - "{\"name\":\"netdev_del\"},{\"name\":\"block_resize\"}," - "{\"name\":\"balloon\"},{\"name\":\"set_link\"},{\"name\":" - "\"getfd\"},{\"name\":\"closefd\"},{\"name\":\"block_passwd\"}," - "{\"name\":\"set_password\"},{\"name\":\"expire_password\"}," - "{\"name\":\"qmp_capabilities\"},{\"name\":" - "\"human-monitor-command\"},{\"name\":\"query-version\"}," - "{\"name\":\"query-commands\"},{\"name\":\"query-chardev\"}," - "{\"name\":\"query-block\"},{\"name\":\"query-blockstats\"}," - "{\"name\":\"query-cpus\"},{\"name\":\"query-pci\"},{\"name\":" - "\"query-kvm\"},{\"name\":\"query-status\"},{\"name\":" - "\"query-mice\"},{\"name\":\"query-vnc\"},{\"name\":" - "\"query-spice\"},{\"name\":\"query-name\"},{\"name\":" - "\"query-uuid\"},{\"name\":\"query-migrate\"},{\"name\":" - "\"query-balloon\"}],\"id\":\"libvirt-2\"}"); +#define DO_TEST_PARSE_FILE(name) \ + DO_TEST_FULL(name, FromFile, NULL, NULL, true) + + + DO_TEST_PARSE_FILE("Simple"); + DO_TEST_PARSE_FILE("NotSoSimple"); + DO_TEST_PARSE_FILE("Harder"); + DO_TEST_PARSE_FILE("VeryHard"); DO_TEST_FULL("add and remove", AddRemove, "{\"name\": \"sample\", \"value\": true}", -- 2.19.2

On Tue, 2019-02-12 at 16:57 +0100, Ján Tomko wrote: [...]
+{"return":[{"filename":\ +"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server",\ +"label":"charmonitor"},{"filename":"pty:/dev/pts/158",\ +"label":"charserial0"}],"id":"libvirt-3"}
Same questions as the previous patch when it comes to pretty printing: it looks like... [...]
@@ -52,10 +55,8 @@ testJSONFromFile(const void *data) if (!(actual = virJSONValueToString(injson, false))) return -1;
... changing the second argument of virJSONValueToString() to true is all that's needed to obtain much more legible output files. Any reason why we shouldn't do that? Everything else looks good. -- Andrea Bolognani / Red Hat / Virtualization

Instead of using JSON in C strings, put it in separate files for easier manipulation. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tests/virjsondata/add-remove-failure-in.json | 1 + tests/virjsondata/add-remove-success-in.json | 1 + tests/virjsondata/add-remove-success-out.json | 1 + tests/virjsontest.c | 38 ++++++++++++------- 4 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 tests/virjsondata/add-remove-failure-in.json create mode 100644 tests/virjsondata/add-remove-success-in.json create mode 100644 tests/virjsondata/add-remove-success-out.json diff --git a/tests/virjsondata/add-remove-failure-in.json b/tests/virjsondata/add-remove-failure-in.json new file mode 100644 index 0000000000..dd264421e9 --- /dev/null +++ b/tests/virjsondata/add-remove-failure-in.json @@ -0,0 +1 @@ +[ 1 ] diff --git a/tests/virjsondata/add-remove-success-in.json b/tests/virjsondata/add-remove-success-in.json new file mode 100644 index 0000000000..b8edd30963 --- /dev/null +++ b/tests/virjsondata/add-remove-success-in.json @@ -0,0 +1 @@ +{"name": "sample", "value": true} diff --git a/tests/virjsondata/add-remove-success-out.json b/tests/virjsondata/add-remove-success-out.json new file mode 100644 index 0000000000..9f1e44637c --- /dev/null +++ b/tests/virjsondata/add-remove-success-out.json @@ -0,0 +1 @@ +{"value":true,"newname":"foo"} diff --git a/tests/virjsontest.c b/tests/virjsontest.c index 48cf5df5a9..5bac08d039 100644 --- a/tests/virjsontest.c +++ b/tests/virjsontest.c @@ -114,12 +114,24 @@ static int testJSONAddRemove(const void *data) { const struct testInfo *info = data; - virJSONValuePtr json; + virJSONValuePtr json = NULL; virJSONValuePtr name = NULL; - char *result = NULL; + char *infile = NULL; + char *indata = NULL; + char *outfile = NULL; + char *actual = NULL; int ret = -1; - json = virJSONValueFromString(info->doc); + if (virAsprintf(&infile, "%s/virjsondata/add-remove-%s-in.json", + abs_srcdir, info->name) < 0 || + virAsprintf(&outfile, "%s/virjsondata/add-remove-%s-out.json", + abs_srcdir, info->name) < 0) + goto cleanup; + + if (virTestLoadFile(infile, &indata) < 0) + goto cleanup; + + json = virJSONValueFromString(indata); if (!json) { VIR_TEST_VERBOSE("Fail to parse %s\n", info->name); ret = -1; @@ -159,20 +171,22 @@ testJSONAddRemove(const void *data) VIR_TEST_VERBOSE("%s", "unexpected failure adding new key\n"); goto cleanup; } - if (!(result = virJSONValueToString(json, false))) { + if (!(actual = virJSONValueToString(json, false))) { VIR_TEST_VERBOSE("%s", "failed to stringize result\n"); goto cleanup; } - if (STRNEQ(info->expect, result)) { - virTestDifference(stderr, info->expect, result); + if (virTestCompareToFile(actual, outfile) < 0) goto cleanup; - } + ret = 0; cleanup: virJSONValueFree(json); virJSONValueFree(name); - VIR_FREE(result); + VIR_FREE(infile); + VIR_FREE(indata); + VIR_FREE(outfile); + VIR_FREE(actual); return ret; } @@ -554,12 +568,8 @@ mymain(void) DO_TEST_PARSE_FILE("Harder"); DO_TEST_PARSE_FILE("VeryHard"); - DO_TEST_FULL("add and remove", AddRemove, - "{\"name\": \"sample\", \"value\": true}", - "{\"value\":true,\"newname\":\"foo\"}", - true); - DO_TEST_FULL("add and remove", AddRemove, - "[ 1 ]", NULL, false); + DO_TEST_FULL("success", AddRemove, NULL, NULL, true); + DO_TEST_FULL("failure", AddRemove, NULL, NULL, false); DO_TEST_FULL("copy and free", Copy, "{\"return\": [{\"name\": \"quit\"}, {\"name\": \"eject\"}," -- 2.19.2

On Tue, 2019-02-12 at 16:57 +0100, Ján Tomko wrote: [...]
+++ b/tests/virjsontest.c @@ -114,12 +114,24 @@ static int testJSONAddRemove(const void *data) { const struct testInfo *info = data; - virJSONValuePtr json; + virJSONValuePtr json = NULL; virJSONValuePtr name = NULL; - char *result = NULL; + char *infile = NULL; + char *indata = NULL; + char *outfile = NULL; + char *actual = NULL;
Feel free to convert this function and the rest of the file to VIR_AUTOFREE() in a follow-up series O:-)
@@ -159,20 +171,22 @@ testJSONAddRemove(const void *data) VIR_TEST_VERBOSE("%s", "unexpected failure adding new key\n"); goto cleanup; } - if (!(result = virJSONValueToString(json, false))) { + if (!(actual = virJSONValueToString(json, false))) {
The amount of data is much smaller in this case, so whether or not it's pretty printed doesn't make a lot of difference. I'd still pretty print everything for consistency and because it just looks much better, but it's okay to leave it as is too. Reviewed-by: Andrea Bolognani <abologna@redhat.com> -- Andrea Bolognani / Red Hat / Virtualization
participants (2)
-
Andrea Bolognani
-
Ján Tomko