blob: 5d9edba8d9a88c3746e567a36c2698af23041712 [file] [log] [blame]
Patrick Venturef18bf832018-10-26 18:14:00 -07001#include "config.h"
2
3#include "elog_serialize.hpp"
Nagaraju Goruganti477b7312018-06-25 23:28:58 -05004#include "log_manager.hpp"
5#include "xyz/openbmc_project/Common/error.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -07006
Patrick Venturef18bf832018-10-26 18:14:00 -07007#include <phosphor-logging/elog-errors.hpp>
Nagaraju Goruganti477b7312018-06-25 23:28:58 -05008#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/log.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -070010#include <sdbusplus/bus.hpp>
11
Patrick Williams2544b412022-10-04 08:41:06 -050012#include <filesystem>
13
Patrick Venturef18bf832018-10-26 18:14:00 -070014#include <gmock/gmock.h>
15#include <gtest/gtest.h>
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050016
17namespace phosphor
18{
19namespace logging
20{
21namespace internal
22{
23
Patrick Williams331c4852021-04-16 15:38:59 -050024namespace fs = std::filesystem;
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050025
26class journalInterface
27{
Patrick Venturef18bf832018-10-26 18:14:00 -070028 public:
Patrick Williamsce0bdf12021-04-16 20:09:22 -050029 virtual ~journalInterface() = default;
30
Patrick Venturef18bf832018-10-26 18:14:00 -070031 virtual void journalSync() = 0;
32 virtual int sd_journal_open(sd_journal** j, int k) = 0;
33 virtual int sd_journal_get_data(sd_journal* j, const char* transactionIdVar,
34 const void** data, size_t length) = 0;
35 virtual void sd_journal_close(sd_journal* j) = 0;
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050036};
37
38class journalImpl : public journalInterface
39{
Patrick Venturef18bf832018-10-26 18:14:00 -070040 public:
Matt Spinler4a375952022-07-01 11:15:33 -050041 void journalSync() override;
42 int sd_journal_open(sd_journal** j, int k) override;
Patrick Venturef18bf832018-10-26 18:14:00 -070043 int sd_journal_get_data(sd_journal* j, const char* transactionIdVar,
Matt Spinler4a375952022-07-01 11:15:33 -050044 const void** data, size_t length) override;
45 void sd_journal_close(sd_journal* j) override;
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050046};
47
Patrick Williamsce0bdf12021-04-16 20:09:22 -050048int journalImpl::sd_journal_open(sd_journal**, int)
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050049{
50 return 1;
51}
52
53void journalImpl::journalSync()
54{
55 return;
56}
57
Patrick Williamsce0bdf12021-04-16 20:09:22 -050058int journalImpl::sd_journal_get_data(sd_journal*, const char*, const void**,
59 size_t)
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050060{
61 return 1;
62}
63
Patrick Williamsce0bdf12021-04-16 20:09:22 -050064void journalImpl::sd_journal_close(sd_journal*)
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050065{
66 return;
67}
68
Patrick Venturef18bf832018-10-26 18:14:00 -070069class MockJournal : public Manager
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050070{
Patrick Venturef18bf832018-10-26 18:14:00 -070071 public:
Patrick Williams45e83522022-07-22 19:26:52 -050072 MockJournal(sdbusplus::bus_t& bus, const char* objPath) :
Patrick Venturef18bf832018-10-26 18:14:00 -070073 Manager(bus, objPath){};
74 MOCK_METHOD0(journalSync, void());
75 MOCK_METHOD2(sd_journal_open, int(sd_journal**, int));
76 MOCK_METHOD4(sd_journal_get_data,
77 int(sd_journal*, const char*, const void**, size_t));
78 MOCK_METHOD1(sd_journal_close, void(sd_journal*));
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050079};
80
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050081class TestLogManager : public testing::Test
82{
Patrick Venturef18bf832018-10-26 18:14:00 -070083 public:
Patrick Williams45e83522022-07-22 19:26:52 -050084 sdbusplus::bus_t bus;
Patrick Venturef18bf832018-10-26 18:14:00 -070085 MockJournal manager;
86 TestLogManager() :
87 bus(sdbusplus::bus::new_default()),
88 manager(bus, "/xyz/openbmc_test/abc")
89 {
90 fs::create_directories(ERRLOG_PERSIST_PATH);
91 }
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050092
Patrick Venturef18bf832018-10-26 18:14:00 -070093 ~TestLogManager()
94 {
Matt Spinler7ec2ab72020-04-03 11:54:29 -050095 // Leave the directory as other testcases use it and they
96 // may be running in parallel from other jobs.
Patrick Venturef18bf832018-10-26 18:14:00 -070097 }
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050098};
99
Patrick Venturef18bf832018-10-26 18:14:00 -0700100} // namespace internal
101} // namespace logging
102} // namespace phosphor