blob: 6f76933a25aef7a8d1dc922b2a25305d799bc953 [file] [log] [blame]
Lei YUd3204142021-11-16 14:57:27 +08001#include <Utils.hpp>
2
3#include <filesystem>
4#include <fstream>
5
6#include <gtest/gtest.h>
7
8namespace fs = std::filesystem;
9class TestUtils : public testing::Test
10{
11 public:
12 std::string testDir;
13 fs::path hwmonDir;
14 fs::path peciDir;
15 TestUtils()
16 {
17 // Create test environment
18 char dir[] = "./testDirXXXXXX";
19 testDir = mkdtemp(dir);
20
21 if (testDir.empty())
22 {
23 throw std::bad_alloc();
24 }
25 hwmonDir = fs::path(testDir) / "hwmon";
26 fs::create_directory(hwmonDir);
27 auto hwmon10 = hwmonDir / "hwmon10";
28 fs::create_directory(hwmonDir / "hwmon10");
29 std::ofstream{hwmon10 / "temp1_input"};
30 std::ofstream{hwmon10 / "temp1_min"};
31 std::ofstream{hwmon10 / "temp1_max"};
32 std::ofstream{hwmon10 / "temp2_input"};
33 createPECIDir();
34 }
35
36 ~TestUtils() override
37 {
38 fs::remove_all(testDir);
39 }
40
41 void createPECIDir()
42 {
43 peciDir = fs::path(testDir) / "peci";
44 auto peci0 =
45 peciDir / "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
46 fs::create_directories(peci0);
47 std::ofstream{peci0 / "temp0_input"};
48 std::ofstream{peci0 / "temp1_input"};
49 std::ofstream{peci0 / "temp2_input"};
50 std::ofstream{peci0 / "name"};
51 auto devDir = peciDir / "peci-0/peci_dev/peci-0";
52 fs::create_directories(devDir);
53 fs::create_directory_symlink("../../../peci-0", devDir / "device");
54 fs::create_directory_symlink("device/0-30", peciDir / "peci-0/0-30");
55
56 // Let's keep this for debugging purpose
57 for (auto p = fs::recursive_directory_iterator(
58 peciDir, fs::directory_options::follow_directory_symlink);
59 p != fs::recursive_directory_iterator(); ++p)
60 {
61 std::string path = p->path().string();
62 fprintf(stderr, "%s\n", path.c_str());
63 if (p.depth() >= 6)
64 {
65 p.disable_recursion_pending();
66 }
67 }
68 }
69};
70
71TEST_F(TestUtils, findFiles_non_exist)
72{
73 std::vector<fs::path> foundPaths;
74 auto ret = findFiles("non-exist", "", foundPaths);
75
76 EXPECT_FALSE(ret);
77 EXPECT_TRUE(foundPaths.empty());
78}
79
80TEST_F(TestUtils, findFiles_in_hwmon_no_match)
81{
82 std::vector<fs::path> foundPaths;
83 auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
84
85 EXPECT_TRUE(ret);
86 EXPECT_EQ(foundPaths.size(), 0u);
87}
88
89TEST_F(TestUtils, findFiles_in_hwmon_match)
90{
91 std::vector<fs::path> foundPaths;
92 auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
93
94 EXPECT_TRUE(ret);
95 EXPECT_EQ(foundPaths.size(), 2u);
96}
97
98TEST_F(TestUtils, findFiles_in_peci_no_match)
99{
100 std::vector<fs::path> foundPaths;
101 auto ret =
102 findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
103 foundPaths, 6);
104
105 EXPECT_TRUE(ret);
106 EXPECT_TRUE(foundPaths.empty());
107}
108
109TEST_F(TestUtils, findFiles_in_peci_match)
110{
111 std::vector<fs::path> foundPaths;
112 auto ret =
113 findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
114 foundPaths, 6);
115 EXPECT_TRUE(ret);
116 EXPECT_EQ(foundPaths.size(), 1u);
117
118 foundPaths.clear();
119
120 ret = findFiles(peciDir,
121 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
122 foundPaths, 6);
123 EXPECT_TRUE(ret);
124 EXPECT_EQ(foundPaths.size(), 3u);
125}
126
127TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
128{
129 std::string p = hwmonDir.string() + "/";
130 std::vector<fs::path> foundPaths;
131 auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
132
133 EXPECT_TRUE(ret);
134 EXPECT_EQ(foundPaths.size(), 2u);
135}
136
137TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
138{
139 std::string p = peciDir.string() + "/";
140 std::vector<fs::path> foundPaths;
141 auto ret =
142 findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
143 foundPaths, 6);
144
145 EXPECT_TRUE(ret);
146 EXPECT_EQ(foundPaths.size(), 3u);
147}
Lei YU0b207a62021-10-20 13:41:51 +0800148
149TEST_F(TestUtils, findFiles_in_sub_peci_match)
150{
151 std::vector<fs::path> foundPaths;
152 auto ret =
153 findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
154 foundPaths, 5);
155 EXPECT_TRUE(ret);
156 EXPECT_EQ(foundPaths.size(), 1u);
157
158 foundPaths.clear();
159
160 ret = findFiles(peciDir / "peci-0",
161 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
162 foundPaths, 5);
163 EXPECT_TRUE(ret);
164 EXPECT_EQ(foundPaths.size(), 3u);
165}