Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 1 | #include "errors.hpp" |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 2 | #include "handler.hpp" |
Patrick Venture | c87de55 | 2020-05-20 20:25:39 -0700 | [diff] [blame] | 3 | #include "handler_impl.hpp" |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 4 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 5 | #include <fstream> |
| 6 | #include <nlohmann/json.hpp> |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <tuple> |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | namespace google |
| 13 | { |
| 14 | namespace ipmi |
| 15 | { |
| 16 | |
| 17 | TEST(HandlerTest, EthCheckValidHappy) |
| 18 | { |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 19 | Handler h; |
William A. Kennington III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame^] | 20 | std::tuple<std::uint8_t, std::string> result = h.getEthDetails("et"); |
| 21 | EXPECT_EQ(12, std::get<0>(result)); |
| 22 | EXPECT_STREQ("et", std::get<1>(result).c_str()); |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 25 | TEST(HandlerTest, CableCheckIllegalPath) |
| 26 | { |
| 27 | Handler h; |
| 28 | EXPECT_THROW(h.getRxPackets("eth0/../../"), IpmiException); |
| 29 | } |
| 30 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 31 | TEST(HandlerTest, readNameFromConfigInstanceVariety) |
| 32 | { |
| 33 | // Make sure it handles the failures and successes as we expect. |
| 34 | struct testCase |
| 35 | { |
| 36 | std::string type; |
| 37 | std::uint8_t instance; |
| 38 | std::string expectedName; |
| 39 | }; |
| 40 | |
| 41 | std::vector<testCase> tests = { |
| 42 | {"cpu", 5, ""}, |
| 43 | {"cpu", 3, "CPU2"}, |
| 44 | }; |
| 45 | |
| 46 | auto j2 = R"( |
| 47 | { |
| 48 | "cpu": [ |
| 49 | {"instance": 1, "name": "CPU0"}, |
| 50 | {"instance": 2, "name": "CPU1"}, |
| 51 | {"instance": 3, "name": "CPU2"}, |
| 52 | {"instance": 4, "name": "CPU3"} |
| 53 | ] |
| 54 | } |
| 55 | )"_json; |
| 56 | |
| 57 | for (const auto& test : tests) |
| 58 | { |
| 59 | EXPECT_STREQ(test.expectedName.c_str(), |
| 60 | readNameFromConfig(test.type, test.instance, j2).c_str()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // TODO: If we can test with phosphor-logging in the future, there are more |
| 65 | // failure cases. |
| 66 | |
| 67 | TEST(HandlerTest, getEntityNameWithNameNotFoundExcepts) |
| 68 | { |
| 69 | const char* testFilename = "test.json"; |
| 70 | std::string contents = R"({"cpu": [{"instance": 1, "name": "CPU0"}]})"; |
| 71 | std::ofstream outputJson(testFilename); |
| 72 | outputJson << contents; |
| 73 | outputJson.flush(); |
| 74 | outputJson.close(); |
| 75 | |
| 76 | Handler h(testFilename); |
| 77 | EXPECT_THROW(h.getEntityName(0x03, 2), IpmiException); |
| 78 | (void)std::remove(testFilename); |
| 79 | } |
| 80 | |
| 81 | TEST(HandlerTest, getEntityNameWithNameFoundReturnsIt) |
| 82 | { |
| 83 | const char* testFilename = "test.json"; |
| 84 | std::string contents = R"({"cpu": [{"instance": 1, "name": "CPU0"}]})"; |
| 85 | std::ofstream outputJson(testFilename); |
| 86 | outputJson << contents; |
| 87 | outputJson.flush(); |
| 88 | outputJson.close(); |
| 89 | |
| 90 | Handler h(testFilename); |
| 91 | EXPECT_STREQ("CPU0", h.getEntityName(0x03, 1).c_str()); |
| 92 | (void)std::remove(testFilename); |
| 93 | } |
| 94 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 95 | // TODO: Add checks for other functions of handler. |
| 96 | |
| 97 | } // namespace ipmi |
| 98 | } // namespace google |