Skip to content

Commit

Permalink
test: Adding Unit test
Browse files Browse the repository at this point in the history
Changes:
-Changes in exisiting guard UT's to check exceptions
-Adding new UT for verify clearResolvedRecords

Test:
Ran UTs in docker env.

Signed-off-by: Chirag Sharma <[email protected]>
  • Loading branch information
Chirag Sharma committed Jul 23, 2021
1 parent 36624f8 commit ef11c3a
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions test/guard_intf_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#include "libguard/guard_common.hpp"
#include "libguard/guard_entity.hpp"
#include "libguard/guard_exception.hpp"
#include "libguard/guard_file.hpp"
#include "libguard/guard_interface.hpp"
#include "libguard/include/guard_record.hpp"
Expand Down Expand Up @@ -76,7 +77,9 @@ TEST_F(TestGuardRecord, ClearGuardGoodPathTest)
openpower::guard::create(*entityPath);
openpower::guard::clearAll();
openpower::guard::GuardRecords records = openpower::guard::getAll();
EXPECT_EQ(records.size(), 0);
EXPECT_EQ(records.size(), 1);
openpower::guard::GuardRecord record = records.at(0);
EXPECT_EQ(record.recordId, 0xFFFFFFFF);
}

TEST_F(TestGuardRecord, DeleteGuardGoodPathTest)
Expand All @@ -100,13 +103,14 @@ TEST_F(TestGuardRecord, DeleteGuardGoodPathTest)
entityPath = openpower::guard::getEntityPath(phyPath);
openpower::guard::clear(*entityPath);
openpower::guard::GuardRecords records = openpower::guard::getAll();
bool isRecordDeleted = true;
bool isRecordDeleted = false;
for (int i = 0; i < (int)records.size(); i++)
{
openpower::guard::GuardRecord record = records.at(i);
if (record.targetId == entityPath)
{
isRecordDeleted = false;
EXPECT_EQ(record.recordId, 0xFFFFFFFF);
isRecordDeleted = true;
break;
}
}
Expand All @@ -125,9 +129,8 @@ TEST_F(TestGuardRecord, NegTestCaseEP)
TEST_F(TestGuardRecord, NegTestCaseFullGuardFile)
{
openpower::guard::libguard_init();
std::string phyPath = "/sys-0";
std::optional<openpower::guard::EntityPath> entityPath =
openpower::guard::getEntityPath(phyPath);
std::string phyPath = " ";
std::optional<openpower::guard::EntityPath> entityPath;
phyPath = "/sys-0/node-0/proc-1/eq-0/fc-0/core-1";
entityPath = openpower::guard::getEntityPath(phyPath);
openpower::guard::create(*entityPath);
Expand All @@ -140,13 +143,10 @@ TEST_F(TestGuardRecord, NegTestCaseFullGuardFile)
phyPath = "/sys-0/node-0/proc-1/eq-0/fc-0/core-0";
entityPath = openpower::guard::getEntityPath(phyPath);
openpower::guard::create(*entityPath);
phyPath = "/sys-0/node-0/proc-1/eq-0/fc-0";
entityPath = openpower::guard::getEntityPath(phyPath);
openpower::guard::create(*entityPath);
phyPath = "/sys-0/node-0/proc-1/eq-0";
phyPath = "/sys-0/node-0/dimm-1";
entityPath = openpower::guard::getEntityPath(phyPath);
EXPECT_THROW({ openpower::guard::create(*entityPath); },
std::runtime_error);
openpower::guard::exception::GuardFileOverFlowed);
}

TEST_F(TestGuardRecord, AlreadyGuardedTC)
Expand All @@ -159,7 +159,7 @@ TEST_F(TestGuardRecord, AlreadyGuardedTC)

// Trying to guard again with same entity
EXPECT_THROW({ openpower::guard::create(*entityPath); },
std::runtime_error);
openpower::guard::exception::AlreadyGuarded);
}

