martinzink commented on code in PR #1152: URL: https://github.com/apache/nifi-minifi-cpp/pull/1152#discussion_r843099859
########## extensions/procfs/tests/ProcFsMonitorTests.cpp: ########## @@ -0,0 +1,157 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "SingleProcessorTestController.h" +#include "Catch.h" +#include "processors/ProcFsMonitor.h" + +using org::apache::nifi::minifi::extensions::procfs::ProcFsMonitor; + +TEST_CASE("ProcFsMonitorTests", "[procfsmonitortests]") { + std::shared_ptr<ProcFsMonitor> proc_fs_monitor = std::make_shared<ProcFsMonitor>("ProcFsMonitor"); + org::apache::nifi::minifi::test::SingleProcessorTestController test_controller_{proc_fs_monitor}; + + SECTION("Absolute JSON") { + test_controller_.plan->setProperty(proc_fs_monitor, ProcFsMonitor::ResultRelativenessProperty.getName(), "Absolute"); + test_controller_.plan->setProperty(proc_fs_monitor, ProcFsMonitor::OutputFormatProperty.getName(), "JSON"); + const auto& result = test_controller_.trigger(); + + REQUIRE(result.at(ProcFsMonitor::Success).size() == 1); + const auto& result_flow_file = result.at(ProcFsMonitor::Success)[0]; + + rapidjson::Document document; + auto content = test_controller_.plan->getContent(result_flow_file); + document.Parse(content.c_str()); + REQUIRE(document.IsObject()); + REQUIRE(document.HasMember("CPU")); + CHECK(document["CPU"].HasMember("cpu")); + CHECK(document.HasMember("Disk")); + CHECK(document.HasMember("Network")); + CHECK(document.HasMember("Process")); + CHECK(document.HasMember("Memory")); + } + + SECTION("Absolute OpenTelemetry") { + test_controller_.plan->setProperty(proc_fs_monitor, ProcFsMonitor::ResultRelativenessProperty.getName(), "Absolute"); + test_controller_.plan->setProperty(proc_fs_monitor, ProcFsMonitor::OutputFormatProperty.getName(), "OpenTelemetry"); + const auto& result = test_controller_.trigger(); + + REQUIRE(result.at(ProcFsMonitor::Success).size() == 1); + const auto& result_flow_file = result.at(ProcFsMonitor::Success)[0]; + + rapidjson::Document document; + auto content = test_controller_.plan->getContent(result_flow_file); + document.Parse(content.c_str()); + REQUIRE(document.IsObject()); + REQUIRE(document.HasMember("Body")); + REQUIRE(document["Body"].HasMember("CPU")); + CHECK(document["Body"]["CPU"].HasMember("cpu")); + CHECK(document["Body"].HasMember("Disk")); + CHECK(document["Body"].HasMember("Network")); + CHECK(document["Body"].HasMember("Process")); + CHECK(document["Body"].HasMember("Memory")); + } + + SECTION("Relative JSON") { + test_controller_.plan->setProperty(proc_fs_monitor, ProcFsMonitor::ResultRelativenessProperty.getName(), "Relative"); + test_controller_.plan->setProperty(proc_fs_monitor, ProcFsMonitor::OutputFormatProperty.getName(), "JSON"); + { + const auto& result = test_controller_.trigger(); + + REQUIRE(result.at(ProcFsMonitor::Success).size() == 1); + const auto& result_flow_file = result.at(ProcFsMonitor::Success)[0]; + + rapidjson::Document document; + auto content = test_controller_.plan->getContent(result_flow_file); + document.Parse(content.c_str()); + REQUIRE(document.IsObject()); + // First trigger has not enough information for relative output + CHECK_FALSE(document.HasMember("CPU")); + CHECK_FALSE(document.HasMember("Disk")); + CHECK_FALSE(document.HasMember("Network")); + CHECK_FALSE(document.HasMember("Process")); + CHECK(document.HasMember("Memory")); + } + sleep(1); + { + const auto& result = test_controller_.trigger(); + + REQUIRE(result.at(ProcFsMonitor::Success).size() == 1); + const auto& result_flow_file = result.at(ProcFsMonitor::Success)[0]; + + rapidjson::Document document; + auto content = test_controller_.plan->getContent(result_flow_file); + document.Parse(content.c_str()); + REQUIRE(document.IsObject()); + CHECK(document.HasMember("CPU")); + CHECK(document.HasMember("Disk")); + CHECK(document.HasMember("Network")); + CHECK(document.HasMember("Process")); + CHECK(document.HasMember("Memory")); + } + } + + SECTION("Relative OpenTelemetry") { + test_controller_.plan->setProperty(proc_fs_monitor, ProcFsMonitor::ResultRelativenessProperty.getName(), "Relative"); + test_controller_.plan->setProperty(proc_fs_monitor, ProcFsMonitor::OutputFormatProperty.getName(), "OpenTelemetry"); + { + const auto& result = test_controller_.trigger(); + + REQUIRE(result.at(ProcFsMonitor::Success).size() == 1); + const auto& result_flow_file = result.at(ProcFsMonitor::Success)[0]; + + rapidjson::Document document; + auto content = test_controller_.plan->getContent(result_flow_file); + document.Parse(content.c_str()); + REQUIRE(document.IsObject()); + REQUIRE(document.HasMember("Body")); + // First trigger has not enough information for relative output + CHECK_FALSE(document["Body"].HasMember("CPU")); + CHECK_FALSE(document["Body"].HasMember("Disk")); + CHECK_FALSE(document["Body"].HasMember("Network")); + CHECK_FALSE(document["Body"].HasMember("Process")); + CHECK(document["Body"].HasMember("Memory")); + } + sleep(1); + { + const auto& result = test_controller_.trigger(); + + REQUIRE(result.at(ProcFsMonitor::Success).size() == 1); + const auto& result_flow_file = result.at(ProcFsMonitor::Success)[0]; + + rapidjson::Document document; + auto content = test_controller_.plan->getContent(result_flow_file); + document.Parse(content.c_str()); + REQUIRE(document.IsObject()); + REQUIRE(document.HasMember("Body")); + CHECK(document["Body"].HasMember("CPU")); + CHECK(document["Body"].HasMember("Disk")); + CHECK(document["Body"].HasMember("Network")); + CHECK(document["Body"].HasMember("Process")); + CHECK(document["Body"].HasMember("Memory")); + } + } + + SECTION("Relative without wait") { + test_controller_.plan->setProperty(proc_fs_monitor, ProcFsMonitor::ResultRelativenessProperty.getName(), "Relative"); + const auto& result1 = test_controller_.trigger(); + REQUIRE(result1.at(ProcFsMonitor::Success).size() == 1); + const auto& result2 = test_controller_.trigger(); + REQUIRE(result2.at(ProcFsMonitor::Success).size() == 1); + } Review Comment: On the contrary. I made this test so it makes sure we dont divide by zero. It turns out I removed a check during the last refactor. I've fixed the issue https://github.com/apache/nifi-minifi-cpp/pull/1152/commits/6248890ce7c068ee8fa4eab52598b18a933b388c, I think we can keep this. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
