Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---

# https://thesofproject.github.io/latest/developer_guides/unit_tests.html

name: Unit tests

# 'workflow_dispatch' allows running this workflow manually from the
# 'Actions' tab

# yamllint disable-line rule:truthy
on: [pull_request, workflow_dispatch]

jobs:
cmocka_utests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with: {fetch-depth: 2}

- name: build and run all defconfigs
run: ./test/test-all-defconfigs.sh
97 changes: 70 additions & 27 deletions scripts/run-mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,83 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright(c) 2021 Intel Corporation. All rights reserved.

# fail on any errors

# Stop on most errors
set -e

# default config of none supplied
CONFIG="tgph_defconfig"
VALGRIND_CMD=""
SOFTOP=$(cd "$(dirname "$0")"/.. && pwd)

usage()
{
cat <<EOF
Usage: [ -v ] [ -c platform_defconfig ]

Re-compiles unit tests with the host toolchain and runs them one by
one, optionally with valgrind. If you don't need valgrind it's faster
and better looking to run "make -j test". See
https://thesofproject.github.io/latest/developer_guides/unit_tests.html
EOF
exit 1
}

parse_opts()
{
# default config if none supplied
CONFIG="tgph_defconfig"
VALGRIND_CMD=""

while getopts "vc:" flag; do
case "${flag}" in
c) CONFIG=${OPTARG}
;;
v) VALGRIND_CMD="valgrind --tool=memcheck --track-origins=yes \
--leak-check=full --show-leak-kinds=all --error-exitcode=1"
;;
?) usage
;;
esac
done

while getopts "vc:" flag; do
case "${flag}" in
c) CONFIG=${OPTARG};;
v) VALGRIND_CMD="valgrind --tool=memcheck --track-origins=yes --leak-check=full --show-leak-kinds=all";;
?) echo "Usage: -v -c defconfig"
exit 1;;
esac
done
shift $((OPTIND -1 ))
# anything left?
test -z "$1" || usage
}

rebuild_ut()
{
# -DINIT_CONFIG is ignored after the first time. Invoke make (or
# -ninja) after this for faster, incremental builds.
rm -rf build_ut/

cmake -S "$SOFTOP" -B build_ut -DBUILD_UNIT_TESTS=ON -DBUILD_UNIT_TESTS_HOST=ON \
-DINIT_CONFIG="${CONFIG}"

# clean build area
rm -rf build_ut
mkdir build_ut
cmake --build build_ut -- -j"$(nproc --all)"
}

# copy initial defconfig
cp src/arch/xtensa/configs/${CONFIG} initial.config
cd build_ut
run_ut()
{
local TESTS; TESTS=$(find build_ut/test -type f -executable -print)
echo test are "${TESTS}"
for test in ${TESTS}
do
if [ x"$test" = x'build_ut/test/cmocka/src/lib/alloc/alloc' ]; then
printf 'SKIP alloc test until it is fixed on HOST (passes with xt-run)'
continue
fi

cmake -DBUILD_UNIT_TESTS=ON -DBUILD_UNIT_TESTS_HOST=ON ..
printf 'Running %s\n' "${test}"
( set -x
${VALGRIND_CMD} ./"${test}"
)
done
}

make -j$(nproc --all)
main()
{
parse_opts "$@"
rebuild_ut
run_ut
}

