test: improve unit tests - use virtual file system

- use virtualFileSystem in ULTs, writeDataToFile() does not create file
but instead saves filename. Call to fileExists() checks if file was
previously written

Related-To: NEO-7006

Signed-off-by: Mateusz Hoppe <mateusz.hoppe@intel.com>
This commit is contained in:
Mateusz Hoppe
2024-03-08 09:48:00 +00:00
committed by Compute-Runtime-Automation
parent 2f03c48c7a
commit 5e15543b14
26 changed files with 107 additions and 46 deletions

View File

@@ -221,6 +221,7 @@ if(BUILD_WITH_L0)
${NEO_SHARED_DIRECTORY}/gmm_helper/resource_info.cpp
${NEO_SHARED_DIRECTORY}/helpers/abort.cpp
${NEO_SHARED_DIRECTORY}/helpers/debug_helpers.cpp
${NEO_SHARED_DIRECTORY}/helpers/file_io.cpp
${NEO_SHARED_DIRECTORY}/utilities/cpuintrinsics.cpp
${NEO_SHARED_DIRECTORY}/utilities/debug_settings_reader_creator.cpp
${NEO_SHARED_DIRECTORY}/utilities/io_functions.cpp

View File

@@ -1,5 +1,5 @@
#
# Copyright (C) 2020-2023 Intel Corporation
# Copyright (C) 2020-2024 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
@@ -9,6 +9,7 @@ target_sources(${TARGET_NAME_L0}
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/disallow_deferred_deleter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/create_builtin_functions_lib.cpp
${NEO_SHARED_DIRECTORY}/helpers/file_io.cpp
)
add_subdirectories()

View File

@@ -13,6 +13,7 @@ if(DEFINED AUB_STREAM_PROJECT_NAME)
list(APPEND L0_AUB_TESTS__TARGET_OBJECTS
${CMAKE_CURRENT_SOURCE_DIR}/aub_hello_world_test.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/common_main.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/virtual_file_system_listener.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/tests_configuration.h
)

View File

