Jan_31_2024-Dev

This commit is contained in:
Ned Wright
2024-01-31 17:34:53 +00:00
parent 752a5785f0
commit 6d67818a94
376 changed files with 8101 additions and 7064 deletions

View File

@@ -121,6 +121,7 @@ RestServer::Impl::init()
struct sockaddr_in addr;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

View File

@@ -4,5 +4,5 @@ link_directories(${ng_module_osrc_zlib_path}/lib)
add_unit_test(
rest_server_ut
"rest_schema_ut.cc;rest_must_param_ut.cc;rest_config_ut.cc"
"singleton;rest;environment;-lz;shell_cmd;-lboost_filesystem;message;instance_awareness;messaging_buffer;-lz;debug_is;time_proxy;mainloop;agent_details;encryptor;event_is;metric;-lboost_context;-lboost_regex;-lboost_system;-lssl;-lcrypto;connkey"
"singleton;messaging;tenant_manager;rest;environment;-lz;shell_cmd;-lboost_filesystem;instance_awareness;-lz;debug_is;time_proxy;mainloop;agent_details;encryptor;event_is;metric;-lboost_context;-lboost_regex;-lboost_system;-lssl;-lcrypto;connkey"
)

71
core/rest/rest_ut/rest_config_ut.cc Executable file → Normal file
View File

@@ -10,11 +10,15 @@
#include "time_proxy.h"
#include "mainloop.h"
#include "rest_server.h"
#include "agent_details.h"
#include "mock/mock_messaging.h"
#include "tenant_manager.h"
using namespace std;
using namespace testing;
USE_DEBUG_FLAG(D_API);
USE_DEBUG_FLAG(D_MAINLOOP);
class RestConfigTest : public Test
{
@@ -46,6 +50,7 @@ public:
Singleton::Consume<Config::I_Config>::from(config)->loadConfiguration(ss);
Debug::setUnitTestFlag(D_API, Debug::DebugLevel::NOISE);
Debug::setUnitTestFlag(D_MAINLOOP, Debug::DebugLevel::NOISE);
Debug::setNewDefaultStdout(&capture_debug);
}
@@ -62,6 +67,9 @@ public:
::Environment env;
ConfigComponent config;
RestServer rest_server;
TenantManager tenant_manager;
AgentDetails agent_details;
NiceMock<MockMessaging> messaging;
};
TEST_F(RestConfigTest, alternative_port_used)
@@ -107,4 +115,67 @@ TEST_F(RestConfigTest, alternative_port_used)
EXPECT_THAT(capture_debug.str(), HasSubstr("REST server started: " + to_string(alternative_port.unpack())));
rest_server.fini();
close(file_descriptor);
}
class TestServer : public ServerRest
{
void doCall() override { g_num = num; }
C2S_PARAM(int, num);
public:
static int g_num;
};
int TestServer::g_num = 0;
TEST_F(RestConfigTest, basic_flow)
{
env.preload();
Singleton::Consume<I_Environment>::from(env)->registerValue<string>("Executable Name", "tmp_test_file");
config.preload();
config.init();
rest_server.init();
time_proxy.init();
mainloop_comp.init();
auto i_rest = Singleton::Consume<I_RestApi>::from(rest_server);
ASSERT_TRUE(i_rest->addRestCall<TestServer>(RestAction::ADD, "test"));
int file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
EXPECT_NE(file_descriptor, -1);
auto primary_port = getConfiguration<uint>("connection", "Nano service API Port Alternative");
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(primary_port.unpack());
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
int socket_enable = 1;
EXPECT_EQ(setsockopt(file_descriptor, SOL_SOCKET, SO_REUSEADDR, &socket_enable, sizeof(int)), 0);
EXPECT_CALL(messaging, sendSyncMessage(_, _, _, _, _))
.WillRepeatedly(Return(HTTPResponse(HTTPStatusCode::HTTP_OK, "")));
auto mainloop = Singleton::Consume<I_MainLoop>::from(mainloop_comp);
I_MainLoop::Routine stop_routine = [&] () {
EXPECT_EQ(connect(file_descriptor, (struct sockaddr*)&sa, sizeof(struct sockaddr)), 0);
string msg = "POST /add-test HTTP/1.1\r\nContent-Length: 10\r\n\r\n{\"num\": 5}";
EXPECT_EQ(write(file_descriptor, msg.data(), msg.size()), msg.size());
while(!TestServer::g_num) {
mainloop->yield(true);
}
mainloop->stopAll();
};
mainloop->addOneTimeRoutine(
I_MainLoop::RoutineType::RealTime,
stop_routine,
"RestConfigTest-alternative_port_used stop routine",
true
);
mainloop->run();
EXPECT_EQ(TestServer::g_num, 5);
}

