Skip to content

Commit 9cf1a74

Browse files
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 <[email protected]> Co-authored-by: Jemale Lockett <[email protected]>
1 parent 172996b commit 9cf1a74

File tree

11 files changed

+114
-100
lines changed

11 files changed

+114
-100
lines changed

.github/workflows/build-quick.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,28 @@ jobs:
2222
-D CMAKE_C_COMPILER_LAUNCHER=ccache \
2323
-D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
2424
-D CMAKE_BUILD_TYPE=Release \
25+
-D BUILD_L0_LOADER_TESTS=1 \
2526
..
2627
make -j$(nproc)
28+
- env:
29+
ZE_ENABLE_LOADER_DEBUG_TRACE: '1'
30+
ZEL_LIBRARY_PATH: '${{ github.workspace }}/build/lib'
31+
working-directory: build
32+
run: ctest -V
33+
2734
build-windows:
2835
if: github.repository_owner == 'oneapi-src'
2936
runs-on: [windows-latest]
3037
steps:
3138
- uses: actions/checkout@v3
3239
- name: Build Loader on Latest Windows
33-
shell: pwsh
3440
run: |
3541
mkdir build
3642
cd build
37-
cmake -D CMAKE_BUILD_TYPE=Release ..
38-
cmake --build . --config Release
43+
cmake -D BUILD_L0_LOADER_TESTS=1 ..
44+
cmake --build . --config Release
45+
- env:
46+
ZE_ENABLE_LOADER_DEBUG_TRACE: '1'
47+
ZEL_LIBRARY_PATH: '${{ github.workspace }}/build/bin/Release'
48+
working-directory: build
49+
run: ctest -C Release -V

CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ else()
8989
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog_headers")
9090
endif()
9191

92+
include(FetchContent)
93+
94+
if(BUILD_L0_LOADER_TESTS)
95+
FetchContent_Declare(
96+
googletest
97+
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
98+
)
99+
100+
# For Windows: Prevent overriding the parent project's compiler/linker settings
101+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
102+
103+
FetchContent_MakeAvailable(googletest)
104+
105+
enable_testing()
106+
endif()
107+
92108
# Update other relevant variables to include the patch
93109
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
94110
set(CMAKE_PROJECT_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
@@ -206,7 +222,8 @@ add_subdirectory(source)
206222
add_subdirectory(samples)
207223

208224
if(BUILD_L0_LOADER_TESTS)
209-
add_subdirectory(test)
225+
include(CTest)
226+
add_subdirectory(test)
210227
endif()
211228

212229
include("os_release_info.cmake")

source/loader/ze_loader.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,18 @@ namespace loader
329329
if (zel_logger->logging_enabled)
330330
zel_logger->get_base_logger()->info("Loader Version {}.{}.{} {}", LOADER_VERSION_MAJOR, LOADER_VERSION_MINOR, LOADER_VERSION_PATCH, LOADER_VERSION_SHA);
331331

332+
add_loader_version();
333+
std::string loaderLibraryPath;
334+
auto loaderLibraryPathEnv = getenv_string("ZEL_LIBRARY_PATH");
335+
if (!loaderLibraryPathEnv.empty()) {
336+
loaderLibraryPath = loaderLibraryPathEnv;
337+
}
338+
#ifdef _WIN32
339+
else {
340+
loaderLibraryPath = readLevelZeroLoaderLibraryPath();
341+
}
342+
#endif
343+
debug_trace_message("Using Loader Library Path: ", loaderLibraryPath);
332344

333345
// To allow for two different sets of drivers to be in use between sysman and core/tools, we use and store the drivers in two vectors.
334346
// alldrivers stores all the drivers for cleanup when the library exits.
@@ -338,7 +350,7 @@ namespace loader
338350
if( getenv_tobool( "ZE_ENABLE_NULL_DRIVER" ) )
339351
{
340352
zel_logger->log_info("Enabling Null Driver");
341-
auto handle = LOAD_DRIVER_LIBRARY( MAKE_LIBRARY_NAME( "ze_null", L0_LOADER_VERSION ) );
353+
auto handle = LOAD_DRIVER_LIBRARY( create_library_path( MAKE_LIBRARY_NAME( "ze_null", L0_LOADER_VERSION ), loaderLibraryPath.c_str()).c_str());
342354
if (debugTraceEnabled) {
343355
std::string message = "ze_null Driver Init";
344356
debug_trace_message(message, "");
@@ -350,7 +362,7 @@ namespace loader
350362
allDrivers.rbegin()->name = "ze_null";
351363
} else if (debugTraceEnabled) {
352364
GET_LIBRARY_ERROR(loadLibraryErrorValue);
353-
std::string errorMessage = "Load Library of " + std::string(MAKE_LIBRARY_NAME( "ze_null", L0_LOADER_VERSION )) + " failed with ";
365+
std::string errorMessage = "Load Library of " + create_library_path( MAKE_LIBRARY_NAME( "ze_null", L0_LOADER_VERSION ), loaderLibraryPath.c_str()) + " failed with ";
354366
debug_trace_message(errorMessage, loadLibraryErrorValue);
355367
loadLibraryErrorValue.clear();
356368
}
@@ -376,17 +388,14 @@ namespace loader
376388
}
377389
}
378390
if(allDrivers.size()==0){
391+
std::string message = "0 Drivers Discovered";
392+
debug_trace_message(message, "");
379393
zel_logger->log_error("0 Drivers Discovered");
380394
return ZE_RESULT_ERROR_UNINITIALIZED;
381395
}
382396
std::copy(allDrivers.begin(), allDrivers.end(), std::back_inserter(zeDrivers));
383397
std::copy(allDrivers.begin(), allDrivers.end(), std::back_inserter(zesDrivers));
384398

