Skip to content

Commit

Permalink
systemtest: add more test pattern to message test
Browse files Browse the repository at this point in the history
  • Loading branch information
franku committed Feb 11, 2020
1 parent d56539b commit a7cc9e2
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 15 deletions.
21 changes: 13 additions & 8 deletions core/src/tests/CMakeLists.txt
Expand Up @@ -393,12 +393,17 @@ if(NOT client-only)
)
endif() # NOT client-only

bareos_add_test(
messages LINK_LIBRARIES dird_objects
bareos
bareosfind
bareoscats
bareossql
${GTEST_LIBRARIES}
${GTEST_MAIN_LIBRARIES}
bareos_add_test(messages
LINK_LIBRARIES
bareos
dird_objects
bareosfind
bareoscats
bareossql
$<$<BOOL:HAVE_PAM>:${PAM_LIBRARIES}>
${LMDB_LIBS}
${GTEST_LIBRARIES}
${GTEST_MAIN_LIBRARIES}
SKIP_GTEST

)
81 changes: 77 additions & 4 deletions core/src/tests/messages.cc
Expand Up @@ -29,37 +29,110 @@

#include "dird/dird_conf.h"
#include "dird/dird_globals.h"
#include "dird/get_database_connection.h"
#include "dird/job.h"
#include "dird/jcr_private.h"
#include "lib/messages_resource.h"
#include "lib/parse_conf.h"
#include "lib/util.h"

#include <memory>
#include <vector>

using directordaemon::InitDirConfig;
using directordaemon::my_config;

namespace directordaemon {
bool DoReloadConfig() { return false; }
} // namespace directordaemon

class LogFiles {
public:
static FILE* Open(const std::string& dir, std::string filename)
{
std::string fullpath = dir + "/" + filename;
FILE* fp = fopen(fullpath.c_str(), "w");
if (!fp) {
std::cout << "Could not open syslog file " << fullpath << std::endl;
return nullptr;
}
fps.push_back(fp);
return fp;
}

LogFiles() = default;

~LogFiles()
{
for (auto fp : fps) {
fflush(fp);
fclose(fp);
}
}

private:
static std::vector<FILE*> fps;
};

std::vector<FILE*> LogFiles::fps;
static FILE* syslog_file{};
static FILE* db_log_file{};

static void SyslogCallback_(int mode, const char* msg)
{
// fake-write a syslog entry
if (syslog_file) { fputs(msg, syslog_file); }
}

static bool DbLogInsertCallback_(JobControlRecord* jcr,
utime_t mtime,
const char* msg)
{
// fake-write a db log entry
if (db_log_file) { fputs(msg, db_log_file); }
return true;
}

TEST(messages, send_message_to_all_configured_destinations)
{
std::string config_dir = getenv_std_string("BAREOS_CONFIG_DIR");
std::string working_dir = getenv_std_string("BAREOS_WORKING_DIR");
std::string backend_dir = getenv_std_string("backenddir");

ASSERT_FALSE(working_dir.empty());
ASSERT_FALSE(config_dir.empty());
ASSERT_FALSE(backend_dir.empty());

SetWorkingDirectory(working_dir.c_str());
InitConsoleMsg(working_dir.c_str());
RegisterSyslogCallback(SyslogCallback_);
SetDbLogInsertCallback(DbLogInsertCallback_);

// parse config
std::unique_ptr<ConfigurationParser> p{
InitDirConfig(config_dir.c_str(), M_ERROR_TERM)};

directordaemon::my_config = p.get();
ASSERT_TRUE(directordaemon::my_config->ParseConfig());

// get message resource
MessagesResource* messages =
dynamic_cast<MessagesResource*>(directordaemon::my_config->GetResWithName(
directordaemon::R_MSGS, "Standard"));

ASSERT_NE(messages, nullptr);

InitMsg(NULL, messages); /* initialize message handler */
// initialize message handler
InitMsg(NULL, messages);

LogFiles cleanup_files;

syslog_file = LogFiles::Open(working_dir, "syslog.txt");
db_log_file = LogFiles::Open(working_dir, "dblog.txt");

ASSERT_NE(syslog_file, nullptr);
ASSERT_NE(db_log_file, nullptr);

JobControlRecord jcr;
jcr.db = reinterpret_cast<BareosDb*>(1);

DispatchMessage(nullptr, M_ERROR, 0, "This is an error message");
DispatchMessage(&jcr, M_ERROR, 0, "This is an error message");
}
@@ -1,7 +1,8 @@
Messages {
Name = Standard
Description = "Reasonable message delivery -- send most everything to email address and to the console."
console = all, !skipped, !saved, !audit
console = all
append = "@logdir@/bareos.log" = all, !skipped, !saved, !audit
catalog = all, !skipped, !saved, !audit
syslog = all
}
8 changes: 6 additions & 2 deletions systemtests/tests/messages-test/testrunner
Expand Up @@ -11,13 +11,15 @@ export TestName

#shellcheck source=../scripts/functions
. "${rscripts}"/functions
"${rscripts}"/cleanup
"${rscripts}"/setup

console_messages_file="${working}"/.conmsg
syslog_file="${working}"/syslog.txt
dblog_file="${working}"/dblog.txt
extra_append_file="${logdir}"/bareos.log

rm -f "$console_messages_file"
rm -f "$syslog_file"
rm -f "$dblog_file"
rm -f "$extra_append_file"

function exit_error() {
Expand All @@ -40,6 +42,8 @@ start_test
${BAREOS_UNITTESTS_BINARY_DIR}/messages || exit_failure

check_output_file "$console_messages_file"
check_output_file "$syslog_file"
check_output_file "$dblog_file"
check_output_file "$extra_append_file"

end_test

0 comments on commit a7cc9e2

Please sign in to comment.