Persist the snmp manager configuration
This commit persist the manager configuration D-Bus objects
and restores it once service starts.
This commit also deletes the associated persistent file whenever
snmp client D-Bus object gets deleted.
Change-Id: I1526b52870ee5dfea30e6527bad3fd12d1191a13
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/test/Makefile.am.include b/test/Makefile.am.include
index f99ab24..97b018c 100644
--- a/test/Makefile.am.include
+++ b/test/Makefile.am.include
@@ -30,10 +30,12 @@
test_notification_SOURCES = \
%reldir%/test_error_notification.cpp \
%reldir%/test_snmp_conf_manager.cpp \
- %reldir%/test_snmp_util.cpp
+ %reldir%/test_snmp_util.cpp \
+ %reldir%/test_snmp_serialize.cpp
test_notification_LDADD = $(top_builddir)/phosphor_network_snmpconf-snmp_conf_manager.o \
$(top_builddir)/phosphor_network_snmpconf-snmp_client.o \
+ $(top_builddir)/phosphor_network_snmpconf-snmp_serialize.o \
$(top_builddir)/phosphor_network_snmpconf-snmp_util.o \
$(top_builddir)/xyz/openbmc_project/Network/Client/Create/phosphor_network_snmpconf-server.o
diff --git a/test/test_snmp_conf_manager.cpp b/test/test_snmp_conf_manager.cpp
index ab29215..ebf492f 100644
--- a/test/test_snmp_conf_manager.cpp
+++ b/test/test_snmp_conf_manager.cpp
@@ -18,11 +18,16 @@
sdbusplus::bus::bus bus;
ConfManager manager;
std::string confDir;
- TestSNMPConfManager() :
- bus(sdbusplus::bus::new_default()), manager(bus, ""){};
+ TestSNMPConfManager() : bus(sdbusplus::bus::new_default()), manager(bus, "")
+ {
+ char tmp[] = "/tmp/snmpManager.XXXXXX";
+ std::string confDir = mkdtemp(tmp);
+ manager.dbusPersistentLocation = confDir;
+ }
~TestSNMPConfManager()
{
+ fs::remove_all(manager.dbusPersistentLocation);
}
void createSNMPClient(std::string ipaddress, uint16_t port)
@@ -37,11 +42,8 @@
void deleteSNMPClient(std::string ipaddress)
{
- if (manager.clients.find(ipaddress) != manager.clients.end())
- {
- auto &it = manager.clients[ipaddress];
- it->delete_();
- }
+ auto &it = manager.clients[ipaddress];
+ it->delete_();
}
};
diff --git a/test/test_snmp_serialize.cpp b/test/test_snmp_serialize.cpp
new file mode 100644
index 0000000..60a51aa
--- /dev/null
+++ b/test/test_snmp_serialize.cpp
@@ -0,0 +1,82 @@
+#include <experimental/filesystem>
+#include <fstream>
+#include <gtest/gtest.h>
+#include <netinet/in.h>
+
+#include "snmp_client.hpp"
+#include "snmp_conf_manager.hpp"
+#include "snmp_serialize.hpp"
+
+namespace phosphor
+{
+namespace network
+{
+namespace snmp
+{
+
+constexpr auto clientObjPath = "/xyz/openbmc_test/snmp/client";
+namespace fs = std::experimental::filesystem;
+
+class TestSerialize : public testing::Test
+{
+ public:
+ sdbusplus::bus::bus bus;
+ ConfManager manager;
+ TestSerialize() :
+ bus(sdbusplus::bus::new_default()),
+ manager(bus, "/xyz/openbmc_test/snmp/manager")
+ {
+ char tmp[] = "/tmp/snmpManager.XXXXXX";
+ std::string confDir = mkdtemp(tmp);
+ manager.dbusPersistentLocation = confDir;
+ }
+ ~TestSerialize()
+ {
+ std::error_code ec;
+ fs::remove_all(manager.dbusPersistentLocation, ec);
+ }
+};
+
+TEST_F(TestSerialize, serialize)
+{
+ Client client(bus, clientObjPath, manager, "1.1.1.1", 23);
+
+ auto path = serialize(client, manager.dbusPersistentLocation);
+ Client restoreClient(bus, clientObjPath, manager);
+
+ deserialize(path, restoreClient);
+
+ EXPECT_EQ("1.1.1.1", restoreClient.address());
+ EXPECT_EQ(23, restoreClient.port());
+}
+
+TEST_F(TestSerialize, deserialize_non_existent_file)
+{
+ Client client(bus, clientObjPath, manager);
+ fs::path path = manager.dbusPersistentLocation;
+ path /= "snmpTest";
+
+ auto ret = deserialize(path, client);
+
+ EXPECT_EQ(false, ret);
+}
+
+TEST_F(TestSerialize, deserialize_empty_file)
+{
+ Client restoreClient(bus, clientObjPath, manager);
+
+ std::fstream file;
+
+ fs::path path = manager.dbusPersistentLocation;
+ path /= "snmpTest";
+
+ file.open(path.string(), std::ofstream::out);
+ file.close();
+ // deserialize the object with empty file
+ auto ret = deserialize(path, restoreClient);
+ EXPECT_EQ(false, ret);
+}
+
+} // namespace snmp
+} // namespace network
+} // namespace phosphor