16 #include <gtest/gtest.h> 17 #include <gmock/gmock.h> 24 #include <rosgraph_msgs/Log.h> 32 using ::testing::AllOf;
33 using ::testing::HasSubstr;
34 using ::testing::Return;
35 using ::testing::StrEq;
37 using ::testing::InSequence;
42 MOCK_METHOD5(CreateLogService,
43 std::shared_ptr<LogService>(
44 const std::string & log_group,
45 const std::string & log_stream,
46 const Aws::Client::ClientConfiguration & client_config,
47 const Aws::SDKOptions & sdk_options,
57 MOCK_METHOD0(publishBatchedData,
bool());
64 const std::string & log_stream,
65 const Aws::Client::ClientConfiguration & client_config)
75 :
LogService(
std::move(log_publisher),
std::move(log_batcher),
std::move(log_file_upload_streamer)) {}
77 MOCK_METHOD1(batchData,
bool(
const std::string & data_to_batch));
78 MOCK_METHOD0(
start,
bool());
80 MOCK_METHOD0(publishBatchedData,
bool());
97 log_message_.name =
"NjhkYjRkZjQ3N2Qw";
98 log_message_.msg =
"ZjQxOGE2MWM5MTFkMWNjMDVkMGY2OTZm";
99 log_message_.level = rosgraph_msgs::Log::DEBUG;
100 Aws::Client::ClientConfiguration config;
102 log_publisher = std::make_shared<LogPublisherMock>(
103 std::string(
"test_group"), std::string(
"test_stream"), config);
104 log_batcher = std::make_shared<LogBatcherMock>();
105 log_service = std::make_shared<LogServiceMock>(log_publisher, log_batcher);
106 log_service_factory = std::make_shared<LogServiceFactoryMock>();
110 int8_t severity_level = rosgraph_msgs::Log::DEBUG,
111 bool publish_topic_names =
true,
112 const std::unordered_set<std::string> & ignore_nodes = std::unordered_set<std::string>())
119 return std::make_shared<LogNode>(logger_options);
124 return boost::make_shared<rosgraph_msgs::Log const>(log_message);
131 std::string log_group =
"YjFjMTM5YTEyNzliMjdlNjBlZGY1ZjQy";
132 std::string log_stream =
"hZDExYmNmMGJkMjMw";
133 Aws::Client::ClientConfiguration config;
134 Aws::SDKOptions sdk_options;
137 EXPECT_CALL(*log_service_factory,
138 CreateLogService(StrEq(log_group), StrEq(log_stream), Eq(config), _, _))
139 .WillOnce(Return(log_service));
141 log_node->Initialize(log_group, log_stream, config, sdk_options, cloudwatch_options, log_service_factory);
143 EXPECT_CALL(*log_service,
start()).Times(1);
152 EXPECT_CALL(*log_service,
shutdown()).Times(1);
153 log_node->shutdown();
160 std::shared_ptr<LogNode> test_subject = build_test_subject();
161 initialize_log_node(test_subject);
166 std::shared_ptr<LogNode> test_subject = build_test_subject();
168 EXPECT_CALL(*log_service, batchData(_)).Times(0);
169 EXPECT_CALL(*log_service,
start()).Times(0);
171 test_subject->RecordLogs(message_to_constptr(log_message_));
176 std::shared_ptr<LogNode> test_subject = build_test_subject();
178 EXPECT_CALL(*log_service, batchData(_)).Times(1);
180 initialize_log_node(test_subject);
181 test_subject->RecordLogs(message_to_constptr(log_message_));
186 std::shared_ptr<LogNode> test_subject = build_test_subject(rosgraph_msgs::Log::ERROR);
188 initialize_log_node(test_subject);
190 ON_CALL(*log_service, batchData(_))
191 .WillByDefault(Return(
true));
192 EXPECT_CALL(*log_service, batchData(_))
195 log_message_.level = rosgraph_msgs::Log::DEBUG;
196 test_subject->RecordLogs(message_to_constptr(log_message_));
197 log_message_.level = rosgraph_msgs::Log::INFO;
198 test_subject->RecordLogs(message_to_constptr(log_message_));
199 log_message_.level = rosgraph_msgs::Log::WARN;
200 test_subject->RecordLogs(message_to_constptr(log_message_));
205 std::shared_ptr<LogNode> test_subject = build_test_subject(rosgraph_msgs::Log::DEBUG,
false);
206 initialize_log_node(test_subject);
208 const std::string log_name_reference_str = std::string(
"[node name: ") + log_message_.name +
"]";
209 const std::string log_topics_reference_str =
"[topics: ]";
211 EXPECT_CALL(*log_service,
214 HasSubstr(log_message_.msg),
215 HasSubstr(log_name_reference_str),
216 Not(HasSubstr(log_topics_reference_str))
218 .WillOnce(Return(
true));
220 test_subject->RecordLogs(message_to_constptr(log_message_));
225 std::shared_ptr<LogNode> test_subject = build_test_subject(rosgraph_msgs::Log::ERROR);
227 initialize_log_node(test_subject);
229 ON_CALL(*log_service, batchData(_))
230 .WillByDefault(Return(
true));
231 EXPECT_CALL(*log_service, batchData(_))
234 log_message_.level = rosgraph_msgs::Log::ERROR;
235 test_subject->RecordLogs(message_to_constptr(log_message_));
236 log_message_.level = rosgraph_msgs::Log::FATAL;
237 test_subject->RecordLogs(message_to_constptr(log_message_));
239 ON_CALL(*log_service, publishBatchedData())
240 .WillByDefault(Return(
true));
241 EXPECT_CALL(*log_service, publishBatchedData())
246 test_subject->TriggerLogPublisher(timer_event);
251 std::shared_ptr<LogNode> test_subject = build_test_subject();
253 initialize_log_node(test_subject);
255 const char* node_name2 =
"zNzQwNjU4NWRi";
256 std::ostringstream log_name_reference_stream2;
257 log_name_reference_stream2 <<
"[node name: " << node_name2 <<
"]";
259 const char* topic1 =
"ZjlkYmUzOTI5ODA0ZT";
260 const char* topic2 =
"jNjIxMWRm";
261 std::ostringstream log_topics_reference_stream2;
262 log_topics_reference_stream2 <<
"[topics: " << topic1 <<
", " << topic2 <<
"] ";
265 InSequence record_log_seq;
267 std::ostringstream log_name_reference_stream1;
268 log_name_reference_stream1 <<
"[node name: " << log_message_.name <<
"]";
269 std::ostringstream log_topics_reference_stream1;
270 log_topics_reference_stream1 <<
"[topics: ]";
272 EXPECT_CALL(*log_service,
274 HasSubstr(
"DEBUG"), HasSubstr(log_message_.msg),
275 HasSubstr(log_name_reference_stream1.str()), HasSubstr(log_topics_reference_stream1.str())
277 .WillOnce(Return(
true));
279 EXPECT_CALL(*log_service,
281 HasSubstr(
"INFO"), HasSubstr(log_message_.msg),
282 HasSubstr(log_name_reference_stream2.str()), HasSubstr(log_topics_reference_stream1.str())
284 .WillOnce(Return(
true));
286 EXPECT_CALL(*log_service,
288 HasSubstr(
"WARN"), HasSubstr(log_message_.msg),
289 HasSubstr(log_name_reference_stream1.str()), HasSubstr(log_topics_reference_stream2.str())
291 .WillOnce(Return(
true));
294 log_message_.level = rosgraph_msgs::Log::DEBUG;
295 test_subject->RecordLogs(message_to_constptr(log_message_));
297 rosgraph_msgs::Log log_message2 = log_message_;
298 log_message2.level = rosgraph_msgs::Log::INFO;
299 log_message2.name = node_name2;
300 test_subject->RecordLogs(message_to_constptr(log_message2));
302 rosgraph_msgs::Log log_message3 = log_message_;
303 log_message3.topics.push_back(topic1);
304 log_message3.topics.push_back(topic2);
305 log_message3.level = rosgraph_msgs::Log::WARN;
306 test_subject->RecordLogs(message_to_constptr(log_message3));
311 std::unordered_set<std::string> ignore_nodes;
312 ignore_nodes.emplace(log_message_.name);
313 std::shared_ptr<LogNode> test_subject = build_test_subject(rosgraph_msgs::Log::DEBUG,
true, ignore_nodes);
315 initialize_log_node(test_subject);
317 ON_CALL(*log_service, batchData(_))
318 .WillByDefault(Return(
true));
319 EXPECT_CALL(*log_service, batchData(_))
322 log_message_.level = rosgraph_msgs::Log::INFO;
323 test_subject->RecordLogs(message_to_constptr(log_message_));
330 int main(
int argc,
char ** argv)
332 testing::InitGoogleTest(&argc, argv);
333 return RUN_ALL_TESTS();
std::shared_ptr< LogNode > log_node
LogPublisherMock(const std::string &log_group, const std::string &log_stream, const Aws::Client::ClientConfiguration &client_config)
std::shared_ptr< LogServiceMock > log_service
std::shared_ptr< LogPublisherMock > log_publisher
std::shared_ptr< LogServiceFactoryMock > log_service_factory
void initialize_log_node(std::shared_ptr< LogNode > &ln)
rosgraph_msgs::Log::ConstPtr message_to_constptr(rosgraph_msgs::Log log_message)
LogServiceMock(std::shared_ptr< Publisher< LogCollection >> log_publisher, std::shared_ptr< DataBatcher< LogType >> log_batcher, std::shared_ptr< FileUploadStreamer< LogCollection >> log_file_upload_streamer=nullptr)
std::shared_ptr< LogBatcherMock > log_batcher
std::shared_ptr< LogNode > build_test_subject(int8_t severity_level=rosgraph_msgs::Log::DEBUG, bool publish_topic_names=true, const std::unordered_set< std::string > &ignore_nodes=std::unordered_set< std::string >())
ROSCONSOLE_DECL void shutdown()
rosgraph_msgs::Log log_message_
int main(int argc, char **argv)
TEST_F(LogNodeFixture, TestInitialize)