Sui Chen | 03eba28 | 2021-02-11 11:35:56 -0800 | [diff] [blame] | 1 | #include "util.hpp" |
| 2 | |
| 3 | #include <filesystem> |
| 4 | #include <fstream> |
| 5 | |
| 6 | #include "gtest/gtest.h" |
| 7 | |
| 8 | TEST(IsNumericPath, invalidNumericPath) |
| 9 | { |
| 10 | std::string badPath{"badNumericPath"}; |
| 11 | int id = -1; |
| 12 | EXPECT_FALSE(metric_blob::isNumericPath(badPath, id)); |
| 13 | EXPECT_EQ(id, -1); |
| 14 | } |
| 15 | |
| 16 | TEST(IsNumericPath, validNumericPath) |
| 17 | { |
| 18 | std::string goodPath{"proc/10000"}; |
| 19 | int id = -1; |
| 20 | EXPECT_TRUE(metric_blob::isNumericPath(goodPath, id)); |
| 21 | EXPECT_EQ(id, 10000); |
| 22 | } |
| 23 | |
| 24 | TEST(ReadFileIntoString, goodFile) |
| 25 | { |
| 26 | const std::string& fileName = "./test_file"; |
| 27 | std::ofstream ofs(fileName, std::ios::trunc); |
| 28 | std::string_view content = "This is\ntest\tcontentt\n\n\n\n.\n\n##$#$"; |
| 29 | ofs << content; |
| 30 | ofs.close(); |
| 31 | std::string readContent = metric_blob::readFileIntoString(fileName); |
| 32 | std::filesystem::remove(fileName); |
| 33 | EXPECT_EQ(readContent, content); |
| 34 | } |
| 35 | |
| 36 | TEST(ReadFileIntoString, inexistentFile) |
| 37 | { |
| 38 | const std::string& fileName = "./inexistent_file"; |
| 39 | std::string readContent = metric_blob::readFileIntoString(fileName); |
| 40 | EXPECT_EQ(readContent, ""); |
| 41 | } |
| 42 | |
| 43 | TEST(GetTcommUtimeStime, validInput) |
| 44 | { |
| 45 | // ticks_per_sec is usually 100 on the BMC |
| 46 | const long ticksPerSec = 100; |
| 47 | |
| 48 | const std::string_view content = "2596 (dbus-broker) R 2577 2577 2577 0 -1 " |
| 49 | "4194560 299 0 1 0 333037 246110 0 0 20 0 " |
| 50 | "1 0 1545 3411968 530 4294967295 65536 " |
| 51 | "246512 2930531712 0 0 0 81923 4"; |
| 52 | |
| 53 | metric_blob::TcommUtimeStime t = |
| 54 | metric_blob::parseTcommUtimeStimeString(content, ticksPerSec); |
| 55 | const float EPS = 0.01; // The difference was 0.000117188 |
| 56 | EXPECT_LT(std::abs(t.utime - 3330.37), EPS); |
| 57 | EXPECT_LT(std::abs(t.stime - 2461.10), EPS); |
| 58 | EXPECT_EQ(t.tcomm, "(dbus-broker)"); |
| 59 | } |
| 60 | |
| 61 | TEST(GetTcommUtimeStime, invalidInput) |
| 62 | { |
| 63 | // ticks_per_sec is usually 100 on the BMC |
| 64 | const long ticksPerSec = 100; |
| 65 | |
| 66 | const std::string_view content = |
| 67 | "x invalid x x x x x x x x x x x x x x x x x x x x x x x x x x x"; |
| 68 | |
| 69 | metric_blob::TcommUtimeStime t = |
| 70 | metric_blob::parseTcommUtimeStimeString(content, ticksPerSec); |
| 71 | |
| 72 | EXPECT_EQ(t.utime, 0); |
| 73 | EXPECT_EQ(t.stime, 0); |
| 74 | EXPECT_EQ(t.tcomm, "invalid"); |
| 75 | } |
| 76 | |
| 77 | TEST(ParseMeminfoValue, validInput) |
| 78 | { |
| 79 | const std::string_view content = "MemTotal: 1027040 kB\n" |
| 80 | "MemFree: 868144 kB\n" |
| 81 | "MemAvailable: 919308 kB\n" |
| 82 | "Buffers: 13008 kB\n" |
| 83 | "Cached: 82840 kB\n" |
| 84 | "SwapCached: 0 kB\n" |
| 85 | "Active: 62076 kB\n"; |
| 86 | int value; |
| 87 | EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "MemTotal:", value)); |
| 88 | EXPECT_EQ(value, 1027040); |
| 89 | EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "MemFree:", value)); |
| 90 | EXPECT_EQ(value, 868144); |
| 91 | EXPECT_TRUE( |
| 92 | metric_blob::parseMeminfoValue(content, "MemAvailable:", value)); |
| 93 | EXPECT_EQ(value, 919308); |
| 94 | EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "Buffers:", value)); |
| 95 | EXPECT_EQ(value, 13008); |
| 96 | EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "Cached:", value)); |
| 97 | EXPECT_EQ(value, 82840); |
| 98 | EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "SwapCached:", value)); |
| 99 | EXPECT_EQ(value, 0); |
| 100 | EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "Active:", value)); |
| 101 | EXPECT_EQ(value, 62076); |
| 102 | } |
| 103 | |
| 104 | TEST(ParseMeminfoValue, invalidInput) |
| 105 | { |
| 106 | const std::string_view invalid = "MemTotal: 1"; |
| 107 | int value = -999; |
| 108 | EXPECT_FALSE(metric_blob::parseMeminfoValue(invalid, "MemTotal:", value)); |
| 109 | EXPECT_EQ(value, -999); |
| 110 | EXPECT_FALSE(metric_blob::parseMeminfoValue(invalid, "x", value)); |
| 111 | EXPECT_EQ(value, -999); |
| 112 | } |
| 113 | |
| 114 | TEST(ParseProcUptime, validInput) |
| 115 | { |
| 116 | const std::string_view content = "266923.67 512184.95"; |
| 117 | const double eps = |
| 118 | 1e-4; // Empirical threshold for floating point number compare |
| 119 | double uptime, idleProcessTime; |
| 120 | EXPECT_EQ(metric_blob::parseProcUptime(content, uptime, idleProcessTime), |
| 121 | true); |
| 122 | EXPECT_LT(abs(uptime - 266923.67), eps); |
| 123 | EXPECT_LT(abs(idleProcessTime - 512184.95), eps); |
| 124 | } |
| 125 | |
| 126 | TEST(TrimStringRight, nonEmptyResult) |
| 127 | { |
| 128 | EXPECT_EQ( |
| 129 | metric_blob::trimStringRight("\n\nabc\n\t\r\x00\x01\x02\x03").size(), |
| 130 | 5); // "\n\nabc" is left |
| 131 | } |
| 132 | |
| 133 | TEST(TrimStringRight, trimToEmpty) |
| 134 | { |
| 135 | EXPECT_TRUE(metric_blob::trimStringRight(" ").empty()); |
| 136 | EXPECT_TRUE(metric_blob::trimStringRight("").empty()); |
| 137 | } |
| 138 | |
| 139 | int main(int argc, char** argv) |
| 140 | { |
| 141 | ::testing::InitGoogleTest(&argc, argv); |
| 142 | return RUN_ALL_TESTS(); |
| 143 | } |