Quellcode durchsuchen

support/testing: new jq runtime test

Signed-off-by: Julien Olivain <ju.o@free.fr>
Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
Julien Olivain vor 1 Jahr
Ursprung
Commit
feab8290a4

+ 2 - 0
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

+ 62 - 0
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")

+ 1 - 0
support/testing/tests/package/test_jq/rootfs-overlay/root/broken.json

@@ -0,0 +1 @@
+[ This is is NOT a JSON file! }

+ 14 - 0
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]
+    }
+}

+ 22 - 0
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"
+  }
+]

+ 1 - 0
support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-3.json

@@ -0,0 +1 @@
+"Hello world!"

+ 1 - 0
support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-4.json

@@ -0,0 +1 @@
+42

+ 1 - 0
support/testing/tests/package/test_jq/rootfs-overlay/root/ex13-5.json

@@ -0,0 +1 @@
+true