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