TEST_F(TestGuardRecord, GetCreatedGuardRecordTC)
Expand Down Expand Up @@ -197,7 +197,7 @@ TEST_F(TestGuardRecord, DeleteByEntityPath)

// Make sure is deleted
openpower::guard::GuardRecords records = openpower::guard::getAll();
EXPECT_EQ(records.size(), 0);
EXPECT_EQ(records.size(), 1);
}

TEST_F(TestGuardRecord, DeleteWithNotExistentEntity)
Expand All @@ -208,7 +208,8 @@ TEST_F(TestGuardRecord, DeleteWithNotExistentEntity)
openpower::guard::getEntityPath(physPath);

// Trying to delete entity which is not present
EXPECT_THROW({ openpower::guard::clear(*entityPath); }, std::runtime_error);
EXPECT_THROW({ openpower::guard::clear(*entityPath); },
openpower::guard::exception::InvalidEntityPath);
}

TEST_F(TestGuardRecord, DeleteByRecordId)
Expand All @@ -226,7 +227,9 @@ TEST_F(TestGuardRecord, DeleteByRecordId)

// Make sure is deleted
openpower::guard::GuardRecords records = openpower::guard::getAll();
EXPECT_EQ(records.size(), 0);
EXPECT_EQ(records.size(), 1);
openpower::guard::GuardRecord record = records.at(0);
EXPECT_EQ(record.recordId, 0xFFFFFFFF);
}

TEST_F(TestGuardRecord, DeleteWithNotExistentRecordId)
Expand All @@ -241,7 +244,7 @@ TEST_F(TestGuardRecord, DeleteWithNotExistentRecordId)

// Trying to delete a record by using returned record id with increment
EXPECT_THROW({ openpower::guard::clear(retGuardRecord.recordId + 1); },
std::runtime_error);
openpower::guard::exception::InvalidEntityPath);
}

TEST_F(TestGuardRecord, GetGuardFilePathTC)
Expand All @@ -260,7 +263,8 @@ TEST_F(TestGuardRecord, GetGuardFilePathWhenLibguradDidNotInitTC)
openpower::guard::utest::setGuardFile("");

// Checking without libguard_init() call.
EXPECT_THROW({ openpower::guard::getGuardFilePath(); }, std::runtime_error);
EXPECT_THROW({ openpower::guard::getGuardFilePath(); },
openpower::guard::exception::InvalidEntityPath);

// Set the guard file since UT reached the end
openpower::guard::utest::setGuardFile(guardFile);
Expand All @@ -271,3 +275,27 @@ TEST_F(TestGuardRecord, GetGuardFilePathWhenLibguradDidNotInitTC)
std::string retGuardFilePath = openpower::guard::getGuardFilePath();
EXPECT_EQ(retGuardFilePath, guardFile);
}

TEST_F(TestGuardRecord, ClearResolvedGuardRecords)
{
openpower::guard::libguard_init();
std::string phyPath = "/sys-0/node-0/dimm-0";
std::optional<openpower::guard::EntityPath> entityPath =
openpower::guard::getEntityPath(phyPath);
openpower::guard::create(*entityPath);
phyPath = "/sys-0/node-0/dimm-1";
entityPath = openpower::guard::getEntityPath(phyPath);
openpower::guard::create(*entityPath);
phyPath = "/sys-0/node-0/dimm-2";
entityPath = openpower::guard::getEntityPath(phyPath);
openpower::guard::create(*entityPath);
phyPath = "/sys-0/node-0/dimm-2";
entityPath = openpower::guard::getEntityPath(phyPath);
openpower::guard::clear(*entityPath);
phyPath = "/sys-0/node-0/dimm-0";
entityPath = openpower::guard::getEntityPath(phyPath);
openpower::guard::clear(*entityPath);
openpower::guard::clearResolvedRecords();
openpower::guard::GuardRecords records = openpower::guard::getAll();
EXPECT_EQ(records.size(), 1);
}

0 comments on commit ef11c3a

Please sign in to comment.