test: mock filesystem in ocloc ult pt 2

Related-To: NEO-14084
Signed-off-by: Marcel Skierkowski <marcel.skierkowski@intel.com>
This commit is contained in:
Marcel Skierkowski
2025-04-09 10:54:53 +00:00
committed by Compute-Runtime-Automation
parent f4815a9b69
commit 092291ce08
4 changed files with 56 additions and 31 deletions

View File

@@ -13,6 +13,7 @@
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/helpers/test_files.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "shared/test/common/mocks/mock_io_functions.h"
#include "opencl/test/unit_test/offline_compiler/environment.h"
#include "opencl/test/unit_test/offline_compiler/mock/mock_argument_helper.h"
@@ -272,9 +273,9 @@ TEST(DecoderTests, GivenFlagsWhichRequireMoreArgsWithoutThemWhenParsingThenError
constexpr auto suppressMessages{false};
MockDecoder decoder{suppressMessages};
::testing::internal::CaptureStdout();
decoder.mockArgHelper->messagePrinter.setSuppressMessages(true);
const auto result = decoder.validateInput(args);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{decoder.mockArgHelper->getLog()};
EXPECT_EQ(-1, result);
@@ -293,9 +294,9 @@ TEST(DecoderTests, givenUnknownDeviceNameWhenValidateInputThenCorrectWarningIsRe
constexpr auto suppressMessages{false};
MockDecoder decoder{suppressMessages};
::testing::internal::CaptureStdout();
decoder.mockArgHelper->messagePrinter.setSuppressMessages(true);
const auto result = decoder.validateInput(args);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{decoder.mockArgHelper->getLog()};
EXPECT_EQ(result, 0);
const std::string expectedWarningMessage{"Warning : missing or invalid -device parameter - results may be inaccurate\n"};
@@ -315,9 +316,9 @@ TEST(DecoderTests, givenDeprecatedDeviceNamesWhenValidateInputThenCorrectWarning
"-device",
acronym.str()};
::testing::internal::CaptureStdout();
decoder.mockArgHelper->messagePrinter.setSuppressMessages(true);
const auto result = decoder.validateInput(args);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{decoder.mockArgHelper->getLog()};
EXPECT_EQ(result, 0);
@@ -348,9 +349,9 @@ TEST(DecoderTests, givenProductNamesThatExistsForIgaWhenValidateInputThenSuccess
"-device",
acronym.str()};
::testing::internal::CaptureStdout();
decoder.mockArgHelper->messagePrinter.setSuppressMessages(true);
const auto result = decoder.validateInput(args);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{decoder.mockArgHelper->getLog()};
EXPECT_EQ(result, 0);
EXPECT_TRUE(output.empty());
@@ -415,9 +416,9 @@ TEST(DecoderTests, GivenMissingDumpFlagWhenParsingValidListOfParametersThenRetur
"-patch",
"test_files/patch"};
::testing::internal::CaptureStdout();
decoder.mockArgHelper->messagePrinter.setSuppressMessages(true);
const auto result = decoder.validateInput(args);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{decoder.mockArgHelper->getLog()};
EXPECT_EQ(0, result);
@@ -444,9 +445,9 @@ TEST(DecoderTests, GivenMissingDumpFlagAndArgHelperOutputEnabledWhenParsingValid
decoder.mockArgHelper->hasOutput = true;
::testing::internal::CaptureStdout();
decoder.mockArgHelper->messagePrinter.setSuppressMessages(true);
const auto result = decoder.validateInput(args);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{decoder.mockArgHelper->getLog()};
EXPECT_EQ(0, result);
EXPECT_TRUE(output.empty()) << output;
decoder.mockArgHelper->hasOutput = false;
@@ -493,6 +494,8 @@ TEST(DecoderTests, GivenProperStructWhenReadingStructFieldsThenFieldsVectorGetsP
}
TEST(DecoderTests, GivenProperPatchListFileWhenParsingTokensThenFileIsParsedCorrectly) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
MockDecoder decoder;
decoder.parseTokens();
@@ -588,6 +591,8 @@ TEST(DecoderTests, GivenValidBinaryWhenReadingPatchTokensFromBinaryThenBinaryIsR
}
TEST(DecoderTests, GivenValidBinaryWithoutPatchTokensWhenProcessingBinaryThenBinaryIsReadCorrectly) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
auto programHeader = createProgramBinaryHeader(1, 0);
std::string kernelName("ExampleKernel");
auto kernelHeader = createKernelBinaryHeaderCommon(static_cast<uint32_t>(kernelName.size() + 1), 0);
@@ -613,6 +618,8 @@ TEST(DecoderTests, GivenValidBinaryWithoutPatchTokensWhenProcessingBinaryThenBin
}
TEST(DecoderTests, givenBinaryWithKernelBinaryHeaderWhenAtLeastOneOfTheKernelSizesExceedSectionSizeThenAbort) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
VariableBackup oclocAbortBackup{&abortOclocExecution, &abortOclocExecutionMock};
std::string kernelName("ExampleKernel");
auto kernelHeader = createKernelBinaryHeaderCommon(static_cast<uint32_t>(kernelName.size() + 1), 0);
@@ -641,6 +648,8 @@ TEST(DecoderTests, givenBinaryWithKernelBinaryHeaderWhenAtLeastOneOfTheKernelSiz
}
TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPatchTokensAreReadCorrectly) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
std::stringstream binarySS;
// ProgramBinaryHeader

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -7,6 +7,9 @@
#include "iga_wrapper_tests.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "shared/test/common/mocks/mock_os_library.h"
#include "opencl/test/unit_test/offline_compiler/mock/mock_iga_dll_guard.h"
#include "opencl/test/unit_test/offline_compiler/stdout_capturer.h"
@@ -15,6 +18,9 @@
namespace NEO {
TEST_F(IgaWrapperTest, GivenInvalidPathToIgaLibraryWhenDisassemblingGenIsaThenFalseIsReturnedAndErrorMessageIsPrinted) {
MockOsLibrary::loadLibraryNewObject = nullptr;
VariableBackup<decltype(NEO::OsLibrary::loadFunc)> funcBackup{&NEO::OsLibrary::loadFunc, MockOsLibrary::load};
MockIgaDllGuard mockIgaDllGuard{"some_invalid_path_to_library"};
mockIgaDllGuard.enable();
@@ -178,6 +184,9 @@ TEST_F(IgaWrapperTest, GivenContextCreationSuccessAndDisassemblationSuccessAndWa
}
TEST_F(IgaWrapperTest, GivenInvalidPathToIgaLibraryWhenAssemblingGenIsaThenFalseIsReturnedAndErrorMessageIsPrinted) {
MockOsLibrary::loadLibraryNewObject = nullptr;
VariableBackup<decltype(NEO::OsLibrary::loadFunc)> funcBackup{&NEO::OsLibrary::loadFunc, MockOsLibrary::load};
MockIgaDllGuard mockIgaDllGuard{"some_invalid_path_to_library"};
mockIgaDllGuard.enable();
@@ -311,4 +320,4 @@ TEST_F(IgaWrapperTest, GivenIgcWrapperWhenCallingSetProductFamilyMultipleTimesTh
EXPECT_TRUE(testedIgaWrapper.isKnownPlatform());
}
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -24,9 +24,9 @@ TEST(OclocConcatTest, GivenNoArgumentsWhenInitializingThenErrorIsReturned) {
auto oclocConcat = MockOclocConcat(&mockArgHelper);
std::vector<std::string> args = {"ocloc", "concat"};
::testing::internal::CaptureStdout();
mockArgHelper.messagePrinter.setSuppressMessages(true);
auto error = oclocConcat.initialize(args);
const auto output = ::testing::internal::GetCapturedStdout();
const auto output = mockArgHelper.getLog();
EXPECT_EQ(static_cast<uint32_t>(OCLOC_INVALID_COMMAND_LINE), error);
const std::string expectedOutput = "No files to concatenate were provided.\n";
@@ -39,9 +39,9 @@ TEST(OclocConcatTest, GivenMissingFilesWhenInitializingThenErrorIsReturned) {
auto oclocConcat = MockOclocConcat(&mockArgHelper);
std::vector<std::string> args = {"ocloc", "concat", "fatBinary1.ar", "fatBinary2.ar"};
::testing::internal::CaptureStdout();
mockArgHelper.messagePrinter.setSuppressMessages(true);
auto error = oclocConcat.initialize(args);
const auto output = ::testing::internal::GetCapturedStdout();
const auto output = mockArgHelper.getLog();
EXPECT_EQ(static_cast<uint32_t>(OCLOC_INVALID_COMMAND_LINE), error);
const std::string expectedOutput = "fatBinary1.ar doesn't exist!\nfatBinary2.ar doesn't exist!\n";
@@ -56,9 +56,9 @@ TEST(OclocConcatTest, GivenValidArgsWhenInitializingThenFileNamesToConcatAndOutp
auto oclocConcat = MockOclocConcat(&mockArgHelper);
std::vector<std::string> args = {"ocloc", "concat", "fatBinary1.ar", "fatBinary2.ar", "-out", "fatBinary.ar"};
::testing::internal::CaptureStdout();
mockArgHelper.messagePrinter.setSuppressMessages(true);
auto error = oclocConcat.initialize(args);
const auto output = ::testing::internal::GetCapturedStdout();
const auto output = mockArgHelper.getLog();
EXPECT_EQ(static_cast<uint32_t>(OCLOC_SUCCESS), error);
EXPECT_TRUE(output.empty());
@@ -74,9 +74,9 @@ TEST(OclocConcatTest, GivenMissingOutFileNameAfterOutArgumentWhenInitalizingThen
auto oclocConcat = MockOclocConcat(&mockArgHelper);
std::vector<std::string> args = {"ocloc", "concat", "fatBinary1.ar", "fatBinary2.ar", "-out"};
::testing::internal::CaptureStdout();
mockArgHelper.messagePrinter.setSuppressMessages(true);
auto error = oclocConcat.initialize(args);
const auto output = ::testing::internal::GetCapturedStdout();
const auto output = mockArgHelper.getLog();
EXPECT_EQ(static_cast<uint32_t>(OCLOC_INVALID_COMMAND_LINE), error);
const std::string expectedOutput = "Missing out file name after \"-out\" argument\n";

View File

@@ -751,13 +751,19 @@ TEST_F(OclocFatBinaryProductAcronymsTests, givenProductsClosedRangeWhenFatBinary
GTEST_SKIP();
}
auto acronymFrom = enabledProductsAcronyms.at(0);
auto acronymTo = enabledProductsAcronyms.at(2);
auto prodFromIt = std::find(enabledProductsAcronyms.begin(), enabledProductsAcronyms.end(), acronymFrom);
auto prodToIt = std::find(enabledProductsAcronyms.begin(), enabledProductsAcronyms.end(), acronymTo);
if (prodFromIt > prodToIt) {
std::swap(prodFromIt, prodToIt);
}
std::vector<ConstStringRef> expected{};
expected.insert(expected.end(), enabledProductsAcronyms.begin(), enabledProductsAcronyms.begin() + 4);
expected.insert(expected.end(), prodFromIt, ++prodToIt);
std::string acronymFrom = enabledProductsAcronyms.front().str();
std::string acronymsTo = (enabledProductsAcronyms.begin() + 3)->str();
std::string acronymsTarget = acronymFrom + ":" + acronymsTo;
std::string acronymsTarget = acronymFrom.str() + ":" + acronymTo.str();
auto got = NEO::getTargetProductsForFatbinary(acronymsTarget, oclocArgHelperWithoutInput.get());
EXPECT_EQ(got, expected);
@@ -1032,14 +1038,15 @@ TEST_F(OclocFatBinaryProductAcronymsTests, givenOpenRangeFromProductWithoutDashe
}
TEST_F(OclocFatBinaryProductAcronymsTests, givenOpenRangeToProductWhenFatBinaryBuildIsInvokedThenSuccessIsReturned) {
if (enabledProductsAcronyms.size() < 3) {
if (enabledProductsAcronyms.size() < 2) {
GTEST_SKIP();
}
auto acronymTo = enabledProductsAcronyms.at(1);
std::vector<ConstStringRef> expected{};
expected.insert(expected.end(), enabledProductsAcronyms.begin(), enabledProductsAcronyms.begin() + 4);
expected.insert(expected.end(), enabledProductsAcronyms.begin(), enabledProductsAcronyms.begin() + 2);
std::string acronymsTarget = ":" + (enabledProductsAcronyms.begin() + 3)->str();
std::string acronymsTarget = ":" + acronymTo.str();
auto got = NEO::getTargetProductsForFatbinary(acronymsTarget, oclocArgHelperWithoutInput.get());
EXPECT_EQ(got, expected);