-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobe_config_test.cc
306 lines (257 loc) · 9.56 KB
/
probe_config_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <utility>
#include <brillo/map_utils.h>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/files/scoped_temp_dir.h>
#include <base/json/json_reader.h>
#include <base/test/task_environment.h>
#include <base/test/test_future.h>
#include <base/strings/stringprintf.h>
#include <base/values.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "runtime_probe/component_category.h"
#include "runtime_probe/functions/sysfs.h"
#include "runtime_probe/probe_config.h"
#include "runtime_probe/utils/file_test_utils.h"
namespace runtime_probe {
namespace {
using ::testing::NiceMock;
constexpr char kConfigName[] = "probe_config.json";
constexpr char kConfigHash[] = "14127A36F3A2509343AF7F19387537F608B07EE1";
class MockComponentCategory : public ComponentCategory {
public:
MOCK_METHOD(void,
Eval,
(base::OnceCallback<void(base::Value::List)>),
(const, override));
};
class ProbeConfigTest : public BaseFileTest {
protected:
// Set a mocked category that would return |category_eval_result| on
// calling ComponentCategory::Eval for |probe_config|.
void SetProbeConfigCategory(ProbeConfig& probe_config,
const std::string& category_name,
const std::string& category_eval_result) {
auto eval_result = base::JSONReader::Read(category_eval_result);
auto category = std::make_unique<NiceMock<MockComponentCategory>>();
ON_CALL(*category, Eval)
.WillByDefault(
[&category_eval_result](
base::OnceCallback<void(base::Value::List)> callback) {
auto eval_result = base::JSONReader::Read(category_eval_result);
std::move(callback).Run(std::move(eval_result->GetList()));
});
probe_config.SetCategoryForTesting(category_name, std::move(category));
}
private:
base::test::SingleThreadTaskEnvironment task_environment_;
};
} // namespace
TEST_F(ProbeConfigTest, LoadConfig) {
const char* config_content = R"({
"sysfs_battery": {
"generic": {
"eval": {
"sysfs": {
"dir_path": "/sys/class/power_supply/BAT0",
"keys": ["model_name", "charge_full_design", "cycle_count"]
}
},
"keys": [],
"expect": {},
"information": {}
}
}
})";
auto dict_value = base::JSONReader::Read(config_content);
EXPECT_TRUE(dict_value.has_value());
auto probe_config = ProbeConfig::FromValue(*dict_value);
EXPECT_TRUE(probe_config);
EXPECT_THAT(brillo::GetMapKeys(probe_config->category_),
::testing::UnorderedElementsAre("sysfs_battery"));
const auto& category = probe_config->category_["sysfs_battery"];
EXPECT_EQ(category->category_name_, "sysfs_battery");
EXPECT_THAT(brillo::GetMapKeys(category->component_),
::testing::UnorderedElementsAre("generic"));
const auto& probe_statement = category->component_["generic"];
EXPECT_EQ(probe_statement->component_name_, "generic");
EXPECT_EQ(probe_statement->key_.size(), 0);
EXPECT_NE(probe_statement->expect_value_, std::nullopt);
EXPECT_EQ(probe_statement->information_->GetDict().size(), 0);
EXPECT_NE(probe_statement->probe_function_, nullptr);
const SysfsFunction* probe_function =
dynamic_cast<SysfsFunction*>(probe_statement->probe_function_.get());
EXPECT_NE(probe_function, nullptr);
}
TEST_F(ProbeConfigTest, FromFileWithRelativePath) {
const auto rel_file_path = GetTestDataPath().Append(kConfigName);
const auto abs_file_path = base::MakeAbsoluteFilePath(rel_file_path);
const auto probe_config = ProbeConfig::FromFile(rel_file_path);
EXPECT_TRUE(probe_config);
EXPECT_EQ(probe_config->path(), abs_file_path);
EXPECT_EQ(probe_config->checksum(), kConfigHash);
}
TEST_F(ProbeConfigTest, FromFileWithAbsolutePath) {
const auto rel_file_path = GetTestDataPath().Append(kConfigName);
const auto abs_file_path = base::MakeAbsoluteFilePath(rel_file_path);
const auto probe_config = ProbeConfig::FromFile(abs_file_path);
EXPECT_TRUE(probe_config);
EXPECT_EQ(probe_config->path(), abs_file_path);
EXPECT_EQ(probe_config->checksum(), kConfigHash);
}
TEST_F(ProbeConfigTest, FromFileWithMissingFile) {
const auto probe_config =
ProbeConfig::FromFile(base::FilePath{"missing_file.json"});
EXPECT_FALSE(probe_config);
}
TEST_F(ProbeConfigTest, FromFileWithInvalidFile) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const base::FilePath rel_path{"invalid_config.json"};
const char invalid_probe_config[] = "foo\nbar";
PCHECK(WriteFile(temp_dir.GetPath().Append(rel_path), invalid_probe_config));
const auto probe_config = ProbeConfig::FromFile(rel_path);
EXPECT_FALSE(probe_config);
}
TEST_F(ProbeConfigTest, FromFileWithSymbolicLink) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const auto rel_file_path = GetTestDataPath().Append(kConfigName);
const auto abs_file_path = base::MakeAbsoluteFilePath(rel_file_path);
const auto symlink_config_path = temp_dir.GetPath().Append("config.json");
PCHECK(base::CreateSymbolicLink(abs_file_path, symlink_config_path));
const auto probe_config = ProbeConfig::FromFile(symlink_config_path);
EXPECT_TRUE(probe_config);
EXPECT_EQ(probe_config->path(), abs_file_path);
EXPECT_EQ(probe_config->checksum(), kConfigHash);
}
TEST_F(ProbeConfigTest, Eval) {
auto dict_value = base::JSONReader::Read("{}");
auto probe_config = ProbeConfig::FromValue(*dict_value);
EXPECT_TRUE(probe_config);
const std::string eval_content_1 = R"([
{
"name": "component_1",
"values": {
"field_1": "value_1"
}
}
])";
const std::string eval_content_2 = R"([
{
"name": "component_2",
"values": {
"field_2": "value_2"
}
}
])";
SetProbeConfigCategory(*probe_config, "category_1", eval_content_1);
SetProbeConfigCategory(*probe_config, "category_2", eval_content_2);
auto ans = base::JSONReader::Read(base::StringPrintf(R"({
"category_1": %s,
"category_2": %s
})",
eval_content_1.c_str(),
eval_content_2.c_str()));
base::test::TestFuture<base::Value::Dict> future;
probe_config->Eval(future.GetCallback());
EXPECT_EQ(future.Get(), ans);
}
TEST_F(ProbeConfigTest, EvalWithDefinedCategory) {
auto dict_value = base::JSONReader::Read("{}");
auto probe_config = ProbeConfig::FromValue(*dict_value);
EXPECT_TRUE(probe_config);
const std::string eval_content_1 = R"([
{
"name": "component_1",
"values": {
"field_1": "value_1"
}
}
])";
const std::string eval_content_2 = R"([
{
"name": "component_2",
"values": {
"field_2": "value_2"
}
}
])";
SetProbeConfigCategory(*probe_config, "category_1", eval_content_1);
SetProbeConfigCategory(*probe_config, "category_2", eval_content_2);
std::vector<std::string> categories{"category_1"};
// The result should contain only given categories.
auto ans = base::JSONReader::Read(base::StringPrintf(R"({
"category_1": %s
})",
eval_content_1.c_str()));
base::test::TestFuture<base::Value::Dict> future;
probe_config->Eval(categories, future.GetCallback());
EXPECT_EQ(future.Get(), ans);
}
TEST_F(ProbeConfigTest, EvalWithUndefinedCategory) {
auto dict_value = base::JSONReader::Read("{}");
auto probe_config = ProbeConfig::FromValue(*dict_value);
EXPECT_TRUE(probe_config);
const std::string eval_content_1 = R"([
{
"name": "component_1",
"values": {
"field_1": "value_1"
}
}
])";
SetProbeConfigCategory(*probe_config, "category_1", eval_content_1);
std::vector<std::string> categories{"category_1", "undefined_category"};
// The result should not contain "undefined_category".
auto ans = base::JSONReader::Read(base::StringPrintf(R"({
"category_1": %s
})",
eval_content_1.c_str()));
base::test::TestFuture<base::Value::Dict> future;
probe_config->Eval(categories, future.GetCallback());
EXPECT_EQ(future.Get(), ans);
}
TEST_F(ProbeConfigTest, GetComponentCategory) {
auto dict_value = base::JSONReader::Read("{}");
auto probe_config = ProbeConfig::FromValue(*dict_value);
EXPECT_TRUE(probe_config);
const std::string eval_content_1 = R"([
{
"name": "component_1",
"values": {
"field_1": "value_1"
}
}
])";
SetProbeConfigCategory(*probe_config, "category_1", eval_content_1);
auto ans = base::JSONReader::Read(eval_content_1);
auto category = probe_config->GetComponentCategory("category_1");
EXPECT_NE(category, nullptr);
base::test::TestFuture<base::Value::List> future;
category->Eval(future.GetCallback());
EXPECT_EQ(future.Get(), ans);
}
TEST_F(ProbeConfigTest, GetComponentCategoryWithUndefinedCategory) {
auto dict_value = base::JSONReader::Read("{}");
auto probe_config = ProbeConfig::FromValue(*dict_value);
EXPECT_TRUE(probe_config);
const std::string eval_content_1 = R"([
{
"name": "component_1",
"values": {
"field_1": "value_1"
}
}
])";
SetProbeConfigCategory(*probe_config, "category_1", eval_content_1);
auto undefined_category =
probe_config->GetComponentCategory("undefined_category");
EXPECT_EQ(undefined_category, nullptr);
}
} // namespace runtime_probe