0
core/rest/rest_ut/rest_must_param_ut.cc Executable file → Normal file
View File

View File

@@ -5,14 +5,15 @@
#include "singleton.h"
#include "mainloop.h"
#include "encryptor.h"
#include "proto_message_comp.h"
#include "i_messaging.h"
#include "messaging.h"
#include "time_proxy.h"
#include "environment.h"
#include "config.h"
#include "config_component.h"
#include "agent_details.h"
#include "messaging_buffer.h"
#include "instance_awareness.h"
#include "tenant_manager.h"
#include <sys/socket.h>
#include <netinet/in.h>
@@ -20,6 +21,7 @@
#include <sstream>
#include "customized_cereal_map.h"
#include "customized_cereal_multimap.h"
#include "mock/mock_agent_details.h"
using namespace std;
@@ -419,26 +421,53 @@ public:
TEST(RestSchema, server_schema)
{
AgentDetails agent_details;
Debug::setUnitTestFlag(D_MESSAGING, Debug::DebugLevel::TRACE);
::Environment env;
TimeProxyComponent time_proxy;
MainloopComponent mainloop_comp;
::Environment env;
Encryptor encryptor;
MessagingBuffer messaging_buffer;
InstanceAwareness instance_awareness;
ShellCmd cmd;
ProtoMessageComp message;
I_Messaging *i_message;
Messaging message;
RestServer server;
ConfigComponent config;
TenantManager tenant_manager;
testing::NiceMock<MockAgentDetails> mock_agent_details;
env.preload();
Singleton::Consume<I_Environment>::from(env)->registerValue<string>("Executable Name", "tmp_test_file");
config.preload();
config.init();
EXPECT_CALL(mock_agent_details, getAccessToken()).WillRepeatedly(testing::Return(string("accesstoken")));
EXPECT_CALL(mock_agent_details, getFogDomain()).WillRepeatedly(testing::Return(string("127.0.0.1")));
EXPECT_CALL(mock_agent_details, getFogPort()).WillRepeatedly(testing::Return(9777));
string config_json =
"{"
" \"agentSettings\": [\n"
" {\n"
" \"id\": \"123\",\n"
" \"key\": \"eventBuffer.maxSizeOnDiskInMB\",\n"
" \"value\": \"1\"\n"
" },\n"
" {\n"
" \"id\": \"123\",\n"
" \"key\": \"eventBuffer.baseFolder\",\n"
" \"value\": \"/test_data/\"\n"
" }]\n"
"}";
istringstream ss(config_json);
Singleton::Consume<Config::I_Config>::from(config)->loadConfiguration(ss);
setConfiguration(false, string("message"), string("HTTPS connection"));
setConfiguration(uint(9777), string("connection"), string("Nano service API Port Primary"));
setConfiguration(uint(9778), string("connection"), string("Nano service API Port Alternative"));
Singleton::Consume<I_Environment>::from(env)->registerValue<string>("Executable Name", "a/b/");
messaging_buffer.init();
message.init();
server.init();
cmd.init();
time_proxy.init();
@@ -467,14 +496,21 @@ TEST(RestSchema, server_schema)
true
);
auto i_message = Singleton::Consume<I_Messaging>::from(message);
i_message = Singleton::Consume<I_Messaging>::from(message);
I_MainLoop::Routine action = [&stop, i_message] () {
GetSchema schema;
Flags<MessageConnConfig> conn_flags;
conn_flags.setFlag(MessageConnConfig::ONE_TIME_CONN);
EXPECT_TRUE(
i_message->sendObject(schema, I_Messaging::Method::GET, "127.0.0.1", 9777, conn_flags, "/add-int")
Flags<MessageConnectionConfig> conn_flags;
conn_flags.setFlag(MessageConnectionConfig::ONE_TIME_CONN);
MessageMetadata message_metadata("127.0.0.1", 9777, conn_flags);
message_metadata.setConnectioFlag(MessageConnectionConfig::UNSECURE_CONN);
auto is_message_sent = i_message->sendSyncMessage(
HTTPMethod::GET,
"/add-int",
schema,
MessageCategory::GENERIC,
message_metadata
);
EXPECT_TRUE(is_message_sent.ok());
vector<string> expected_req = { "must_int" };
EXPECT_EQ(schema.required.get(), expected_req);
ProperitiesSchema properties(schema.properties.get());
@@ -494,7 +530,6 @@ TEST(RestSchema, server_schema)
TEST(RestSchema, short_connection_server)
{
TimeProxyComponent time_proxy;
ProtoMessageComp message;
AgentDetails agent_details;
MainloopComponent mainloop_comp;
::Environment env;