From d9e174b604f0a143c01bfd984a6ab91765f00655 Mon Sep 17 00:00:00 2001 From: Oren Leiman Date: Fri, 19 Jan 2024 11:32:44 -0800 Subject: [PATCH] transform/logging: Unit tests for log event JSON Includes test utils module for transform logging. Signed-off-by: Oren Leiman (cherry picked from commit 274bb6ac93d0409894c57d366f7c4adea8be984a) --- src/v/transform/logging/tests/CMakeLists.txt | 21 +++++ src/v/transform/logging/tests/model_test.cc | 94 ++++++++++++++++++++ src/v/transform/logging/tests/utils.cc | 30 +++++++ src/v/transform/logging/tests/utils.h | 21 +++++ 4 files changed, 166 insertions(+) create mode 100644 src/v/transform/logging/tests/CMakeLists.txt create mode 100644 src/v/transform/logging/tests/model_test.cc create mode 100644 src/v/transform/logging/tests/utils.cc create mode 100644 src/v/transform/logging/tests/utils.h diff --git a/src/v/transform/logging/tests/CMakeLists.txt b/src/v/transform/logging/tests/CMakeLists.txt new file mode 100644 index 000000000000..bf64eb38fca5 --- /dev/null +++ b/src/v/transform/logging/tests/CMakeLists.txt @@ -0,0 +1,21 @@ +v_cc_library( + NAME transform_logging_test_utils + HDRS + utils.h + SRCS + utils.cc + DEPS + v::utils +) + +rp_test( + UNIT_TEST + GTEST + BINARY_NAME transform_logging + SOURCES + model_test.cc + LIBRARIES + v::transform_logging + v::transform_logging_test_utils + v::gtest_main +) diff --git a/src/v/transform/logging/tests/model_test.cc b/src/v/transform/logging/tests/model_test.cc new file mode 100644 index 000000000000..8b46c89f67dc --- /dev/null +++ b/src/v/transform/logging/tests/model_test.cc @@ -0,0 +1,94 @@ +/* + * Copyright 2024 Redpanda Data, Inc. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.md + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0 + */ + +#include "json/document.h" +#include "transform/logging/event.h" +#include "transform/logging/tests/utils.h" + +#include + +namespace transform::logging { + +TEST(TransformLogEventTest, ValidateLogEventJson) { + using namespace std::chrono_literals; + + ss::sstring message = "Hello"; + auto now = transform::logging::event::clock_type::now(); + model::node_id node_id{0}; + ss::log_level level{ss::log_level::info}; + model::transform_name name{"dummy"}; + + transform::logging::event ev{node_id, now, level, message}; + iobuf b; + ev.to_json(model::transform_name_view{name()}, b); + + auto doc = testing::parse_json(std::move(b)); + + EXPECT_TRUE(doc.IsObject()); + + EXPECT_TRUE( + doc.HasMember("body") && doc["body"].IsString() + && doc["body"].GetString() == message); + + EXPECT_TRUE( + doc.HasMember("timeUnixNano") && doc["timeUnixNano"].IsUint64() + && doc["timeUnixNano"].GetUint64() == now.time_since_epoch() / 1ns); + + EXPECT_TRUE( + doc.HasMember("severityNumber") && doc["severityNumber"].IsUint() + && doc["severityNumber"].GetUint() + == transform::logging::log_level_to_severity(level)); + + EXPECT_TRUE( + doc.HasMember("attributes") && doc["attributes"].IsArray() + && doc["attributes"].GetArray().Size() == 2); + + const auto& attrs = doc["attributes"].GetArray(); + + for (const auto& attr : attrs) { + EXPECT_TRUE( + attr.IsObject() && attr.HasMember("key") && attr["key"].IsString() + && attr.HasMember("value") && attr["value"].IsObject()); + } + + EXPECT_TRUE( + attrs[0]["key"].GetString() == ss::sstring("transform_name") + && attrs[0]["value"]["stringValue"].IsString() + && attrs[0]["value"]["stringValue"].GetString() == name); + + EXPECT_TRUE( + attrs[1]["key"].GetString() == ss::sstring("node") + && attrs[1]["value"]["intValue"].IsInt() + && attrs[1]["value"]["intValue"].GetInt() == node_id); +} + +TEST(TransformLogEventTest, LogLevelToSeverity) { + for (auto lvl : + {ss::log_level::trace, + ss::log_level::debug, + ss::log_level::info, + ss::log_level::warn, + ss::log_level::error}) { + transform::logging::event ev{ + model::node_id{0}, + transform::logging::event::clock_type::now(), + lvl, + "bar"}; + iobuf b; + ev.to_json(model::transform_name_view{"foo"}, b); + auto doc = testing::parse_json(std::move(b)); + EXPECT_TRUE( + doc.HasMember("severityNumber") && doc["severityNumber"].IsUint() + && doc["severityNumber"].GetUint() + == transform::logging::log_level_to_severity(ev.level)); + } +} +} // namespace transform::logging diff --git a/src/v/transform/logging/tests/utils.cc b/src/v/transform/logging/tests/utils.cc new file mode 100644 index 000000000000..ea7d60f3c31f --- /dev/null +++ b/src/v/transform/logging/tests/utils.cc @@ -0,0 +1,30 @@ +/* + * Copyright 2024 Redpanda Data, Inc. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.md + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0 + */ + +#include "transform/logging/tests/utils.h" + +#include "bytes/streambuf.h" +#include "json/istreamwrapper.h" + +#include + +namespace transform::logging::testing { + +json::Document parse_json(iobuf resp) { + iobuf_istreambuf ibuf{resp}; + std::istream stream{&ibuf}; + json::Document doc; + json::IStreamWrapper wrapper(stream); + doc.ParseStream(wrapper); + return doc; +} + +} // namespace transform::logging::testing diff --git a/src/v/transform/logging/tests/utils.h b/src/v/transform/logging/tests/utils.h new file mode 100644 index 000000000000..b163955c2867 --- /dev/null +++ b/src/v/transform/logging/tests/utils.h @@ -0,0 +1,21 @@ +/* + * Copyright 2024 Redpanda Data, Inc. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.md + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0 + */ + +#pragma once + +#include "bytes/iobuf.h" +#include "json/document.h" + +namespace transform::logging::testing { + +json::Document parse_json(iobuf resp); + +} // namespace transform::logging::testing