Skip to contentSkip to conversation

binman: Check code-coverage requirements

PATCH In review Simon Glass 2 participants 10 messages Apr 10, 2025 – Apr 12, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover binman: Check code-coverage requirements

This series adds a cover-coverage check to CI for Binman. The iMX8 tests are still not completed, so a work-around is included for those.

A few fixes are included for some other problems.

Changes in v2:
- Add new patch to add coverage to the requirements.txt file
- Rebase to -master

Jiaxun Yang (1):
binman: Workaround lz4 cli padding in test cases

Simon Glass (7):
binman: Add coverage to requirements
binman: Exclude dist-packages and site-packages
binman: Drop GetRootSkipAtStart()
binman: fit: Drop unused code
binman: Drop algo check in CheckSetHashValue()
binman: Work around missing test coverage
CI: Run code-coverage test for Binman

 .azure-pipelines.yml                           |  5 ++++-
 .gitlab-ci.yml                                 |  4 +++-
 tools/binman/etype/fit.py                      |  2 --
 tools/binman/etype/section.py                  | 17 -----------------
 tools/binman/ftest.py                          |  7 +++++--
 tools/binman/main.py                           |  8 +++++++-
 tools/binman/requirements.txt                  |  1 +
 tools/binman/state.py                          |  3 +--
 .../binman/test/184_compress_section_size.dts  |  1 +
 tools/u_boot_pylib/test_util.py                | 18 ++++++++++++++++--
 10 files changed, 38 insertions(+), 28 deletions(-)
Signature · 4 lines
2.43.0

base-commit: 8a2cf6307a2ccc09c39dde486b6d9375b78c82c2
branch: bing2
#1 lore raw

PATCH v2 1/8 binman: Add coverage to requirements

We need the code-coverage package to run the coverage tests. Add this package.

---

Changes in v2:
- Add new patch to add coverage to the requirements.txt file

 tools/binman/requirements.txt | 1 +
 1 file changed, 1 insertion(+)
tools/binman/requirements.txt +1 -0
index f068ef75a30..7db72e888e3 100644
@@ -1,3 +1,4 @@
+coverage==7.8.0
 importlib_resources==6.5.2
 jsonschema==4.23.0
 pycryptodomex==3.21.0
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/8 binman: Exclude dist-packages and site-packages

In reply to Simon Glass

Newer versions of the python3-coverage tool require a directory separator before and after the directory name. Add this so that system package are not included in the coverage report.

---

(no changes since v1)

 tools/u_boot_pylib/test_util.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/u_boot_pylib/test_util.py +1 -1