385-
add_loader_version();
386-
std::string loaderLibraryPath;
387-
#ifdef _WIN32
388-
loaderLibraryPath = readLevelZeroLoaderLibraryPath();
389-
#endif
390399
typedef ze_result_t (ZE_APICALL *getVersion_t)(zel_component_version_t *version);
391400
if( getenv_tobool( "ZE_ENABLE_VALIDATION_LAYER" ) )
392401
{
@@ -415,6 +424,7 @@ namespace loader
415424
tracingLayerEnabled = true;
416425
}
417426
std::string tracingLayerLibraryPath = create_library_path(MAKE_LAYER_NAME( "ze_tracing_layer" ), loaderLibraryPath.c_str());
427+
debug_trace_message("Tracing Layer Library Path: ", tracingLayerLibraryPath);
418428
tracingLayer = LOAD_DRIVER_LIBRARY( tracingLayerLibraryPath.c_str() );
419429
if(tracingLayer)
420430
{

test/CMakeLists.txt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
# Copyright (C) 2024 Intel Corporation
22
# SPDX-License-Identifier: MIT
3-
add_subdirectory(layers)
4-
add_subdirectory(test_api)
3+
4+
add_executable(
5+
tests
6+
loader_api.cpp
7+
)
8+
target_include_directories(tests PRIVATE ${CMAKE_SOURCE_DIR}/include)
9+
target_link_libraries(
10+
tests
11+
GTest::gtest_main
12+
${TARGET_LOADER_NAME}
13+
)
14+
15+
# For some reason the MSVC runtime libraries used by googletest and test
16+
# binaries don't match, so force the test binary to use the dynamic runtime.
17+
if(MSVC)
18+
target_compile_options(tests PRIVATE "/MD$<$<CONFIG:Debug>:d>")
19+
endif()
20+
21+
add_test(NAME tests COMMAND tests)
22+
set_property(TEST tests PROPERTY ENVIRONMENT "ZE_ENABLE_NULL_DRIVER=1")

test/layers/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/layers/validation/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/layers/validation/handle_lifetime_tracking/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

test/loader_api.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* SPDX-License-Identifier: MIT
6+
*
7+
*/
8+
9+
#include "gtest/gtest.h"
10+
11+
#include "loader/ze_loader.h"
12+
#include "ze_api.h"
13+
14+
namespace {
15+
16+
TEST(
17+
LoaderAPI,
18+
GivenLevelZeroLoaderPresentWhenCallingzeGetLoaderVersionsAPIThenValidVersionIsReturned) {
19+
20+
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(0));
21+
22+
size_t size = 0;
23+
EXPECT_EQ(ZE_RESULT_SUCCESS, zelLoaderGetVersions(&size, nullptr));
24+
EXPECT_GT(size, 0);
25+
26+
std::vector<zel_component_version_t> versions(size);
27+
EXPECT_EQ(ZE_RESULT_SUCCESS, zelLoaderGetVersions(&size, versions.data()));
28+
29+
std::cout << "Found " << versions.size() << " versions" << std::endl;
30+
std::cout << std::endl;
31+
const std::string loader_name = "loader";
32+
for (auto &component : versions) {
33+
std::cout << "component.component_name: " << component.component_name << std::endl;
34+
std::cout << "component.component_lib_version.major: " << component.component_lib_version.major << std::endl;
35+
std::cout << "component.spec_version: " << component.spec_version << std::endl;
36+
std::cout << "component.component_lib_name: " << component.component_name << std::endl;
37+
std::cout << std::endl;
38+
39+
if (loader_name == component.component_name) {
40+
EXPECT_GE(component.component_lib_version.major, 1);
41+
}
42+
}
43+
}
44+
45+
} // namespace

test/test_api/CMakeLists.txt

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/test_api/src/main.cpp

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)