Skip to content

Commit

Permalink
uBPF unit tests that run natively with out Python.
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
  • Loading branch information
Alan Jowett authored and Alan-Jowett committed Sep 30, 2022
1 parent b3ea3a0 commit 954756f
Show file tree
Hide file tree
Showing 15 changed files with 1,052 additions and 33 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
macos_release:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: macos-11
build_type: RelWithDebInfo
enable_sanitizers: false
Expand All @@ -52,6 +53,7 @@ jobs:
macos_release_coverage:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: macos-11
build_type: RelWithDebInfo
enable_sanitizers: false
Expand All @@ -62,6 +64,7 @@ jobs:
macos_release_sanitizers:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: macos-11
build_type: RelWithDebInfo
enable_sanitizers: true
Expand All @@ -72,6 +75,7 @@ jobs:
macos_debug:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: macos-11
build_type: Debug
enable_sanitizers: false
Expand All @@ -82,6 +86,7 @@ jobs:
macos_debug_coverage:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: macos-11
build_type: Debug
enable_sanitizers: false
Expand All @@ -92,6 +97,7 @@ jobs:
macos_debug_sanitizers:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: macos-11
build_type: Debug
enable_sanitizers: true
Expand All @@ -102,16 +108,29 @@ jobs:
linux_release:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: ubuntu-20.04
build_type: RelWithDebInfo
enable_sanitizers: false
enable_coverage: false
scan_build: false
upload_packages: true

linux_release_arm64:
uses: ./.github/workflows/posix.yml
with:
arch: arm64
platform: ubuntu-20.04
build_type: RelWithDebInfo
enable_sanitizers: false
enable_coverage: false
scan_build: false
upload_packages: false

linux_release_scan_build:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: ubuntu-20.04
build_type: RelWithDebInfo
enable_sanitizers: false
Expand All @@ -122,6 +141,7 @@ jobs:
linux_release_coverage:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: ubuntu-20.04
build_type: RelWithDebInfo
enable_sanitizers: false
Expand All @@ -132,6 +152,7 @@ jobs:
linux_release_sanitizers:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: ubuntu-20.04
build_type: RelWithDebInfo
enable_sanitizers: true
Expand All @@ -142,16 +163,29 @@ jobs:
linux_debug:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: ubuntu-20.04
build_type: Debug
enable_sanitizers: false
enable_coverage: false
scan_build: false
upload_packages: false

linux_debug_arm64:
uses: ./.github/workflows/posix.yml
with:
arch: arm64
platform: ubuntu-20.04
build_type: Debug
enable_sanitizers: false
enable_coverage: false
scan_build: false
upload_packages: true

linux_debug_coverage:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: ubuntu-20.04
build_type: Debug
enable_sanitizers: false
Expand All @@ -162,9 +196,24 @@ jobs:
linux_debug_sanitizers:
uses: ./.github/workflows/posix.yml
with:
arch: x86_64
platform: ubuntu-20.04
build_type: Debug
enable_sanitizers: true
enable_coverage: false
scan_build: false
upload_packages: false

finish:
needs:
- macos_release_coverage
- macos_debug_coverage
- linux_release_coverage
- linux_debug_coverage
runs-on: ubuntu-20.04
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
55 changes: 49 additions & 6 deletions .github/workflows/posix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ name: Posix
on:
workflow_call:
inputs:
arch:
description: 'Architecture'
required: true
type: string

