diff --git a/DEVELOPERS b/DEVELOPERS index c103bdadf8..4b8d195f9f 100644 --- a/DEVELOPERS +++ b/DEVELOPERS @@ -1790,6 +1790,8 @@ F: support/testing/tests/package/test_gzip.py F: support/testing/tests/package/test_highway.py F: support/testing/tests/package/test_hwloc.py F: support/testing/tests/package/test_iperf3.py +F: support/testing/tests/package/test_jq.py +F: support/testing/tests/package/test_jq/ F: support/testing/tests/package/test_kexec.py F: support/testing/tests/package/test_kexec/ F: support/testing/tests/package/test_kmscube.py diff --git a/support/testing/tests/package/test_jq.py b/support/testing/tests/package/test_jq.py new file mode 100644 index 0000000000..67420abb6b --- /dev/null +++ b/support/testing/tests/package/test_jq.py @@ -0,0 +1,62 @@ +import json +import os + +import infra.basetest + + +class TestJq(infra.basetest.BRTest): + rootfs_overlay = \ + infra.filepath("tests/package/test_jq/rootfs-overlay") + config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \ + f""" + BR2_PACKAGE_JQ=y + BR2_ROOTFS_OVERLAY="{rootfs_overlay}" + BR2_TARGET_ROOTFS_CPIO=y + # BR2_TARGET_ROOTFS_TAR is not set + """ + + def test_run(self): + cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio") + self.emulator.boot(arch="armv5", + kernel="builtin", + options=["-initrd", cpio_file]) + self.emulator.login() + + # Check the program can execute. + self.assertRunOk("jq --version") + + # Run jq on examples extracted from JSON RFC: + # https://www.rfc-editor.org/rfc/rfc8259.txt + for i in range(1, 6): + fname = f"ex13-{i}.json" + cmd = f"jq -M '.' {fname}" + self.assertRunOk(cmd) + + # Check the execution fails on a non JSON file. + cmd = "jq -M '.' broken.json" + _, ret = self.emulator.run(cmd) + self.assertNotEqual(ret, 0) + + # Check an execution of a simple query. Note that output is a + # JSON (quoted) string. + cmd = "jq -M '.[1].City' ex13-2.json" + out, ret = self.emulator.run(cmd) + self.assertEqual(ret, 0) + self.assertEqual(out[0], '"SUNNYVALE"') + + # Run the same query with the -r option, to output raw text + # (i.e. strings without quotes). + cmd = "jq -r -M '.[1].City' ex13-2.json" + out, ret = self.emulator.run(cmd) + self.assertEqual(ret, 0) + self.assertEqual(out[0], "SUNNYVALE") + + # Print the ex13-2.json file as compact JSON (with option -c). + cmd = "jq -c -M '.' ex13-2.json" + out, ret = self.emulator.run(cmd) + self.assertEqual(ret, 0) + # We reload this compact string using the Python json parser, + # to test interoperability. We check the same element as in + # previous queries in the Python object. + json_data = json.loads(out[0]) + self.assertEqual(json_data[1]["City"], "SUNNYVALE") diff --git a/support/testing/tests/package/test_jq/rootfs-overlay/root/broken.json b/support/testing/tests/package/test_jq/rootfs-overlay/root/broken.json new file mode 100644 index 0000000000..fc2bf94565 --- /dev/null +++ b/support/testing/tests/package/test_jq/rootfs-overlay/root/broken.json @@ -0,0 +1 @@ +[ This is is NOT a JSON file! } diff --git a/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-1.json b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-1.json new file mode 100644 index 0000000000..52b7b6493f --- /dev/null +++ b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-1.json @@ -0,0 +1,14 @@ +{ + "Image": { + "Width": 800, + "Height": 600, + "Title": "View from 15th Floor", + "Thumbnail": { + "Url": "http://www.example.com/image/481989943", + "Height": 125, + "Width": 100 + }, + "Animated" : false, + "IDs": [116, 943, 234, 38793] + } +} diff --git a/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-2.json b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-2.json new file mode 100644 index 0000000000..b4545b28c8 --- /dev/null +++ b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-2.json @@ -0,0 +1,22 @@ +[ + { + "precision": "zip", + "Latitude": 37.7668, + "Longitude": -122.3959, + "Address": "", + "City": "SAN FRANCISCO", + "State": "CA", + "Zip": "94107", + "Country": "US" + }, + { + "precision": "zip", + "Latitude": 37.371991, + "Longitude": -122.026020, + "Address": "", + "City": "SUNNYVALE", + "State": "CA", + "Zip": "94085", + "Country": "US" + } +] diff --git a/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-3.json b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-3.json new file mode 100644 index 0000000000..6ccebb9abe --- /dev/null +++ b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-3.json @@ -0,0 +1 @@ +"Hello world!" diff --git a/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-4.json b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-4.json new file mode 100644 index 0000000000..d81cc0710e --- /dev/null +++ b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-4.json @@ -0,0 +1 @@ +42 diff --git a/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-5.json b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-5.json new file mode 100644 index 0000000000..27ba77ddaf --- /dev/null +++ b/support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-5.json @@ -0,0 +1 @@ +true