Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing msg in simu #30

Merged
merged 2 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/70-graphite/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
####Copyright 2013 Centreon####Licensed under the Apache License,
Version 2.0(the "License");
##
## Copyright 2013 Centreon
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
Expand All @@ -17,9 +19,9 @@ include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CONAN_INCLUDE_DIRS_JSON11})
include_directories(${CONAN_INCLUDE_DIRS_SPDLOG})

#TLS module.
# TLS module.
add_library(70-graphite SHARED
#Sources
# Sources
${CMAKE_SOURCE_DIR}/src/70-graphite/connector.cc
${CMAKE_SOURCE_DIR}/src/70-graphite/factory.cc
${CMAKE_SOURCE_DIR}/src/70-graphite/macro_cache.cc
Expand Down
1 change: 0 additions & 1 deletion src/70-simu/connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "com/centreon/broker/simu/connector.hh"
#include <fstream>
#include <sstream>
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/broker/simu/stream.hh"

using namespace com::centreon::broker;
Expand Down
61 changes: 32 additions & 29 deletions src/70-simu/factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
#include <cstring>
#include <json11.hpp>
#include <memory>
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "com/centreon/broker/misc/variant.hh"
#include "com/centreon/broker/simu/connector.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::simu;
using namespace json11;
Expand All @@ -40,8 +41,7 @@ static std::string find_param(config::endpoint const& cfg,
std::string const& key) {
std::map<std::string, std::string>::const_iterator it{cfg.params.find(key)};
if (cfg.params.end() == it)
throw exceptions::msg()
<< "lua: no '" << key << "' defined for endpoint '" << cfg.name << "'";
throw msg_fmt("lua: no '{}' defined for endpoint '{}'", key, cfg.name);
return it->second;
}

Expand Down Expand Up @@ -70,10 +70,10 @@ bool factory::has_endpoint(config::endpoint& cfg) const {
*
* @return New endpoint.
*/
io::endpoint* factory::new_endpoint(
config::endpoint& cfg,
bool& is_acceptor,
std::shared_ptr<persistent_cache> cache __attribute__((unused))) const {
io::endpoint* factory::new_endpoint(config::endpoint& cfg,
bool& is_acceptor,
std::shared_ptr<persistent_cache> cache
__attribute__((unused))) const {
std::map<std::string, misc::variant> conf_map;
std::string err;

Expand All @@ -82,21 +82,21 @@ io::endpoint* factory::new_endpoint(
Json const& js{cfg.cfg["lua_parameter"]};

if (!err.empty())
throw exceptions::msg() << "simu: couldn't read a configuration json";
throw msg_fmt("simu: couldn't read a configuration json");

if (js.is_object()) {
Json const& name{js["name"]};
Json const& type{js["type"]};
Json const& value{js["value"]};

if (name.string_value().empty())
throw exceptions::msg()
<< "simu: couldn't read a configuration field because"
<< " its name is empty";
throw msg_fmt(
"simu: couldn't read a configuration field because its name is "
"empty");
if (value.string_value().empty())
throw exceptions::msg()
<< "simu: couldn't read a configuration field because"
<< "' configuration field because its value is empty";
throw msg_fmt(
"simu: couldn't read a configuration field because its value is "
"empty");
std::string t((type.string_value().empty()) ? "string"
: type.string_value());
if (t == "string" || t == "password")
Expand All @@ -112,7 +112,8 @@ io::endpoint* factory::new_endpoint(
conf_map.insert({name.string_value(), misc::variant(val)});
else
ko = true;
} catch (std::exception const& e) {
}
catch (std::exception const& e) {
ko = true;
}
// Second attempt using floating point numbers
Expand All @@ -123,14 +124,15 @@ io::endpoint* factory::new_endpoint(
conf_map.insert({name.string_value(), misc::variant(val)});
else
ko = true;
} catch (std::exception const& e) {
}
catch (std::exception const& e) {
ko = true;
}
}
if (ko)
throw exceptions::msg()
<< "simu: unable to read '" << name.string_value() << "' content ("
<< value.string_value() << ") as a number";
throw msg_fmt("simu: unable to read '{}' content ({}) as a number",
name.string_value(),
value.string_value());
}
} else if (js.is_array()) {
for (Json const& obj : js.array_items()) {
Expand All @@ -139,13 +141,13 @@ io::endpoint* factory::new_endpoint(
Json const& value{obj["value"]};

if (name.string_value().empty())
throw exceptions::msg()
<< "lua: couldn't read a configuration field because"
<< " its name is empty";
throw msg_fmt(
"lua: couldn't read a configuration field because its name is "
"empty");
if (value.string_value().empty())
throw exceptions::msg()
<< "simu: couldn't read a configuration field because"
<< "' configuration field because its value is empty";
throw msg_fmt(
"simu: couldn't read a configuration field because its value is "
"empty");
std::string t((type.string_value().empty()) ? "string"
: type.string_value());
if (t == "string" || t == "password")
Expand All @@ -155,10 +157,11 @@ io::endpoint* factory::new_endpoint(
try {
int val = std::stol(value.string_value());
conf_map.insert({name.string_value(), misc::variant(val)});
} catch (std::exception const& e) {
throw exceptions::msg()
<< "lua: unable to read '" << name.string_value() << "' content ("
<< value.string_value() << ") as a number";
}
catch (std::exception const& e) {
throw msg_fmt("lua: unable to read '{}' content ({}) as a number",
name.string_value(),
value.string_value());
}
}
}
Expand Down
58 changes: 29 additions & 29 deletions src/70-simu/luabinding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
#include <cassert>
#include <fstream>
#include <memory>
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "com/centreon/broker/io/events.hh"
#include "com/centreon/broker/logging/logging.hh"
#include "com/centreon/broker/lua/broker_log.hh"
#include "com/centreon/broker/lua/broker_utils.hh"
#include "com/centreon/broker/mapping/entry.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::simu;

Expand Down Expand Up @@ -100,27 +101,23 @@ void luabinding::_load_script() {
// script loading
if (luaL_loadfile(_L, _lua_script.c_str()) != 0) {
char const* error_msg(lua_tostring(_L, -1));
throw exceptions::msg()
<< "simu: '" << _lua_script << "' could not be loaded: " << error_msg;
throw msg_fmt("simu: '{}' could not be loaded: {}", _lua_script, error_msg);
}

// Script compilation
if (lua_pcall(_L, 0, 0, 0) != 0) {
throw exceptions::msg()
<< "simu: '" << _lua_script << "' could not be compiled";
throw msg_fmt("simu: '{}' could not be compiled", _lua_script);
}

// Checking for init() availability: this function is mandatory
lua_getglobal(_L, "init");
if (!lua_isfunction(_L, lua_gettop(_L)))
throw exceptions::msg()
<< "simu: '" << _lua_script << "' init() global function is missing";
throw msg_fmt("simu: '{}' init() global function is missing", _lua_script);

// Checking for read() availability: this function is mandatory
lua_getglobal(_L, "read");
if (!lua_isfunction(_L, lua_gettop(_L)))
throw exceptions::msg()
<< "simu: '" << _lua_script << "' read() global function is missing";
throw msg_fmt("simu: '{}' read() global function is missing", _lua_script);
}

/**
Expand All @@ -138,7 +135,8 @@ void luabinding::_init_script(
for (std::map<std::string, misc::variant>::const_iterator
it(conf_params.begin()),
end(conf_params.end());
it != end; ++it) {
it != end;
++it) {
switch (it->second.user_type()) {
case misc::variant::type_int:
case misc::variant::type_uint:
Expand Down Expand Up @@ -168,8 +166,8 @@ void luabinding::_init_script(
}
}
if (lua_pcall(_L, 1, 0, 0) != 0)
throw exceptions::msg()
<< "simu: error running function `init'" << lua_tostring(_L, -1);
throw msg_fmt("simu: error running function `init' {}",
lua_tostring(_L, -1));
}

/**
Expand All @@ -190,17 +188,17 @@ bool luabinding::read(std::shared_ptr<io::data>& data) {
lua_getglobal(_L, "read");

if (lua_pcall(_L, 0, 1, 0) != 0)
throw exceptions::msg()
<< "simu: error running function `read' " << lua_tostring(_L, -1);
throw msg_fmt("simu: error running function `read' {}",
lua_tostring(_L, -1));

if (lua_istable(_L, -1))
retval = _parse_event(data);
else if (lua_isnil(_L, -1))
lua_pop(_L, -1);
else {
throw exceptions::msg()
<< "simu: `read' must return a table or a nil value ("
<< lua_type(_L, -1) << " type return)";
throw msg_fmt(
"simu: `read' must return a table or a nil value ({} type return)",
lua_type(_L, -1));
}

return retval;
Expand Down Expand Up @@ -245,8 +243,8 @@ bool luabinding::_parse_event(std::shared_ptr<io::data>& d) {
else if (lua_isstring(_L, -1))
map.insert({key, misc::variant(lua_tostring(_L, -1))});
else
throw exceptions::msg() << "simu: item with key " << key
<< " is not supported for a broker event";
throw msg_fmt(
"simu: item with key {} is not supported for a broker event", key);
}
lua_pop(_L, 1); // remove value, keep key for lua_next
}
Expand All @@ -260,7 +258,8 @@ bool luabinding::_parse_event(std::shared_ptr<io::data>& d) {
if (t) {
// Browse all mapping to unserialize the object.
for (mapping::entry const* current_entry(info->get_mapping());
!current_entry->is_null(); ++current_entry) {
!current_entry->is_null();
++current_entry) {
if (!current_entry->get_name_v2())
continue;
std::map<std::string, misc::variant>::const_iterator it{
Expand All @@ -278,8 +277,8 @@ bool luabinding::_parse_event(std::shared_ptr<io::data>& d) {
current_entry->set_int(*t, it->second.as_long());
break;
case mapping::source::SHORT:
current_entry->set_short(*t,
static_cast<short>(it->second.as_long()));
current_entry->set_short(
*t, static_cast<short>(it->second.as_long()));
break;
case mapping::source::STRING:
current_entry->set_string(*t, it->second.as_string());
Expand All @@ -291,18 +290,19 @@ bool luabinding::_parse_event(std::shared_ptr<io::data>& d) {
current_entry->set_uint(*t, it->second.as_ulong());
break;
default:
throw exceptions::msg() << "simu: invalid mapping for "
<< "object of type '" << info->get_name()
<< "': " << current_entry->get_type()
<< " is not a known type ID";
throw msg_fmt(
"simu: invalid mapping for object of type '{}': {} is not a "
"known type ID",
info->get_name(),
current_entry->get_type());
}
}
}
d.reset(t.release());
} else
throw exceptions::msg()
<< "simu: cannot create object of ID " << map["_type"].as_int()
<< " whereas it has been registered";
throw msg_fmt(
"simu: cannot create object of ID {} whereas it has been registered",
map["_type"].as_int());
} else {
logging::info(logging::high)
<< "simu: cannot unserialize event of ID " << map["_type"].as_uint()
Expand Down
16 changes: 9 additions & 7 deletions tests/broker/simu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
#include <list>
#include <memory>
#include "com/centreon/broker/config/applier/init.hh"
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "com/centreon/broker/instance_broadcast.hh"
#include "com/centreon/broker/modules/loader.hh"
#include "com/centreon/broker/neb/events.hh"
#include "com/centreon/broker/simu/luabinding.hh"
#include "com/centreon/broker/storage/status.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::misc;
using namespace com::centreon::broker::simu;
Expand All @@ -37,7 +38,8 @@ class SimuGenericTest : public ::testing::Test {
void SetUp() override {
try {
config::applier::init();
} catch (std::exception const& e) {
}
catch (std::exception const& e) {
(void)e;
}
}
Expand Down Expand Up @@ -69,7 +71,7 @@ class SimuGenericTest : public ::testing::Test {
TEST_F(SimuGenericTest, MissingScript) {
std::map<std::string, misc::variant> conf;
ASSERT_THROW(new luabinding("/tmp/this_script_does_not_exist.lua", conf),
exceptions::msg);
msg_fmt);
}

// When a lua script with error such as number divided by nil is loaded
Expand All @@ -80,7 +82,7 @@ TEST_F(SimuGenericTest, FaultyScript) {
CreateScript(filename,
"local a = { 1, 2, 3 }\n"
"local b = 18 / a[4]");
ASSERT_THROW(new luabinding(filename, conf), exceptions::msg);
ASSERT_THROW(new luabinding(filename, conf), msg_fmt);
RemoveFile(filename);
}

Expand All @@ -90,7 +92,7 @@ TEST_F(SimuGenericTest, WithoutInit) {
std::map<std::string, misc::variant> conf;
std::string filename("/tmp/without_init.lua");
CreateScript(filename, "local a = { 1, 2, 3 }\n");
ASSERT_THROW(new luabinding(filename, conf), exceptions::msg);
ASSERT_THROW(new luabinding(filename, conf), msg_fmt);
RemoveFile(filename);
}

Expand All @@ -103,7 +105,7 @@ TEST_F(SimuGenericTest, IncompleteScript) {
"end\n"
"local a = { 1, 2, 3 }\n");
std::map<std::string, misc::variant> conf;
ASSERT_THROW(new luabinding(filename, conf), exceptions::msg);
ASSERT_THROW(new luabinding(filename, conf), msg_fmt);
RemoveFile(filename);
}

Expand All @@ -120,7 +122,7 @@ TEST_F(SimuGenericTest, ReadReturnValue1) {
std::map<std::string, misc::variant> conf;
std::unique_ptr<luabinding> lb(new luabinding(filename, conf));
std::shared_ptr<io::data> d;
ASSERT_THROW(lb->read(d), exceptions::msg);
ASSERT_THROW(lb->read(d), msg_fmt);
RemoveFile(filename);
}

Expand Down