index dd671965263..ed216c4fc4e 100644
@@ -56,7 +56,7 @@ def run_test_coverage(prog, filter_fname, exclude_list, build_dir,
     else:
         glob_list = []
     glob_list += exclude_list
-    glob_list += ['*libfdt.py', '*site-packages*', '*dist-packages*']
+    glob_list += ['*libfdt.py', '*/site-packages/*', '*/dist-packages/*']
     glob_list += ['*concurrencytest*']
     test_cmd = 'test' if 'binman' in prog or 'patman' in prog else '-t'
     prefix = ''
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/8 binman: Drop GetRootSkipAtStart()

In reply to Simon Glass

This method is not called anymore, so drop it.

---

(no changes since v1)

 tools/binman/etype/section.py | 17 -----------------
 1 file changed, 17 deletions(-)
tools/binman/etype/section.py +0 -17
index 5e11cf58d28..4c4c8c417f8 100644
@@ -662,23 +662,6 @@ class Entry_section(Entry):
         else:
             raise ValueError("%s: No such property '%s'" % (msg, prop_name))
 
-    def GetRootSkipAtStart(self):
-        """Get the skip-at-start value for the top-level section
-
-        This is used to find out the starting offset for root section that
-        contains this section. If this is a top-level section then it returns
-        the skip-at-start offset for this section.
-
-        This is used to get the absolute position of section within the image.
-
-        Returns:
-            Integer skip-at-start value for the root section containing this
-                section
-        """
-        if self.section:
-            return self.section.GetRootSkipAtStart()
-        return self._skip_at_start
-
     def GetStartOffset(self):
         """Get the start offset for this section
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/8 binman: fit: Drop unused code

In reply to Simon Glass

The key-name-hint case is not tested so is presumably not used. Drop it.

---

(no changes since v1)

 tools/binman/etype/fit.py | 2 --
 1 file changed, 2 deletions(-)
tools/binman/etype/fit.py +0 -2
index 803fb66ea83..ed3cac4ee7e 100644
@@ -562,8 +562,6 @@ class Entry_fit(Entry_section):
             for subnode in node.subnodes:
                 if (subnode.name.startswith('signature') or
                     subnode.name.startswith('cipher')):
-                    if subnode.props.get('key-name-hint') is None:
-                        continue
                     hint = subnode.props['key-name-hint'].value
                     name = tools.get_input_filename(
                         f"{hint}.key" if subnode.name.startswith('signature')
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 5/8 binman: Drop algo check in CheckSetHashValue()

In reply to Simon Glass

The CheckAddHashValue() function is always called before this one, so the algorithm check is never used. Replace it with an assert to avoid a coverage error.

---

(no changes since v1)

 tools/binman/state.py | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/binman/state.py +1 -2
index 6772d3678fe..f4d885c772a 100644
@@ -411,8 +411,7 @@ def CheckSetHashValue(node, get_data_func):
             m = hashlib.sha256()
             m.update(get_data_func())
             data = m.digest()
-        if data is None:
-            raise ValueError(f"Node '{node.path}': Unknown hash algorithm '{algo}'")
+        assert data
         for n in GetUpdateNodes(hash_node):
             n.SetData('value', data)
 
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 6/8 binman: Workaround lz4 cli padding in test cases

In reply to Simon Glass

From: Jiaxun Yang <[email protected]>

Newer lz4 util is not happy with any padding at end of file, it would abort with error message like:

Stream followed by undecodable data at position 43.

Workaround by skipping testCompUtilPadding test case and manually strip padding in testCompressSectionSize test case.

---

(no changes since v1)

 tools/binman/ftest.py                           | 7 +++++--
 tools/binman/test/184_compress_section_size.dts | 1 +
 2 files changed, 6 insertions(+), 2 deletions(-)
tools/binman/ftest.py +5 -2
index 948fcc02259..92df2a03650 100644
@@ -4613,6 +4613,8 @@ class TestFunctional(unittest.TestCase):
         dtb.Scan()
         props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size',
                                         'uncomp-size'])
+        data = data[:0x30]
+        data = data.rstrip(b'\xff')
         orig = self._decompress(data)
         self.assertEqual(COMPRESS_DATA + U_BOOT_DATA, orig)
         expected = {
@@ -6218,8 +6220,9 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
 
     def testCompUtilPadding(self):
         """Test padding of compression algorithms"""
-        # Skip zstd because it doesn't support padding
-        for bintool in [v for k,v in self.comp_bintools.items() if k != 'zstd']:
+        # Skip zstd and lz4 because they doesn't support padding
+        for bintool in [v for k,v in self.comp_bintools.items()
+                        if not k in ['zstd', 'lz4']]:
             self._CheckBintool(bintool)
             data = bintool.compress(COMPRESS_DATA)
             self.assertNotEqual(COMPRESS_DATA, data)
tools/binman/test/184_compress_section_size.dts +1 -0
index 95ed30add1a..1c1dbd5f580 100644
@@ -6,6 +6,7 @@
 		section {
 			size = <0x30>;
 			compress = "lz4";
+			pad-byte = <0xff>;
 			blob {
 				filename = "compress";
 			};
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 7/8 binman: Work around missing test coverage

In reply to Simon Glass

The iMX8 entry-types don't have proper test coverage. Add a work-around to skip this for now.

---

(no changes since v1)

 tools/binman/main.py            |  8 +++++++-
 tools/u_boot_pylib/test_util.py | 16 +++++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)
tools/binman/main.py +7 -1
index 619840e7d55..326f5c93155 100755
@@ -94,10 +94,16 @@ def RunTestCoverage(toolpath, build_dir, args):
     if toolpath:
         for path in toolpath:
             extra_args += ' --toolpath %s' % path
+
+    # Some files unfortunately don't thave the required test coverage. This will
+    # eventually be fixed, but exclude them for now
     test_util.run_test_coverage('tools/binman/binman', None,
             ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*',
              'tools/u_boot_pylib/*'],
-            build_dir, all_set, extra_args or None, args=args)
+            build_dir, all_set, extra_args or None, args=args,
+            allow_failures=['tools/binman/btool/cst.py',
+                            'tools/binman/etype/nxp_imx8mcst.py',
+                            'tools/binman/etype/nxp_imx8mimage.py'])
 
 def RunBinman(args):
     """Main entry point to binman once arguments are parsed
tools/u_boot_pylib/test_util.py +15 -1
index ed216c4fc4e..4835847bfc6 100644
@@ -8,6 +8,7 @@ import doctest
 import glob
 import multiprocessing
 import os
+import re
 import sys
 import unittest
 
@@ -25,7 +26,7 @@ except:
 
 def run_test_coverage(prog, filter_fname, exclude_list, build_dir,
                       required=None, extra_args=None, single_thread='-P1',
-                      args=None):
+                      args=None, allow_failures=None):
     """Run tests and check that we get 100% coverage
 
     Args:
@@ -96,6 +97,19 @@ def run_test_coverage(prog, filter_fname, exclude_list, build_dir,
         print('Coverage error: %s, but should be 100%%' % coverage)
         ok = False
     if not ok:
+        if allow_failures:
+            # for line in lines:
+                # print('.', line, re.match(r'^(tools/.*py) *\d+ *(\d+) *(\d+)%$', line))
+            lines = [re.match(r'^(tools/.*py) *\d+ *(\d+) *\d+%$', line)
+                     for line in stdout.splitlines()]
+            bad = []
+            for mat in lines:
+                if mat and mat.group(2) != '0':
+                    fname = mat.group(1)
+                    if fname not in allow_failures:
+                        bad.append(fname)
+            if not bad:
+                return
         raise ValueError('Test coverage failure')
 
 
Signature · 1 line
2.43.0
#8 lore raw

PATCH v2 8/8 CI: Run code-coverage test for Binman

In reply to Simon Glass

Binman includes a good set of tests covering all of its functionality. This includes a code-coverage test.

However to date the code-coverage test has not been checked automatically by CI, relying on people to run 'binman test -T' themselves.

Plug the gap to avoid bugs creeping in future.

---

Changes in v2:
- Rebase to -master

 .azure-pipelines.yml | 5 ++++-
 .gitlab-ci.yml       | 4 +++-
 2 files changed, 7 insertions(+), 2 deletions(-)
.azure-pipelines.yml +4 -1
index 7a33d403a77..4496506289b 100644
@@ -144,7 +144,10 @@ stages:
           export PATH=${UBOOT_TRAVIS_BUILD_DIR}/scripts/dtc:${PATH}
           ./tools/buildman/buildman -T0 -o ${UBOOT_TRAVIS_BUILD_DIR} -w --board tools-only
           set -ex
-          ./tools/binman/binman --toolpath ${UBOOT_TRAVIS_BUILD_DIR}/tools test
+          export TOOLPATH="--toolpath ${UBOOT_TRAVIS_BUILD_DIR}/tools --toolpath /opt/coreboot"
+          ./tools/binman/binman ${TOOLPATH} test
+          # Avoid "Permission denied: 'cov'" error by using a temporary file
+          COVERAGE_FILE=/tmp/.coverage ./tools/binman/binman ${TOOLPATH} test -T
           ./tools/buildman/buildman -t
           ./tools/dtoc/dtoc -t
           ./tools/patman/patman test
.gitlab-ci.yml +3 -1
index 42ec28a9ad8..94ca2c0f4a2 100644
@@ -196,7 +196,9 @@ Run binman, buildman, dtoc, Kconfig and patman testsuites:
       ./tools/buildman/buildman -T0 -o ${UBOOT_TRAVIS_BUILD_DIR} -w
         --board tools-only;
       set -e;
-      ./tools/binman/binman --toolpath ${UBOOT_TRAVIS_BUILD_DIR}/tools test;
+      export TOOLPATH="--toolpath ${UBOOT_TRAVIS_BUILD_DIR}/tools --toolpath /opt/coreboot";
+      ./tools/binman/binman ${TOOLPATH} test;
+      ./tools/binman/binman ${TOOLPATH} test -T;
       ./tools/buildman/buildman -t;
       ./tools/dtoc/dtoc -t;
       ./tools/patman/patman test;
Signature · 1 line
2.43.0
#9 lore raw
In reply to Simon Glass
Quoted 10 lines · Simon Glass

This series adds a cover-coverage check to CI for Binman. The iMX8 tests are still not completed, so a work-around is included for those.

A few fixes are included for some other problems.

Changes in v2:
- Add new patch to add coverage to the requirements.txt file
- Rebase to -master

[...]

Applied to u-boot/master, thanks!

[1/8] binman: Add coverage to requirements

      commit: ac964c099b5a90076add8aef6bcd6eb3d96eb458
[2/8] binman: Exclude dist-packages and site-packages
      commit: a876295e1bec85f8318807424bb9de8794a4d7f7
[3/8] binman: Drop GetRootSkipAtStart()
      commit: 0148be7cd49e8f801662e5af74203e2cdf93a5bb
[4/8] binman: fit: Drop unused code
      commit: d664c29ec37964db5e191debb2cafeed3a170255
[5/8] binman: Drop algo check in CheckSetHashValue()
      commit: 702b7a3e2eb49b144936e061c0107518544c4b1a
[6/8] binman: Workaround lz4 cli padding in test cases
      commit: 06a0d9eee835b5b6242e773cbab34ec9aba19443
[7/8] binman: Work around missing test coverage
      commit: 2911f2c1ee83760737dc162b1852b0e3b414e6ac
[8/8] CI: Run code-coverage test for Binman
      commit: 6f875290eb107106059f4edfcf8afe31bed9a378
Signature · 1 line
Tom