From nobody Sun Feb 8 17:47:42 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1498567879150658.0742699641681; Tue, 27 Jun 2017 05:51:19 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2BEC6C04B92C; Tue, 27 Jun 2017 12:51:16 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D7A2387FAF; Tue, 27 Jun 2017 12:51:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 88D491853E33; Tue, 27 Jun 2017 12:51:15 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v5RCl6XK023817 for ; Tue, 27 Jun 2017 08:47:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3C8608A765; Tue, 27 Jun 2017 12:47:06 +0000 (UTC) Received: from andariel.redhat.com (ovpn-204-24.brq.redhat.com [10.40.204.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 115A28A784; Tue, 27 Jun 2017 12:47:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2BEC6C04B92C Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2BEC6C04B92C From: Peter Krempa To: libvir-list@redhat.com Date: Tue, 27 Jun 2017 14:46:46 +0200 Message-Id: <6fd024b6ffba75503bb8853d659fa83a5fc142de.1498567512.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 5/9] tests: json: Add test for the deflattening function X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 27 Jun 2017 12:51:16 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a few test cases to verify that the old behaviour does not break and that new one behaves sanely. Reviewed-by: John Ferlan --- tests/virjsondata/deflatten-basic-file-in.json | 8 +++ tests/virjsondata/deflatten-basic-file-out.json | 10 +++ tests/virjsondata/deflatten-basic-generic-in.json | 14 ++++ .../deflatten-concat-double-key-in.json | 7 ++ .../deflatten-concat-double-key-out.json | 9 +++ tests/virjsondata/deflatten-concat-in.json | 5 ++ tests/virjsondata/deflatten-concat-out.json | 9 +++ tests/virjsondata/deflatten-deep-file-in.json | 9 +++ tests/virjsondata/deflatten-deep-file-out.json | 11 ++++ tests/virjsondata/deflatten-deep-generic-in.json | 9 +++ tests/virjsondata/deflatten-double-key-in.json | 4 ++ tests/virjsondata/deflatten-double-key-out.json | 6 ++ tests/virjsondata/deflatten-nested-in.json | 16 +++++ tests/virjsondata/deflatten-nested-out.json | 18 ++++++ tests/virjsondata/deflatten-unflattened-in.json | 12 ++++ tests/virjsondata/deflatten-unflattened-out.json | 14 ++++ tests/virjsontest.c | 74 ++++++++++++++++++= ++++ 17 files changed, 235 insertions(+) create mode 100644 tests/virjsondata/deflatten-basic-file-in.json create mode 100644 tests/virjsondata/deflatten-basic-file-out.json create mode 100644 tests/virjsondata/deflatten-basic-generic-in.json create mode 100644 tests/virjsondata/deflatten-concat-double-key-in.json create mode 100644 tests/virjsondata/deflatten-concat-double-key-out.json create mode 100644 tests/virjsondata/deflatten-concat-in.json create mode 100644 tests/virjsondata/deflatten-concat-out.json create mode 100644 tests/virjsondata/deflatten-deep-file-in.json create mode 100644 tests/virjsondata/deflatten-deep-file-out.json create mode 100644 tests/virjsondata/deflatten-deep-generic-in.json create mode 100644 tests/virjsondata/deflatten-double-key-in.json create mode 100644 tests/virjsondata/deflatten-double-key-out.json create mode 100644 tests/virjsondata/deflatten-nested-in.json create mode 100644 tests/virjsondata/deflatten-nested-out.json create mode 100644 tests/virjsondata/deflatten-unflattened-in.json create mode 100644 tests/virjsondata/deflatten-unflattened-out.json diff --git a/tests/virjsondata/deflatten-basic-file-in.json b/tests/virjson= data/deflatten-basic-file-in.json new file mode 100644 index 000000000..54f8bcd88 --- /dev/null +++ b/tests/virjsondata/deflatten-basic-file-in.json @@ -0,0 +1,8 @@ +{ + "file.int": 1, + "file.string": "string", + "file.object": { + "data":"value", + "foo":"bar" + } +} diff --git a/tests/virjsondata/deflatten-basic-file-out.json b/tests/virjso= ndata/deflatten-basic-file-out.json new file mode 100644 index 000000000..1a48eda54 --- /dev/null +++ b/tests/virjsondata/deflatten-basic-file-out.json @@ -0,0 +1,10 @@ +{ + "file": { + "int": 1, + "string": "string", + "object": { + "data": "value", + "foo": "bar" + } + } +} diff --git a/tests/virjsondata/deflatten-basic-generic-in.json b/tests/virj= sondata/deflatten-basic-generic-in.json new file mode 100644 index 000000000..08304287d --- /dev/null +++ b/tests/virjsondata/deflatten-basic-generic-in.json @@ -0,0 +1,14 @@ +{ + "foo.int": 1, + "bar.int": 1, + "foo.string": "string", + "foo.object": { + "data":"value", + "foo":"bar" + }, + "blurb.string": "string", + "blurb.object": { + "data":"value", + "foo":"bar" + } +} diff --git a/tests/virjsondata/deflatten-concat-double-key-in.json b/tests/= virjsondata/deflatten-concat-double-key-in.json new file mode 100644 index 000000000..5bb020ca9 --- /dev/null +++ b/tests/virjsondata/deflatten-concat-double-key-in.json @@ -0,0 +1,7 @@ +{ + "file.nest":{ + "into":"is already here" + }, + "file.nest.into":2, + "file.nest.there":"too" +} diff --git a/tests/virjsondata/deflatten-concat-double-key-out.json b/tests= /virjsondata/deflatten-concat-double-key-out.json new file mode 100644 index 000000000..5624ef123 --- /dev/null +++ b/tests/virjsondata/deflatten-concat-double-key-out.json @@ -0,0 +1,9 @@ +{ + "file": { + "nest": { + "into": "is already here" + }, + "nest.into": 2, + "nest.there": "too" + } +} diff --git a/tests/virjsondata/deflatten-concat-in.json b/tests/virjsondata= /deflatten-concat-in.json new file mode 100644 index 000000000..91a824375 --- /dev/null +++ b/tests/virjsondata/deflatten-concat-in.json @@ -0,0 +1,5 @@ +{ + "file.nest":{}, + "file.nest.into":2, + "file.nest.there":"too" +} diff --git a/tests/virjsondata/deflatten-concat-out.json b/tests/virjsondat= a/deflatten-concat-out.json new file mode 100644 index 000000000..539d2cc30 --- /dev/null +++ b/tests/virjsondata/deflatten-concat-out.json @@ -0,0 +1,9 @@ +{ + "file": { + "nest": { + + }, + "nest.into": 2, + "nest.there": "too" + } +} diff --git a/tests/virjsondata/deflatten-deep-file-in.json b/tests/virjsond= ata/deflatten-deep-file-in.json new file mode 100644 index 000000000..f1b1586c0 --- /dev/null +++ b/tests/virjsondata/deflatten-deep-file-in.json @@ -0,0 +1,9 @@ +{ + "file.double.nest1":"some", + "file.double.nest2":"more", + "file.double.nest3": { + "even":"objects" + }, + "file.very.deeply.nested.object.chains.nest1":"some", + "file.very.deeply.nested.object.chains.nest2":"stuff" +} diff --git a/tests/virjsondata/deflatten-deep-file-out.json b/tests/virjson= data/deflatten-deep-file-out.json new file mode 100644 index 000000000..a5910c9f7 --- /dev/null +++ b/tests/virjsondata/deflatten-deep-file-out.json @@ -0,0 +1,11 @@ +{ + "file": { + "double.nest1": "some", + "double.nest2": "more", + "double.nest3": { + "even": "objects" + }, + "very.deeply.nested.object.chains.nest1": "some", + "very.deeply.nested.object.chains.nest2": "stuff" + } +} diff --git a/tests/virjsondata/deflatten-deep-generic-in.json b/tests/virjs= ondata/deflatten-deep-generic-in.json new file mode 100644 index 000000000..6a4edfb9b --- /dev/null +++ b/tests/virjsondata/deflatten-deep-generic-in.json @@ -0,0 +1,9 @@ +{ + "foo.double.nest1":"some", + "foo.double.nest2":"more", + "bar.double.nest3": { + "even":"objects" + }, + "foo.very.deeply.nested.object.chains.nest1":"some", + "foo.very.deeply.nested.object.chains.nest2":"stuff" +} diff --git a/tests/virjsondata/deflatten-double-key-in.json b/tests/virjson= data/deflatten-double-key-in.json new file mode 100644 index 000000000..7bca2921b --- /dev/null +++ b/tests/virjsondata/deflatten-double-key-in.json @@ -0,0 +1,4 @@ +{ + "file.nest":1, + "file.nest.into":2 +} diff --git a/tests/virjsondata/deflatten-double-key-out.json b/tests/virjso= ndata/deflatten-double-key-out.json new file mode 100644 index 000000000..ca6766e5b --- /dev/null +++ b/tests/virjsondata/deflatten-double-key-out.json @@ -0,0 +1,6 @@ +{ + "file": { + "nest": 1, + "nest.into": 2 + } +} diff --git a/tests/virjsondata/deflatten-nested-in.json b/tests/virjsondata= /deflatten-nested-in.json new file mode 100644 index 000000000..38e69683e --- /dev/null +++ b/tests/virjsondata/deflatten-nested-in.json @@ -0,0 +1,16 @@ +{ + "file.nest": { + "even.objects":"can", + "even.contain":"some", + "even.more":{ + "deeply.nested":"objects", + "deeply.needing":"deflattening", + "deeply.some.even":"more", + "deeply.some.than":{ + "others.thought.was":"even", + "others.thought.completely":"necessary" + }, + "perhaps":"flat value" + } + } +} diff --git a/tests/virjsondata/deflatten-nested-out.json b/tests/virjsondat= a/deflatten-nested-out.json new file mode 100644 index 000000000..acdcd1fc8 --- /dev/null +++ b/tests/virjsondata/deflatten-nested-out.json @@ -0,0 +1,18 @@ +{ + "file": { + "nest": { + "even.objects": "can", + "even.contain": "some", + "even.more": { + "deeply.nested": "objects", + "deeply.needing": "deflattening", + "deeply.some.even": "more", + "deeply.some.than": { + "others.thought.was": "even", + "others.thought.completely": "necessary" + }, + "perhaps": "flat value" + } + } + } +} diff --git a/tests/virjsondata/deflatten-unflattened-in.json b/tests/virjso= ndata/deflatten-unflattened-in.json new file mode 100644 index 000000000..e8a68fe17 --- /dev/null +++ b/tests/virjsondata/deflatten-unflattened-in.json @@ -0,0 +1,12 @@ +{ + "file.blah": { + "any":"possible", + "combination":{ + "of":"json", + "nested":{ + "objects":"should", + "be":"possible" + } + } + } +} diff --git a/tests/virjsondata/deflatten-unflattened-out.json b/tests/virjs= ondata/deflatten-unflattened-out.json new file mode 100644 index 000000000..c02cb4864 --- /dev/null +++ b/tests/virjsondata/deflatten-unflattened-out.json @@ -0,0 +1,14 @@ +{ + "file": { + "blah": { + "any": "possible", + "combination": { + "of": "json", + "nested": { + "objects": "should", + "be": "possible" + } + } + } + } +} diff --git a/tests/virjsontest.c b/tests/virjsontest.c index b67f68ccb..d69b22bf3 100644 --- a/tests/virjsontest.c +++ b/tests/virjsontest.c @@ -9,6 +9,8 @@ #include "virjson.h" #include "testutils.h" +#define VIR_FROM_THIS VIR_FROM_NONE + struct testInfo { const char *doc; const char *expect; @@ -312,6 +314,65 @@ testJSONCopy(const void *data) static int +testJSONDeflatten(const void *data) +{ + const struct testInfo *info =3D data; + virJSONValuePtr injson =3D NULL; + virJSONValuePtr deflattened =3D NULL; + char *infile =3D NULL; + char *indata =3D NULL; + char *outfile =3D NULL; + char *actual =3D NULL; + int ret =3D -1; + + if (virAsprintf(&infile, "%s/virjsondata/deflatten-%s-in.json", + abs_srcdir, info->doc) < 0 || + virAsprintf(&outfile, "%s/virjsondata/deflatten-%s-out.json", + abs_srcdir, info->doc) < 0) + goto cleanup; + + if (virTestLoadFile(infile, &indata) < 0) + goto cleanup; + + if (!(injson =3D virJSONValueFromString(indata))) + goto cleanup; + + if ((deflattened =3D virJSONValueObjectDeflatten(injson))) { + if (!info->pass) { + if (virTestGetVerbose()) + fprintf(stderr, "deflattening should have failed\n"); + + goto cleanup; + } + } else { + if (!info->pass) + ret =3D 0; + + goto cleanup; + } + + if (!(actual =3D virJSONValueToString(deflattened, true))) + goto cleanup; + + if (virTestCompareToFile(actual, outfile) < 0) + goto cleanup; + + ret =3D 0; + + cleanup: + virJSONValueFree(injson); + virJSONValueFree(deflattened); + VIR_FREE(infile); + VIR_FREE(outfile); + VIR_FREE(outfile); + VIR_FREE(actual); + + return ret; + +} + + +static int mymain(void) { int ret =3D 0; @@ -448,6 +509,19 @@ mymain(void) "{ \"a\": {}, \"b\": 1, \"c\": \"str\", \"d\": [] }", NULL, true); +#define DO_TEST_DEFLATTEN(name, pass) \ + DO_TEST_FULL(name, Deflatten, name, NULL, pass) + + DO_TEST_DEFLATTEN("unflattened", true); + DO_TEST_DEFLATTEN("basic-file", true); + DO_TEST_DEFLATTEN("basic-generic", false); + DO_TEST_DEFLATTEN("deep-file", true); + DO_TEST_DEFLATTEN("deep-generic", false); + DO_TEST_DEFLATTEN("nested", true); + DO_TEST_DEFLATTEN("double-key", true); + DO_TEST_DEFLATTEN("concat", true); + DO_TEST_DEFLATTEN("concat-double-key", true); + return (ret =3D=3D 0) ? EXIT_SUCCESS : EXIT_FAILURE; } --=20 2.12.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list