platform:
required: true
type: string
Expand Down Expand Up @@ -63,20 +68,29 @@ jobs:
sudo apt-get install -y \
ccache \
ninja-build \
cmake
cmake \
lcov
if [[ "${{ inputs.scan_build }}" == "true" ]] ; then
sudo apt-get install -y \
clang-tools
fi
if [[ "${{ inputs.arch }}" == "arm64" ]] ; then
sudo apt install -y \
g++-aarch64-linux-gnu \
gcc-aarch64-linux-gnu \
qemu-user
fi
- name: Install system dependencies (macOS)
if: inputs.platform == 'macos-11'
run: |
brew install \
cmake \
ninja \
ccache
ccache \
lcov
- name: Configure the project
run: |
Expand All @@ -87,15 +101,24 @@ jobs:
command_prefix="scan-build -o scan_build_report"
fi
if [[ "${{ inputs.arch }}" == "arm64" ]] ; then
arch_flags="-DCMAKE_TOOLCHAIN_FILE=cmake/arm64.cmake"
test_flags=""
else
arch_flags=""
test_flags="-DUBPF_ENABLE_TESTS=true"
fi
${command_prefix} cmake \
-G Ninja \
-S . \
-B build \
-DCMAKE_BUILD_TYPE=${{ inputs.build_type }} \
-DUBPF_ENABLE_COVERAGE=${{ inputs.enable_sanitizers }} \
-DUBPF_ENABLE_SANITIZERS=${{ inputs.enable_coverage }} \
-DUBPF_ENABLE_TESTS=true \
-DUBPF_ENABLE_INSTALL=true
-DUBPF_ENABLE_COVERAGE=${{ inputs.enable_coverage }} \
-DUBPF_ENABLE_SANITIZERS=${{ inputs.enable_sanitizers }} \
${test_flags} \
-DUBPF_ENABLE_INSTALL=true \
${arch_flags}
- name: Build the project
run: |
Expand All @@ -118,12 +141,32 @@ jobs:
retention-days: 5

- name: Run the tests
if: inputs.arch == 'x86_64'
run: |
cmake \
--build build \
--target test \
-- -v
- name: Run the tests ARM64
if: inputs.arch == 'arm64'
run: |
qemu-aarch64 -L /usr/aarch64-linux-gnu build/unit_tests/ubpf_unit_test
- name: Generate code coverage report
if: inputs.enable_coverage == true
run: |
mkdir -p coverage
lcov --capture --directory build --include '*/vm/*' --output-file coverage/lcov.info
- name: Coveralls Parallel
if: inputs.enable_coverage == true
uses: coverallsapp/github-action@v1.1.2
with:
github-token: ${{ secrets.github_token }}
flag-name: run-${{inputs.build_type}}-${{inputs.platform}}
parallel: true

- name: Run the install target
run: |
mkdir install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Run the tests
working-directory: build
run: |
ctest -C ${{ inputs.build_type }} -V
ctest -C ${{ inputs.build_type }} --output-on-failure
- name: Generate the TGZ package
run: |
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ if(UBPF_ENABLE_TESTS)
endif()

add_subdirectory("vm")
add_subdirectory("unit_tests")

if(UBPF_ENABLE_PACKAGE)
include("cmake/packaging.cmake")
endif()

16 changes: 16 additions & 0 deletions cmake/arm64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright (c) 2022-present, IO Visor Project
# All rights reserved.
#
# This source code is licensed in accordance with the terms specified in
# the LICENSE file found in the root directory of this source tree.
#

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
1 change: 1 addition & 0 deletions cmake/settings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)

if(UBPF_ENABLE_COVERAGE)
target_compile_options("ubpf_settings" INTERFACE
--coverage
-fprofile-arcs
-ftest-coverage
)
Expand Down
6 changes: 3 additions & 3 deletions tests/stack3.data
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- asm
stdw [r10-256],123
stdw [r10-512],456
stdw [r10-256], 123
stdw [r10-512], 456
mov r2, r10
sub r2, 256
ldxdw r0, [r2]
Expand All @@ -10,4 +10,4 @@ add r0, r1

exit
-- result
579
0x243
51 changes: 51 additions & 0 deletions unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Copyright (c) 2022-present, IO Visor Project
# All rights reserved.
#
# This source code is licensed in accordance with the terms specified in
# the LICENSE file found in the root directory of this source tree.
#

set(CMAKE_CXX_STANDARD 20)

include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

if(UBPF_ENABLE_TESTS)
enable_testing()
endif()

add_executable(
ubpf_unit_test
bpf_assembler.cc
code_gen_tests.cc
)

target_include_directories("ubpf_unit_test" PRIVATE
"${CMAKE_SOURCE_DIR}/vm"
"${CMAKE_BINARY_DIR}/vm"
"${CMAKE_SOURCE_DIR}/vm/inc"
"${CMAKE_BINARY_DIR}/vm/inc"
)

target_link_libraries(
ubpf_unit_test
GTest::gtest_main
ubpf
ubpf_settings
)

if (UBPF_ENABLE_TESTS)
include(GoogleTest)
gtest_discover_tests(ubpf_unit_test
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif()
Loading

0 comments on commit 954756f

Please sign in to comment.