From 1aefac8ae0899f0fbadd79e43ef38cc0af034b0f Mon Sep 17 00:00:00 2001 From: groot Date: Mon, 29 Apr 2019 12:31:48 +0800 Subject: [PATCH] fix unitest load config error Former-commit-id: 8bf7417881a421bf5c28a31474743484e8fea155 --- cpp/src/utils/CommonUtil.cpp | 15 +++++++++++++++ cpp/src/utils/CommonUtil.h | 2 ++ cpp/unittest/vecwise_test.cpp | 7 ++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cpp/src/utils/CommonUtil.cpp b/cpp/src/utils/CommonUtil.cpp index 00e9149d14..86bb3797ac 100644 --- a/cpp/src/utils/CommonUtil.cpp +++ b/cpp/src/utils/CommonUtil.cpp @@ -132,6 +132,21 @@ bool CommonUtil::IsFileExist(const std::string &path) { return (access(path.c_str(), F_OK) == 0); } +std::string CommonUtil::GetExePath() { + const size_t bug_len = 1024; + char exe_path[bug_len]; + size_t cnt = readlink("/proc/self/exe", exe_path, bug_len); + if(cnt < 0|| cnt >= bug_len) { + return ""; + } + + std::string store_path = exe_path; + if(store_path.rfind('/') != store_path.length()){ + std::string sub_str = store_path.substr(0, store_path.rfind('/')); + return sub_str + "/"; + } + return store_path; +} } } diff --git a/cpp/src/utils/CommonUtil.h b/cpp/src/utils/CommonUtil.h index 798ad62817..5667b39e58 100755 --- a/cpp/src/utils/CommonUtil.h +++ b/cpp/src/utils/CommonUtil.h @@ -22,6 +22,8 @@ class CommonUtil { static bool IsDirectoryExit(const std::string &path); static ServerError CreateDirectory(const std::string &path); static ServerError DeleteDirectory(const std::string &path); + + static std::string GetExePath(); }; } diff --git a/cpp/unittest/vecwise_test.cpp b/cpp/unittest/vecwise_test.cpp index d966522e2a..f123fdb403 100644 --- a/cpp/unittest/vecwise_test.cpp +++ b/cpp/unittest/vecwise_test.cpp @@ -8,13 +8,18 @@ #include #include "server/ServerConfig.h" +#include "utils/CommonUtil.h" INITIALIZE_EASYLOGGINGPP +using namespace zilliz::vecwise; + int main(int argc, char **argv) { - std::string config_filename = "../../../conf/server_config.yaml"; + std::string exe_path = server::CommonUtil::GetExePath(); + std::string config_filename = exe_path + "../../../conf/server_config.yaml"; zilliz::vecwise::server::ServerConfig& config = zilliz::vecwise::server::ServerConfig::GetInstance(); config.LoadConfigFile(config_filename); + std::cout << "Load config file form: " << config_filename << std::endl; ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();