TESTS=`find test -type f -executable -print`
echo test are ${TESTS}
for test in ${TESTS}
do
echo got ${test}
${VALGRIND_CMD} ./${test}
done
main "$@"
6 changes: 4 additions & 2 deletions scripts/xtensa-build-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ die()
print_usage()
{
cat <<EOF
Re-configures and re-builds SOF using the corresponding compiler and
the <platform>_defconfig file.
Re-configures and re-builds SOF using the corresponding compiler and the
<platform>_defconfig file. Implements and saves the manual configuration
described in
https://thesofproject.github.io/latest/developer_guides/firmware/cmake.html

usage: $0 [options] platform(s)

Expand Down
1 change: 1 addition & 0 deletions src/arch/xtensa/configs/suecreek_gcc_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ CONFIG_INTEL_DMIC=y
CONFIG_INTEL_SSP=y
CONFIG_LP_MEMORY_BANKS=1
CONFIG_HP_MEMORY_BANKS=47
CONFIG_TRACE=n
13 changes: 12 additions & 1 deletion test/cmocka/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ add_library(cmocka STATIC IMPORTED)
if(BUILD_UNIT_TESTS_HOST)
set(UT_CC_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/cmocka-host-gcc-toolchain.cmake)
set(UT_TOOLCHAIN "")
# TODO: add an option to support valgrind.
# In the mean time use scripts/run-mocks.sh for valgrind.
set(SIMULATOR "")
elseif(BUILD_UNIT_TESTS_XTENSA_GCC)
# GCC xtensa build for future Qemu.
set(UT_CC_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/cmocka-xtensa-gcc-toolchain.cmake)
set(UT_TOOLCHAIN "-DTOOLCHAIN=${TOOLCHAIN}")
set(SIMULATOR xt-run --exit_with_target_code)
else()
# xtensa XCC build for xt-run.
set(UT_CC_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/cmocka-xtensa-xt-toolchain.cmake)
set(UT_TOOLCHAIN "")
set(SIMULATOR xt-run --exit_with_target_code)
endif()

if(DEFINED CMOCKA_DIRECTORY)
Expand Down Expand Up @@ -102,7 +107,13 @@ function(cmocka_test test_name)
# Cmocka requires this define for stdint.h that defines uintptr
target_compile_definitions(${test_name} PRIVATE -D_UINTPTR_T_DEFINED)

add_test(NAME ${test_name} COMMAND xt-run --exit_with_target_code ${test_name})
# Skip running alloc test on HOST until it's fixed (it passes and is run
# with xt-run)
if( "alloc" STREQUAL "${test_name}" AND BUILD_UNIT_TESTS_HOST)
message(WARNING "SKIP alloc test on HOST, built but not run")
else()
add_test(NAME ${test_name} COMMAND ${SIMULATOR} ${test_name})
endif()

sof_append_relative_path_definitions(${test_name})
endfunction()
Expand Down
5 changes: 5 additions & 0 deletions test/cmocka/src/common_mocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void WEAK __panic(uint32_t p, char *filename, uint32_t linenum)
exit(EXIT_FAILURE);
}

#if CONFIG_TRACE
void WEAK trace_log_filtered(bool send_atomic, const void *log_entry, const struct tr_ctx *ctx,
uint32_t lvl, uint32_t id_1, uint32_t id_2, int arg_count, ...)
{
Expand All @@ -115,6 +116,7 @@ void WEAK trace_log_filtered(bool send_atomic, const void *log_entry, const stru
void WEAK trace_flush_dma_to_mbox(void)
{
}
#endif

#if CONFIG_LIBRARY
volatile void *task_context_get(void);
Expand Down Expand Up @@ -330,6 +332,8 @@ int WEAK arch_cpu_is_core_enabled(int id)
int WEAK test_bench_trace = 1;
int WEAK debug;

/* ... but not always in unit tests */
#if CONFIG_TRACE
/* look up subsystem class name from table */
char * WEAK get_trace_class(uint32_t trace_class)
{
Expand All @@ -339,6 +343,7 @@ char * WEAK get_trace_class(uint32_t trace_class)
*/
return "unknown";
}
#endif

uint8_t * WEAK get_library_mailbox(void)
{
Expand Down
39 changes: 39 additions & 0 deletions test/test-all-defconfigs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

set -e

SOFTOP=$(cd "$(dirname "$0")"/.. && pwd)

BUILDTOP=build_ut_defs

rebuild_config()
{
local conf="$1"
mkdir -p "$BUILDTOP"
( set -x
cmake -DINIT_CONFIG="$conf" -B "$BUILDTOP"/"$conf" -DBUILD_UNIT_TESTS=ON \
-DBUILD_UNIT_TESTS_HOST=ON

cmake --build "$BUILDTOP"/"$conf" -- -j"$(nproc)"
)
}

main()
{
local defconfig

# First make sure all configurations build
for d in "$SOFTOP"/src/arch/xtensa/configs/*_defconfig; do
defconfig=$(basename "$d")
rebuild_config "$defconfig"
done

# Now run all the tests
for d in "$BUILDTOP"/*_defconfig; do
( set -x
cmake --build "$d" -- test
)
done
}

main "$@"