Commit 686f4d34 authored by CryTime's avatar CryTime
Browse files

add more units tests

parent 651d2264
......@@ -132,8 +132,8 @@ message IdIdentifier {
message ServiceIdentifier {
oneof identifier {
NameIdentifier names = 1;
IdIdentifier ids = 2;
NameIdentifier names = 1;
IdIdentifier ids = 2;
}
}
......
......@@ -169,9 +169,9 @@ grpc::Status engine_impl::GetContact(grpc::ServerContext* context,
itcontactname;
itcontactname = contact::contacts.find(request->name());
if (itcontactname != contact::contacts.end()) {
if (itcontactname != contact::contacts.end())
selectedcontact = itcontactname->second;
} else {
else {
contactpromise.set_value(std::make_pair(false, nullptr));
return 1;
}
......@@ -186,13 +186,13 @@ grpc::Status engine_impl::GetContact(grpc::ServerContext* context,
command_manager::instance().enqueue(lambda);
auto promiseresponse = f1.get();
if (promiseresponse.first == true) {
if (promiseresponse.first) {
*response = *(promiseresponse.second);
return grpc::Status::OK;
} else {
} else
return grpc::Status(grpc::INVALID_ARGUMENT,
grpc::string("contact not found"));
}
}
grpc::Status engine_impl::GetService(grpc::ServerContext* context,
......@@ -228,9 +228,9 @@ grpc::Status engine_impl::GetService(grpc::ServerContext* context,
IdIdentifier ids = request->ids();
itserviceids = service::services_by_id.find(
std::make_pair(ids.host_id(), ids.service_id()));
if (itserviceids != service::services_by_id.end()) {
if (itserviceids != service::services_by_id.end())
selectedservice = itserviceids->second;
} else {
else {
servicepromise.set_value(std::make_pair(false, nullptr));
return 1;
}
......@@ -252,13 +252,13 @@ grpc::Status engine_impl::GetService(grpc::ServerContext* context,
command_manager::instance().enqueue(lambda);
auto promiseresponse = f1.get();
if (promiseresponse.first == true) {
if (promiseresponse.first) {
*response = *(promiseresponse.second);
return grpc::Status::OK;
} else {
} else
return grpc::Status(grpc::INVALID_ARGUMENT,
grpc::string("service not found"));
}
return grpc::Status::OK;
}
......
......@@ -364,12 +364,15 @@ int main(int argc, char** argv) {
status = client.GetHostByHostId(val, &response) ? 0 : 1;
std::cout << "GetHost" << std::endl;
std::cout << response.name() << std::endl;
std::cout << response.alias() << std::endl;
std::cout << response.id() << std::endl;
std::cout << response.address() << std::endl;
} else if (strcmp(argv[2], "byhostname") == 0) {
EngineHost response;
std::string str(argv[3]);
status = client.GetHostByHostName(str, &response) ? 0 : 1;
std::cout << "GetHost" << std::endl;
std::cout << response.id() << std::endl;
std::cout << response.name() << std::endl;
}
} else if (strcmp(argv[1], "GetContact") == 0) {
if (argc != 3) {
......@@ -382,6 +385,8 @@ int main(int argc, char** argv) {
status = client.GetContact(str, &response) ? 0 : 1;
std::cout << "GetContact" << std::endl;
std::cout << response.name() << std::endl;
std::cout << response.alias() << std::endl;
std::cout << response.email() << std::endl;
} else if (strcmp(argv[1], "GetService") == 0) {
if (argc != 5) {
std::cout << "GetService require arguments : GetService [mode] [hostname "
......@@ -395,7 +400,10 @@ int main(int argc, char** argv) {
status =
client.GetServiceByNames(hostname, servicename, &response) ? 0 : 1;
std::cout << "GetService" << std::endl;
std::cout << response.host_id() << std::endl;
std::cout << response.service_id() << std::endl;
std::cout << response.host_name() << std::endl;
std::cout << response.description() << std::endl;
} else if (strcmp(argv[2], "byids") == 0) {
EngineService response;
uint32_t hostid = atoi(argv[3]);
......
......@@ -92,6 +92,9 @@ class EngineRpc : public TestEngine {
else
_svc = svc;
}
contact_map const& cm{engine::contact::contacts};
_contact = cm.begin()->second;
}
void TearDown() override {
......@@ -125,6 +128,7 @@ class EngineRpc : public TestEngine {
protected:
std::shared_ptr<engine::host> _host;
std::shared_ptr<engine::contact> _contact;
std::shared_ptr<engine::service> _svc;
std::shared_ptr<engine::anomalydetection> _ad;
};
......@@ -157,6 +161,9 @@ TEST_F(EngineRpc, GetHost) {
std::condition_variable condvar;
std::mutex mutex;
bool continuerunning = false;
std::vector<std::string> vectests = {"test_host", "test_host", "12",
"127.0.0.1"};
int j = 0;
auto fn = [&continuerunning, &mutex, &condvar]() {
std::unique_lock<std::mutex> lock(mutex);
......@@ -171,8 +178,8 @@ TEST_F(EngineRpc, GetHost) {
};
std::thread th(fn);
auto output = execute("GetHost byhostname test_host");
auto output2 = execute("GetHost byhostid 12");
auto output = execute("GetHost byhostid 12");
auto output2 = execute("GetHost byhostname test_host");
{
std::lock_guard<std::mutex> lock(mutex);
continuerunning = true;
......@@ -180,7 +187,10 @@ TEST_F(EngineRpc, GetHost) {
condvar.notify_one();
th.join();
ASSERT_EQ(output.back(), "12");
std::list<std::string>::const_iterator it = output.begin();
++it;
for (; it != output.end() && j < vectests.size(); ++it, ++j)
ASSERT_EQ(it->c_str(), vectests[j]);
ASSERT_EQ(output2.back(), "test_host");
erpc.shutdown();
}
......@@ -190,6 +200,9 @@ TEST_F(EngineRpc, GetContact) {
std::condition_variable condvar;
std::mutex mutex;
bool continuerunning = false;
std::vector<std::string> vectests = {"admin", "admin", "admin@centreon.com"};
int j = 0;
_contact->set_email("admin@centreon.com");
auto fn = [&continuerunning, &mutex, &condvar]() {
std::unique_lock<std::mutex> lock(mutex);
......@@ -212,7 +225,10 @@ TEST_F(EngineRpc, GetContact) {
condvar.notify_one();
th.join();
ASSERT_EQ(output.back(), "admin");
std::list<std::string>::const_iterator it = output.begin();
++it;
for (; it != output.end() && j < vectests.size(); ++it, ++j)
ASSERT_EQ(it->c_str(), vectests[j]);
erpc.shutdown();
}
......@@ -221,7 +237,10 @@ TEST_F(EngineRpc, GetService) {
std::condition_variable condvar;
std::mutex mutex;
bool continuerunning = false;
std::vector<std::string> vectests = {"12", "13", "test_host", "test_description"};
int j = 0;
_svc->set_description("test_description");
auto fn = [&continuerunning, &mutex, &condvar]() {
std::unique_lock<std::mutex> lock(mutex);
while (true) {
......@@ -235,8 +254,8 @@ TEST_F(EngineRpc, GetService) {
};
std::thread th(fn);
auto output = execute("GetService byids 12 13");
auto output2 = execute("GetService bynames test_host test_svc");
auto output = execute("GetService bynames test_host test_svc");
auto output2 = execute("GetService byids 12 13");
{
std::lock_guard<std::mutex> lock(mutex);
continuerunning = true;
......@@ -244,8 +263,14 @@ TEST_F(EngineRpc, GetService) {
condvar.notify_one();
th.join();
ASSERT_EQ(output.back(), "test_host");
ASSERT_EQ(output2.back(), "13");
std::list<std::string>::const_iterator it = output.begin();
++it;
for (; it != output.end() && j < vectests.size(); ++it, ++j)
ASSERT_EQ(it->c_str(), vectests[j]);
//ASSERT_EQ(output.back(), "test_host");
//ASSERT_EQ(output2.back(), "13");
erpc.shutdown();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment