From 48876666cf061c112d198de2c7109c06d14e1e90 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Tue, 14 Dec 2021 13:44:35 -0500 Subject: [PATCH] Add target for native unit test execution for nrfconnect in build_examples.py (#12956) * Add a nrf test target for native unit test execution * Remove obsolete script * Avoid the need to cd ... use ctest directly and tell it to not run cmake * Restyle fixes * Remove errand space in build nocmake --- .github/workflows/examples-nrfconnect.yaml | 3 +- scripts/build/build/targets.py | 2 ++ scripts/build/builders/nrf.py | 35 +++++++++++++++---- .../testdata/all_targets_except_host.txt | 1 + .../build/testdata/build_all_except_host.txt | 11 ++++++ .../glob_star_targets_except_host.txt | 1 + .../tests/nrfconnect_native_posix_tests.sh | 30 ---------------- 7 files changed, 44 insertions(+), 39 deletions(-) delete mode 100755 scripts/tests/nrfconnect_native_posix_tests.sh diff --git a/.github/workflows/examples-nrfconnect.yaml b/.github/workflows/examples-nrfconnect.yaml index 188ade3a47df0b..4b3690ec4ba66d 100644 --- a/.github/workflows/examples-nrfconnect.yaml +++ b/.github/workflows/examples-nrfconnect.yaml @@ -147,8 +147,7 @@ jobs: - name: Run unit tests for Zephyr native_posix_64 platform timeout-minutes: 10 run: | - scripts/run_in_build_env.sh "scripts/tests/nrfconnect_native_posix_tests.sh native_posix_64" - + scripts/run_in_build_env.sh "./scripts/build/build_examples.py --target nrf-native-posix-64-tests build" - name: Uploading Failed Test Logs uses: actions/upload-artifact@v2 if: ${{ failure() }} && ${{ !env.ACT }} diff --git a/scripts/build/build/targets.py b/scripts/build/build/targets.py index d89b2394887eba..47a4b538c8e29c 100644 --- a/scripts/build/build/targets.py +++ b/scripts/build/build/targets.py @@ -258,6 +258,8 @@ def Efr32Targets(): def NrfTargets(): target = Target('nrf', NrfConnectBuilder) + yield target.Extend('native-posix-64-tests', board=NrfBoard.NATIVE_POSIX_64, app=NrfApp.UNIT_TESTS) + targets = [ target.Extend('nrf5340', board=NrfBoard.NRF5340), target.Extend('nrf52840', board=NrfBoard.NRF52840), diff --git a/scripts/build/builders/nrf.py b/scripts/build/builders/nrf.py index 85cfd996d41476..24fcf9f7624853 100644 --- a/scripts/build/builders/nrf.py +++ b/scripts/build/builders/nrf.py @@ -27,18 +27,21 @@ class NrfApp(Enum): SHELL = auto() PUMP = auto() PUMP_CONTROLLER = auto() + UNIT_TESTS = auto() - def ExampleName(self): + def AppPath(self): if self == NrfApp.LIGHT: - return 'lighting-app' + return 'examples/lighting-app' elif self == NrfApp.LOCK: - return 'lock-app' + return 'examples/lock-app' elif self == NrfApp.SHELL: - return 'shell' + return 'examples/shell' elif self == NrfApp.PUMP: - return 'pump-app' + return 'examples/pump-app' elif self == NrfApp.PUMP_CONTROLLER: - return 'pump-controller-app' + return 'examples/pump-controller-app' + elif self == NrfApp.UNIT_TESTS: + return 'src/test_driver' else: raise Exception('Unknown app type: %r' % self) @@ -53,6 +56,8 @@ def AppNamePrefix(self): return 'chip-nrf-pump-example' elif self == NrfApp.PUMP_CONTROLLER: return 'chip-nrf-pump-controller-example' + elif self == NrfApp.UNIT_TESTS: + return 'chip-nrf-unit-tests' else: raise Exception('Unknown app type: %r' % self) @@ -67,6 +72,9 @@ def _FlashBundlePrefix(self): return 'chip-nrfconnect-pump-example' elif self == NrfApp.PUMP_CONTROLLER: return 'chip-nrfconnect-pump-controller-example' + elif self == NrfApp.UNIT_TESTS: + raise Exception( + 'Unit tests compile natively and do not have a flashbundle') else: raise Exception('Unknown app type: %r' % self) @@ -78,12 +86,15 @@ def FlashBundleName(self): class NrfBoard(Enum): NRF52840 = auto() NRF5340 = auto() + NATIVE_POSIX_64 = auto() def GnArgName(self): if self == NrfBoard.NRF52840: return 'nrf52840dk_nrf52840' elif self == NrfBoard.NRF5340: return 'nrf5340dk_nrf5340_cpuapp' + elif self == NrfBoard.NATIVE_POSIX_64: + return 'native_posix_64' else: raise Exception('Unknown board type: %r' % self) @@ -136,7 +147,7 @@ def generate(self): outdir=shlex.quote(self.output_dir), board=self.board.GnArgName(), sourcedir=shlex.quote(os.path.join( - self.root, 'examples', self.app.ExampleName(), 'nrfconnect')), + self.root, self.app.AppPath(), 'nrfconnect')), rpcs=" -- -DOVERLAY_CONFIG=rpc.overlay" if self.enable_rpcs else "" ).strip() @@ -149,6 +160,13 @@ def _build(self): self._Execute(['ninja', '-C', self.output_dir], title='Building ' + self.identifier) + if self.app == NrfApp.UNIT_TESTS: + # Note: running zephyr/zephyr.elf has the same result except it creates + # a flash.bin in the current directory. ctest has more options and does not + # pollute the source directory + self._Execute(['ctest', '--build-nocmake', '-V', '--output-on-failure', '--test-dir', self.output_dir], + title='Run Tests ' + self.identifier) + def _generate_flashbundle(self): logging.info(f'Generating flashbundle at {self.output_dir}') @@ -162,6 +180,9 @@ def build_outputs(self): } def flashbundle(self): + if self.app == NrfApp.UNIT_TESTS: + return dict() + with open(os.path.join(self.output_dir, self.app.FlashBundleName()), 'r') as fp: return { l.strip(): os.path.join(self.output_dir, l.strip()) for l in fp.readlines() if l.strip() diff --git a/scripts/build/testdata/all_targets_except_host.txt b/scripts/build/testdata/all_targets_except_host.txt index d560f0a03abef2..ea4a929c3be31c 100644 --- a/scripts/build/testdata/all_targets_except_host.txt +++ b/scripts/build/testdata/all_targets_except_host.txt @@ -88,6 +88,7 @@ mbed-CY8CPROTO_062_4343W-pigweed-release mbed-CY8CPROTO_062_4343W-shell-debug (NOGLOB: Compile only for debugging purpose - https://os.mbed.com/docs/mbed-os/latest/program-setup/build-profiles-and-rules.html) mbed-CY8CPROTO_062_4343W-shell-develop (NOGLOB: Compile only for debugging purpose - https://os.mbed.com/docs/mbed-os/latest/program-setup/build-profiles-and-rules.html) mbed-CY8CPROTO_062_4343W-shell-release +nrf-native-posix-64-tests nrf-nrf52840-light nrf-nrf52840-light-rpc nrf-nrf52840-lock diff --git a/scripts/build/testdata/build_all_except_host.txt b/scripts/build/testdata/build_all_except_host.txt index b4e972902f9e01..139966e0a740c1 100644 --- a/scripts/build/testdata/build_all_except_host.txt +++ b/scripts/build/testdata/build_all_except_host.txt @@ -509,6 +509,11 @@ mbed-tools configure -t GCC_ARM -m CY8CPROTO_062_4343W -p {root}/examples/shell/ # Generating mbed-CY8CPROTO_062_4343W-shell-release cmake -S {root}/examples/shell/mbed -B {out}/mbed-CY8CPROTO_062_4343W-shell-release -GNinja -DCMAKE_BUILD_TYPE=release -DMBED_OS_PATH={root}/third_party/mbed-os/repo -DMBED_OS_POSIX_SOCKET_PATH={root}/third_party/mbed-os-posix-socket/repo +# Generating nrf-native-posix-64-tests +bash -c 'source "$ZEPHYR_BASE/zephyr-env.sh"; +export GNUARMEMB_TOOLCHAIN_PATH="$PW_PIGWEED_CIPD_INSTALL_DIR"; +west build --cmake-only -d {out}/nrf-native-posix-64-tests -b native_posix_64 {root}/src/test_driver/nrfconnect' + # Generating nrf-nrf52840-light bash -c 'source "$ZEPHYR_BASE/zephyr-env.sh"; export GNUARMEMB_TOOLCHAIN_PATH="$PW_PIGWEED_CIPD_INSTALL_DIR"; @@ -1071,6 +1076,12 @@ bash -c 'rm -rf {out}/mbed-CY8CPROTO_062_4343W-shell-release/chip-*' # Building mbed-CY8CPROTO_062_4343W-shell-release cmake --build {out}/mbed-CY8CPROTO_062_4343W-shell-release +# Building nrf-native-posix-64-tests +ninja -C {out}/nrf-native-posix-64-tests + +# Run Tests nrf-native-posix-64-tests +ctest --build-nocmake -V --output-on-failure --test-dir {out}/nrf-native-posix-64-tests + # Building nrf-nrf52840-light ninja -C {out}/nrf-nrf52840-light diff --git a/scripts/build/testdata/glob_star_targets_except_host.txt b/scripts/build/testdata/glob_star_targets_except_host.txt index 9717a3c3ef460a..32f0e5e91dc31c 100644 --- a/scripts/build/testdata/glob_star_targets_except_host.txt +++ b/scripts/build/testdata/glob_star_targets_except_host.txt @@ -36,6 +36,7 @@ mbed-CY8CPROTO_062_4343W-light-release mbed-CY8CPROTO_062_4343W-lock-release mbed-CY8CPROTO_062_4343W-pigweed-release mbed-CY8CPROTO_062_4343W-shell-release +nrf-native-posix-64-tests nrf-nrf52840-light nrf-nrf52840-light-rpc nrf-nrf52840-lock diff --git a/scripts/tests/nrfconnect_native_posix_tests.sh b/scripts/tests/nrfconnect_native_posix_tests.sh deleted file mode 100755 index 174bd059bcc487..00000000000000 --- a/scripts/tests/nrfconnect_native_posix_tests.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash - -# -# Copyright (c) 2020 Project CHIP Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -CHIP_ROOT="$(dirname "$0")/../.." -BOARD=native_posix -[[ -n $1 ]] && BOARD="$1" - -set -x -[[ -n $ZEPHYR_BASE ]] && source "$ZEPHYR_BASE/zephyr-env.sh" -env - -cd "$CHIP_ROOT/src/test_driver/nrfconnect" && - west build -b "$BOARD" && - cd build && - timeout 5m ctest -V --output-on-failure