Skip to content

Commit

Permalink
Enable testing with googletest
Browse files Browse the repository at this point in the history
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 <lisanna.dettwyler@intel.com>
Co-authored-by: Jemale Lockett <jemale.lockett@intel.com>
  • Loading branch information
lisanna-dettwyler and Jemale committed Jul 29, 2024
1 parent 35fa646 commit d11de6b
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 103 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/build-quick.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@ jobs:
-D CMAKE_BUILD_TYPE=Release \
..
make -j$(nproc)
- env:
ZEL_LIBRARY_PATH: ${{ github.workspace }}/build/lib
ZE_ENABLE_NULL_DRIVER: '1'
ZE_ENABLE_LOADER_DEBUG_TRACE: '1'
run: ./build/bin/tests

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
cmake ..
cmake --build . --config Release
- env:
ZEL_LIBRARY_PATH: ${{ github.workspace }}/build/bin/Release
ZE_ENABLE_NULL_DRIVER: '1'
ZE_ENABLE_LOADER_DEBUG_TRACE: '1'
run: ./build/bin/Release/tests.exe
21 changes: 17 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -204,10 +220,7 @@ set(TARGET_LOADER_NAME ze_loader)

add_subdirectory(source)
add_subdirectory(samples)

if(BUILD_L0_LOADER_TESTS)
add_subdirectory(test)
endif()
add_subdirectory(test)

include("os_release_info.cmake")
get_os_release_info(os_name os_version os_codename)
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ cmake --build . --target package
cmake --build . --target install
```

# Running tests

The project builds a `tests` binary which contains tests from the `tests`
project subdirectory, as well as a `zello_world` binary from the `samples`
project subdirectory. Set the environment variable `ZEL_LIBRARY_PATH` to point
to `build/lib` on Linux and `build/bin/Release` (as an absolute path) on
Windows. Set `ZE_ENABLE_NULL_DRIVER=1` if your system has no working driver
installed. Refer to
[.github/workflows/build-quick.yml](.github/workflows/build-quick.yml) for
examples.

# Debug Trace
The Level Zero Loader has the ability to print warnings and errors which occur within the internals of the Level Zero Loader itself.

Expand Down
24 changes: 17 additions & 7 deletions source/loader/ze_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,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, "");
Expand All @@ -299,7 +311,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();
}
Expand All @@ -325,15 +337,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" ) )
{
Expand Down Expand Up @@ -362,6 +371,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)
{
Expand Down
19 changes: 17 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# 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$<$<CONFIG:Debug>:d>")
endif()
3 changes: 0 additions & 3 deletions test/layers/CMakeLists.txt

This file was deleted.

3 changes: 0 additions & 3 deletions test/layers/validation/CMakeLists.txt

This file was deleted.

This file was deleted.

45 changes: 45 additions & 0 deletions test/loader_api.cpp
Original file line number Diff line number Diff line change
@@ -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<zel_component_version_t> 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
17 changes: 0 additions & 17 deletions test/test_api/CMakeLists.txt

This file was deleted.

19 changes: 0 additions & 19 deletions test/test_api/src/main.cpp

This file was deleted.

43 changes: 0 additions & 43 deletions test/test_api/src/test_api.cpp

This file was deleted.

0 comments on commit d11de6b

Please sign in to comment.