| // Copyright 2021 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include <fuchsia/diagnostics/cpp/fidl.h> | |
| #include <fuchsia/logger/cpp/fidl.h> | |
| #include <lib/sys/cpp/service_directory.h> | |
| #include <lib/syslog/cpp/log_settings.h> | |
| #include <lib/syslog/cpp/macros.h> | |
| #include <algorithm> | |
| #include <iterator> | |
| #include <gtest/gtest.h> | |
| #include <src/lib/diagnostics/fidl_log_message_parser/log_message.h> | |
| const char* TAG = "integration_test"; | |
| void WriteLogs() { | |
| fuchsia_logging::LogSettingsBuilder builder; | |
| builder.WithTags({TAG}).BuildAndInitialize(); | |
| // WARNING: Test is sensitive to line numbers and file name. | |
| // These log lines are added to the top of the file to prevent changes to the test below from | |
| // changing the expected outputs. | |
| FX_LOGS(INFO) << "Hello info"; // Line 25 | |
| FX_LOGS(WARNING) << "Hello warning"; // Line 26 | |
| FX_LOGS(ERROR) << "Hello error"; // Line 27 | |
| } | |
| std::vector<fuchsia::logger::LogMessage> GetLogs() { | |
| // Synchronously get the logs from ArchiveAccessor. | |
| fuchsia::diagnostics::ArchiveAccessorSyncPtr accessor; | |
| auto services = sys::ServiceDirectory::CreateFromNamespace(); | |
| services->Connect(accessor.NewRequest()); | |
| fuchsia::diagnostics::BatchIteratorSyncPtr iterator; | |
| auto params = fuchsia::diagnostics::StreamParameters(); | |
| params.set_stream_mode(fuchsia::diagnostics::StreamMode::SNAPSHOT); | |
| params.set_data_type(fuchsia::diagnostics::DataType::LOGS); | |
| params.set_format(fuchsia::diagnostics::Format::FXT); | |
| params.set_client_selector_configuration( | |
| fuchsia::diagnostics::ClientSelectorConfiguration::WithSelectAll(true)); | |
| EXPECT_EQ(ZX_OK, accessor->StreamDiagnostics(std::move(params), iterator.NewRequest())); | |
| fuchsia::diagnostics::BatchIterator_GetNext_Result result; | |
| std::vector<fuchsia::logger::LogMessage> ret; | |
| EXPECT_EQ(ZX_OK, iterator->GetNext(&result)); | |
| for (auto& content : result.response().batch) { | |
| auto result = diagnostics::fidl_log_message_parser::ConvertFormattedContentToLogMessages( | |
| std::move(content)); | |
| if (result.is_error()) { | |
| EXPECT_TRUE(false) << "Received an error: " << result.error(); | |
| return {}; | |
| } | |
| for (auto& value : result.take_value()) { | |
| if (value.is_error()) { | |
| EXPECT_TRUE(false) << "Value had an error: " << value.error(); | |
| return {}; | |
| } | |
| ret.emplace_back(value.take_value()); | |
| } | |
| } | |
| return ret; | |
| } | |
| const size_t EXPECTED_LOGS = 3; | |
| TEST(fidl_log_message_parser, ConversionWorks) { | |
| WriteLogs(); | |
| auto logs = GetLogs(); | |
| while (logs.size() != EXPECTED_LOGS) { | |
| // Repeat with 1s wait if logs do not appear yet. | |
| // This can happen when the test Archivist has not yet finished reading the logs off of the | |
| // wire. | |
| sleep(1); | |
| printf("Retrying reading logs\n"); | |
| logs = GetLogs(); | |
| } | |
| ASSERT_EQ(logs.size(), EXPECTED_LOGS); | |
| EXPECT_GT(logs[0].time.get(), 0u); | |
| EXPECT_GT(logs[0].pid, 0u); | |
| EXPECT_GT(logs[0].tid, 0u); | |
| EXPECT_EQ(logs[0].msg, "[test.cc(25)] Hello info"); | |
| ASSERT_EQ(logs[0].tags.size(), 1u); | |
| EXPECT_EQ(logs[0].tags[0], TAG); | |
| EXPECT_EQ(logs[0].severity, static_cast<int8_t>(fuchsia::logger::LogLevelFilter::INFO)); | |
| EXPECT_GT(logs[1].time.get(), 0u); | |
| EXPECT_GT(logs[1].pid, 0u); | |
| EXPECT_GT(logs[1].tid, 0u); | |
| EXPECT_EQ(logs[1].msg, | |
| "[src/lib/diagnostics/fidl_log_message_parser/integration/test.cc(26)] Hello warning"); | |
| ASSERT_EQ(logs[1].tags.size(), 1u); | |
| EXPECT_EQ(logs[1].tags[0], TAG); | |
| EXPECT_EQ(logs[1].severity, static_cast<int8_t>(fuchsia::logger::LogLevelFilter::WARN)); | |
| EXPECT_GT(logs[2].time.get(), 0u); | |
| EXPECT_GT(logs[2].pid, 0u); | |
| EXPECT_GT(logs[2].tid, 0u); | |
| EXPECT_EQ(logs[2].msg, | |
| "[src/lib/diagnostics/fidl_log_message_parser/integration/test.cc(27)] Hello error"); | |
| ASSERT_EQ(logs[2].tags.size(), 1u); | |
| EXPECT_EQ(logs[2].tags[0], TAG); | |
| EXPECT_EQ(logs[2].severity, static_cast<int8_t>(fuchsia::logger::LogLevelFilter::ERROR)); | |
| } |