support/testing: factor out run_cmd_on_host
authorRicardo Martincoski <ricardo.martincoski@gmail.com>
Thu, 8 Aug 2019 23:10:13 +0000 (20:10 -0300)
committerThomas Petazzoni <thomas.petazzoni@bootlin.com>
Sun, 11 Aug 2019 20:11:58 +0000 (22:11 +0200)
Currently many test cases call subprocess.check_output on their own.
Factor out that code to an infra method so the call get standardized.

This will be handful when switching the test infra to use Python 3.

Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
support/testing/infra/__init__.py
support/testing/tests/core/test_hardening.py
support/testing/tests/fs/test_ext.py
support/testing/tests/fs/test_f2fs.py
support/testing/tests/fs/test_jffs2.py
support/testing/tests/fs/test_squashfs.py
support/testing/tests/fs/test_ubi.py

index 1d4d18bbe98f1f48758877db1fa205bbac0e3ae6..43045d01732a446e5814d85001a6c97e159813c1 100644 (file)
@@ -55,6 +55,15 @@ def download(dldir, filename):
     return finalpath
 
 
+def run_cmd_on_host(builddir, cmd):
+    """Call subprocess.check_output and return the text output."""
+    out = subprocess.check_output(cmd,
+                                  stderr=open(os.devnull, "w"),
+                                  cwd=builddir,
+                                  env={"LANG": "C"})
+    return out
+
+
 def get_elf_arch_tag(builddir, prefix, fpath, tag):
     """
     Runs the cross readelf on 'fpath', then extracts the value of tag 'tag'.
@@ -66,7 +75,7 @@ def get_elf_arch_tag(builddir, prefix, fpath, tag):
     """
     cmd = ["host/bin/{}-readelf".format(prefix),
            "-A", os.path.join("target", fpath)]
-    out = subprocess.check_output(cmd, cwd=builddir, env={"LANG": "C"})
+    out = run_cmd_on_host(builddir, cmd)
     regexp = re.compile("^  {}: (.*)$".format(tag))
     for line in out.splitlines():
         m = regexp.match(line)
@@ -93,7 +102,7 @@ def get_elf_prog_interpreter(builddir, prefix, fpath):
     """
     cmd = ["host/bin/{}-readelf".format(prefix),
            "-l", os.path.join("target", fpath)]
-    out = subprocess.check_output(cmd, cwd=builddir, env={"LANG": "C"})
+    out = run_cmd_on_host(builddir, cmd)
     regexp = re.compile("^ *\[Requesting program interpreter: (.*)\]$")
     for line in out.splitlines():
         m = regexp.match(line)
index 4d19b9f96d93261bbd729819a2e1cb7ff64a28a5..edfb3315db1266d42b173fa6da36239a4575f5de 100644 (file)
@@ -1,5 +1,4 @@
 import os
-import subprocess
 import json
 
 import infra.basetest
@@ -30,10 +29,7 @@ class TestHardeningBase(infra.basetest.BRTest):
                "--file={}".format(filepath)]
         # Checksec is being used for elf file analysis only.  There are no
         # assumptions of target/run-time checks as part of this testing.
-        ret = subprocess.check_output(cmd,
-                                      stderr=open(os.devnull, "w"),
-                                      cwd=self.builddir,
-                                      env={"LANG": "C"})
+        ret = infra.run_cmd_on_host(self.builddir, cmd)
         return json.loads(ret)
 
 
index f5f9e9fdf178552606838fc3f0233ee945f71c43..16b308cf6d9f54b17108d3041cb489374b42e957 100644 (file)
@@ -1,5 +1,4 @@
 import os
-import subprocess
 
 import infra.basetest
 
@@ -15,10 +14,7 @@ CHECK_FS_TYPE_CMD = "mount | grep '/dev/root on / type {}'"
 
 def dumpe2fs_run(builddir, image):
     cmd = ["host/sbin/dumpe2fs", os.path.join("images", image)]
-    ret = subprocess.check_output(cmd,
-                                  stderr=open(os.devnull, "w"),
-                                  cwd=builddir,
-                                  env={"LANG": "C"})
+    ret = infra.run_cmd_on_host(builddir, cmd)
     return ret.strip().splitlines()
 
 
index 819c619a3647aa7dbaf7e80045be0bcadc473ba0..950d4cf25b0cfe521c08e9450b3a14637c2ab73a 100644 (file)
@@ -1,5 +1,4 @@
 import os
-import subprocess
 
 import infra.basetest
 
@@ -29,9 +28,7 @@ class TestF2FS(infra.basetest.BRTest):
 
     def test_run(self):
         img = os.path.join(self.builddir, "images", "rootfs.f2fs")
-        out = subprocess.check_output(["host/sbin/dump.f2fs", img],
-                                      cwd=self.builddir,
-                                      env={"LANG": "C"})
+        out = infra.run_cmd_on_host(self.builddir, ["host/sbin/dump.f2fs", img])
         out = out.splitlines()
         prop = dumpf2fs_getprop(out, "Info: total sectors")
         self.assertEqual(prop, "262144 (128 MB)")
index 2ff5099180b0d24bf23219abbdfb08bb76902145..f5066ff01f583fb4b0dd9b4f0574acabe8363e7f 100644 (file)
@@ -1,5 +1,4 @@
 import os
-import subprocess
 
 import infra.basetest
 
@@ -30,9 +29,8 @@ class TestJffs2(infra.basetest.BRTest):
 
     def test_run(self):
         img = os.path.join(self.builddir, "images", "rootfs.jffs2")
-        out = subprocess.check_output(["host/sbin/jffs2dump", "-c", img],
-                                      cwd=self.builddir,
-                                      env={"LANG": "C"})
+        cmd = ["host/sbin/jffs2dump", "-c", img]
+        out = infra.run_cmd_on_host(self.builddir, cmd)
         out = out.splitlines()
         self.assertTrue(jffs2dump_find_file(out, "busybox"))
 
index 066c054342c25a1bdb666fd465f9c2d9b24b85f5..234f4944be0e6e85996fcfe60365ea6df28b1fae 100644 (file)
@@ -15,9 +15,7 @@ class TestSquashfs(infra.basetest.BRTest):
 
     def test_run(self):
         unsquashfs_cmd = ["host/bin/unsquashfs", "-s", "images/rootfs.squashfs"]
-        out = subprocess.check_output(unsquashfs_cmd,
-                                      cwd=self.builddir,
-                                      env={"LANG": "C"})
+        out = infra.run_cmd_on_host(self.builddir, unsquashfs_cmd)
         out = out.splitlines()
         self.assertEqual(out[0],
                          "Found a valid SQUASHFS 4:0 superblock on images/rootfs.squashfs.")
index e024d417df20d04245535f04c6be2032e75a8741..7321f83da922919084c8f679533ef88f1646c51c 100644 (file)
@@ -21,9 +21,7 @@ class TestUbi(infra.basetest.BRTest):
     # To be investigated.
     def test_run(self):
         img = os.path.join(self.builddir, "images", "rootfs.ubi")
-        out = subprocess.check_output(["file", img],
-                                      cwd=self.builddir,
-                                      env={"LANG": "C"})
+        out = infra.run_cmd_on_host(self.builddir, ["file", img])
         out = out.splitlines()
         self.assertIn("UBI image, version 1", out[0])