PEL: HostNotifier testcase refactoring

This commit doesn't change the behavior, it just makes the following
changes to the testcase infrastructure so there is less duplication:

* Move the hostIface, mockHostIface, repo, and dataIface instances into
  the test class HostNotifierTest so that all testcases can use them,
  and then remove creating them in every testcase.

* MockHostIface is now a pointer, so have to change its use.

* Make the DataInterface and HosttInterface mocks be NiceMocks so that
  gtest won't print warnings about functions called that don't have
  EXPECT_CALLs.

* Have the default behavior of sendNewLogCmd be to send a command
  successfully so that it doesn't have to be specified in every
  testcase.

Change-Id: Ic393933629753b884299ee4dfa30a8bb6007ee63
diff --git a/test/openpower-pels/host_notifier_test.cpp b/test/openpower-pels/host_notifier_test.cpp
index 9bab030..c51d560 100644
--- a/test/openpower-pels/host_notifier_test.cpp
+++ b/test/openpower-pels/host_notifier_test.cpp
@@ -29,6 +29,7 @@
 using namespace openpower::pels;
 using ::testing::_;
 using ::testing::Invoke;
+using ::testing::NiceMock;
 using ::testing::Return;
 namespace fs = std::filesystem;
 using namespace std::chrono;
@@ -39,10 +40,23 @@
 class HostNotifierTest : public CleanPELFiles
 {
   public:
-    HostNotifierTest()
+    HostNotifierTest() : repo(repoPath)
     {
         auto r = sd_event_default(&event);
         EXPECT_TRUE(r >= 0);
+
+        hostIface =
+            std::make_unique<NiceMock<MockHostInterface>>(event, dataIface);
+
+        mockHostIface = reinterpret_cast<MockHostInterface*>(hostIface.get());
+
+        auto send = [this](uint32_t id, uint32_t size) {
+            return this->mockHostIface->send(0);
+        };
+
+        // Unless otherwise specified, sendNewLogCmd should always pass.
+        ON_CALL(*mockHostIface, sendNewLogCmd(_, _))
+            .WillByDefault(Invoke(send));
     }
 
     ~HostNotifierTest()
@@ -52,6 +66,10 @@
 
   protected:
     sd_event* event;
+    Repository repo;
+    NiceMock<MockDataInterface> dataIface;
+    std::unique_ptr<HostInterface> hostIface;
+    MockHostInterface* mockHostIface;
 };
 
 /**
@@ -99,8 +117,6 @@
 // Test that host state change callbacks work
 TEST_F(HostNotifierTest, TestHostStateChange)
 {
-    MockDataInterface dataIface;
-
     bool hostState = false;
     bool called = false;
     DataInterfaceBase::HostStateChangeFunc func = [&hostState,
@@ -139,12 +155,6 @@
 // notification queue.
 TEST_F(HostNotifierTest, TestPolicyAckedPEL)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
     auto pel = makePEL();
@@ -169,12 +179,6 @@
 // Test the 'don't report' PEL flag
 TEST_F(HostNotifierTest, TestPolicyDontReport)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
     // dontReportToHostFlagBit
@@ -194,12 +198,6 @@
 // is no HMC.
 TEST_F(HostNotifierTest, TestPolicyHiddenNoHMC)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
     // hiddenFlagBit
@@ -221,12 +219,6 @@
 // Don't need to enqueue a hidden log already acked by the HMC
 TEST_F(HostNotifierTest, TestPolicyHiddenWithHMCAcked)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
     // hiddenFlagBit
@@ -251,12 +243,6 @@
 // the policy with hidden log notification.
 TEST_F(HostNotifierTest, TestPolicyHiddenWithHMCManaged)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
     // hiddenFlagBit
@@ -276,9 +262,6 @@
 // Test that PELs are enqueued on startup
 TEST_F(HostNotifierTest, TestStartup)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
     // Give the repo 10 PELs to start with
     for (int i = 0; i < 10; i++)
     {
@@ -286,9 +269,6 @@
         repo.add(pel);
     }
 
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
     ASSERT_EQ(notifier.queueSize(), 10);
@@ -306,26 +286,10 @@
 // Test the simple path were PELs get sent to the host
 TEST_F(HostNotifierTest, TestSendCmd)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
     sdeventplus::Event sdEvent{event};
 
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
-    MockHostInterface& mockHostIface =
-        reinterpret_cast<MockHostInterface&>(*hostIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
-    auto send = [&mockHostIface](uint32_t id, uint32_t size) {
-        return mockHostIface.send(0);
-    };
-
-    EXPECT_CALL(mockHostIface, sendNewLogCmd(_, _))
-        .WillRepeatedly(Invoke(send));
-
     // Add a PEL with the host off
     auto pel = makePEL();
     repo.add(pel);
@@ -337,7 +301,7 @@
     runEvents(sdEvent, 1);
 
     // It was sent up
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 1);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 1);
     EXPECT_EQ(notifier.queueSize(), 0);
 
     // Verify the state was written to the PEL.
@@ -353,13 +317,13 @@
     // Dispatch it by hitting the event loop (no commands sent yet)
     // Don't need to test this step discretely in the future
     runEvents(sdEvent, 1);
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 1);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 1);
     EXPECT_EQ(notifier.queueSize(), 0);
 
     // Send the command
     runEvents(sdEvent, 1);
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 2);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 2);
     EXPECT_EQ(notifier.queueSize(), 0);
 
     pel = makePEL();
@@ -368,7 +332,7 @@
     // dispatch and process the command
     runEvents(sdEvent, 2);
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 3);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 3);
     EXPECT_EQ(notifier.queueSize(), 0);
 }
 
@@ -376,9 +340,6 @@
 // it will send PELs
 TEST_F(HostNotifierTest, TestStartAfterHostUp)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
     // Add PELs right away
     auto pel = makePEL();
     repo.add(pel);
@@ -387,19 +348,6 @@
 
     sdeventplus::Event sdEvent{event};
 
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
-    MockHostInterface& mockHostIface =
-        reinterpret_cast<MockHostInterface&>(*hostIface);
-
-    auto send = [&mockHostIface](uint32_t id, uint32_t size) {
-        return mockHostIface.send(0);
-    };
-
-    EXPECT_CALL(mockHostIface, sendNewLogCmd(_, _))
-        .WillRepeatedly(Invoke(send));
-
     // Create the HostNotifier class with the host already up
     dataIface.changeHostState(true);
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
@@ -407,34 +355,25 @@
     // It should start sending PELs right away
     runEvents(sdEvent, 2);
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 2);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 2);
     EXPECT_EQ(notifier.queueSize(), 0);
 }
 
 // Test that a single failure will cause a retry
 TEST_F(HostNotifierTest, TestHostRetry)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
     sdeventplus::Event sdEvent{event};
 
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
-    MockHostInterface& mockHostIface =
-        reinterpret_cast<MockHostInterface&>(*hostIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
-    auto sendFailure = [&mockHostIface](uint32_t id, uint32_t size) {
-        return mockHostIface.send(1);
+    auto sendFailure = [this](uint32_t id, uint32_t size) {
+        return this->mockHostIface->send(1);
     };
-    auto sendSuccess = [&mockHostIface](uint32_t id, uint32_t size) {
-        return mockHostIface.send(0);
+    auto sendSuccess = [this](uint32_t id, uint32_t size) {
+        return this->mockHostIface->send(0);
     };
 
-    EXPECT_CALL(mockHostIface, sendNewLogCmd(_, _))
+    EXPECT_CALL(*mockHostIface, sendNewLogCmd(_, _))
         .WillOnce(Invoke(sendFailure))
         .WillOnce(Invoke(sendSuccess))
         .WillOnce(Invoke(sendSuccess));
@@ -448,14 +387,14 @@
     runEvents(sdEvent, 2);
 
     // The command failed, so the queue isn't empty
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 1);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 1);
     EXPECT_EQ(notifier.queueSize(), 1);
 
     // Run the events again to let the timer expire and the
     // command to be retried, which will be successful.
-    runEvents(sdEvent, 2, mockHostIface.getReceiveRetryDelay());
+    runEvents(sdEvent, 2, mockHostIface->getReceiveRetryDelay());
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 2);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 2);
     EXPECT_EQ(notifier.queueSize(), 0);
 
     // This one should pass with no problems
@@ -465,32 +404,23 @@
     // Dispatch and handle the command
     runEvents(sdEvent, 2);
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 3);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 3);
     EXPECT_EQ(notifier.queueSize(), 0);
 }
 
 // Test that all commands fail and notifier will give up
 TEST_F(HostNotifierTest, TestHardFailure)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
     sdeventplus::Event sdEvent{event};
 
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
-    MockHostInterface& mockHostIface =
-        reinterpret_cast<MockHostInterface&>(*hostIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
     // Every call will fail
-    auto sendFailure = [&mockHostIface](uint32_t id, uint32_t size) {
-        return mockHostIface.send(1);
+    auto sendFailure = [this](uint32_t id, uint32_t size) {
+        return this->mockHostIface->send(1);
     };
 
-    EXPECT_CALL(mockHostIface, sendNewLogCmd(_, _))
+    EXPECT_CALL(*mockHostIface, sendNewLogCmd(_, _))
         .WillRepeatedly(Invoke(sendFailure));
 
     dataIface.changeHostState(true);
@@ -499,10 +429,10 @@
     repo.add(pel);
 
     // Clock more retries than necessary
-    runEvents(sdEvent, 40, mockHostIface.getReceiveRetryDelay());
+    runEvents(sdEvent, 40, mockHostIface->getReceiveRetryDelay());
 
     // Should have stopped after the 15 Tries
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 15);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 15);
     EXPECT_EQ(notifier.queueSize(), 1);
 
     // Now add another PEL, and it should start trying again
@@ -510,36 +440,19 @@
     pel = makePEL();
     repo.add(pel);
 
-    runEvents(sdEvent, 40, mockHostIface.getReceiveRetryDelay());
+    runEvents(sdEvent, 40, mockHostIface->getReceiveRetryDelay());
 
     // Tried an additional 15 times
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 30);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 30);
     EXPECT_EQ(notifier.queueSize(), 2);
 }
 
 // Cancel an in progress command
 TEST_F(HostNotifierTest, TestCancelCmd)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
     sdeventplus::Event sdEvent{event};
-
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
-    MockHostInterface& mockHostIface =
-        reinterpret_cast<MockHostInterface&>(*hostIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
-    auto send = [&mockHostIface](uint32_t id, uint32_t size) {
-        return mockHostIface.send(0);
-    };
-
-    EXPECT_CALL(mockHostIface, sendNewLogCmd(_, _))
-        .WillRepeatedly(Invoke(send));
-
     dataIface.changeHostState(true);
 
     // Add and send one PEL, but don't enter the event loop
@@ -568,33 +481,17 @@
 
     runEvents(sdEvent, 1);
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 1);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 1);
     EXPECT_EQ(notifier.queueSize(), 0);
 }
 
 // Test that acking a PEL persist across power cycles
 TEST_F(HostNotifierTest, TestPowerCycleAndAcks)
 {
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
     sdeventplus::Event sdEvent{event};
 
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
-    MockHostInterface& mockHostIface =
-        reinterpret_cast<MockHostInterface&>(*hostIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
-    auto send = [&mockHostIface](uint32_t id, uint32_t size) {
-        return mockHostIface.send(0);
-    };
-
-    EXPECT_CALL(mockHostIface, sendNewLogCmd(_, _))
-        .WillRepeatedly(Invoke(send));
-
     // Add 2 PELs with host off
     auto pel = makePEL();
     repo.add(pel);
@@ -609,7 +506,7 @@
     runEvents(sdEvent, 2);
 
     // The were both sent.
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 2);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 2);
     EXPECT_EQ(notifier.queueSize(), 0);
 
     dataIface.changeHostState(false);
@@ -621,7 +518,7 @@
     dataIface.changeHostState(true);
     runEvents(sdEvent, 2);
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 4);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 4);
     EXPECT_EQ(notifier.queueSize(), 0);
 
     // Ack them and verify the state in the PEL.
@@ -656,26 +553,9 @@
     // Host responds with either Ack or full
     // and repeat
 
-    Repository repo{repoPath};
-    MockDataInterface dataIface;
-
     sdeventplus::Event sdEvent{event};
-
-    std::unique_ptr<HostInterface> hostIface =
-        std::make_unique<MockHostInterface>(event, dataIface);
-
-    MockHostInterface& mockHostIface =
-        reinterpret_cast<MockHostInterface&>(*hostIface);
-
     HostNotifier notifier{repo, dataIface, std::move(hostIface)};
 
-    auto send = [&mockHostIface](uint32_t id, uint32_t size) {
-        return mockHostIface.send(0);
-    };
-
-    EXPECT_CALL(mockHostIface, sendNewLogCmd(_, _))
-        .WillRepeatedly(Invoke(send));
-
     dataIface.changeHostState(true);
 
     // Add and dispatch/send one PEL
@@ -684,7 +564,7 @@
     repo.add(pel);
     runEvents(sdEvent, 2);
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 1);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 1);
     EXPECT_EQ(notifier.queueSize(), 0);
 
     // Host is full
@@ -702,37 +582,37 @@
     // Clock it, nothing should be sent still.
     runEvents(sdEvent, 1);
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 1);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 1);
     EXPECT_EQ(notifier.queueSize(), 1);
 
     // Add another PEL and clock it, still nothing sent
     pel = makePEL();
     repo.add(pel);
     runEvents(sdEvent, 2);
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 1);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 1);
     EXPECT_EQ(notifier.queueSize(), 2);
 
     // Let the host full timer expire to trigger a retry.
     // Add some extra event passes just to be sure nothing new is sent.
-    runEvents(sdEvent, 5, mockHostIface.getHostFullRetryDelay());
+    runEvents(sdEvent, 5, mockHostIface->getHostFullRetryDelay());
 
     // The timer expiration will send just the 1, not both
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 2);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 2);
     EXPECT_EQ(notifier.queueSize(), 1);
 
     // Host still full
     notifier.setHostFull(id);
 
     // Let the host full timer attempt again
-    runEvents(sdEvent, 2, mockHostIface.getHostFullRetryDelay());
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 3);
+    runEvents(sdEvent, 2, mockHostIface->getHostFullRetryDelay());
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 3);
 
     // Add yet another PEL with the retry timer expired.
     // It shouldn't get sent out.
     pel = makePEL();
     repo.add(pel);
     runEvents(sdEvent, 2);
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 3);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 3);
 
     // The last 2 PELs still on the queue
     EXPECT_EQ(notifier.queueSize(), 2);
@@ -743,43 +623,28 @@
     // Now the remaining 2 PELs will be dispatched
     runEvents(sdEvent, 3);
 
-    EXPECT_EQ(mockHostIface.numCmdsProcessed(), 5);
+    EXPECT_EQ(mockHostIface->numCmdsProcessed(), 5);
     EXPECT_EQ(notifier.queueSize(), 0);
 }
 
 // Test when the host says it was send a malformed PEL
 TEST_F(HostNotifierTest, TestBadPEL)
 {
-    MockDataInterface dataIface;
     sdeventplus::Event sdEvent{event};
 
     {
-        Repository repo{repoPath};
-
-        std::unique_ptr<HostInterface> hostIface =
-            std::make_unique<MockHostInterface>(event, dataIface);
-
-        MockHostInterface& mockHostIface =
-            reinterpret_cast<MockHostInterface&>(*hostIface);
-
-        HostNotifier notifier{repo, dataIface, std::move(hostIface)};
-
-        auto send = [&mockHostIface](uint32_t id, uint32_t size) {
-            return mockHostIface.send(0);
-        };
-
-        EXPECT_CALL(mockHostIface, sendNewLogCmd(_, _))
-            .WillRepeatedly(Invoke(send));
+        Repository repo1{repoPath};
+        HostNotifier notifier{repo1, dataIface, std::move(hostIface)};
 
         dataIface.changeHostState(true);
 
         // Add a PEL and dispatch and send it
         auto pel = makePEL();
         auto id = pel->id();
-        repo.add(pel);
+        repo1.add(pel);
 
         runEvents(sdEvent, 2);
-        EXPECT_EQ(mockHostIface.numCmdsProcessed(), 1);
+        EXPECT_EQ(mockHostIface->numCmdsProcessed(), 1);
         EXPECT_EQ(notifier.queueSize(), 0);
 
         // The host rejected it.
@@ -790,7 +655,7 @@
 
         // Check the state was saved in the PEL itself
         Repository::LogID i{Repository::LogID::Pel{id}};
-        auto data = repo.getPELData(i);
+        auto data = repo1.getPELData(i);
         PEL pelFromRepo{*data};
         EXPECT_EQ(pelFromRepo.hostTransmissionState(),
                   TransmissionState::badPEL);
@@ -803,12 +668,12 @@
 
     // Now restore the repo, and make sure it doesn't come back
     {
-        Repository repo{repoPath};
+        Repository repo1{repoPath};
 
-        std::unique_ptr<HostInterface> hostIface =
+        std::unique_ptr<HostInterface> hostIface1 =
             std::make_unique<MockHostInterface>(event, dataIface);
 
-        HostNotifier notifier{repo, dataIface, std::move(hostIface)};
+        HostNotifier notifier{repo1, dataIface, std::move(hostIface1)};
 
         EXPECT_EQ(notifier.queueSize(), 0);
     }