Skip to contentSkip to conversation

Add Binman code-coverage test to CI

PATCH Superseded Simon Glass 2 participants 9 messages Jun 21, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add Binman code-coverage test to CI

Binman has 100% code coverage to ensure that future changes and refactors do not break existing entry types. This is a critical feature, given that it is relied on to produce images for all sorts of different SoCs and vendors.

With the NXP additions the 'binman test -T' step was missed, so the Binman coverage test is currently failing.

This series provides a means to close the testing gap. It cannot be applied until the tests are added, which should happen before -next is applied to -master

Changes in v2:
- Add new patch to fix a few typos in toolchain code
- Add to azure also (oops)
- Add to buildman requirements instead
- Fix 'envronment' typo

Simon Glass (6):
buildman: Add python3-coverage
buildman: Add python3-pycryptodome
buildman: Fix a few typos in toolchain code
buildman: Support building within a Python venv
u_boot_pylib: Use correct coverage tool within venv
CI: Run code-coverage test for Binman

 .azure-pipelines.yml            |  5 +-
 .gitlab-ci.yml                  |  4 +-
 tools/buildman/bsettings.py     |  3 ++
 tools/buildman/requirements.txt |  2 +
 tools/buildman/test.py          | 83 +++++++++++++++++++++++++++++++++
 tools/buildman/toolchain.py     | 35 +++++++++++---
 tools/u_boot_pylib/test_util.py | 11 +++--
 7 files changed, 132 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/6 buildman: Add python3-coverage

Add this package so we can run code-coverage tests for Binman.

---

Changes in v2:
- Add to buildman requirements instead

 tools/buildman/requirements.txt | 1 +
 1 file changed, 1 insertion(+)
tools/buildman/requirements.txt +1 -0
index 4a31e69e4cb..564e54898a4 100644
@@ -1,3 +1,4 @@
+coverage==6.2
 jsonschema==4.17.3
 pyyaml==6.0
 yamllint==1.26.3
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 2/6 buildman: Add python3-pycryptodome

In reply to Simon Glass

This is used by some Binman entry types, so add it to allow more tests to pass.

---

Changes in v2:
- Add to buildman requirements instead

 tools/buildman/requirements.txt | 1 +
 1 file changed, 1 insertion(+)
tools/buildman/requirements.txt +1 -0
index 564e54898a4..052d0ed5c6f 100644
@@ -1,4 +1,5 @@
 coverage==6.2
 jsonschema==4.17.3
+pycryptodome==3.20
 pyyaml==6.0
 yamllint==1.26.3
Signature · 1 line
2.34.1
#5 lore raw

PATCH v2 3/6 buildman: Fix a few typos in toolchain code

In reply to Simon Glass

Fix 'Thie' and capitalise 'unicode'.

---

Changes in v2:
- Add new patch to fix a few typos in toolchain code

 tools/buildman/toolchain.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/buildman/toolchain.py +2 -2
index 79c7c11a110..324ad0e0821 100644
@@ -175,9 +175,9 @@ class Toolchain:
     def MakeEnvironment(self, full_path):
         """Returns an environment for using the toolchain.
 
-        Thie takes the current environment and adds CROSS_COMPILE so that
+        This takes the current environment and adds CROSS_COMPILE so that
         the tool chain will operate correctly. This also disables localized
-        output and possibly unicode encoded output of all build tools by
+        output and possibly Unicode encoded output of all build tools by
         adding LC_ALL=C.
 
         Note that os.environb is used to obtain the environment, since in some
Signature · 1 line
2.34.1
#6 lore raw

PATCH v2 4/6 buildman: Support building within a Python venv

In reply to Simon Glass

The Python virtualenv tool sets up a few things in the environment, putting its path first in the PATH environment variable and setting up a sys.prefix different from the sys.base_prefix value.

At present buildman puts the toolchain path first in PATH so that it can be found easily during the build. For sandbox this causes problems since /usr/bin/gcc (for example) results in '/usr/bin' being prepended to the PATH variable. As a result, the venv is partially disabled.

The result is that sandbox builds within a venv ignore the venv, e.g. when looking for packages.

Correct this by detecting the venv and adding the toolchain path after the venv path.

---

Changes in v2:
- Fix 'envronment' typo

 tools/buildman/bsettings.py |  3 ++
 tools/buildman/test.py      | 83 +++++++++++++++++++++++++++++++++++++
 tools/buildman/toolchain.py | 31 ++++++++++++--
 3 files changed, 113 insertions(+), 4 deletions(-)
tools/buildman/bsettings.py +3 -0
index e225ac2ca0f..1be1d45e0fa 100644
@@ -31,6 +31,9 @@ def setup(fname=''):
 def add_file(data):
     settings.readfp(io.StringIO(data))
 
+def add_section(name):
+    settings.add_section(name)
+
 def get_items(section):
     """Get the items from a section of the config.
 
