Skip to content

Commit

Permalink
feat: additional test and logcollector arch separation
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioDonda committed Jan 15, 2025
1 parent fe319e5 commit 814c8d0
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 59 deletions.
12 changes: 6 additions & 6 deletions src/modules/logcollector/include/logcollector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Logcollector {
/// @param log Message to send
/// @param collectorType type of logcollector
/// @pre The message queue must be set with SetMessageQueue
void SendMessage(const std::string& location, const std::string& log, const std::string& collectorType);
virtual void SendMessage(const std::string& location, const std::string& log, const std::string& collectorType);

/// @brief Enqueues an ASIO task (coroutine)
/// @param task Task to enqueue
Expand All @@ -71,6 +71,11 @@ class Logcollector {
return s_instance;
}

/// @brief Add platform specific implementation of IReader to logcollector.
/// @param ConfgurationParser where to get parameters.
/// @param logcollector instance.
void AddPlatformSpecificReader(std::shared_ptr<const configuration::ConfigurationParser> configurationParser);

protected:
/// @brief Constructor
Logcollector() { }
Expand Down Expand Up @@ -111,9 +116,4 @@ class Logcollector {
std::list<boost::asio::steady_timer*> m_timers;
};

/// @brief Add platform specific implementation of IReader to logcollector.
/// @param ConfgurationParser where to get parameters.
/// @param logcollector instance.
void AddPlatformSpecificReader(std::shared_ptr<const configuration::ConfigurationParser> configurationParser, Logcollector &logcollector);

}
15 changes: 0 additions & 15 deletions src/modules/logcollector/src/another_reader_unix.cpp

This file was deleted.

15 changes: 0 additions & 15 deletions src/modules/logcollector/src/event_reader_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@
namespace logcollector
{

void AddPlatformSpecificReader(std::shared_ptr<const configuration::ConfigurationParser> configurationParser, Logcollector &logcollector)
{
const auto refreshInterval = configurationParser->GetConfig<time_t>("logcollector", "channel_refresh").value_or(config::logcollector::CHANNEL_REFRESH_INTERVAL);

const auto windowsConfig = configurationParser->GetConfig<std::vector<std::map<std::string, std::string>>>("logcollector", "windows").value_or(
std::vector<std::map<std::string, std::string>> {});

for (auto& entry : windowsConfig)
{
const auto channel = entry.at("channel");
const auto query = entry.at("query");
logcollector.AddReader(std::make_shared<WindowsEventTracerReader>(logcollector, channel, query, refreshInterval));
}
}

WindowsEventTracerReader::WindowsEventTracerReader(Logcollector &logcollector,
const std::string channel,
const std::string query,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/logcollector/src/logcollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void Logcollector::Setup(std::shared_ptr<const configuration::ConfigurationParse

SetupFileReader(configurationParser);

AddPlatformSpecificReader(configurationParser, *this);
AddPlatformSpecificReader(configurationParser);
}

void Logcollector::SetupFileReader(const std::shared_ptr<const configuration::ConfigurationParser> configurationParser)
Expand Down
17 changes: 17 additions & 0 deletions src/modules/logcollector/src/logcollector_unix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <config.h>
#include <timeHelper.h>

#include <chrono>
#include <map>

#include <logcollector.hpp>

namespace logcollector
{

void Logcollector::AddPlatformSpecificReader([[maybe_unused]] std::shared_ptr<const configuration::ConfigurationParser> configurationParser)
{

}

}
29 changes: 29 additions & 0 deletions src/modules/logcollector/src/logcollector_win.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "event_reader_win.hpp"

#include <config.h>
#include <timeHelper.h>

#include <chrono>
#include <map>

#include <logcollector.hpp>

namespace logcollector
{

void Logcollector::AddPlatformSpecificReader(std::shared_ptr<const configuration::ConfigurationParser> configurationParser)
{
const auto refreshInterval = configurationParser->GetConfig<time_t>("logcollector", "channel_refresh").value_or(config::logcollector::CHANNEL_REFRESH_INTERVAL);

const auto windowsConfig = configurationParser->GetConfig<std::vector<std::map<std::string, std::string>>>("logcollector", "windows").value_or(
std::vector<std::map<std::string, std::string>> {});

for (auto& entry : windowsConfig)
{
const auto channel = entry.at("channel");
const auto query = entry.at("query");
AddReader(std::make_shared<WindowsEventTracerReader>(*this, channel, query, refreshInterval));
}
}

}
23 changes: 23 additions & 0 deletions src/modules/logcollector/tests/unit/event_reader_mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,26 @@ class MockWinAPIWrapper : public IWinAPIWrapper

};

//Has some duplicated methods from logcollector_mock
class LogcollectorMock : public Logcollector {
public:
LogcollectorMock() {
ON_CALL(*this, AddReader(::testing::_))
.WillByDefault(::testing::Invoke([this](std::shared_ptr<IReader> reader) {
this->Logcollector::AddReader(reader);
})
);
}

MOCK_METHOD(void, AddReader, (std::shared_ptr<IReader> reader), (override));
MOCK_METHOD(void, EnqueueTask, (Awaitable task), (override));
MOCK_METHOD(void, SendMessage, (const std::string& channel, const std::string& message, int collectorType), ());
boost::asio::awaitable<void> Wait([[maybe_unused]]std::chrono::milliseconds ms)
{
return Logcollector::Wait(ms);
}
void Stop()
{
Logcollector::Stop();
}
};
17 changes: 5 additions & 12 deletions src/modules/logcollector/tests/unit/event_reader_win_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <event_reader_win.hpp>
#include <logcollector.hpp>
#include "event_reader_mocks.hpp"
#include "logcollector_mock.hpp"

#include <sstream>
#include <spdlog/spdlog.h>
Expand Down Expand Up @@ -62,7 +61,7 @@ TEST_F(WindowsEventChannel, AddReader)
.WillOnce(SaveArg<0>(&capturedReader1))
.WillOnce(SaveArg<0>(&capturedReader2));

AddPlatformSpecificReader(config, mockedLogcollector);
mockedLogcollector.AddPlatformSpecificReader(config);

ASSERT_NE(capturedReader1, nullptr);
ASSERT_NE(capturedReader2, nullptr);
Expand Down Expand Up @@ -119,20 +118,16 @@ TEST_F(WindowsEventChannel, RunMethodSuccessfulSubscription)
EXPECT_NO_THROW(ioContext.run());
}

