From 188894e369f22b596a06d76dc666c8ec0e23d8dd Mon Sep 17 00:00:00 2001 From: Marcel Skierkowski Date: Mon, 20 Jan 2025 11:27:06 +0000 Subject: [PATCH] refactor: mock filesystem in sysman tests Related-To: NEO-7006 Signed-off-by: Marcel Skierkowski --- .../tools/source/sysman/linux/fs_access.cpp | 8 +-- .../sources/sysman/linux/test_sysman.cpp | 64 ++++++++++++++++++- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/level_zero/tools/source/sysman/linux/fs_access.cpp b/level_zero/tools/source/sysman/linux/fs_access.cpp index 4725d7ab54..0de8f23d05 100644 --- a/level_zero/tools/source/sysman/linux/fs_access.cpp +++ b/level_zero/tools/source/sysman/linux/fs_access.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2023 Intel Corporation + * Copyright (C) 2020-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -252,7 +252,7 @@ ze_result_t FsAccess::getRealPath(const std::string path, std::string &val) { ze_result_t FsAccess::listDirectory(const std::string path, std::vector &list) { list.clear(); - ::DIR *procDir = ::opendir(path.c_str()); + ::DIR *procDir = NEO::SysCalls::opendir(path.c_str()); if (!procDir) { return getResult(errno); } @@ -260,7 +260,7 @@ ze_result_t FsAccess::listDirectory(const std::string path, std::vectord_name); if (!name.compare(".") || !name.compare("..")) { @@ -271,7 +271,7 @@ ze_result_t FsAccess::listDirectory(const std::string path, std::vector mockOpendir(&NEO::SysCalls::sysCallsOpendir, [](const char *name) -> DIR * { + return reinterpret_cast(0xc001); + }); + + VariableBackup mockReaddir( + &NEO::SysCalls::sysCallsReaddir, [](DIR * dir) -> struct dirent * { + static int callCount = 0; + callCount++; + if (callCount > 1) { + return nullptr; + } + static struct dirent mockEntry = {0, 0, 0, 0, "mockDir"}; + return &mockEntry; + }); + + VariableBackup mockClosedir(&NEO::SysCalls::sysCallsClosedir, [](DIR *dir) -> int { + return 0; + }); + PublicSysfsAccess *tempSysfsAccess = new PublicSysfsAccess(); char cwd[PATH_MAX]; std::string path = getcwd(cwd, PATH_MAX); @@ -754,6 +773,27 @@ TEST_F(SysmanDeviceFixture, GivenCreateSysfsAccessHandleWhenCallinggetSysfsAcces } TEST_F(SysmanDeviceFixture, GivenValidPidWhenCallingProcfsAccessGetFileDescriptorsThenSuccessIsReturned) { + VariableBackup mockOpendir(&NEO::SysCalls::sysCallsOpendir, [](const char *name) -> DIR * { + return reinterpret_cast(0xc001); + }); + + VariableBackup mockReaddir( + &NEO::SysCalls::sysCallsReaddir, [](DIR * dir) -> struct dirent * { + static int callCount = 0; + if (callCount > 2) { + return nullptr; + } + static struct dirent mockEntry[] = { + {0, 0, 0, 0, "mockDir"}, + {0, 0, 0, 0, "mockDir2"}, + {0, 0, 0, 0, "mockDir3"}, + }; + return &mockEntry[callCount++]; + }); + + VariableBackup mockClosedir(&NEO::SysCalls::sysCallsClosedir, [](DIR *dir) -> int { + return 0; + }); auto procfsAccess = pLinuxSysmanImp->getProcfsAccess(); ::pid_t processID = getpid(); @@ -762,6 +802,28 @@ TEST_F(SysmanDeviceFixture, GivenValidPidWhenCallingProcfsAccessGetFileDescripto } TEST_F(SysmanDeviceFixture, GivenValidProcfsAccessHandleWhenCallingListProcessesThenSuccessIsReturned) { + VariableBackup mockOpendir(&NEO::SysCalls::sysCallsOpendir, [](const char *name) -> DIR * { + return reinterpret_cast(0xc001); + }); + + VariableBackup mockReaddir( + &NEO::SysCalls::sysCallsReaddir, [](DIR * dir) -> struct dirent * { + static int callCount = 0; + if (callCount > 2) { + return nullptr; + } + static struct dirent mockEntry[] = { + {0, 0, 0, 0, "mockDir"}, + {0, 0, 0, 0, "mockDir2"}, + {0, 0, 0, 0, "mockDir3"}, + }; + return &mockEntry[callCount++]; + }); + + VariableBackup mockClosedir(&NEO::SysCalls::sysCallsClosedir, [](DIR *dir) -> int { + return 0; + }); + auto procfsAccess = pLinuxSysmanImp->getProcfsAccess(); std::vector<::pid_t> listPid;