tools/buildman/test.py +83 -0

95 lines not on this page · raw message

tools/buildman/toolchain.py +27 -4
index 324ad0e0821..6ca79c2c0f9 100644
@@ -172,13 +172,14 @@ class Toolchain:
         else:
             raise ValueError('Unknown arg to GetEnvArgs (%d)' % which)
 
-    def MakeEnvironment(self, full_path):
+    def MakeEnvironment(self, full_path, env=None):
         """Returns an environment for using the toolchain.
 
         This takes the current environment and adds CROSS_COMPILE so that
         the tool chain will operate correctly. This also disables localized
         output and possibly Unicode encoded output of all build tools by
-        adding LC_ALL=C.
+        adding LC_ALL=C. For the case where full_path is False, it prepends
+        the toolchain to PATH
 
         Note that os.environb is used to obtain the environment, since in some
         cases the environment many contain non-ASCII characters and we see
@@ -187,15 +188,21 @@ class Toolchain:
           UnicodeEncodeError: 'utf-8' codec can't encode characters in position
              569-570: surrogates not allowed
 
+        When running inside a Python venv, care is taken not to put the
+        toolchain path before the venv path, so that builds initiated by
+        buildman will still respect the venv.
+
         Args:
             full_path: Return the full path in CROSS_COMPILE and don't set
                 PATH
+            env (dict of bytes): Original environment, used for testing
         Returns:
             Dict containing the (bytes) environment to use. This is based on the
             current environment, with changes as needed to CROSS_COMPILE, PATH
             and LC_ALL.
         """
-        env = dict(os.environb)
+        env = dict(env or os.environb)
+
         wrapper = self.GetWrapper()
 
         if self.override_toolchain:
@@ -206,7 +213,23 @@ class Toolchain:
                 wrapper + os.path.join(self.path, self.cross))
         else:
             env[b'CROSS_COMPILE'] = tools.to_bytes(wrapper + self.cross)
-            env[b'PATH'] = tools.to_bytes(self.path) + b':' + env[b'PATH']
+
+            # Detect a Python virtualenv and avoid defeating it
+            if sys.prefix != sys.base_prefix:
+                paths = env[b'PATH'].split(b':')
+                new_paths = []
+                to_insert = tools.to_bytes(self.path)
+                insert_after = tools.to_bytes(sys.prefix)
+                for path in paths:
+                    new_paths.append(path)
+                    if to_insert and path.startswith(insert_after):
+                        new_paths.append(to_insert)
+                        to_insert = None
+                if to_insert:
+                    new_paths.append(to_insert)
+                env[b'PATH'] = b':'.join(new_paths)
+            else:
+                env[b'PATH'] = tools.to_bytes(self.path) + b':' + env[b'PATH']
 
         env[b'LC_ALL'] = b'C'
 
Signature · 1 line
2.34.1
#7 lore raw

PATCH v2 5/6 u_boot_pylib: Use correct coverage tool within venv

In reply to Simon Glass

When running within a Python venv we must use the 'coverage' tool (which is within the venv) so that the venv packages are used in preference to system packages. Otherwise the coverage tests run in a different environment from the normal tests and may fail due to missing packages.

Handle this by detecting the venv and changing the tool name.

---

(no changes since v1)

 tools/u_boot_pylib/test_util.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
tools/u_boot_pylib/test_util.py +8 -3
index f18d385d995..857ce58c98c 100644
@@ -60,12 +60,17 @@ def run_test_coverage(prog, filter_fname, exclude_list, build_dir, required=None
     prefix = ''
     if build_dir:
         prefix = 'PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools ' % build_dir
-    cmd = ('%spython3-coverage run '
-           '--omit "%s" %s %s %s %s' % (prefix, ','.join(glob_list),
+
+    # Detect a Python virtualenv and use 'coverage' instead
+    covtool = ('python3-coverage' if sys.prefix == sys.base_prefix else
+               'coverage')
+
+    cmd = ('%s%s run '
+           '--omit "%s" %s %s %s %s' % (prefix, covtool, ','.join(glob_list),
                                         prog, extra_args or '', test_cmd,
                                         single_thread or '-P1'))
     os.system(cmd)
-    stdout = command.output('python3-coverage', 'report')
+    stdout = command.output(covtool, 'report')
     lines = stdout.splitlines()
     if required:
         # Convert '/path/to/name.py' just the module name 'name'
Signature · 1 line
2.34.1
#8 lore raw

PATCH v2 6/6 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:
- Add to azure also (oops)

 .azure-pipelines.yml | 5 ++++-
 .gitlab-ci.yml       | 4 +++-
 2 files changed, 7 insertions(+), 2 deletions(-)
.azure-pipelines.yml +4 -1
index 27f69583c65..65d1d639e49 100644
@@ -128,7 +128,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 165f765a833..eb01fa4868d 100644
@@ -201,7 +201,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.34.1