regulators: Add clearErrorHistory to System class

Add a clearErrorHistory() method to the System class.  The method should
clear all data on previously logged errors.  If the error occurs again
in the future, it will be logged again.

This method is normally called when the system is being powered on.  For
code that runs repeatedly, errors are only logged once per boot.

When the system is powered off, hardware may be replaced that fixes the
problem.  Thus, errors are cleared during power on.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I6206cc22900c97f5fbfcd39fde81e448a89ac94d
diff --git a/phosphor-regulators/src/system.cpp b/phosphor-regulators/src/system.cpp
index 3cc2eb3..a36d1ee 100644
--- a/phosphor-regulators/src/system.cpp
+++ b/phosphor-regulators/src/system.cpp
@@ -43,6 +43,15 @@
     }
 }
 
+void System::clearErrorHistory()
+{
+    // Clear error history in each chassis
+    for (std::unique_ptr<Chassis>& oneChassis : chassis)
+    {
+        oneChassis->clearErrorHistory();
+    }
+}
+
 void System::closeDevices(Services& services)
 {
     // Close devices in each chassis
diff --git a/phosphor-regulators/src/system.hpp b/phosphor-regulators/src/system.hpp
index 715cb4b..16100c2 100644
--- a/phosphor-regulators/src/system.hpp
+++ b/phosphor-regulators/src/system.hpp
@@ -66,6 +66,16 @@
     void clearCache();
 
     /**
+     * Clears all error history.
+     *
+     * All data on previously logged errors will be deleted.  If errors occur
+     * again in the future they will be logged again.
+     *
+     * This method is normally called when the system is being powered on.
+     */
+    void clearErrorHistory();
+
+    /**
      * Close the regulator devices in the system.
      *
      * @param services system services like error logging and the journal
diff --git a/phosphor-regulators/test/system_tests.cpp b/phosphor-regulators/test/system_tests.cpp
index 31df5d6..441bbfe 100644
--- a/phosphor-regulators/test/system_tests.cpp
+++ b/phosphor-regulators/test/system_tests.cpp
@@ -20,6 +20,7 @@
 #include "i2c_interface.hpp"
 #include "id_map.hpp"
 #include "mock_action.hpp"
+#include "mock_error_logging.hpp"
 #include "mock_journal.hpp"
 #include "mock_sensors.hpp"
 #include "mock_services.hpp"
@@ -31,6 +32,7 @@
 #include "sensors.hpp"
 #include "services.hpp"
 #include "system.hpp"
+#include "test_sdbus_error.hpp"
 #include "test_utils.hpp"
 
 #include <memory>
@@ -47,6 +49,7 @@
 
 using ::testing::A;
 using ::testing::Return;
+using ::testing::Throw;
 using ::testing::TypedEq;
 
 static const std::string chassisInvPath{
@@ -118,6 +121,84 @@
     EXPECT_FALSE(presenceDetectionPtr->getCachedPresence().has_value());
 }
 
+TEST(SystemTests, ClearErrorHistory)
+{
+    // Create SensorMonitoring.  Will fail with a DBus exception.
+    std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+    EXPECT_CALL(*action, execute)
+        .WillRepeatedly(Throw(TestSDBusError{"Unable to set sensor value"}));
+    std::vector<std::unique_ptr<Action>> actions{};
+    actions.emplace_back(std::move(action));
+    std::unique_ptr<SensorMonitoring> sensorMonitoring =
+        std::make_unique<SensorMonitoring>(std::move(actions));
+
+    // Create Rail
+    std::unique_ptr<Configuration> configuration{};
+    std::unique_ptr<Rail> rail = std::make_unique<Rail>(
+        "vddr1", std::move(configuration), std::move(sensorMonitoring));
+
+    // Create Device that contains Rail
+    std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+        std::make_unique<i2c::MockedI2CInterface>();
+    std::unique_ptr<PresenceDetection> presenceDetection{};
+    std::unique_ptr<Configuration> deviceConfiguration{};
+    std::vector<std::unique_ptr<Rail>> rails{};
+    rails.emplace_back(std::move(rail));
+    std::unique_ptr<Device> device = std::make_unique<Device>(
+        "reg1", true,
+        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
+        std::move(i2cInterface), std::move(presenceDetection),
+        std::move(deviceConfiguration), std::move(rails));
+
+    // Create Chassis that contains Device
+    std::vector<std::unique_ptr<Device>> devices{};
+    devices.emplace_back(std::move(device));
+    std::unique_ptr<Chassis> chassis =
+        std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
+
+    // Create System that contains Chassis
+    std::vector<std::unique_ptr<Rule>> rules{};
+    std::vector<std::unique_ptr<Chassis>> chassisVec{};
+    chassisVec.emplace_back(std::move(chassis));
+    System system{std::move(rules), std::move(chassisVec)};
+
+    // Create mock services
+    MockServices services{};
+
+    // Expect Sensors service to be called 5+5=10 times
+    MockSensors& sensors = services.getMockSensors();
+    EXPECT_CALL(sensors, startRail).Times(10);
+    EXPECT_CALL(sensors, setValue).Times(0);
+    EXPECT_CALL(sensors, endRail).Times(10);
+
+    // Expect Journal service to be called 3+3=6 times to log error messages
+    MockJournal& journal = services.getMockJournal();
+    EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
+        .Times(6);
+    EXPECT_CALL(journal, logError(A<const std::string&>())).Times(6);
+
+    // Expect ErrorLogging service to be called 1+1=2 times to log a DBus error
+    MockErrorLogging& errorLogging = services.getMockErrorLogging();
+    EXPECT_CALL(errorLogging, logDBusError).Times(2);
+
+    // Monitor sensors 5 times.  Should fail every time, write to journal 3
+    // times, and log one error.
+    for (int i = 1; i <= 5; ++i)
+    {
+        system.monitorSensors(services);
+    }
+
+    // Clear error history
+    system.clearErrorHistory();
+
+    // Monitor sensors 5 times again.  Should fail every time, write to journal
+    // 3 times, and log one error.
+    for (int i = 1; i <= 5; ++i)
+    {
+        system.monitorSensors(services);
+    }
+}
+
 TEST(SystemTests, CloseDevices)
 {
     // Specify an empty rules vector