refactor: Mock file system in ULTs

Functions: fileExists and loadDataToFile
use IO functions from namespace IoFunctions

Now tests that use these functions
are mocked by default,
but some still require access to real files
and have been restored the ability to read files.
They will be mocked in next PRs.

Related-To: NEO-7006
Signed-off-by: Marcel Skierkowski <marcel.skierkowski@intel.com>
This commit is contained in:
Marcel Skierkowski
2025-04-09 15:18:28 +00:00
committed by Compute-Runtime-Automation
parent 077fe1ab48
commit 5d01677454
56 changed files with 349 additions and 80 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -9,6 +9,7 @@
#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/helpers/stdio.h"
#include "shared/source/utilities/io_functions.h"
#include <cstring>
#include <map>
@@ -42,9 +43,9 @@ bool fileExists(const std::string &fileName) {
return true;
}
fopen_s(&pFile, fileName.c_str(), "rb");
pFile = NEO::IoFunctions::fopenPtr(fileName.c_str(), "rb");
if (pFile) {
fclose(pFile);
NEO::IoFunctions::fclosePtr(pFile);
}
return pFile != nullptr;
}

View File

@@ -11,6 +11,7 @@
#include "shared/source/compiler_interface/intermediate_representations.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/helpers/array_count.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/hash.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/string.h"
@@ -372,6 +373,7 @@ TEST(CompilerCacheTests, GivenEmptyBinaryWhenCachingThenBinaryIsNotCached) {
}
TEST(CompilerCacheTests, GivenNonExistantConfigWhenLoadingFromCacheThenNullIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
CompilerCache cache(CompilerCacheConfig{});
size_t size;
auto ret = cache.loadCachedBinary("----do-not-exists----", size);
@@ -409,6 +411,8 @@ TEST(CompilerInterfaceCachedTests, GivenNoCachedBinaryWhenBuildingThenErrorIsRet
}
TEST(CompilerInterfaceCachedTests, GivenCachedBinaryWhenBuildingThenSuccessIsReturned) {
USE_REAL_FILE_SYSTEM();
TranslationInput inputArgs{IGC::CodeType::oclC, IGC::CodeType::oclGenBin};
auto src = "#include \"header.h\"\n__kernel k() {}";
@@ -635,6 +639,8 @@ TEST_F(CompilerInterfaceOclElfCacheTest, givenNonEmptyTranslationOutputWhenProce
}
TEST_F(CompilerInterfaceOclElfCacheTest, GivenKernelWithIncludesWhenBuildingThenPackBinaryOnCacheSaveAndUnpackBinaryOnLoadFromCache) {
USE_REAL_FILE_SYSTEM();
gEnvironment->igcPushDebugVars(igcDebugVarsDeviceBinary);
TranslationInput inputArgs{IGC::CodeType::oclC, IGC::CodeType::oclGenBin};
@@ -669,6 +675,8 @@ TEST_F(CompilerInterfaceOclElfCacheTest, GivenKernelWithIncludesWhenBuildingThen
}
TEST_F(CompilerInterfaceOclElfCacheTest, GivenKernelWithIncludesWhenLoadedCacheDoesNotUnpackCorrectlyThenDoNotEndInCacheAndContinueCompilation) {
USE_REAL_FILE_SYSTEM();
gEnvironment->igcPushDebugVars(igcDebugVarsInvalidDeviceBinary);
TranslationInput inputArgs{IGC::CodeType::oclC, IGC::CodeType::oclGenBin};
@@ -700,6 +708,8 @@ TEST_F(CompilerInterfaceOclElfCacheTest, GivenKernelWithIncludesWhenLoadedCacheD
}
TEST_F(CompilerInterfaceOclElfCacheTest, GivenKernelWithIncludesAndDebugDataWhenBuildingThenPackBinaryOnCacheSaveAndUnpackBinaryOnLoadFromCache) {
USE_REAL_FILE_SYSTEM();
gEnvironment->igcPushDebugVars(igcDebugVarsDeviceBinaryDebugData);
TranslationInput inputArgs{IGC::CodeType::oclC, IGC::CodeType::oclGenBin};
@@ -734,6 +744,8 @@ TEST_F(CompilerInterfaceOclElfCacheTest, GivenKernelWithIncludesAndDebugDataWhen
}
TEST_F(CompilerInterfaceOclElfCacheTest, GivenBinaryWhenBuildingThenPackBinaryOnCacheSaveAndUnpackBinaryOnLoadFromCache) {
USE_REAL_FILE_SYSTEM();
gEnvironment->igcPushDebugVars(igcDebugVarsDeviceBinary);
TranslationInput inputArgs{IGC::CodeType::oclC, IGC::CodeType::oclGenBin};
@@ -768,6 +780,8 @@ TEST_F(CompilerInterfaceOclElfCacheTest, GivenBinaryWhenBuildingThenPackBinaryOn
}
TEST_F(CompilerInterfaceOclElfCacheTest, GivenBinaryWhenLoadedCacheDoesNotUnpackCorrectlyThenDoNotEndInCacheAndContinueCompilation) {
USE_REAL_FILE_SYSTEM();
gEnvironment->igcPushDebugVars(igcDebugVarsInvalidDeviceBinary);
TranslationInput inputArgs{IGC::CodeType::oclC, IGC::CodeType::oclGenBin};
@@ -796,6 +810,8 @@ TEST_F(CompilerInterfaceOclElfCacheTest, GivenBinaryWhenLoadedCacheDoesNotUnpack
}
TEST_F(CompilerInterfaceOclElfCacheTest, GivenBinaryAndDebugDataWhenBuildingThenPackBinaryOnCacheSaveAndUnpackBinaryOnLoadFromCache) {
USE_REAL_FILE_SYSTEM();
gEnvironment->igcPushDebugVars(igcDebugVarsDeviceBinaryDebugData);
TranslationInput inputArgs{IGC::CodeType::oclC, IGC::CodeType::oclGenBin};
@@ -967,4 +983,4 @@ TEST_F(CompilerCacheHelperMockedWhitelistedIncludesTests, GivenEnabledCacheAndNo
EXPECT_EQ(CachingMode::preProcess, CompilerCacheHelper::getCachingMode(&cache, testCase.second, sourceRef))
<< "Failed for source: " << testCase.first;
}
}
}

View File

@@ -45,7 +45,7 @@ class CompilerInterfaceTest : public DeviceFixture,
public:
void SetUp() override {
DeviceFixture::setUp();
USE_REAL_FILE_SYSTEM();
// create the compiler interface
this->pCompilerInterface = new MockCompilerInterface();
bool initRet = pCompilerInterface->initialize(std::make_unique<CompilerCache>(CompilerCacheConfig{}), true);
@@ -152,12 +152,16 @@ TEST(CompilerInterfaceCreateInstance, WhenInitializeFailedThenReturnNull) {
}
TEST_F(CompilerInterfaceTest, WhenCompilingToIsaThenSuccessIsReturned) {
USE_REAL_FILE_SYSTEM();
TranslationOutput translationOutput;
auto err = pCompilerInterface->build(*pDevice, inputArgs, translationOutput);
EXPECT_EQ(TranslationOutput::ErrorCode::success, err);
}
TEST_F(CompilerInterfaceTest, WhenPreferredIntermediateRepresentationSpecifiedThenPreserveIt) {
USE_REAL_FILE_SYSTEM();
CompilerCacheConfig config = {};
config.enabled = false;
auto tempCompilerCache = std::make_unique<CompilerCache>(config);
@@ -191,6 +195,8 @@ TEST_F(CompilerInterfaceTest, whenFclTranslatorReturnsNullptrThenBuildFailsGrace
}
TEST_F(CompilerInterfaceTest, whenIgcTranslatorReturnsNullptrThenBuildFailsGracefully) {
USE_REAL_FILE_SYSTEM();
CompilerCacheConfig config = {};
config.enabled = false;
auto tempCompilerCache = std::make_unique<CompilerCache>(config);
@@ -203,6 +209,8 @@ TEST_F(CompilerInterfaceTest, whenIgcTranslatorReturnsNullptrThenBuildFailsGrace
}
TEST_F(CompilerInterfaceTest, GivenOptionsWhenCompilingToIsaThenSuccessIsReturned) {
USE_REAL_FILE_SYSTEM();
std::string internalOptions = "SOME_OPTION";
MockCompilerDebugVars fclDebugVars;
@@ -226,6 +234,8 @@ TEST_F(CompilerInterfaceTest, GivenOptionsWhenCompilingToIsaThenSuccessIsReturne
}
TEST_F(CompilerInterfaceTest, WhenCompilingToIrThenSuccessIsReturned) {
USE_REAL_FILE_SYSTEM();
MockCompilerDebugVars fclDebugVars;
retrieveBinaryKernelFilename(fclDebugVars.fileName, "CopyBufferShared_simd32_", ".spv");
gEnvironment->fclPushDebugVars(fclDebugVars);
@@ -307,6 +317,8 @@ TEST_F(CompilerInterfaceTest, GivenForceBuildFailureWhenLinkingIrThenLinkFailure
}
TEST_F(CompilerInterfaceTest, WhenLinkIsCalledThenOclGenBinIsTheTranslationTarget) {
USE_REAL_FILE_SYSTEM();
// link only from .ll to gen ISA
MockCompilerDebugVars igcDebugVars;
retrieveBinaryKernelFilename(igcDebugVars.fileName, "CopyBufferShared_simd32_", ".spv");
@@ -375,6 +387,8 @@ TEST_F(CompilerInterfaceTest, GivenForceBuildFailureWhenCreatingLibraryThenLinkF
}
TEST_F(CompilerInterfaceTest, WhenCreateLibraryIsCalledThenLlvmBcIsUsedAsIntermediateRepresentation) {
USE_REAL_FILE_SYSTEM();
// create library from .ll to IR
MockCompilerDebugVars igcDebugVars;
retrieveBinaryKernelFilename(igcDebugVars.fileName, "CopyBufferShared_simd32_", ".spv");
@@ -741,6 +755,8 @@ struct WasLockedListener {
};
TEST_F(CompilerInterfaceTest, givenUpdatedSpecConstValuesWhenBuildProgramThenProperValuesArePassed) {
USE_REAL_FILE_SYSTEM();
struct MockTranslationContextSpecConst : public MockIgcOclTranslationCtx {
IGC::OclTranslationOutputBase *TranslateImpl(
CIF::Version_t outVersion,
@@ -1478,4 +1494,4 @@ TEST(getOclCExtensionVersion, whenCheckingVersionOfUntrackedExtensionThenReturns
cl_version defaultVer = CL_MAKE_VERSION(7, 2, 5);
cl_version ver = NEO::getOclCExtensionVersion("other", defaultVer);
EXPECT_EQ(defaultVer, ver);
}
}

View File

@@ -346,6 +346,8 @@ TEST(AllocationInfoLogging, givenBaseGraphicsAllocationWhenGettingImplementation
}
TEST(DebugSettingsManager, givenDisabledDebugManagerWhenCreateThenOnlyReleaseVariablesAreRead) {
USE_REAL_FILE_SYSTEM();
bool settingsFileExists = fileExists(SettingsReader::settingsFileName);
if (!settingsFileExists) {
const char data[] = "LogApiCalls = 1\nMakeAllBuffersResident = 1";
@@ -375,7 +377,6 @@ TEST(DebugSettingsManager, givenEnabledDebugManagerWhenCreateThenAllVariablesAre
writeDataToFile(SettingsReader::settingsFileName, &data, sizeof(data));
SettingsReader *reader = MockSettingsReader::createFileReader();
EXPECT_NE(nullptr, reader);
FullyEnabledTestDebugManager debugManager;
@@ -389,6 +390,7 @@ TEST(DebugSettingsManager, givenEnabledDebugManagerWhenCreateThenAllVariablesAre
}
TEST(DebugSettingsManager, GivenLogsEnabledAndDumpToFileWhenPrintDebuggerLogCalledThenStringPrintedToFile) {
USE_REAL_FILE_SYSTEM();
if (!NEO::fileLoggerInstance().enabled()) {
GTEST_SKIP();
}
@@ -415,6 +417,7 @@ TEST(DebugSettingsManager, GivenLogsEnabledAndDumpToFileWhenPrintDebuggerLogCall
}
TEST(DebugSettingsManager, GivenLogsDisabledAndDumpToFileWhenPrintDebuggerLogCalledThenStringIsNotPrintedToFile) {
USE_REAL_FILE_SYSTEM();
if (!NEO::debugManager.disabled()) {
GTEST_SKIP();
}
@@ -435,6 +438,7 @@ TEST(DebugSettingsManager, GivenLogsDisabledAndDumpToFileWhenPrintDebuggerLogCal
}
TEST(DebugSettingsManager, GivenLogsEnabledWhenLogCacheOperationCalledThenStringPrintedToFile) {
USE_REAL_FILE_SYSTEM();
if (!NEO::usmReusePerfLoggerInstance().enabled()) {
GTEST_SKIP();
}
@@ -512,4 +516,4 @@ TEST(DebugSettingsManager, GivenHardwareOrHardwareWithAubCsrTypeAndTbxFaultsEnab
NEO::debugManager.flags.SetCommandStreamReceiver.set(3);
EXPECT_FALSE(NEO::debugManager.isTbxPageFaultManagerEnabled());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2024 Intel Corporation
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -12,8 +12,10 @@
#include "shared/source/helpers/ptr_math.h"
#include "shared/source/helpers/string.h"
#include "shared/source/program/program_info.h"
#include "shared/source/utilities/io_functions.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "shared/test/common/mocks/mock_modules_zebin.h"
#include "shared/test/common/test_macros/test.h"
@@ -119,6 +121,8 @@ TEST(UnpackSingleDeviceBinaryZebin, WhenValidBinaryAndMatchedWithRequestedTarget
}
TEST(UnpackSingleDeviceBinaryZebin, givenDumpZEBinFlagSetWhenUnpackingZebinBinaryThenEachTimeZebinIsDumpedToFile) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
DebugManagerStateRestore dbgRestorer;
debugManager.flags.DumpZEBin.set(true);
@@ -513,4 +517,4 @@ TEST(UnpackSingleDeviceBinaryZebin, GivenMiscZebinSectionWithNameDifferentThanBu
EXPECT_FALSE(unpackResult.deviceBinary.empty());
EXPECT_EQ(zebin.storage.data(), unpackResult.deviceBinary.begin());
}
}

View File

@@ -1193,6 +1193,7 @@ TEST(DrmQueryTest, GivenRpsMaxFreqFilesExistWhenFrequenciesAreQueriedThenValidVa
}
TEST(DrmQueryTest, GivenRpsMaxFreqFileDoesntExistWhenFrequencyIsQueriedThenFallbackToLegacyInterface) {
USE_REAL_FILE_SYSTEM();
int expectedMaxFrequency = 2000;
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2024 Intel Corporation
* Copyright (C) 2019-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -22,9 +22,10 @@ TEST(FileLogger, GivenLogAllocationMemoryPoolFlagThenLogsCorrectInfo) {
DebugVariables flags;
flags.LogAllocationMemoryPool.set(true);
FullyEnabledFileLogger fileLogger(testFile, flags);
fileLogger.useRealFiles(false);
// Log file not created
bool logFileCreated = fileExists(fileLogger.getLogFileName());
bool logFileCreated = virtualFileExists(fileLogger.getLogFileName());
EXPECT_FALSE(logFileCreated);
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
@@ -89,9 +90,10 @@ TEST(FileLogger, givenLogAllocationStdoutWhenLogAllocationThenLogToStdoutInstead
flags.LogAllocationMemoryPool.set(true);
flags.LogAllocationStdout.set(true);
FullyEnabledFileLogger fileLogger(testFile, flags);
fileLogger.useRealFiles(false);
// Log file not created
bool logFileCreated = fileExists(fileLogger.getLogFileName());
bool logFileCreated = virtualFileExists(fileLogger.getLogFileName());
EXPECT_FALSE(logFileCreated);
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
@@ -140,7 +142,7 @@ TEST(FileLogger, givenLogAllocationStdoutWhenLogAllocationThenLogToStdoutInstead
EXPECT_TRUE(output.find("Handle: 4") != std::string::npos);
EXPECT_TRUE(output.find("\n") != std::string::npos);
logFileCreated = fileExists(fileLogger.getLogFileName());
logFileCreated = virtualFileExists(fileLogger.getLogFileName());
EXPECT_FALSE(logFileCreated);
}
@@ -149,9 +151,10 @@ TEST(FileLogger, GivenDrmAllocationWithoutBOThenNoHandleLogged) {
DebugVariables flags;
flags.LogAllocationMemoryPool.set(true);
FullyEnabledFileLogger fileLogger(testFile, flags);
fileLogger.useRealFiles(false);
// Log file not created
bool logFileCreated = fileExists(fileLogger.getLogFileName());
bool logFileCreated = virtualFileExists(fileLogger.getLogFileName());
EXPECT_FALSE(logFileCreated);
MockDrmAllocation allocation(0u, AllocationType::buffer, MemoryPool::system64KBPages);
@@ -185,9 +188,10 @@ TEST(FileLogger, GivenLogAllocationMemoryPoolFlagSetFalseThenAllocationIsNotLogg
DebugVariables flags;
flags.LogAllocationMemoryPool.set(false);
FullyEnabledFileLogger fileLogger(testFile, flags);
fileLogger.useRealFiles(false);
// Log file not created
bool logFileCreated = fileExists(fileLogger.getLogFileName());
bool logFileCreated = virtualFileExists(fileLogger.getLogFileName());
EXPECT_FALSE(logFileCreated);
MockDrmAllocation allocation(0u, AllocationType::buffer, MemoryPool::system64KBPages);

View File

@@ -1,23 +1,22 @@
/*
* Copyright (C) 2021-2022 Intel Corporation
* Copyright (C) 2021-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/helpers/file_io.h"
#include "shared/source/os_interface/linux/os_inc.h"
#include "shared/source/utilities/cpu_info.h"
#include "shared/source/utilities/io_functions.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "gtest/gtest.h"
#include <cstdio>
#include <fstream>
using namespace NEO;
TEST(CpuInfo, givenProcCpuinfoFileIsNotExistsWhenIsCpuFlagPresentIsCalledThenValidValueIsReturned) {
USE_REAL_FILE_SYSTEM();
std::string cpuinfoFile = "test_files/linux/proc/cpuinfo";
EXPECT_FALSE(fileExists(cpuinfoFile));
@@ -25,4 +24,4 @@ TEST(CpuInfo, givenProcCpuinfoFileIsNotExistsWhenIsCpuFlagPresentIsCalledThenVal
EXPECT_FALSE(testCpuInfo.isCpuFlagPresent("flag1"));
EXPECT_FALSE(testCpuInfo.isCpuFlagPresent("flag2"));
EXPECT_FALSE(testCpuInfo.isCpuFlagPresent("nonExistingCpuFlag"));
}
}