blob: 6cb2a536d52f83131a5be7e4eb78a147c1ca26bf [file] [log] [blame]
Ratan Gupta1dc91782018-04-19 16:47:12 +05301#include "snmp_conf_manager.hpp"
2
3#include "xyz/openbmc_project/Common/error.hpp"
4
5#include <gtest/gtest.h>
6#include <sdbusplus/bus.hpp>
7
8namespace phosphor
9{
10namespace network
11{
12namespace snmp
13{
14
Ratan Guptaa7ff3852018-11-16 14:05:57 +053015auto managerObjPath = "/xyz/openbmc_test/snmp/manager";
Ratan Gupta9c4fed62018-11-16 17:47:54 +053016using InvalidArgument =
17 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053018
Ratan Gupta1dc91782018-04-19 16:47:12 +053019class TestSNMPConfManager : public testing::Test
20{
21 public:
22 sdbusplus::bus::bus bus;
23 ConfManager manager;
24 std::string confDir;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053025 TestSNMPConfManager() :
26 bus(sdbusplus::bus::new_default()), manager(bus, managerObjPath)
Ratan Gupta212f53e2018-04-30 17:28:05 +053027 {
28 char tmp[] = "/tmp/snmpManager.XXXXXX";
29 std::string confDir = mkdtemp(tmp);
30 manager.dbusPersistentLocation = confDir;
31 }
Ratan Gupta1dc91782018-04-19 16:47:12 +053032
33 ~TestSNMPConfManager()
34 {
Ratan Gupta212f53e2018-04-30 17:28:05 +053035 fs::remove_all(manager.dbusPersistentLocation);
Ratan Gupta1dc91782018-04-19 16:47:12 +053036 }
37
Ratan Guptaa7ff3852018-11-16 14:05:57 +053038 std::string createSNMPClient(std::string ipaddress, uint16_t port)
Ratan Gupta1dc91782018-04-19 16:47:12 +053039 {
Ratan Guptaa7ff3852018-11-16 14:05:57 +053040 return manager.client(ipaddress, port);
Ratan Gupta1dc91782018-04-19 16:47:12 +053041 }
42
43 ClientList &getSNMPClients()
44 {
45 return manager.clients;
46 }
47
Ratan Guptaa7ff3852018-11-16 14:05:57 +053048 bool isClientExist(const std::string &ipaddress)
49 {
50 for (const auto &val : manager.clients)
51 {
52 if (val.second.get()->address() == ipaddress)
53 {
54 return true;
55 }
56 }
57 return false;
58 }
59
Ratan Gupta1dc91782018-04-19 16:47:12 +053060 void deleteSNMPClient(std::string ipaddress)
61 {
Ratan Guptaa7ff3852018-11-16 14:05:57 +053062 for (const auto &val : manager.clients)
63 {
64 if (val.second.get()->address() == ipaddress)
65 {
66 val.second.get()->delete_();
67 }
68 }
Ratan Gupta1dc91782018-04-19 16:47:12 +053069 }
70};
71
72// Add single SNMP client
73TEST_F(TestSNMPConfManager, AddSNMPClient)
74{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053075 // check the created object path
76 auto path = createSNMPClient("192.168.1.1", 24);
77 std::string expectedPath = managerObjPath;
78 expectedPath += std::string("/1");
Ratan Gupta1dc91782018-04-19 16:47:12 +053079
Ratan Guptaa7ff3852018-11-16 14:05:57 +053080 EXPECT_EQ(path, expectedPath);
Ratan Gupta1dc91782018-04-19 16:47:12 +053081
Ratan Guptaa7ff3852018-11-16 14:05:57 +053082 // check whether the client created
Ratan Gupta1dc91782018-04-19 16:47:12 +053083 auto &clients = getSNMPClients();
84 EXPECT_EQ(1, clients.size());
Ratan Guptaa7ff3852018-11-16 14:05:57 +053085 EXPECT_EQ(true, isClientExist("192.168.1.1"));
Ratan Gupta1dc91782018-04-19 16:47:12 +053086}
87
88// Add multiple SNMP client
89TEST_F(TestSNMPConfManager, AddMultipleSNMPClient)
90{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053091 // add multiple clients and check whether the object path is generated
92 // correctly.
Ratan Gupta1dc91782018-04-19 16:47:12 +053093 createSNMPClient("192.168.1.1", 24);
Ratan Guptaa7ff3852018-11-16 14:05:57 +053094 auto path = createSNMPClient("192.168.1.2", 24);
95 std::string expectedPath = managerObjPath;
96 expectedPath += std::string("/2");
Ratan Gupta1dc91782018-04-19 16:47:12 +053097
Ratan Guptaa7ff3852018-11-16 14:05:57 +053098 EXPECT_EQ(path, expectedPath);
99
100 // check both the clients get created
Ratan Gupta1dc91782018-04-19 16:47:12 +0530101 auto &clients = getSNMPClients();
102 EXPECT_EQ(2, clients.size());
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530103
104 EXPECT_EQ(true, isClientExist("192.168.1.1"));
105 EXPECT_EQ(true, isClientExist("192.168.1.2"));
Ratan Gupta1dc91782018-04-19 16:47:12 +0530106}
107
Ratan Gupta9c4fed62018-11-16 17:47:54 +0530108// Add duplicate SNMP client
109TEST_F(TestSNMPConfManager, AddDuplicateSNMPClient)
110{
111 createSNMPClient("192.168.1.1", 24);
112 EXPECT_THROW(createSNMPClient("192.168.1.1", 24), InvalidArgument);
113}
114
Ratan Gupta1dc91782018-04-19 16:47:12 +0530115// Delete SNMP client
116TEST_F(TestSNMPConfManager, DeleteSNMPClient)
117{
Ratan Gupta1dc91782018-04-19 16:47:12 +0530118 createSNMPClient("192.168.1.1", 24);
119 createSNMPClient("192.168.1.2", 24);
120
121 auto &clients = getSNMPClients();
122 EXPECT_EQ(2, clients.size());
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530123
Ratan Gupta1dc91782018-04-19 16:47:12 +0530124 deleteSNMPClient("192.168.1.1");
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530125
126 auto path = createSNMPClient("192.168.1.3", 24);
127 std::string expectedPath = managerObjPath;
128 expectedPath += std::string("/3");
129 EXPECT_EQ(path, expectedPath);
130
131 EXPECT_EQ(2, clients.size());
132 EXPECT_EQ(true, isClientExist("192.168.1.2"));
133 EXPECT_EQ(false, isClientExist("192.168.1.1"));
134 EXPECT_EQ(true, isClientExist("192.168.1.3"));
Ratan Gupta1dc91782018-04-19 16:47:12 +0530135}
136
137} // namespace snmp
Ratan Guptaaea53d02018-09-06 17:56:59 +0530138} // namespace network
Ratan Gupta1dc91782018-04-19 16:47:12 +0530139} // namespace phosphor