Buildroot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [Buildroot] [PATCH 1/1] support/testing: new jq runtime test
@ 2024-02-04 22:19 Julien Olivain
  2024-02-06 17:34 ` Peter Korsgaard
  0 siblings, 1 reply; 2+ messages in thread
From: Julien Olivain @ 2024-02-04 22:19 UTC (permalink / raw)
  To: buildroot; +Cc: Julien Olivain

Signed-off-by: Julien Olivain <ju.o@free.fr>
---
 DEVELOPERS                                    |  2 +
 support/testing/tests/package/test_jq.py      | 62 +++++++++++++++++++
 .../test_jq/rootfs-overlay/root/broken.json   |  1 +
 .../test_jq/rootfs-overlay/root/ex13-1.json   | 14 +++++
 .../test_jq/rootfs-overlay/root/ex13-2.json   | 22 +++++++
 .../test_jq/rootfs-overlay/root/ex13-3.json   |  1 +
 .../test_jq/rootfs-overlay/root/ex13-4.json   |  1 +
 .../test_jq/rootfs-overlay/root/ex13-5.json   |  1 +
 8 files changed, 104 insertions(+)
 create mode 100644 support/testing/tests/package/test_jq.py
 create mode 100644 support/testing/tests/package/test_jq/rootfs-overlay/root/broken.json
 create mode 100644 support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-1.json
 create mode 100644 support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-2.json
 create mode 100644 support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-3.json
 create mode 100644 support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-4.json
 create mode 100644 support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-5.json

diff --git a/DEVELOPERS b/DEVELOPERS
index 02b7516a92..91d2c6d869 100644
--- a/DEVELOPERS
+++ b/DEVELOPERS
@@ -1778,6 +1778,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
-- 
2.43.0

_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-06 17:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-04 22:19 [Buildroot] [PATCH 1/1] support/testing: new jq runtime test Julien Olivain
2024-02-06 17:34 ` Peter Korsgaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox