blob: 60c60634a64bd2928567718b33ef24e9621d3814 [file] [log] [blame]
Brandon Kimdab96f12021-02-18 11:21:37 -08001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Sui Chen03eba282021-02-11 11:35:56 -080015#include "util.hpp"
16
17#include <filesystem>
18#include <fstream>
19
20#include "gtest/gtest.h"
21
22TEST(IsNumericPath, invalidNumericPath)
23{
24 std::string badPath{"badNumericPath"};
25 int id = -1;
26 EXPECT_FALSE(metric_blob::isNumericPath(badPath, id));
27 EXPECT_EQ(id, -1);
28}
29
30TEST(IsNumericPath, validNumericPath)
31{
32 std::string goodPath{"proc/10000"};
33 int id = -1;
34 EXPECT_TRUE(metric_blob::isNumericPath(goodPath, id));
35 EXPECT_EQ(id, 10000);
36}
37
Michael Shenb63d6312021-04-26 13:30:57 +080038TEST(ReadFileThenGrepIntoString, goodFile)
Sui Chen03eba282021-02-11 11:35:56 -080039{
40 const std::string& fileName = "./test_file";
41 std::ofstream ofs(fileName, std::ios::trunc);
42 std::string_view content = "This is\ntest\tcontentt\n\n\n\n.\n\n##$#$";
43 ofs << content;
44 ofs.close();
Michael Shenb63d6312021-04-26 13:30:57 +080045 std::string readContent = metric_blob::readFileThenGrepIntoString(fileName);
Sui Chen03eba282021-02-11 11:35:56 -080046 std::filesystem::remove(fileName);
47 EXPECT_EQ(readContent, content);
48}
49
Michael Shenb63d6312021-04-26 13:30:57 +080050TEST(ReadFileThenGrepIntoString, inexistentFile)
Sui Chen03eba282021-02-11 11:35:56 -080051{
52 const std::string& fileName = "./inexistent_file";
Michael Shenb63d6312021-04-26 13:30:57 +080053 std::string readContent = metric_blob::readFileThenGrepIntoString(fileName);
Sui Chen03eba282021-02-11 11:35:56 -080054 EXPECT_EQ(readContent, "");
55}
56
57TEST(GetTcommUtimeStime, validInput)
58{
59 // ticks_per_sec is usually 100 on the BMC
60 const long ticksPerSec = 100;
61
Patrick Williamsc66ebc32024-08-16 15:21:56 -040062 const std::string_view content =
63 "2596 (dbus-broker) R 2577 2577 2577 0 -1 "
64 "4194560 299 0 1 0 333037 246110 0 0 20 0 "
65 "1 0 1545 3411968 530 4294967295 65536 "
66 "246512 2930531712 0 0 0 81923 4";
Sui Chen03eba282021-02-11 11:35:56 -080067
68 metric_blob::TcommUtimeStime t =
69 metric_blob::parseTcommUtimeStimeString(content, ticksPerSec);
70 const float EPS = 0.01; // The difference was 0.000117188
71 EXPECT_LT(std::abs(t.utime - 3330.37), EPS);
72 EXPECT_LT(std::abs(t.stime - 2461.10), EPS);
73 EXPECT_EQ(t.tcomm, "(dbus-broker)");
74}
75
76TEST(GetTcommUtimeStime, invalidInput)
77{
78 // ticks_per_sec is usually 100 on the BMC
79 const long ticksPerSec = 100;
80
81 const std::string_view content =
82 "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";
83
84 metric_blob::TcommUtimeStime t =
85 metric_blob::parseTcommUtimeStimeString(content, ticksPerSec);
86
87 EXPECT_EQ(t.utime, 0);
88 EXPECT_EQ(t.stime, 0);
89 EXPECT_EQ(t.tcomm, "invalid");
90}
91
92TEST(ParseMeminfoValue, validInput)
93{
Patrick Williamsc66ebc32024-08-16 15:21:56 -040094 const std::string_view content =
95 "MemTotal: 1027040 kB\n"
96 "MemFree: 868144 kB\n"
97 "MemAvailable: 919308 kB\n"
98 "Buffers: 13008 kB\n"
99 "Cached: 82840 kB\n"
100 "SwapCached: 0 kB\n"
101 "Active: 62076 kB\n";
Sui Chen03eba282021-02-11 11:35:56 -0800102 int value;
103 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "MemTotal:", value));
104 EXPECT_EQ(value, 1027040);
105 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "MemFree:", value));
106 EXPECT_EQ(value, 868144);
107 EXPECT_TRUE(
108 metric_blob::parseMeminfoValue(content, "MemAvailable:", value));
109 EXPECT_EQ(value, 919308);
110 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "Buffers:", value));
111 EXPECT_EQ(value, 13008);
112 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "Cached:", value));
113 EXPECT_EQ(value, 82840);
114 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "SwapCached:", value));
115 EXPECT_EQ(value, 0);
116 EXPECT_TRUE(metric_blob::parseMeminfoValue(content, "Active:", value));
117 EXPECT_EQ(value, 62076);
118}
119
120TEST(ParseMeminfoValue, invalidInput)
121{
122 const std::string_view invalid = "MemTotal: 1";
123 int value = -999;
124 EXPECT_FALSE(metric_blob::parseMeminfoValue(invalid, "MemTotal:", value));
125 EXPECT_EQ(value, -999);
126 EXPECT_FALSE(metric_blob::parseMeminfoValue(invalid, "x", value));
127 EXPECT_EQ(value, -999);
128}
129
130TEST(ParseProcUptime, validInput)
131{
132 const std::string_view content = "266923.67 512184.95";
133 const double eps =
134 1e-4; // Empirical threshold for floating point number compare
135 double uptime, idleProcessTime;
136 EXPECT_EQ(metric_blob::parseProcUptime(content, uptime, idleProcessTime),
137 true);
138 EXPECT_LT(abs(uptime - 266923.67), eps);
139 EXPECT_LT(abs(idleProcessTime - 512184.95), eps);
140}
141
142TEST(TrimStringRight, nonEmptyResult)
143{
144 EXPECT_EQ(
145 metric_blob::trimStringRight("\n\nabc\n\t\r\x00\x01\x02\x03").size(),
146 5); // "\n\nabc" is left
147}
148
149TEST(TrimStringRight, trimToEmpty)
150{
151 EXPECT_TRUE(metric_blob::trimStringRight(" ").empty());
152 EXPECT_TRUE(metric_blob::trimStringRight("").empty());
153}
154
155int main(int argc, char** argv)
156{
157 ::testing::InitGoogleTest(&argc, argv);
158 return RUN_ALL_TESTS();
Patrick Williams2be45232023-05-10 07:51:22 -0500159}