sync code

This commit is contained in:
Ned Wright
2024-09-17 10:53:09 +00:00
parent 3fe0b42fcd
commit 586150fe4f
143 changed files with 1886 additions and 380 deletions

View File

@@ -0,0 +1,2 @@
add_library(service_health_status service_health_status.cc)
add_subdirectory(service_health_status_ut)

View File

@@ -0,0 +1,104 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (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 "service_health_status.h"
#include <fstream>
#include <string>
#include "debug.h"
#include "rest.h"
#include "customized_cereal_map.h"
#include "service_health_update_event.h"
using namespace std;
USE_DEBUG_FLAG(D_SERVICE_HEALTH_STATUS);
class I_ServiceHealthStatusImpl
{
public:
virtual const map<string, string> & getErrors() const = 0;
protected:
virtual ~I_ServiceHealthStatusImpl() {}
};
class ServiceHealthStatus::Impl
:
public Singleton::Provide<I_ServiceHealthStatusImpl>::SelfInterface,
public Listener<ServiceHealthUpdateEvent>
{
public:
void init();
const map<string, string> & getErrors() const override { return errors_map; }
void upon(const ServiceHealthUpdateEvent &event) override;
private:
map<string, string> errors_map;
};
class ServiceHealthStatusRest
:
public ServerRest,
Singleton::Consume<I_ServiceHealthStatusImpl>
{
using ErrorsMap = map<string, string>;
public:
void
doCall()
{
errors = Singleton::Consume<I_ServiceHealthStatusImpl>::by<ServiceHealthStatusRest>()->getErrors();
healthy = errors.get().empty();
dbgTrace(D_SERVICE_HEALTH_STATUS)
<< "Heath status requested. "
<< (healthy ? "Service is healthy." : "Service is not healthy.");
}
private:
S2C_PARAM(bool, healthy);
S2C_PARAM(ErrorsMap, errors);
};
void
ServiceHealthStatus::Impl::init()
{
if (!Singleton::exists<I_RestApi>()) return;
Singleton::Consume<I_RestApi>::by<ServiceHealthStatus>()->addRestCall<ServiceHealthStatusRest>(
RestAction::SHOW,
"health"
);
registerListener();
}
void
ServiceHealthStatus::Impl::upon(const ServiceHealthUpdateEvent &event)
{
dbgTrace(D_SERVICE_HEALTH_STATUS)
<< "Service health update event. Error: "
<< event.getComponent()
<< " - "
<< event.getError();
if (event.isHealthyUpdate()) {
errors_map.clear();
} else {
errors_map[event.getComponent()] = event.getError();
}
}
ServiceHealthStatus::ServiceHealthStatus() : Component("ServiceHealthStatus"), pimpl(make_unique<Impl>()) {}
ServiceHealthStatus::~ServiceHealthStatus() {}
void ServiceHealthStatus::init() { pimpl->init(); }

View File

@@ -0,0 +1,8 @@
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${BOOST_ROOT}/lib)
add_unit_test(
service_health_status_ut
"service_health_status_ut.cc"
"service_health_status;rest;event_is;connkey;mainloop;encryptor;messaging;-lz;metric;-lboost_regex;-lboost_context;-lcrypto;"
)

View File

@@ -0,0 +1,73 @@
#include "service_health_status.h"
#include "cptest.h"
#include "environment.h"
#include "config.h"
#include "config_component.h"
#include "debug.h"
#include "connkey.h"
#include "rest.h"
#include "rest_server.h"
#include "mock/mock_rest_api.h"
#include "service_health_update_event.h"
using namespace std;
using namespace testing;
USE_DEBUG_FLAG(D_GEO_DB);
class HealthCheckStatusTest : public Test
{
public:
HealthCheckStatusTest()
{
EXPECT_CALL(mock_rest, mockRestCall(RestAction::SHOW, "health", _))
.WillOnce(WithArg<2>(Invoke(this, &HealthCheckStatusTest::showHealthCheckStatus)));
health_check_status.init();
}
bool
showHealthCheckStatus(const unique_ptr<RestInit> &p)
{
show_health_check_status = p->getRest();
return true;
}
::Environment env;
ConfigComponent config;
ServiceHealthStatus health_check_status;
NiceMock<MockRestApi> mock_rest;
unique_ptr<ServerRest> show_health_check_status;
};
TEST_F(HealthCheckStatusTest, testHealthCheckStatus)
{
ServiceHealthUpdateEvent().notify();
stringstream ss("{}");
Maybe<string> maybe_res = show_health_check_status->performRestCall(ss);
EXPECT_TRUE(maybe_res.ok());
EXPECT_EQ(maybe_res.unpack(),
"{\n"
" \"healthy\": true,\n"
" \"errors\": {}\n"
"}"
);
}
TEST_F(HealthCheckStatusTest, testNotHealthyService)
{
ServiceHealthUpdateEvent("test", "test description").notify();
stringstream ss("{}");
Maybe<string> maybe_res = show_health_check_status->performRestCall(ss);
EXPECT_TRUE(maybe_res.ok());
EXPECT_EQ(maybe_res.unpack(),
"{\n"
" \"healthy\": false,\n"
" \"errors\": {\n"
" \"test\": \"test description\"\n"
" }\n"
"}"
);
}