@@ -28,6 +28,7 @@ target_sources(${TARGET_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/mock.h
${CMAKE_CURRENT_SOURCE_DIR}/white_box.h
${NEO_SHARED_TEST_DIRECTORY}/common/common_main.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/virtual_file_system_listener.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/tests_configuration.h
${NEO_SOURCE_DIR}/level_zero/core/test/common/test_modules/gen_kernel.cmake
${NEO_SOURCE_DIR}/level_zero/core/test/common/ult_specific_config_l0.cpp

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
* Copyright (C) 2020-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -7,12 +7,12 @@
#include "shared/source/compiler_interface/external_functions.h"
#include "shared/source/device_binary_format/patchtokens_decoder.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/kernel/kernel_descriptor_from_patchtokens.h"
#include "shared/source/os_interface/os_interface.h"
#include "shared/source/program/kernel_info.h"
#include "shared/source/program/kernel_info_from_patchtokens.h"
#include "shared/test/common/compiler_interface/linker_mock.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/helpers/unit_test_helper.h"
#include "shared/test/common/mocks/mock_compilers.h"
#include "shared/test/common/mocks/mock_elf.h"
@@ -393,11 +393,11 @@ HWTEST_F(ModuleWithZebinAndL0DebuggerTest, GivenDumpElfFlagAndZebinWhenInitializ
zebin.storage.data(), zebin.storage.size());
std::string fileName = "dumped_debug_module.elf";
EXPECT_FALSE(fileExists(fileName));
EXPECT_FALSE(virtualFileExists(fileName));
EXPECT_EQ(moduleMock->initialize(&moduleDesc, neoDevice), ZE_RESULT_SUCCESS);
EXPECT_TRUE(fileExists(fileName));
std::remove(fileName.c_str());
EXPECT_TRUE(virtualFileExists(fileName));
removeVirtualFile(fileName.c_str());
}
HWTEST_F(ModuleWithZebinAndL0DebuggerTest, GivenZebinNoDebugDataWhenInitializingModuleThenDoNotRegisterElfAndDoNotNotifyModuleCreate) {

View File

@@ -16,7 +16,6 @@
#include "shared/source/helpers/addressing_mode_helper.h"
#include "shared/source/helpers/blit_helper.h"
#include "shared/source/helpers/compiler_product_helper.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/gfx_core_helper.h"
#include "shared/source/kernel/implicit_args_helper.h"
#include "shared/source/os_interface/os_inc_base.h"
@@ -24,6 +23,7 @@
#include "shared/test/common/compiler_interface/linker_mock.h"
#include "shared/test/common/device_binary_format/patchtokens_tests.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/mocks/mock_device.h"
#include "shared/test/common/mocks/mock_elf.h"
#include "shared/test/common/mocks/mock_graphics_allocation.h"
@@ -3369,13 +3369,13 @@ HWTEST_F(ModuleTranslationUnitTest, givenDumpZebinWhenBuildingFromSpirvThenZebin
ze_result_t result = ZE_RESULT_ERROR_MODULE_BUILD_FAILURE;
std::string fileName = "dumped_zebin_module.elf";
EXPECT_FALSE(fileExists(fileName));
EXPECT_FALSE(virtualFileExists(fileName));
result = moduleTu.buildFromSpirV(binary, sizeof(binary), nullptr, "", nullptr);
EXPECT_EQ(result, ZE_RESULT_SUCCESS);
EXPECT_TRUE(fileExistsHasSize(fileName));
std::remove(fileName.c_str());
EXPECT_TRUE(virtualFileExists(fileName));
removeVirtualFile(fileName);
PatchTokensTestData::ValidEmptyProgram programTokens;
mockCompilerInterface->output.intermediateRepresentation.size = programTokens.storage.size();
@@ -3389,9 +3389,8 @@ HWTEST_F(ModuleTranslationUnitTest, givenDumpZebinWhenBuildingFromSpirvThenZebin
result = moduleTu2.buildFromSpirV(binary, sizeof(binary), nullptr, "", nullptr);
EXPECT_FALSE(fileExists(fileName));
EXPECT_FALSE(fileExistsHasSize(fileName));
std::remove(fileName.c_str());
EXPECT_FALSE(virtualFileExists(fileName));
removeVirtualFile(fileName);
}
HWTEST2_F(ModuleTranslationUnitTest, givenDebugFlagSetForceAllResourcesUncachedWhenGetInternalOptionsThenCorrectBuildOptionIsSet, IsAtLeastXeHpgCore) {

View File

@@ -32,6 +32,7 @@ target_sources(${TARGET_NAME} PRIVATE
${NEO_SOURCE_DIR}/level_zero/core/source/dll/create_builtin_functions_lib.cpp
${NEO_SOURCE_DIR}/level_zero/tools/test/unit_tests/sources/debug/debug_session_helper.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/common_main.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/virtual_file_system_listener.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/tests_configuration.h
)

View File

@@ -32,6 +32,7 @@ target_sources(${TARGET_NAME} PRIVATE
${NEO_SOURCE_DIR}/level_zero/core/test/unit_tests/sources/builtin/create_ult_builtin_functions_lib.cpp
${NEO_SOURCE_DIR}/level_zero/tools/test/unit_tests/sources/debug/debug_session_helper.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/common_main.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/virtual_file_system_listener.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/tests_configuration.h
)

View File

@@ -27,6 +27,7 @@ set(RUNTIME_SRCS_DLL_BASE
${NEO_SHARED_DIRECTORY}/helpers/abort.cpp
${NEO_SHARED_DIRECTORY}/helpers/allow_deferred_deleter.cpp
${NEO_SHARED_DIRECTORY}/helpers/debug_helpers.cpp
${NEO_SHARED_DIRECTORY}/helpers/file_io.cpp
${NEO_SHARED_DIRECTORY}/utilities/cpuintrinsics.cpp
${NEO_SHARED_DIRECTORY}/utilities/debug_settings_reader_creator.cpp
${NEO_SHARED_DIRECTORY}/utilities/io_functions.cpp

View File

@@ -37,6 +37,7 @@ set(IGDRCL_SRCS_LIB_ULT_ENV
${CMAKE_CURRENT_SOURCE_DIR}/command_queue_ult.cpp
${NEO_SOURCE_DIR}/shared/test/common/helpers/custom_event_listener.h
${NEO_SOURCE_DIR}/shared/test/common/common_main.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/virtual_file_system_listener.cpp
${NEO_SOURCE_DIR}/opencl/test/unit_test/command_queue/command_queue_fixture.cpp
${NEO_SOURCE_DIR}/opencl/test/unit_test/command_queue/command_queue_fixture.h
${NEO_SOURCE_DIR}/opencl/test/unit_test/fixtures/built_in_fixture.cpp

View File

@@ -1,5 +1,5 @@
#
# Copyright (C) 2018-2023 Intel Corporation
# Copyright (C) 2018-2024 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
@@ -20,6 +20,7 @@ add_executable(igdrcl_${target_name}
${CMAKE_CURRENT_SOURCE_DIR}/mock_os_layer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mock_os_layer.h
${CMAKE_CURRENT_SOURCE_DIR}/os_interface_linux_tests.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/virtual_file_system_listener.cpp
${NEO_SHARED_DIRECTORY}/os_interface/linux/sys_calls_linux.cpp
${NEO_SHARED_DIRECTORY}/dll/create_memory_manager_drm.cpp
${NEO_SHARED_DIRECTORY}/dll/device_dll.cpp

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
* Copyright (C) 2020-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -14,6 +14,7 @@
#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/memory_manager/allocation_properties.h"
#include "shared/source/memory_manager/memory_manager.h"
#include "shared/source/os_interface/os_context.h"

View File

@@ -68,7 +68,6 @@ set(NEO_CORE_HELPERS
${CMAKE_CURRENT_SOURCE_DIR}/engine_node_helper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/engine_node_helper.h
${CMAKE_CURRENT_SOURCE_DIR}/extendable_enum.h
${CMAKE_CURRENT_SOURCE_DIR}/file_io.cpp
${CMAKE_CURRENT_SOURCE_DIR}/file_io_load.cpp
${CMAKE_CURRENT_SOURCE_DIR}/file_io.h
${CMAKE_CURRENT_SOURCE_DIR}/flat_batch_buffer_helper.h

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2023 Intel Corporation
* Copyright (C) 2018-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -61,14 +61,3 @@ bool fileExistsHasSize(const std::string &fileName) {
}
return pFile != nullptr && nsize > 0;
}
void dumpFileIncrement(const char *data, size_t dataSize, const std::string &filename, const std::string &extension) {
std::ofstream fstream;
auto filenameWithExt = filename + extension;
int suffix = 0;
while (fileExists(filenameWithExt)) {
filenameWithExt = filename + "_" + std::to_string(suffix) + extension;
suffix++;
}
writeDataToFile(filenameWithExt.c_str(), data, dataSize);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 Intel Corporation
* Copyright (C) 2022-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -48,3 +48,13 @@ std::unique_ptr<char[]> loadDataFromFile(
retSize = nsize;
return ret;
}
void dumpFileIncrement(const char *data, size_t dataSize, const std::string &filename, const std::string &extension) {
auto filenameWithExt = filename + extension;
int suffix = 0;
while (fileExists(filenameWithExt)) {
filenameWithExt = filename + "_" + std::to_string(suffix) + extension;
suffix++;
}
writeDataToFile(filenameWithExt.c_str(), data, dataSize);
}

View File

@@ -18,6 +18,7 @@
#include "shared/test/common/helpers/memory_leak_listener.h"
#include "shared/test/common/helpers/test_files.h"
#include "shared/test/common/helpers/ult_hw_config.inl"
#include "shared/test/common/helpers/virtual_file_system_listener.h"
#include "shared/test/common/libult/global_environment.h"
#include "shared/test/common/libult/signal_utils.h"
#include "shared/test/common/mocks/mock_gmm_client_context.h"
@@ -365,6 +366,7 @@ int main(int argc, char **argv) {
}
listeners.Append(new MemoryLeakListener);
listeners.Append(new NEO::VirtualFileSystemListener);
addUltListener(listeners);
ultListenersInitialized = true;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 Intel Corporation
* Copyright (C) 2022-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -63,3 +63,14 @@ bool fileExistsHasSize(const std::string &fileName) {
}
return pFile != nullptr && nsize > 0;
}
void removeVirtualFile(const std::string &fileName) {
NEO::virtualFileList.erase(fileName);
}
bool virtualFileExists(const std::string &fileName) {
if (NEO::virtualFileList.count(fileName) > 0) {
return true;
}
return false;
}

View File

@@ -0,0 +1,11 @@
/*
* Copyright (C) 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/helpers/file_io.h"
bool virtualFileExists(const std::string &fileName);
void removeVirtualFile(const std::string &fileName);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 Intel Corporation
* Copyright (C) 2022-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -7,6 +7,8 @@
#include "shared/test/common/helpers/virtual_file_system_listener.h"
#include "shared/test/common/helpers/mock_file_io.h"
namespace NEO {
std::set<std::string> virtualFileList;

View File

@@ -1,5 +1,5 @@
#
# Copyright (C) 2021-2023 Intel Corporation
# Copyright (C) 2021-2024 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
@@ -110,6 +110,7 @@ set(neo_libult_common_SRCS_LIB_ULT
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/unit_test_helper.h
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/unit_test_helper.inl
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/unit_test_helper_bdw_and_later.inl
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/file_io.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/libult/abort.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/libult/create_tbx_sockets.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/libult/debug_settings_reader_creator.cpp

View File

@@ -21,6 +21,7 @@ add_executable(neo_shared_tests
${CMAKE_CURRENT_SOURCE_DIR}/ult_specific_config.cpp
${NEO_SHARED_DIRECTORY}/helpers/allow_deferred_deleter.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/common_main.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/virtual_file_system_listener.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/tests_configuration.h
$<TARGET_OBJECTS:mock_gmm>
$<TARGET_OBJECTS:neo_libult_common>

View File

@@ -26,7 +26,12 @@
#include "common/StateSaveAreaHeader.h"
#include <set>
using namespace NEO;
namespace NEO {
extern std::set<std::string> virtualFileList;
}
struct RawBinarySipFixture : public DeviceFixture {
void setUp() {
@@ -746,4 +751,4 @@ TEST_F(DebugBuiltinSipTest, givenDebuggerWhenInitSipKernelThenDbgSipIsLoadedFrom
auto sipAllocation = SipKernel::getSipKernel(*pDevice, nullptr).getSipAllocation();
EXPECT_NE(nullptr, sipAllocation);
EXPECT_EQ(SipKernelMock::classType, SipClassType::builtins);
}
}

View File

@@ -18,6 +18,7 @@
#include "shared/test/common/utilities/base_object_utils.h"
#include <cstdio>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
@@ -341,7 +342,10 @@ TEST(DebugSettingsManager, givenDisabledDebugManagerWhenCreateThenOnlyReleaseVar
bool settingsFileExists = fileExists(SettingsReader::settingsFileName);
if (!settingsFileExists) {
const char data[] = "LogApiCalls = 1\nMakeAllBuffersResident = 1";
writeDataToFile(SettingsReader::settingsFileName, &data, sizeof(data));
std::ofstream file;
file.open(SettingsReader::settingsFileName);
file << data;
file.close();
}
SettingsReader *reader = SettingsReader::createFileReader();
@@ -355,7 +359,7 @@ TEST(DebugSettingsManager, givenDisabledDebugManagerWhenCreateThenOnlyReleaseVar
EXPECT_EQ(0, debugManager.flags.LogApiCalls.get());
if (!settingsFileExists) {
remove(SettingsReader::settingsFileName);
std::remove(SettingsReader::settingsFileName);
}
}
@@ -363,7 +367,10 @@ TEST(DebugSettingsManager, givenEnabledDebugManagerWhenCreateThenAllVariablesAre
bool settingsFileExists = fileExists(SettingsReader::settingsFileName);
if (!settingsFileExists) {
const char data[] = "LogApiCalls = 1\nMakeAllBuffersResident = 1";
writeDataToFile(SettingsReader::settingsFileName, &data, sizeof(data));
std::ofstream file;
file.open(SettingsReader::settingsFileName);
file << data;
file.close();
}
SettingsReader *reader = SettingsReader::createFileReader();
@@ -377,7 +384,7 @@ TEST(DebugSettingsManager, givenEnabledDebugManagerWhenCreateThenAllVariablesAre
EXPECT_EQ(1, debugManager.flags.LogApiCalls.get());
if (!settingsFileExists) {
remove(SettingsReader::settingsFileName);
std::remove(SettingsReader::settingsFileName);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
* Copyright (C) 2020-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -12,6 +12,7 @@
#include "shared/test/common/mocks/mock_io_functions.h"
#include "shared/test/common/test_macros/test.h"
#include <fstream>
#include <unordered_map>
namespace NEO {
@@ -20,9 +21,11 @@ TEST(DebugSettingsManager, givenDisabledDebugManagerAndMockEnvVariableWhenCreate
bool settingsFileExists = fileExists(SettingsReader::settingsFileName);
if (!settingsFileExists) {
const char data[] = "LogApiCalls = 1\nMakeAllBuffersResident = 1";
writeDataToFile(SettingsReader::settingsFileName, &data, sizeof(data));
std::ofstream file;
file.open(SettingsReader::settingsFileName);
file << data;
file.close();
}
SettingsReader *reader = SettingsReader::createFileReader();
EXPECT_NE(nullptr, reader);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
* Copyright (C) 2020-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -14,6 +14,7 @@
#include "shared/source/helpers/string.h"
#include "shared/source/program/program_info.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/mocks/mock_modules_zebin.h"
#include "shared/test/common/test_macros/test.h"
@@ -138,12 +139,12 @@ TEST(UnpackSingleDeviceBinaryZebin, givenDumpZEBinFlagSetWhenUnpackingZebinBinar
EXPECT_FALSE(fileExists(fileNameInc));
NEO::unpackSingleDeviceBinary<NEO::DeviceBinaryFormat::zebin>(ArrayRef<const uint8_t>::fromAny(&zebin, 1U), "", targetDevice, unpackErrors, unpackWarnings);
EXPECT_TRUE(fileExistsHasSize(fileName));
EXPECT_TRUE(virtualFileExists(fileName));
NEO::unpackSingleDeviceBinary<NEO::DeviceBinaryFormat::zebin>(ArrayRef<const uint8_t>::fromAny(&zebin, 1U), "", targetDevice, unpackErrors, unpackWarnings);
EXPECT_TRUE(fileExistsHasSize(fileNameInc));
EXPECT_TRUE(virtualFileExists(fileNameInc));
std::remove(fileName.c_str());
std::remove(fileNameInc.c_str());
removeVirtualFile(fileName);
removeVirtualFile(fileNameInc);
}
TEST(UnpackSingleDeviceBinaryZebin, WhenValidBinaryForDifferentDeviceThenUnpackingFails) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2023 Intel Corporation
* Copyright (C) 2018-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -36,6 +36,15 @@ class MockSettingsReader : public SettingsReader {
const char *appSpecificLocation(const std::string &name) override { return name.c_str(); };
};
namespace SettingsReaderTests {
void writeDataToFile(const char *filename, const void *pData, size_t dataSize) {
std::ofstream file;
file.open(filename);
file.write(static_cast<const char *>(pData), dataSize);
file.close();
}
TEST(SettingsReader, WhenCreatingSettingsReaderThenReaderIsCreated) {
auto reader = std::unique_ptr<SettingsReader>(SettingsReader::create(ApiSpecificConfig::getRegistryPath()));
EXPECT_NE(nullptr, reader.get());
@@ -114,3 +123,4 @@ TEST(SettingsReader, GivenFalseWhenPrintingDebugStringThenNoOutput) {
std::string output = testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.c_str(), "");
}
} // namespace SettingsReaderTests