Add unit tests for the phosphor-logging server
-Add unit tests for error wrapping
Change-Id: Ib15620d84de8ab5abdc85b8f88dd7c05f83fd6f3
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
diff --git a/configure.ac b/configure.ac
index d9c022d..3ae2407 100644
--- a/configure.ac
+++ b/configure.ac
@@ -89,11 +89,15 @@
AS_IF([test "x$CALLOUTS_YAML" == "x"], \
[CALLOUTS_YAML="callouts-example.yaml"])
-AC_ARG_VAR(ERRLOG_PERSIST_PATH, [Path of directory housing persisted errors.])
-AS_IF([test "x$ERRLOG_PERSIST_PATH" == "x"], \
- [ERRLOG_PERSIST_PATH="/var/lib/phosphor-logging/errors"])
-AC_DEFINE_UNQUOTED([ERRLOG_PERSIST_PATH], ["$ERRLOG_PERSIST_PATH"], \
- [Path of directory housing persisted errors])
+AC_ARG_VAR(ERRLOG_PERSIST_PATH, [Path of directory housing persisted errors.])\
+AS_IF([test "x$ERRLOG_PERSIST_PATH" == "x"],
+ AS_IF([test "x$enable_oe_sdk" == "xyes"], \
+ [ERRLOG_PERSIST_PATH="/tmp/errors"])
+ AS_IF([test "x$enable_oe_sdk" == "x"], \
+ [ERRLOG_PERSIST_PATH="/var/lib/phosphor-logging/errors"])
+ AC_DEFINE_UNQUOTED([ERRLOG_PERSIST_PATH], ["$ERRLOG_PERSIST_PATH"], \
+ [Path of directory housing persisted errors])
+)
# Compile error metadata handlers if we're asked to do so.
AC_ARG_ENABLE([metadata-processing],
diff --git a/log_manager.cpp b/log_manager.cpp
index 71858f6..cbbf9a8 100644
--- a/log_manager.cpp
+++ b/log_manager.cpp
@@ -43,6 +43,16 @@
return reqLevel;
}
+int Manager::getRealErrSize()
+{
+ return realErrors.size();
+}
+
+int Manager::getInfoErrSize()
+{
+ return infoErrors.size();
+}
+
void Manager::commit(uint64_t transactionId, std::string errMsg)
{
auto level = getLevel(errMsg);
diff --git a/log_manager.hpp b/log_manager.hpp
index d5ca4e9..3da450d 100644
--- a/log_manager.hpp
+++ b/log_manager.hpp
@@ -108,6 +108,18 @@
}
}
+ /** @brief Returns the count of high severity errors
+ *
+ * @return int - count of real errors
+ */
+ int getRealErrSize();
+
+ /** @brief Returns the count of Info errors
+ *
+ * @return int - count of info errors
+ */
+ int getInfoErrSize();
+
private:
/*
* @fn _commit()
diff --git a/test/Makefile.am b/test/Makefile.am
index c9946d7..68ad467 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -3,7 +3,7 @@
TESTS = $(check_PROGRAMS)
check_PROGRAMS = \
- elog_unittest \
+ elog_errorwrap_test \
serialization_test_path \
serialization_test_properties
@@ -19,6 +19,7 @@
test_ldflags = \
-lgtest_main -lgtest \
+ -lgmock_main -lgmock \
$(PTHREAD_LIBS) \
$(OESDK_TESTCASE_FLAGS) \
$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
@@ -35,10 +36,13 @@
$(top_builddir)/elog-process-metadata.o
-elog_unittest_CPPFLAGS = $(test_cppflags)
-elog_unittest_CXXFLAGS = $(test_cxxflags)
-elog_unittest_LDFLAGS = $(test_ldflags)
-elog_unittest_SOURCES = elog_unittest.cpp
+elog_errorwrap_test_CPPFLAGS = $(test_cppflags)
+elog_errorwrap_test_CXXFLAGS = $(test_cxxflags)
+elog_errorwrap_test_SOURCES = elog_errorwrap_test.cpp
+elog_errorwrap_test_LDADD = $(test_ldadd)
+elog_errorwrap_test_LDFLAGS = \
+ $(test_ldflags) \
+ -lstdc++fs
serialization_test_path_CPPFLAGS = $(test_cppflags)
serialization_test_path_CXXFLAGS = $(test_cxxflags)
diff --git a/test/elog_errorwrap_test.cpp b/test/elog_errorwrap_test.cpp
new file mode 100644
index 0000000..f341447
--- /dev/null
+++ b/test/elog_errorwrap_test.cpp
@@ -0,0 +1,32 @@
+#include "elog_errorwrap_test.hpp"
+
+namespace phosphor
+{
+namespace logging
+{
+namespace internal
+{
+
+TEST_F(TestLogManager, logCap)
+{
+ for (auto i = 0; i < ERROR_INFO_CAP + 20; i++)
+ {
+ manager.commitWithLvl(i, "FOO", 6);
+ }
+
+ // Max num of Info( and below Sev) errors can be created is qual to
+ // ERROR_INFO_CAP
+ EXPECT_EQ(ERROR_INFO_CAP , manager.getInfoErrSize());
+
+ for (auto i = 0; i < ERROR_CAP + 20; i++)
+ {
+ manager.commitWithLvl(i, "FOO", 0);
+ }
+ // Max num of high severity errors can be created is qual to ERROR_CAP
+ EXPECT_EQ(ERROR_CAP, manager.getRealErrSize());
+
+}
+
+}// namespace internal
+}// namespace logging
+}// namespace phosphor
diff --git a/test/elog_errorwrap_test.hpp b/test/elog_errorwrap_test.hpp
new file mode 100644
index 0000000..7c673b6
--- /dev/null
+++ b/test/elog_errorwrap_test.hpp
@@ -0,0 +1,109 @@
+#include "log_manager.hpp"
+#include "xyz/openbmc_project/Common/error.hpp"
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+#include <sdbusplus/bus.hpp>
+#include <experimental/filesystem>
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog-errors.hpp>
+#include "elog_serialize.hpp"
+#include "config.h"
+
+namespace phosphor
+{
+namespace logging
+{
+namespace internal
+{
+
+namespace fs = std::experimental::filesystem;
+
+class journalInterface
+{
+ public:
+ virtual void journalSync() = 0;
+ virtual int sd_journal_open(sd_journal** j, int k) = 0;
+ virtual int sd_journal_get_data(sd_journal* j,
+ const char* transactionIdVar,
+ const void** data,
+ size_t length) = 0;
+ virtual void sd_journal_close(sd_journal* j) = 0;
+};
+
+class journalImpl : public journalInterface
+{
+ public:
+ void journalSync();
+ int sd_journal_open(sd_journal** j, int k);
+ int sd_journal_get_data(sd_journal* j,
+ const char* transactionIdVar,
+ const void** data,
+ size_t length);
+ void sd_journal_close(sd_journal* j);
+};
+
+
+
+int journalImpl::sd_journal_open(sd_journal** j, int k)
+{
+ return 1;
+}
+
+void journalImpl::journalSync()
+{
+ return;
+}
+
+int journalImpl::sd_journal_get_data(sd_journal* j,
+ const char* transactionIdVar,
+ const void** data,
+ size_t length)
+{
+ return 1;
+}
+
+void journalImpl::sd_journal_close(sd_journal* j)
+{
+ return;
+}
+
+
+class MockJournal : public Manager
+{
+ public:
+ MockJournal(sdbusplus::bus::bus& bus,
+ const char* objPath): Manager(bus, objPath) {};
+ MOCK_METHOD0(journalSync, void());
+ MOCK_METHOD2(sd_journal_open, int(sd_journal**, int));
+ MOCK_METHOD4(sd_journal_get_data, int(sd_journal*,
+ const char*,
+ const void**, size_t));
+ MOCK_METHOD1(sd_journal_close, void(sd_journal*));
+};
+
+
+
+class TestLogManager : public testing::Test
+{
+ public:
+ sdbusplus::bus::bus bus;
+ MockJournal manager;
+ TestLogManager()
+ : bus(sdbusplus::bus::new_default()),
+ manager(bus, "/xyz/openbmc_test/abc")
+ {
+ fs::create_directories(ERRLOG_PERSIST_PATH);
+ }
+
+ ~TestLogManager()
+ {
+ fs::remove_all(ERRLOG_PERSIST_PATH);
+ }
+
+};
+
+
+}// nmaespace internal
+}// namespace logging
+}// namespace phosphor
diff --git a/test/elog_unittest.cpp b/test/elog_unittest.cpp
deleted file mode 100644
index febccf9..0000000
--- a/test/elog_unittest.cpp
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <phosphor-logging/elog.hpp>
-#include <gtest/gtest.h>
-
-// TODO - need to get gtest working in the SDK
-TEST(BasicLog, Zero) {
- EXPECT_EQ(1, 1);
-}