Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 1 | #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 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace network |
| 11 | { |
| 12 | namespace snmp |
| 13 | { |
| 14 | |
| 15 | class TestSNMPConfManager : public testing::Test |
| 16 | { |
| 17 | public: |
| 18 | sdbusplus::bus::bus bus; |
| 19 | ConfManager manager; |
| 20 | std::string confDir; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 21 | TestSNMPConfManager() : bus(sdbusplus::bus::new_default()), manager(bus, "") |
| 22 | { |
| 23 | char tmp[] = "/tmp/snmpManager.XXXXXX"; |
| 24 | std::string confDir = mkdtemp(tmp); |
| 25 | manager.dbusPersistentLocation = confDir; |
| 26 | } |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 27 | |
| 28 | ~TestSNMPConfManager() |
| 29 | { |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 30 | fs::remove_all(manager.dbusPersistentLocation); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void createSNMPClient(std::string ipaddress, uint16_t port) |
| 34 | { |
| 35 | manager.client(ipaddress, port); |
| 36 | } |
| 37 | |
| 38 | ClientList &getSNMPClients() |
| 39 | { |
| 40 | return manager.clients; |
| 41 | } |
| 42 | |
| 43 | void deleteSNMPClient(std::string ipaddress) |
| 44 | { |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 45 | auto &it = manager.clients[ipaddress]; |
| 46 | it->delete_(); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 47 | } |
| 48 | }; |
| 49 | |
| 50 | // Add single SNMP client |
| 51 | TEST_F(TestSNMPConfManager, AddSNMPClient) |
| 52 | { |
| 53 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 54 | |
| 55 | createSNMPClient("192.168.1.1", 24); |
| 56 | |
| 57 | auto &clients = getSNMPClients(); |
| 58 | EXPECT_EQ(1, clients.size()); |
| 59 | EXPECT_EQ(true, clients.find("192.168.1.1") != clients.end()); |
| 60 | } |
| 61 | |
| 62 | // Add multiple SNMP client |
| 63 | TEST_F(TestSNMPConfManager, AddMultipleSNMPClient) |
| 64 | { |
| 65 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 66 | |
| 67 | createSNMPClient("192.168.1.1", 24); |
| 68 | createSNMPClient("192.168.1.2", 24); |
| 69 | |
| 70 | auto &clients = getSNMPClients(); |
| 71 | EXPECT_EQ(2, clients.size()); |
| 72 | EXPECT_EQ(true, clients.find("192.168.1.1") != clients.end()); |
| 73 | EXPECT_EQ(true, clients.find("192.168.1.2") != clients.end()); |
| 74 | } |
| 75 | |
| 76 | // Delete SNMP client |
| 77 | TEST_F(TestSNMPConfManager, DeleteSNMPClient) |
| 78 | { |
| 79 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 80 | |
| 81 | createSNMPClient("192.168.1.1", 24); |
| 82 | createSNMPClient("192.168.1.2", 24); |
| 83 | |
| 84 | auto &clients = getSNMPClients(); |
| 85 | EXPECT_EQ(2, clients.size()); |
| 86 | deleteSNMPClient("192.168.1.1"); |
| 87 | EXPECT_EQ(1, clients.size()); |
| 88 | EXPECT_EQ(true, clients.find("192.168.1.2") != clients.end()); |
| 89 | } |
| 90 | |
| 91 | } // namespace snmp |
Ratan Gupta | aea53d0 | 2018-09-06 17:56:59 +0530 | [diff] [blame] | 92 | } // namespace network |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 93 | } // namespace phosphor |