From bf2c7bcd51252654ad3f91d76adff9b7d54c89cc Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 13 Dec 2021 14:45:04 -0500 Subject: [PATCH 1/5] Add a nrf test target for native unit test execution --- .github/workflows/examples-nrfconnect.yaml | 3 +- scripts/build/build/targets.py | 2 + scripts/build/builders/nrf.py | 37 +++++++++++++++---- .../testdata/all_targets_except_host.txt | 1 + .../build/testdata/build_all_except_host.txt | 11 ++++++ .../glob_star_targets_except_host.txt | 1 + 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/.github/workflows/examples-nrfconnect.yaml b/.github/workflows/examples-nrfconnect.yaml index b22a210938bb16..9529d713653bb8 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..32e5608ebcfd4e 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,8 @@ 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 +85,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 +146,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 +159,16 @@ def _build(self): self._Execute(['ninja', '-C', self.output_dir], title='Building ' + self.identifier) + if self.app == NrfApp.UNIT_TESTS: + # 'cd' is because running the tests creates a file named 'flash.bin' in the + # current directory. Try to not pollute the source directory. + # + # Note: running zephyr/zephyr.elf works just as fine, but ctest seems + # to have more options + self._Execute(['bash', '-c', "cd %s && ctest -V --output-on-failure" % self.output_dir], + title='Run Tests ' + self.identifier) + + def _generate_flashbundle(self): logging.info(f'Generating flashbundle at {self.output_dir}') @@ -162,6 +182,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..fc247180642732 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 +bash -c 'cd {out}/nrf-native-posix-64-tests && ctest -V --output-on-failure' + # 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 From e7cad5d2ae122ebf063e0f1b19ecd83faacad8fd Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 13 Dec 2021 14:45:29 -0500 Subject: [PATCH 2/5] Remove obsolete script --- .../tests/nrfconnect_native_posix_tests.sh | 30 ------------------- 1 file changed, 30 deletions(-) delete mode 100755 scripts/tests/nrfconnect_native_posix_tests.sh 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 From 407faa8326babcce6c064aaa7b13362938d43347 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 13 Dec 2021 14:57:01 -0500 Subject: [PATCH 3/5] Avoid the need to cd ... use ctest directly and tell it to not run cmake --- scripts/build/builders/nrf.py | 10 ++++------ scripts/build/testdata/build_all_except_host.txt | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/scripts/build/builders/nrf.py b/scripts/build/builders/nrf.py index 32e5608ebcfd4e..a9ee06bf92b7f9 100644 --- a/scripts/build/builders/nrf.py +++ b/scripts/build/builders/nrf.py @@ -160,12 +160,10 @@ def _build(self): title='Building ' + self.identifier) if self.app == NrfApp.UNIT_TESTS: - # 'cd' is because running the tests creates a file named 'flash.bin' in the - # current directory. Try to not pollute the source directory. - # - # Note: running zephyr/zephyr.elf works just as fine, but ctest seems - # to have more options - self._Execute(['bash', '-c', "cd %s && ctest -V --output-on-failure" % self.output_dir], + # 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) diff --git a/scripts/build/testdata/build_all_except_host.txt b/scripts/build/testdata/build_all_except_host.txt index fc247180642732..a5488b82aeae0f 100644 --- a/scripts/build/testdata/build_all_except_host.txt +++ b/scripts/build/testdata/build_all_except_host.txt @@ -1080,7 +1080,7 @@ cmake --build {out}/mbed-CY8CPROTO_062_4343W-shell-release ninja -C {out}/nrf-native-posix-64-tests # Run Tests nrf-native-posix-64-tests -bash -c 'cd {out}/nrf-native-posix-64-tests && ctest -V --output-on-failure' +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 From 71f033765daec54a55a12595221d884fdceda5f3 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 13 Dec 2021 14:57:26 -0500 Subject: [PATCH 4/5] Restyle fixes --- scripts/build/builders/nrf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/build/builders/nrf.py b/scripts/build/builders/nrf.py index a9ee06bf92b7f9..415da21461632a 100644 --- a/scripts/build/builders/nrf.py +++ b/scripts/build/builders/nrf.py @@ -73,7 +73,8 @@ def _FlashBundlePrefix(self): 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') + raise Exception( + 'Unit tests compile natively and do not have a flashbundle') else: raise Exception('Unknown app type: %r' % self) @@ -166,7 +167,6 @@ def _build(self): 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}') From 54ac994a7b8a79a5442d5da88f81ba88b3d5e5fe Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 13 Dec 2021 15:00:26 -0500 Subject: [PATCH 5/5] Remove errand space in build nocmake --- scripts/build/builders/nrf.py | 2 +- scripts/build/testdata/build_all_except_host.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/build/builders/nrf.py b/scripts/build/builders/nrf.py index 415da21461632a..24fcf9f7624853 100644 --- a/scripts/build/builders/nrf.py +++ b/scripts/build/builders/nrf.py @@ -164,7 +164,7 @@ def _build(self): # 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], + self._Execute(['ctest', '--build-nocmake', '-V', '--output-on-failure', '--test-dir', self.output_dir], title='Run Tests ' + self.identifier) def _generate_flashbundle(self): diff --git a/scripts/build/testdata/build_all_except_host.txt b/scripts/build/testdata/build_all_except_host.txt index a5488b82aeae0f..139966e0a740c1 100644 --- a/scripts/build/testdata/build_all_except_host.txt +++ b/scripts/build/testdata/build_all_except_host.txt @@ -1080,7 +1080,7 @@ cmake --build {out}/mbed-CY8CPROTO_062_4343W-shell-release 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 +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