TEST_F(WindowsEventChannel, ProcessEvent_SendsMessage)
TEST_F(WindowsEventChannel, ProcessEventSendsMessage)
{
//TODO:
GTEST_SKIP();
// Arrange: Create mocks
auto mockedWinAPI = std::make_shared<MockWinAPIWrapper>();
auto mockedLogcollector = LogcollectorMock();

auto reader = std::make_shared<WindowsEventTracerReader>(
mockedLogcollector, channelName, query, defaultChannelRefresh, mockedWinAPI);

// Mock EvtRender to return a fake event XML string
std::wstring fakeEventXml = L"<Event><Message>Test Log</Message></Event>";
DWORD fakeBufferUsed = static_cast<DWORD>((fakeEventXml.size() + 1) * sizeof(wchar_t)); // Include null terminator
DWORD fakeBufferUsed = static_cast<DWORD>((fakeEventXml.size() + 1) * sizeof(wchar_t));

EXPECT_CALL(*mockedWinAPI, EvtRender(_, _, EvtRenderEventXml, 0, nullptr, _, _))
.WillOnce(DoAll(SetArgPointee<5>(fakeBufferUsed), Return(TRUE)));
Expand All @@ -143,11 +138,9 @@ TEST_F(WindowsEventChannel, ProcessEvent_SendsMessage)
}),
Return(TRUE)));

// Expect the SendMessage method to be called with the processed log
// EXPECT_CALL(*mockedLogcollector, SendMessage(channelName, "Test Log", _))
// .Times(1);
EXPECT_CALL(mockedLogcollector, SendMessage(channelName, "<Event><Message>Test Log</Message></Event>", collectorType))
.Times(1);

// Act: Simulate an event
EVT_HANDLE mockEventHandle = reinterpret_cast<EVT_HANDLE>(2);
reader->ProcessEvent(mockEventHandle);
}
10 changes: 0 additions & 10 deletions src/modules/logcollector/tests/unit/logcollector_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ class LogcollectorMock : public Logcollector {
}
MOCK_METHOD(void, AddReader, (std::shared_ptr<IReader> reader), (override));
MOCK_METHOD(void, EnqueueTask, (Awaitable task), (override));
// TODO:
// MOCK_METHOD(void, SendMessage, (const std::string& channel, const std::string& message, int collectorType), ());
boost::asio::awaitable<void> Wait([[maybe_unused]]std::chrono::milliseconds ms)
{
return Logcollector::Wait(ms);
}
void Stop()
{
Logcollector::Stop();
}
};

class PushMessageMock {
Expand Down

0 comments on commit 814c8d0

Please sign in to comment.