Skip to content

Commit

Permalink
libguard: Adding function to erase guardrecords
Browse files Browse the repository at this point in the history
Changes:
-Adding new function to erase/wipout all guard records present
in GUARD file.
-Adding option -r, to erase all the guard records.
-Adding UT for the newly introduced function.
-Updating the README.md with new option details.
-Setting the GUARD_VERSION in configuartion data to get the
values correctly.

Test Results:
Running main() from ../googletest/src/gtest_main.cc
[==========] Running 15 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 15 tests from TestGuardRecord
[ RUN      ] TestGuardRecord.CreateGuardRecord
[       OK ] TestGuardRecord.CreateGuardRecord (0 ms)
[ RUN      ] TestGuardRecord.ClearGuardGoodPathTest
[       OK ] TestGuardRecord.ClearGuardGoodPathTest (0 ms)
[ RUN      ] TestGuardRecord.DeleteGuardGoodPathTest
[       OK ] TestGuardRecord.DeleteGuardGoodPathTest (0 ms)
[ RUN      ] TestGuardRecord.NegTestCaseEP
[       OK ] TestGuardRecord.NegTestCaseEP (0 ms)
[ RUN      ] TestGuardRecord.NegTestCaseFullGuardFile
No resolved Guard record found
Size left in GUARD file is 0
[       OK ] TestGuardRecord.NegTestCaseFullGuardFile (0 ms)
[ RUN      ] TestGuardRecord.AlreadyGuardedTC
Already guard record is available in the GUARD partition
[       OK ] TestGuardRecord.AlreadyGuardedTC (0 ms)
[ RUN      ] TestGuardRecord.GetCreatedGuardRecordTC
[       OK ] TestGuardRecord.GetCreatedGuardRecordTC (0 ms)
[ RUN      ] TestGuardRecord.DeleteByEntityPath
[       OK ] TestGuardRecord.DeleteByEntityPath (0 ms)
[ RUN      ] TestGuardRecord.DeleteWithNotExistentEntity
Guard record not found
[       OK ] TestGuardRecord.DeleteWithNotExistentEntity (0 ms)
[ RUN      ] TestGuardRecord.DeleteByRecordId
[       OK ] TestGuardRecord.DeleteByRecordId (0 ms)
[ RUN      ] TestGuardRecord.DeleteWithNotExistentRecordId
Guard record not found
[       OK ] TestGuardRecord.DeleteWithNotExistentRecordId (0 ms)
[ RUN      ] TestGuardRecord.GetGuardFilePathTC
[       OK ] TestGuardRecord.GetGuardFilePathTC (0 ms)
[ RUN      ] TestGuardRecord.GetGuardFilePathWhenLibguradDidNotInitTC
Guard file is not initialised.
[       OK ] TestGuardRecord.GetGuardFilePathWhenLibguradDidNotInitTC (0 ms)
[ RUN      ] TestGuardRecord.ClearResolvedGuardRecord
[       OK ] TestGuardRecord.ClearResolvedGuardRecord (0 ms)
[ RUN      ] TestGuardRecord.EraseAllRecordsTest
[       OK ] TestGuardRecord.EraseAllRecordsTest (0 ms)
[----------] 15 tests from TestGuardRecord (7 ms total)

[----------] Global test environment tear-down
[==========] 15 tests from 1 test suite ran. (7 ms total)
[  PASSED  ] 15 tests.
------------------------------------------------------------------------------

Ok:                 1
Expected Fail:      0
Fail:               0
Unexpected Pass:    0
Skipped:            0
Timeout:            0

Signed-off-by: Chirag Sharma <[email protected]>
  • Loading branch information
Chirag Sharma committed Sep 2, 2021
1 parent 7049ee9 commit 5f03deb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Options:
-i,--delete TEXT Invalidate a single Guard record, expects physical
path as input
-I,--invalidate-all Invalidates all the Guard records
-l,--list Listing of GUARDed resources.
-l,--list List all the Guard'ed resources
-r,--reset Erase all the Guard records
-v,--version Version of GUARD tool.
```
Expand All @@ -51,6 +52,10 @@ Physical path formats supported by guard tool:-
* /sys-0/node-0/proc-0/mc-0/mi-0/mcc-0
* sys-0/node-0/proc-0/mc-0/mi-0/mcc-0

**Note:**
-r option will mark recordId of existing records in GUARD file as 0xffffffff
-e option will delete all records present in the GUARD file.

### Examples

* To create a guard record.
Expand All @@ -72,3 +77,7 @@ guard -I
```
guard -i sys-0/node-0/proc-0/mc-0/mi-0/mcc-0
```
* To erase all the guard records
```
guard -r
```
17 changes: 15 additions & 2 deletions guard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "libguard/guard_interface.hpp"
#include "libguard/include/guard_record.hpp"

#include <config.h>

#include <CLI/CLI.hpp>

using namespace std;
Expand Down Expand Up @@ -108,6 +110,11 @@ void guardCreate(const std::string& physicalPath)
std::cout << "Success" << std::endl;
}

void guardReset()
{
eraseAllRecords();
}

static void exitWithError(const std::string& help, const char* err)
{
std::cerr << "ERROR: " << err << std::endl << help << std::endl;
Expand All @@ -118,12 +125,13 @@ int main(int argc, char** argv)
{
try
{
CLI::App app{"GUARD Tool"};
CLI::App app{"Guard Tool"};
std::optional<std::string> createGuardStr;
std::optional<std::string> deleteGuardStr;
bool listGuardRecords = false;
bool clearAll = false;
bool listResolvedGuardRecords = false;
bool reset = false;
bool gversion = false;

app.set_help_flag("-h, --help", "Guard CLI tool options");
Expand All @@ -139,6 +147,7 @@ int main(int argc, char** argv)
app.add_flag("-a, --listresolvedrecords", listResolvedGuardRecords,
"List all the resolved Guard'ed resources")
->group("");
app.add_flag("-r, --reset", reset, "Erase all the Guard records");
app.add_flag("-v, --version", gversion, "Version of GUARD tool");

CLI11_PARSE(app, argc, argv);
Expand All @@ -165,9 +174,13 @@ int main(int argc, char** argv)
{
guardList(true);
}
else if (reset)
{
guardReset();
}
else if (gversion)
{
std::cout << "Guard tool version is " << GUARD_VERSION << std::endl;
std::cout << "Guard tool " << GUARD_VERSION << std::endl;
}
else
{
Expand Down
7 changes: 7 additions & 0 deletions libguard/guard_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ void clearAll()
}
}

void eraseAllRecords()
{
GuardFile file(guardFilePath);

file.erase(0, file.size());
}

void libguard_init(bool enableDevtree)
{
initialize();
Expand Down
7 changes: 7 additions & 0 deletions libguard/guard_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ void libguard_init(bool enableDevtree = true);
*/
const fs::path& getGuardFilePath();

/**
* @brief Erase/wipeout all the guard records from GUARD file
*
* @return NULL
*/
void eraseAllRecords();

namespace utest
{
void setGuardFile(const fs::path& file);
Expand Down
6 changes: 4 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ conf_data.set_quoted('GUARD_PRSV_PATH', get_option('GUARD_PRSV_PATH'),
description : 'GUARD file in pnor prsv partition'
)

conf_data.set_quoted('GUARD_VERSION', 'v1.0',
description : 'Setting guard tool version'
)

conf_data.set('DEV_TREE', get_option('devtree').enabled(),
description : 'Use device tree to get physical path value'
)
Expand All @@ -40,8 +44,6 @@ configure_file(output: 'config.h',
)

#add_project_arguments('-DPGUARD', language : 'cpp')
add_project_arguments('-DGUARD_VERSION', language : 'cpp')
set_variable('GUARD_VERSION', '1.0')

subdir('libguard')

Expand Down
16 changes: 15 additions & 1 deletion test/guard_intf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ TEST_F(TestGuardRecord, GetGuardFilePathWhenLibguradDidNotInitTC)

// Checking without libguard_init() call.
EXPECT_THROW({ openpower::guard::getGuardFilePath(); },
openpower::guard::exception::InvalidEntry);
openpower::guard::exception::GuardFileOpenFailed);

// Set the guard file since UT reached the end
openpower::guard::utest::setGuardFile(guardFile);
Expand Down Expand Up @@ -299,3 +299,17 @@ TEST_F(TestGuardRecord, ClearResolvedGuardRecord)
openpower::guard::GuardRecords records = openpower::guard::getAll();
EXPECT_EQ(records.size(), 4);
}

TEST_F(TestGuardRecord, EraseAllRecordsTest)
{
openpower::guard::libguard_init();
std::string phyPath = "/sys-0/node-0/proc-1/eq-0/fc-0/core-0";
std::optional<openpower::guard::EntityPath> entityPath =
openpower::guard::getEntityPath(phyPath);
openpower::guard::create(*entityPath);
openpower::guard::GuardRecords records = openpower::guard::getAll();
EXPECT_EQ(records.size(), 1);
openpower::guard::eraseAllRecords();
records = openpower::guard::getAll();
EXPECT_EQ(records.size(), 0);
}

0 comments on commit 5f03deb

Please sign in to comment.