From 785c6fe0f5c44961683e313949f9d27374ecc45b Mon Sep 17 00:00:00 2001 From: Lisanna Dettwyler Date: Thu, 18 Jul 2024 16:39:01 +0000 Subject: [PATCH] Enable testing with ctest Adds a single loader API test using googletest. Adds the ZEL_LIBRARY_PATH environment variable, which allows using a local copy of the loader for the purposes of testing. Signed-off-by: Lisanna Dettwyler Co-authored-by: Jemale Lockett --- .github/workflows/build-quick.yml | 15 +++++-- CMakeLists.txt | 19 +++++++- source/loader/ze_loader.cpp | 24 +++++++--- test/CMakeLists.txt | 27 ++++++++++- test/layers/CMakeLists.txt | 3 -- test/layers/validation/CMakeLists.txt | 3 -- .../handle_lifetime_tracking/CMakeLists.txt | 2 - test/loader_api.cpp | 45 +++++++++++++++++++ test/test_api/CMakeLists.txt | 17 ------- test/test_api/src/main.cpp | 19 -------- test/test_api/src/test_api.cpp | 43 ------------------ 11 files changed, 117 insertions(+), 100 deletions(-) delete mode 100644 test/layers/CMakeLists.txt delete mode 100644 test/layers/validation/CMakeLists.txt delete mode 100644 test/layers/validation/handle_lifetime_tracking/CMakeLists.txt create mode 100644 test/loader_api.cpp delete mode 100644 test/test_api/CMakeLists.txt delete mode 100644 test/test_api/src/main.cpp delete mode 100644 test/test_api/src/test_api.cpp diff --git a/.github/workflows/build-quick.yml b/.github/workflows/build-quick.yml index 546d613..ba92094 100644 --- a/.github/workflows/build-quick.yml +++ b/.github/workflows/build-quick.yml @@ -22,17 +22,26 @@ jobs: -D CMAKE_C_COMPILER_LAUNCHER=ccache \ -D CMAKE_CXX_COMPILER_LAUNCHER=ccache \ -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_L0_LOADER_TESTS=1 \ .. make -j$(nproc) + - env: + ZE_ENABLE_LOADER_DEBUG_TRACE: '1' + working-directory: build + run: ctest -V + build-windows: if: github.repository_owner == 'oneapi-src' runs-on: [windows-latest] steps: - uses: actions/checkout@v3 - name: Build Loader on Latest Windows - shell: pwsh run: | mkdir build cd build - cmake -D CMAKE_BUILD_TYPE=Release .. - cmake --build . --config Release \ No newline at end of file + cmake -D BUILD_L0_LOADER_TESTS=1 .. + cmake --build . --config Release + - env: + ZE_ENABLE_LOADER_DEBUG_TRACE: '1' + working-directory: build + run: ctest -C Release -V diff --git a/CMakeLists.txt b/CMakeLists.txt index 2dcb024..d8c6892 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,22 @@ else() include_directories("${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog_headers") endif() +include(FetchContent) + +if(BUILD_L0_LOADER_TESTS) + FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip + ) + + # For Windows: Prevent overriding the parent project's compiler/linker settings + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + + FetchContent_MakeAvailable(googletest) + + enable_testing() +endif() + # Update other relevant variables to include the patch set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") set(CMAKE_PROJECT_VERSION_PATCH "${PROJECT_VERSION_PATCH}") @@ -206,7 +222,8 @@ add_subdirectory(source) add_subdirectory(samples) if(BUILD_L0_LOADER_TESTS) - add_subdirectory(test) + include(CTest) + add_subdirectory(test) endif() include("os_release_info.cmake") diff --git a/source/loader/ze_loader.cpp b/source/loader/ze_loader.cpp index db17222..217d4d9 100644 --- a/source/loader/ze_loader.cpp +++ b/source/loader/ze_loader.cpp @@ -283,12 +283,24 @@ namespace loader if (zel_logger->logging_enabled) zel_logger->get_base_logger()->info("Loader Version {}.{}.{} {}", LOADER_VERSION_MAJOR, LOADER_VERSION_MINOR, LOADER_VERSION_PATCH, LOADER_VERSION_SHA); + add_loader_version(); + std::string loaderLibraryPath; + auto loaderLibraryPathEnv = getenv_string("ZEL_LIBRARY_PATH"); + if (!loaderLibraryPathEnv.empty()) { + loaderLibraryPath = loaderLibraryPathEnv; + } +#ifdef _WIN32 + else { + loaderLibraryPath = readLevelZeroLoaderLibraryPath(); + } +#endif + debug_trace_message("Using Loader Library Path: ", loaderLibraryPath); drivers.reserve( discoveredDrivers.size() + getenv_tobool( "ZE_ENABLE_NULL_DRIVER" ) ); if( getenv_tobool( "ZE_ENABLE_NULL_DRIVER" ) ) { zel_logger->log_info("Enabling Null Driver"); - auto handle = LOAD_DRIVER_LIBRARY( MAKE_LIBRARY_NAME( "ze_null", L0_LOADER_VERSION ) ); + auto handle = LOAD_DRIVER_LIBRARY( create_library_path( MAKE_LIBRARY_NAME( "ze_null", L0_LOADER_VERSION ), loaderLibraryPath.c_str()).c_str()); if (debugTraceEnabled) { std::string message = "ze_null Driver Init"; debug_trace_message(message, ""); @@ -300,7 +312,7 @@ namespace loader drivers.rbegin()->name = "ze_null"; } else if (debugTraceEnabled) { GET_LIBRARY_ERROR(loadLibraryErrorValue); - std::string errorMessage = "Load Library of " + std::string(MAKE_LIBRARY_NAME( "ze_null", L0_LOADER_VERSION )) + " failed with "; + std::string errorMessage = "Load Library of " + create_library_path( MAKE_LIBRARY_NAME( "ze_null", L0_LOADER_VERSION ), loaderLibraryPath.c_str()) + " failed with "; debug_trace_message(errorMessage, loadLibraryErrorValue); loadLibraryErrorValue.clear(); } @@ -326,15 +338,12 @@ namespace loader } } if(drivers.size()==0){ + std::string message = "0 Drivers Discovered"; + debug_trace_message(message, ""); zel_logger->log_error("0 Drivers Discovered"); return ZE_RESULT_ERROR_UNINITIALIZED; } - add_loader_version(); - std::string loaderLibraryPath; -#ifdef _WIN32 - loaderLibraryPath = readLevelZeroLoaderLibraryPath(); -#endif typedef ze_result_t (ZE_APICALL *getVersion_t)(zel_component_version_t *version); if( getenv_tobool( "ZE_ENABLE_VALIDATION_LAYER" ) ) { @@ -363,6 +372,7 @@ namespace loader tracingLayerEnabled = true; } std::string tracingLayerLibraryPath = create_library_path(MAKE_LAYER_NAME( "ze_tracing_layer" ), loaderLibraryPath.c_str()); + debug_trace_message("Tracing Layer Library Path: ", tracingLayerLibraryPath); tracingLayer = LOAD_DRIVER_LIBRARY( tracingLayerLibraryPath.c_str() ); if(tracingLayer) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d4bf2c6..dc9d690 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,27 @@ # Copyright (C) 2024 Intel Corporation # SPDX-License-Identifier: MIT -add_subdirectory(layers) -add_subdirectory(test_api) + +add_executable( + tests + loader_api.cpp +) +target_include_directories(tests PRIVATE ${CMAKE_SOURCE_DIR}/include) +target_link_libraries( + tests + GTest::gtest_main + ${TARGET_LOADER_NAME} +) + +# For some reason the MSVC runtime libraries used by googletest and test +# binaries don't match, so force the test binary to use the dynamic runtime. +if(MSVC) + target_compile_options(tests PRIVATE "/MD$<$:d>") +endif() + +add_test(NAME tests COMMAND tests) +if(MSVC) + get_target_property(binary_dir ${TARGET_LOADER_NAME} RUNTIME_OUTPUT_DIRECTORY) +else() + get_target_property(binary_dir ${TARGET_LOADER_NAME} LIBRARY_OUTPUT_DIRECTORY) +endif() +set_property(TEST tests PROPERTY ENVIRONMENT "ZEL_LIBRARY_PATH=${binary_dir};ZE_ENABLE_NULL_DRIVER=1") \ No newline at end of file diff --git a/test/layers/CMakeLists.txt b/test/layers/CMakeLists.txt deleted file mode 100644 index 44f6b79..0000000 --- a/test/layers/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -# Copyright (C) 2024 Intel Corporation -# SPDX-License-Identifier: MIT -add_subdirectory(validation) diff --git a/test/layers/validation/CMakeLists.txt b/test/layers/validation/CMakeLists.txt deleted file mode 100644 index 553709d..0000000 --- a/test/layers/validation/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -# Copyright (C) 2024 Intel Corporation -# SPDX-License-Identifier: MIT -add_subdirectory(handle_lifetime_tracking) diff --git a/test/layers/validation/handle_lifetime_tracking/CMakeLists.txt b/test/layers/validation/handle_lifetime_tracking/CMakeLists.txt deleted file mode 100644 index 975ce12..0000000 --- a/test/layers/validation/handle_lifetime_tracking/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -# Copyright (C) 2024 Intel Corporation -# SPDX-License-Identifier: MIT diff --git a/test/loader_api.cpp b/test/loader_api.cpp new file mode 100644 index 0000000..4fdf8e1 --- /dev/null +++ b/test/loader_api.cpp @@ -0,0 +1,45 @@ +/* + * + * Copyright (C) 2024 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "gtest/gtest.h" + +#include "loader/ze_loader.h" +#include "ze_api.h" + +namespace { + +TEST( + LoaderAPI, + GivenLevelZeroLoaderPresentWhenCallingzeGetLoaderVersionsAPIThenValidVersionIsReturned) { + + EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(0)); + + size_t size = 0; + EXPECT_EQ(ZE_RESULT_SUCCESS, zelLoaderGetVersions(&size, nullptr)); + EXPECT_GT(size, 0); + + std::vector versions(size); + EXPECT_EQ(ZE_RESULT_SUCCESS, zelLoaderGetVersions(&size, versions.data())); + + std::cout << "Found " << versions.size() << " versions" << std::endl; + std::cout << std::endl; + const std::string loader_name = "loader"; + for (auto &component : versions) { + std::cout << "component.component_name: " << component.component_name << std::endl; + std::cout << "component.component_lib_version.major: " << component.component_lib_version.major << std::endl; + std::cout << "component.spec_version: " << component.spec_version << std::endl; + std::cout << "component.component_lib_name: " << component.component_name << std::endl; + std::cout << std::endl; + + if (loader_name == component.component_name) { + EXPECT_GE(component.component_lib_version.major, 1); + } + } +} + +} // namespace diff --git a/test/test_api/CMakeLists.txt b/test/test_api/CMakeLists.txt deleted file mode 100644 index e2778f1..0000000 --- a/test/test_api/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright (C) 2024 Intel Corporation -# SPDX-License-Identifier: MIT - -# function(add_test) - -# endfunction() - -# add_loader_test( -# NAME test_loader_api -# GROUP "/layer_tests/loader_api" -# SOURCES -# src/test_loader_api.cpp -# src/main.cpp -# LINK_LIBRARIES -# level_zero_tests::logging -# level_zero_tests::utils -# ) diff --git a/test/test_api/src/main.cpp b/test/test_api/src/main.cpp deleted file mode 100644 index 0333b49..0000000 --- a/test/test_api/src/main.cpp +++ /dev/null @@ -1,19 +0,0 @@ -/* - * - * Copyright (C) 2024 Intel Corporation - * - * SPDX-License-Identifier: MIT - * - */ - -#include "logging/logging.hpp" -#include "utils/utils.hpp" -#include "gmock/gmock.h" - -int main(int argc, char **argv) { - ::testing::InitGoogleMock(&argc, argv); - std::vector command_line(argv + 1, argv + argc); - level_zero_tests::init_logging(command_line); - - return RUN_ALL_TESTS(); -} diff --git a/test/test_api/src/test_api.cpp b/test/test_api/src/test_api.cpp deleted file mode 100644 index 2c0981b..0000000 --- a/test/test_api/src/test_api.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * Copyright (C) 2024 Intel Corporation - * - * SPDX-License-Identifier: MIT - * - */ - -#include "gtest/gtest.h" - -#include "logging/logging.hpp" -#include "test_harness/test_harness.hpp" -#include -#include - -namespace lzt = level_zero_tests; - -namespace { - -TEST( - LoaderTest, - GivenLevelZeroLoaderPresentWhenCallingzeGetLoaderVersionsAPIThenValidVersionIsReturned) { - - size_t size = 0; - EXPECT_EQ(ZE_RESULT_SUCCESS, zelLoaderGetVersions(&size, nullptr)); - - std::vector versions; - - EXPECT_EQ(ZE_RESULT_SUCCESS, zelLoaderGetVersions(&size, versions.data())); - - for (auto &component : versions) { - LOG_INFO << "component.component_name: " << component.component_name; - LOG_INFO << " " << component.component_lib_version.major; - LOG_INFO << "component.spec_version: " << component.spec_version; - LOG_INFO << "component.component_lib_name: " << component.component_name; - - if ("loader" == component.component_name) { - EXPECT_GE(component.component_lib_version.major, 1); - } - } -} - -} // namespace