Welcome to mirror list, hosted at ThFree Co, Russian Federation.

messages_resource.cc « tests « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bff646252106581fb2d6f96ffbb43af98b9bf618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2020-2022 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/

#if defined(HAVE_MINGW)
#  include "include/bareos.h"
#  include "gtest/gtest.h"
#else
#  include "gtest/gtest.h"
#  include "include/bareos.h"
#endif

#include "dird/dird_conf.h"
#include "dird/dird_globals.h"
#include "dird/get_database_connection.h"
#include "dird/job.h"
#include "dird/director_jcr_impl.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;

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;
    }
    list_of_filepointers.insert(std::pair<std::string, FILE*>(filename, fp));
    return fp;
  }

  LogFiles() = default;

  ~LogFiles()
  {
    for (auto fp : list_of_filepointers) {
      fflush(fp.second);
      fclose(fp.second);
    }
    list_of_filepointers.clear();
  }

  static std::map<std::string, FILE*> list_of_filepointers;
};

std::map<std::string, FILE*> LogFiles::list_of_filepointers;

static void SyslogCallback_(int, const char* msg)
{
  try {
    FILE* fp = LogFiles::list_of_filepointers.at("syslog");
    // fake-write a syslog entry
    if (fp) { fputs(msg, fp); }
  } catch (const std::out_of_range& e) {
    std::cout << "Could not find <syslog> filepointer";
  }
}

static bool DbLogInsertCallback_(JobControlRecord*, utime_t, const char* msg)
{
  try {
    FILE* fp = LogFiles::list_of_filepointers.at("dblog");
    // fake-write a db log entry
    if (fp) { fputs(msg, fp); }
  } catch (const std::out_of_range& e) {
    std::cout << "Could not find <dblog> filepointer";
  }

  return true;
}

TEST(messages_resource, 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 log_dir = getenv_std_string("BAREOS_LOG_DIR");
  std::string backend_dir = getenv_std_string("backenddir");

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

  std::string regress_debug = getenv_std_string("REGRESS_DEBUG");

  if (!regress_debug.empty()) {
    if (regress_debug == "1") { debug_level = 200; }
  }

  SetWorkingDirectory(working_dir.c_str());
  InitConsoleMsg(log_dir.c_str());

  // 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);

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

  // this object cleans up all files at exit
  LogFiles cleanup;

  // init syslog mock-up
  ASSERT_NE(LogFiles::Open(log_dir, "syslog"), nullptr);
  RegisterSyslogCallback(SyslogCallback_);

  // init catalog mock-up
  JobControlRecord jcr;
  jcr.db = reinterpret_cast<BareosDb*>(0xdeadbeef);
  ASSERT_NE(LogFiles::Open(log_dir, "dblog"), nullptr);
  SetDbLogInsertCallback(DbLogInsertCallback_);

  DispatchMessage(&jcr, M_ERROR, 0, "\n!!!This is a test error message!!!\n");
  TermMsg();
}