summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMatt Spinler <spinler@us.ibm.com>2019-12-18 13:48:08 -0600
committerMatt Spinler <spinler@us.ibm.com>2020-02-14 19:15:42 +0000
commitf682b40dcde8badb7029a063e299e1bdf1501444 (patch)
tree00bec044d02c3d5190e84a1a976640dce294678d /test
parent7ae2fa69f2bd15cbd22be54aeefbb49d3ad06fc5 (diff)
downloadphosphor-logging-f682b40dcde8badb7029a063e299e1bdf1501444.tar.gz
phosphor-logging-f682b40dcde8badb7029a063e299e1bdf1501444.zip
PEL: Add ability to create event logs
There are cases where the PEL code wants to be able to create OpenBMC event logs (and thus PELs) for problems it encounters when trying to create or import other PELs. For example, if the host were to send down a malformed PEL, this code would like to create a new event log and capture part of that bad PEL in the new PEL for debug purposes, as the malformed PEL cannot be reported anywhere since it is malformed. To handle this, create the EventLogger class that provides a log() function that allows the PEL extension code to create OpenBMC event logs (and thus PELs) from within. The underlying function to do the event log creating is passed in via the constructor so that it can be changed for testing. The sd_event_add_defer function (wrapped by sdeventplus) is used to dispatch the creation of a single event, so that the entry point is from the event loop. If there are still events left on the queue after that, then they will be also be scheduled with sd_event_add_defer so that the events are always created from event loop calls. EventLogger does not allow events to be added to the queue if it is being done from within the creation function so that the code can't get stuck in a loop of creating a new event every time an event is created. Signed-off-by: Matt Spinler <spinler@us.ibm.com> Change-Id: I6a9062074dc62cfb6043139ff0a9f3dfcd06c708
Diffstat (limited to 'test')
-rw-r--r--test/openpower-pels/Makefile.include9
-rw-r--r--test/openpower-pels/event_logger_test.cpp126
-rw-r--r--test/openpower-pels/pel_manager_test.cpp16
3 files changed, 145 insertions, 6 deletions
diff --git a/test/openpower-pels/Makefile.include b/test/openpower-pels/Makefile.include
index a700a50..09eb36b 100644
--- a/test/openpower-pels/Makefile.include
+++ b/test/openpower-pels/Makefile.include
@@ -4,6 +4,7 @@ check_PROGRAMS += \
additional_data_test \
ascii_string_test \
bcd_time_test \
+ event_logger_test \
extended_user_header_test \
failing_mtms_test \
fru_identity_test \
@@ -353,3 +354,11 @@ json_utils_test_LDADD = \
$(test_ldadd) \
$(top_builddir)/extensions/openpower-pels/json_utils.o
json_utils_test_LDFLAGS = $(test_ldflags)
+
+event_logger_test_SOURCES = \
+ %reldir%/event_logger_test.cpp
+event_logger_test_CPPFLAGS = $(test_cppflags)
+event_logger_test_CXXFLAGS = $(test_cxxflags) $(SDEVENTPLUS_CFLAGS)
+event_logger_test_LDADD = \
+ $(test_ldadd)
+event_logger_test_LDFLAGS = $(test_ldflags) $(SDEVENTPLUS_LIBS)
diff --git a/test/openpower-pels/event_logger_test.cpp b/test/openpower-pels/event_logger_test.cpp
new file mode 100644
index 0000000..1a4d8fc
--- /dev/null
+++ b/test/openpower-pels/event_logger_test.cpp
@@ -0,0 +1,126 @@
+/**
+ * Copyright © 2019 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "extensions/openpower-pels/event_logger.hpp"
+#include "log_manager.hpp"
+
+#include <gtest/gtest.h>
+
+using namespace openpower::pels;
+using namespace phosphor::logging;
+
+class CreateHelper
+{
+ public:
+ void create(const std::string& name, Entry::Level level,
+ const EventLogger::ADMap& ad)
+ {
+ _createCount++;
+ _prevName = name;
+ _prevLevel = level;
+ _prevAD = ad;
+
+ // Try to create another event from within the creation
+ // function. Should never work or else we could get stuck
+ // infinitely creating events.
+ if (_eventLogger)
+ {
+ AdditionalData d;
+ _eventLogger->log(name, level, d);
+ }
+ }
+
+ size_t _createCount = 0;
+ std::string _prevName;
+ Entry::Level _prevLevel;
+ EventLogger::ADMap _prevAD;
+ EventLogger* _eventLogger = nullptr;
+};
+
+void runEvents(sd_event* event, size_t numEvents)
+{
+ sdeventplus::Event e{event};
+
+ for (size_t i = 0; i < numEvents; i++)
+ {
+ e.run(std::chrono::milliseconds(1));
+ }
+}
+
+TEST(EventLoggerTest, TestCreateEvents)
+{
+ sd_event* sdEvent = nullptr;
+ auto r = sd_event_default(&sdEvent);
+ ASSERT_TRUE(r >= 0);
+
+ CreateHelper ch;
+
+ EventLogger eventLogger(
+ sdEvent, std::bind(std::mem_fn(&CreateHelper::create), &ch,
+ std::placeholders::_1, std::placeholders::_2,
+ std::placeholders::_3));
+
+ ch._eventLogger = &eventLogger;
+
+ AdditionalData ad;
+ ad.add("key1", "value1");
+
+ eventLogger.log("one", Entry::Level::Error, ad);
+ EXPECT_EQ(eventLogger.queueSize(), 1);
+
+ runEvents(sdEvent, 1);
+
+ // Verify 1 event was created
+ EXPECT_EQ(eventLogger.queueSize(), 0);
+ EXPECT_EQ(ch._prevName, "one");
+ EXPECT_EQ(ch._prevLevel, Entry::Level::Error);
+ EXPECT_EQ(ch._prevAD, ad.getData());
+ EXPECT_EQ(ch._createCount, 1);
+
+ // Create 2 more, and run 1 event loop at a time and check the results
+ eventLogger.log("two", Entry::Level::Error, ad);
+ eventLogger.log("three", Entry::Level::Error, ad);
+
+ EXPECT_EQ(eventLogger.queueSize(), 2);
+
+ runEvents(sdEvent, 1);
+
+ EXPECT_EQ(ch._createCount, 2);
+ EXPECT_EQ(ch._prevName, "two");
+ EXPECT_EQ(eventLogger.queueSize(), 1);
+
+ runEvents(sdEvent, 1);
+ EXPECT_EQ(ch._createCount, 3);
+ EXPECT_EQ(ch._prevName, "three");
+ EXPECT_EQ(eventLogger.queueSize(), 0);
+
+ // Add them all again and run them all at once
+ eventLogger.log("three", Entry::Level::Error, ad);
+ eventLogger.log("two", Entry::Level::Error, ad);
+ eventLogger.log("one", Entry::Level::Error, ad);
+ runEvents(sdEvent, 3);
+
+ EXPECT_EQ(ch._createCount, 6);
+ EXPECT_EQ(ch._prevName, "one");
+ EXPECT_EQ(eventLogger.queueSize(), 0);
+
+ // Run extra events - doesn't do anything
+ runEvents(sdEvent, 1);
+ EXPECT_EQ(ch._createCount, 6);
+ EXPECT_EQ(ch._prevName, "one");
+ EXPECT_EQ(eventLogger.queueSize(), 0);
+
+ sd_event_unref(sdEvent);
+}
diff --git a/test/openpower-pels/pel_manager_test.cpp b/test/openpower-pels/pel_manager_test.cpp
index 08124ff..28802f4 100644
--- a/test/openpower-pels/pel_manager_test.cpp
+++ b/test/openpower-pels/pel_manager_test.cpp
@@ -67,6 +67,11 @@ std::optional<fs::path> findAnyPELInRepo()
return std::nullopt;
}
+void eventLoggerStub(const std::string&, phosphor::logging::Entry::Level,
+ const EventLogger::ADMap&)
+{
+}
+
// Test that using the RAWPEL=<file> with the Manager::create() call gets
// a PEL saved in the repository.
TEST_F(ManagerTest, TestCreateWithPEL)
@@ -74,7 +79,8 @@ TEST_F(ManagerTest, TestCreateWithPEL)
std::unique_ptr<DataInterfaceBase> dataIface =
std::make_unique<DataInterface>(bus);
- openpower::pels::Manager manager{logManager, std::move(dataIface)};
+ openpower::pels::Manager manager{logManager, std::move(dataIface),
+ eventLoggerStub};
// Create a PEL, write it to a file, and pass that filename into
// the create function.
@@ -141,13 +147,11 @@ TEST_F(ManagerTest, TestCreateWithMessageRegistry)
registryFile << registry;
registryFile.close();
- auto bus = sdbusplus::bus::new_default();
- phosphor::logging::internal::Manager logManager(bus, "logging_path");
-
std::unique_ptr<DataInterfaceBase> dataIface =
std::make_unique<DataInterface>(logManager.getBus());
- openpower::pels::Manager manager{logManager, std::move(dataIface)};
+ openpower::pels::Manager manager{logManager, std::move(dataIface),
+ eventLoggerStub};
std::vector<std::string> additionalData;
std::vector<std::string> associations;
@@ -191,7 +195,7 @@ TEST_F(ManagerTest, TestDBusMethods)
std::unique_ptr<DataInterfaceBase> dataIface =
std::make_unique<DataInterface>(bus);
- Manager manager{logManager, std::move(dataIface)};
+ Manager manager{logManager, std::move(dataIface), eventLoggerStub};
// Create a PEL, write it to a file, and pass that filename into
// the create function so there's one in the repo.
OpenPOWER on IntegriCloud