Skip to content
Snippets Groups Projects
Verified Commit 52c022da authored by Timm Fitschen's avatar Timm Fitschen
Browse files

DRAFT: conan packaging

parent 6c7b003a
No related branches found
No related tags found
No related merge requests found
Pipeline #9817 failed
...@@ -27,73 +27,85 @@ project(libcaosdb ...@@ -27,73 +27,85 @@ project(libcaosdb
DESCRIPTION "C++ client libraries for CaosDB" DESCRIPTION "C++ client libraries for CaosDB"
LANGUAGES CXX) LANGUAGES CXX)
# override options with variables (globally)
# needed for turning off unused code from grpc
cmake_policy(SET CMP0077 NEW)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
# dependency management with conan ###########################################
### DEPENDENCY MANAGEMENT with CONAN
###########################################
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup() conan_basic_setup()
###########################################
### GENERAL SETUP of SOURCES
###########################################
set(PROJECT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include") set(PROJECT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include")
add_subdirectory(src)
add_subdirectory(include)
add_subdirectory(doc)
add_library(caosdb STATIC ${libcaosdb_INCL} ${libcaosdb_SRC})
add_executable(caosdbcli src/caosdbcli.cpp)
option(LINTING "clang-tidy and iwye" ON) #######################################################
if(LINTING) ### LINTING with CLANG-TIDY and INCLUDE-WHAT-YOU-USE
find_program(iwyu NAMES include-what-you-use iwyu PATHS ${CMAKE_SOURCE_DIR}/tools/include-what-you-use/${iwyu_os}/bin) #######################################################
# linting is active when BUILD_TYPE == "Debug"
if("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
#set(_LINTING ON)
endif()
if(_LINTING)
find_program(iwyu
NAMES include-what-you-use iwyu
PATHS ${CMAKE_SOURCE_DIR}/tools/include-what-you-use/${iwyu_os}/bin)
if(NOT iwyu) if(NOT iwyu)
message(STATUS "include-what-you-use: Not found") message(WARNING "include-what-you-use: Not found")
else() else()
message(STATUS "include-what-you-use: ${iwyu}") message(STATUS "include-what-you-use: ${iwyu}")
set(_CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu}) set(_CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu} "-Xiwyu" "--cxx17ns")
endif() endif()
find_program(clang_tidy NAMES clang-tidy clang-tidy-11) find_program(clang_tidy NAMES clang-tidy clang-tidy-11)
if(NOT clang_tidy) if(NOT clang_tidy)
message(STATUS "clang-tidy: Not found") message(WARNING "clang-tidy: Not found")
else() else()
message(STATUS "clang-tidy: ${clang_tidy}") message(STATUS "clang-tidy: ${clang_tidy}")
set(_CMAKE_CXX_CLANG_TIDY_CHECKS
"--checks=*,-fuchsia-*,-llvm-include-order,-llvmlibc-*")
set(_CMAKE_CXX_CLANG_TIDY "${clang_tidy}" set(_CMAKE_CXX_CLANG_TIDY "${clang_tidy}"
"--header-filter=caosdb/.*[^\(\.pb\.h\)]$" "--header-filter=caosdb/.*[^\(\.pb\.h\)]$"
"--checks=*,-fuchsia-*,-llvm-include-order,-llvmlibc-*"
"--warnings-as-errors=*") "--warnings-as-errors=*")
option(AUTO_FIX_LINTING "Append --fix option to clang-tidy" OFF)
if(AUTO_FIX_LINTING)
set(APPEND AUTO_FIX_LINTING "--fix")
endif()
endif() endif()
else() set_target_properties(caosdb PROPERTIES
message(STATUS "LINTING is OFF") CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY};${_CMAKE_CXX_CLANG_TIDY_CHECKS}"
CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}"
)
set_target_properties(caosdbcli PROPERTIES
CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY};${_CMAKE_CXX_CLANG_TIDY_CHECKS}"
CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}"
)
endif() endif()
add_subdirectory(src)
add_subdirectory(include)
add_subdirectory(doc)
add_library(caosdb STATIC ${libcaosdb_INCL} ${libcaosdb_SRC}) #######################################################
set_target_properties(caosdb PROPERTIES ### UNIT TEST
CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY}" #######################################################
CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}"
)
add_executable(caosdbcli src/caosdbcli.cpp)
set_target_properties(caosdbcli PROPERTIES
CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY}"
CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}"
)
target_link_libraries(caosdbcli caosdb caosdb_info_v1alpha1 ${CONAN_LIBS})
if("${CMAKE_BUILD_TYPE}" MATCHES "Debug") if("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
# supress warnings during build of gtest
cmake_policy(SET CMP0054 NEW)
target_link_libraries(caosdb gcov)
enable_testing()
add_subdirectory(test) add_subdirectory(test)
set_target_properties(caosdb PROPERTIES set_target_properties(caosdb PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}") COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
endif() endif()
#################################################################### ####################################################################
## CODE GENERATION (WITH GRPC) ### CODE GENERATION (WITH GRPC)
#################################################################### ####################################################################
# Protobuf/Grpc source files # Protobuf/Grpc source files
...@@ -149,8 +161,13 @@ target_include_directories(caosdb_info_v1alpha1 PUBLIC ...@@ -149,8 +161,13 @@ target_include_directories(caosdb_info_v1alpha1 PUBLIC
) )
target_link_libraries(caosdb target_link_libraries(caosdb
caosdb_info_v1alpha1 caosdb_info_v1alpha1
${CONAN_LIBS} ${CONAN_LIBS}
)
target_link_libraries(caosdbcli
caosdb
caosdb_info_v1alpha1
${CONAN_LIBS}
) )
...@@ -265,3 +282,17 @@ write_basic_package_version_file( ...@@ -265,3 +282,17 @@ write_basic_package_version_file(
) )
install(FILES ${PROJECT_SOURCE_DIR}/caosdbConfigVersion.cmake install(FILES ${PROJECT_SOURCE_DIR}/caosdbConfigVersion.cmake
DESTINATION ${libcaosdb_CMAKE_DEST}) DESTINATION ${libcaosdb_CMAKE_DEST})
#######################################################
### code formatting with clang-format
#######################################################
option(AUTOFORMATTING "call clang-format at configure time" ON)
if(AUTOFORMATTING)
file(GLOB format_test_sources test/*.cpp)
execute_process(COMMAND clang-format -i --verbose ${libcaosdb_INCL}
${libcaosdb_SRC} ${libcaosdb_TEST_SRC}
${PROJECT_SOURCE_DIR}/src/caosdbcli.cpp
${format_test_sources}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
endif()
...@@ -24,6 +24,7 @@ set(libcaosdb_INCL ...@@ -24,6 +24,7 @@ set(libcaosdb_INCL
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.h ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.h
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.h ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.h
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/utils.h ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/utils.h
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/info.h
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/exceptions.h ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/exceptions.h
) )
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#ifndef CAOSDB_CONNECTION_H #ifndef CAOSDB_CONNECTION_H
#define CAOSDB_CONNECTION_H #define CAOSDB_CONNECTION_H
/** /**
* @file connection.h * @file caosdb/connection.h
* @author Timm Fitschen * @author Timm Fitschen
* @date 2021-05-18 * @date 2021-05-18
* @brief Configuration and setup of the connection. * @brief Configuration and setup of the connection.
...@@ -34,23 +34,19 @@ ...@@ -34,23 +34,19 @@
namespace caosdb { namespace caosdb {
namespace authentication { namespace authentication {
class Authenticator; class Authenticator;
} } // namespace authentication
} // namespace caosdb
namespace caosdb {
namespace info { namespace info {
namespace v1alpha1 {
class VersionInfo; class VersionInfo;
}
} // namespace info } // namespace info
} // namespace caosdb } // namespace caosdb
namespace grpc { namespace grpc {
class ChannelCredentials; class ChannelCredentials;
} } // namespace grpc
namespace caosdb::connection { namespace caosdb::connection {
using caosdb::authentication::Authenticator; using caosdb::authentication::Authenticator;
using caosdb::info::VersionInfo;
using caosdb::info::v1alpha1::GeneralInfoService; using caosdb::info::v1alpha1::GeneralInfoService;
using caosdb::info::v1alpha1::VersionInfo;
using grpc::ChannelCredentials; using grpc::ChannelCredentials;
class CACertificateProvider { class CACertificateProvider {
...@@ -103,6 +99,9 @@ private: ...@@ -103,6 +99,9 @@ private:
public: public:
InsecureCaosDBConnectionConfig(const std::string &host, int port); InsecureCaosDBConnectionConfig(const std::string &host, int port);
InsecureCaosDBConnectionConfig(
const std::string &host, int port,
const std::shared_ptr<Authenticator> &authenticator);
[[nodiscard]] auto getChannelCredentials() const [[nodiscard]] auto getChannelCredentials() const
-> std::shared_ptr<ChannelCredentials> override; -> std::shared_ptr<ChannelCredentials> override;
[[nodiscard]] auto toString() const -> std::string override; [[nodiscard]] auto toString() const -> std::string override;
...@@ -139,7 +138,7 @@ public: ...@@ -139,7 +138,7 @@ public:
-> std::ostream &; -> std::ostream &;
[[nodiscard]] auto getGeneralInfoService() const [[nodiscard]] auto getGeneralInfoService() const
-> GeneralInfoService::Stub &; -> GeneralInfoService::Stub &;
[[nodiscard]] auto getVersionInfo() const -> VersionInfo; [[nodiscard]] auto getVersionInfo() const -> std::unique_ptr<VersionInfo>;
}; };
} // namespace caosdb::connection } // namespace caosdb::connection
#endif #endif
...@@ -36,6 +36,9 @@ public: ...@@ -36,6 +36,9 @@ public:
: runtime_error(what_arg) {} : runtime_error(what_arg) {}
}; };
/**
* @brief The connection to the CaosDB server is down.
*/
class ConnectionError : public runtime_error { class ConnectionError : public runtime_error {
public: public:
explicit ConnectionError(const std::string &what_arg) explicit ConnectionError(const std::string &what_arg)
......
/*
* This file is a part of the CaosDB Project.
*
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef CAOSDB_INFO_H
#define CAOSDB_INFO_H
/**
* @file caosdb/info.h
* @author Timm Fitschen
* @date 2021-07-02
* @brief General information about the CaosDBServer.
*/
#include <cstdint> // for uint32_t
#include <string> // for string
#include "caosdb/info/v1alpha1/main.pb.h" // for VersionInfo
namespace caosdb::info {
using ProtoVersionInfo = caosdb::info::v1alpha1::VersionInfo;
/**
* @brief Wrapper class for the VersionInfo protobuf.
*/
class VersionInfo {
private:
ProtoVersionInfo *info;
public:
explicit VersionInfo(ProtoVersionInfo *info);
[[nodiscard]] auto GetMajor() const -> uint32_t;
[[nodiscard]] auto GetMinor() const -> uint32_t;
[[nodiscard]] auto GetPatch() const -> uint32_t;
[[nodiscard]] auto GetPreRelease() const -> const std::string &;
[[nodiscard]] auto GetBuild() const -> const std::string &;
};
} // namespace caosdb::info
#endif
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
set(libcaosdb_SRC set(libcaosdb_SRC
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.cpp ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.cpp
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.cpp ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/info.cpp
) )
# pass variable to parent scope # pass variable to parent scope
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "caosdb/info/v1alpha1/main.grpc.pb.h" // for GeneralInfoService #include "caosdb/info/v1alpha1/main.grpc.pb.h" // for GeneralInfoService
#include "caosdb/info/v1alpha1/main.pb.h" // for GetVersionInfoResp... #include "caosdb/info/v1alpha1/main.pb.h" // for GetVersionInfoResp...
#include "caosdb/utils.h" // for load_string_file #include "caosdb/utils.h" // for load_string_file
#include "caosdb/info.h" // for VersionInfo
#include "grpcpp/impl/codegen/status_code_enum.h" // for StatusCode, UNAUTH... #include "grpcpp/impl/codegen/status_code_enum.h" // for StatusCode, UNAUTH...
namespace grpc { namespace grpc {
class Channel; class Channel;
...@@ -41,10 +42,10 @@ namespace caosdb::connection { ...@@ -41,10 +42,10 @@ namespace caosdb::connection {
using caosdb::authentication::Authenticator; using caosdb::authentication::Authenticator;
using caosdb::exceptions::AuthenticationError; using caosdb::exceptions::AuthenticationError;
using caosdb::exceptions::ConnectionError; using caosdb::exceptions::ConnectionError;
using caosdb::info::VersionInfo;
using caosdb::info::v1alpha1::GeneralInfoService; using caosdb::info::v1alpha1::GeneralInfoService;
using caosdb::info::v1alpha1::GetVersionInfoRequest; using caosdb::info::v1alpha1::GetVersionInfoRequest;
using caosdb::info::v1alpha1::GetVersionInfoResponse; using caosdb::info::v1alpha1::GetVersionInfoResponse;
using caosdb::info::v1alpha1::VersionInfo;
using caosdb::utils::load_string_file; using caosdb::utils::load_string_file;
using grpc::InsecureChannelCredentials; using grpc::InsecureChannelCredentials;
using grpc::SslCredentials; using grpc::SslCredentials;
...@@ -87,7 +88,15 @@ auto operator<<(std::ostream &out, const CaosDBConnectionConfig &config) ...@@ -87,7 +88,15 @@ auto operator<<(std::ostream &out, const CaosDBConnectionConfig &config)
InsecureCaosDBConnectionConfig::InsecureCaosDBConnectionConfig( InsecureCaosDBConnectionConfig::InsecureCaosDBConnectionConfig(
const std::string &host, int port) const std::string &host, int port)
: CaosDBConnectionConfig(host, port) { : CaosDBConnectionConfig(host, port) {
this->credentials = grpc::InsecureChannelCredentials(); this->credentials = InsecureChannelCredentials();
}
InsecureCaosDBConnectionConfig::InsecureCaosDBConnectionConfig(
const std::string &host, int port,
const std::shared_ptr<Authenticator> &authenticator)
: CaosDBConnectionConfig(host, port) {
this->credentials = grpc::CompositeChannelCredentials(
InsecureChannelCredentials(), authenticator->getCallCredentials());
} }
auto InsecureCaosDBConnectionConfig::getChannelCredentials() const auto InsecureCaosDBConnectionConfig::getChannelCredentials() const
...@@ -106,6 +115,7 @@ SslCaosDBConnectionConfig::SslCaosDBConnectionConfig( ...@@ -106,6 +115,7 @@ SslCaosDBConnectionConfig::SslCaosDBConnectionConfig(
: CaosDBConnectionConfig(host, port) { : CaosDBConnectionConfig(host, port) {
SslCredentialsOptions options; SslCredentialsOptions options;
options.pem_root_certs = cacert->getCACertPem(); options.pem_root_certs = cacert->getCACertPem();
std::cout << "HERE" << options.pem_root_certs;
this->credentials = SslCredentials(options); this->credentials = SslCredentials(options);
} }
...@@ -147,13 +157,9 @@ auto operator<<(std::ostream &out, const CaosDBConnection &connection) ...@@ -147,13 +157,9 @@ auto operator<<(std::ostream &out, const CaosDBConnection &connection)
return out; return out;
} }
auto CaosDBConnection::getGeneralInfoService() const [[nodiscard]] auto CaosDBConnection::getVersionInfo() const
-> GeneralInfoService::Stub & { -> std::unique_ptr<VersionInfo> {
return *stub_; GetVersionInfoRequest request;
}
[[nodiscard]] auto CaosDBConnection::getVersionInfo() const -> VersionInfo {
const GetVersionInfoRequest request;
GetVersionInfoResponse response; GetVersionInfoResponse response;
grpc::ClientContext context; grpc::ClientContext context;
const grpc::Status status = const grpc::Status status =
...@@ -170,8 +176,7 @@ auto CaosDBConnection::getGeneralInfoService() const ...@@ -170,8 +176,7 @@ auto CaosDBConnection::getGeneralInfoService() const
throw std::runtime_error(status.error_message()); throw std::runtime_error(status.error_message());
} }
} }
VersionInfo result(response.version_info()); return std::make_unique<VersionInfo>(response.release_version_info());
return result;
} }
} // namespace caosdb::connection } // namespace caosdb::connection
/*
* This file is a part of the CaosDB Project.
*
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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, see <https://www.gnu.org/licenses/>.
*
*/
#include "caosdb/info.h"
#include "caosdb/info/v1alpha1/main.pb.h" // for GetVersionInfoResponse
namespace caosdb::info {
using ProtoVersionInfo = caosdb::info::v1alpha1::VersionInfo;
VersionInfo::VersionInfo(ProtoVersionInfo *info) { this->info = info; }
[[nodiscard]] auto VersionInfo::GetMajor() const -> uint32_t {
return this->info->major();
}
[[nodiscard]] auto VersionInfo::GetMinor() const -> uint32_t {
return this->info->minor();
}
[[nodiscard]] auto VersionInfo::GetPatch() const -> uint32_t {
return this->info->patch();
}
[[nodiscard]] auto VersionInfo::GetPreRelease() const -> const std::string & {
return this->info->pre_release();
}
[[nodiscard]] auto VersionInfo::GetBuild() const -> const std::string & {
return this->info->build();
}
} // namespace caosdb::info
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
#include "caosdb/info/v1alpha1/main.pb.h"
#include "caosdb/constants.h" #include "caosdb/constants.h"
#include "caosdb/connection.h" #include "caosdb/connection.h"
#include "caosdb/authentication.h" #include "caosdb/authentication.h"
#include "caosdb/utils.h" #include "caosdb/utils.h"
#include "caosdb/info.h"
auto main() -> int { auto main() -> int {
...@@ -46,6 +46,9 @@ auto main() -> int { ...@@ -46,6 +46,9 @@ auto main() -> int {
const auto port = std::stoi(port_str); const auto port = std::stoi(port_str);
const auto user = caosdb::utils::get_env_var("CAOSDB_USER", "admin"); const auto user = caosdb::utils::get_env_var("CAOSDB_USER", "admin");
const auto password = caosdb::utils::get_env_var("CAOSDB_PASSWORD", "caosdb"); const auto password = caosdb::utils::get_env_var("CAOSDB_PASSWORD", "caosdb");
std::cout << "USER: " << user << "\n";
std::cout << "PORT: " << port_str << "\n";
std::cout << "HOST: " << host << "\n";
auto auth = auto auth =
std::make_shared<caosdb::authentication::PlainPasswordAuthenticator>( std::make_shared<caosdb::authentication::PlainPasswordAuthenticator>(
...@@ -53,13 +56,12 @@ auto main() -> int { ...@@ -53,13 +56,12 @@ auto main() -> int {
auto cacert = auto cacert =
std::make_shared<caosdb::connection::PemFileCACertProvider>(pem_file); std::make_shared<caosdb::connection::PemFileCACertProvider>(pem_file);
auto config = std::make_shared<caosdb::connection::SslCaosDBConnectionConfig>( auto config = std::make_shared<caosdb::connection::SslCaosDBConnectionConfig>(
host, port, cacert, auth); host, port, cacert);
caosdb::connection::CaosDBConnection connection(config); caosdb::connection::CaosDBConnection connection(config);
std::cout << std::endl << connection << std::endl; std::cout << std::endl << connection << std::endl;
const auto &v_info = connection.getVersionInfo(); const auto &v_info = connection.getVersionInfo();
// const auto &build = v_info.build(); std::cout << "VersionInfo(" << v_info->GetMajor() << "." << v_info->GetMinor()
// std::cout << "BUILD(" << build[0] << ")" << std::endl; << "." << v_info->GetPatch() << "-" << v_info->GetPreRelease()
std::cout << "VersionInfo(" << v_info.major() << "." << v_info.minor() << "." << "-" << v_info->GetBuild() << ")" << std::endl;
<< v_info.patch() << ")" << std::endl;
return 0; return 0;
} }
...@@ -22,8 +22,23 @@ ...@@ -22,8 +22,23 @@
set(test_cases set(test_cases
test_connection test_connection
test_utils test_utils
test_info
) )
###################################################
### Set up tests using GoogleTest (GTest)
###################################################
# special linting for tests
set(_CMAKE_CXX_CLANG_TIDY_TEST_CHECKS
"${_CMAKE_CXX_CLANG_TIDY_CHECKS},-cert-err58-cpp,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-modernize-use-trailing-return-type,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers"
)
# supress warnings during build of gtest
cmake_policy(SET CMP0054 NEW)
enable_testing()
# add special cmake functions for gtest # add special cmake functions for gtest
include(GoogleTest) include(GoogleTest)
...@@ -33,17 +48,30 @@ math(EXPR len_test_cases "${len_test_cases} - 1") ...@@ -33,17 +48,30 @@ math(EXPR len_test_cases "${len_test_cases} - 1")
foreach (i RANGE "${len_test_cases}") foreach (i RANGE "${len_test_cases}")
list(GET test_cases ${i} test_case_name) list(GET test_cases ${i} test_case_name)
add_executable(${test_case_name} ${test_case_name}.cpp) add_executable(${test_case_name} ${test_case_name}.cpp)
target_link_libraries(${test_case_name} PRIVATE caosdb ${CONAN_LIBS_GTEST} ${CONAN_LIBS_BOOST}) set(libcaosdb_TEST_SRC "${CMAKE_CURRENT_SOURCE_DIR}/${test_case_name}.cpp
set_target_properties(${test_case_name} PROPERTIES CXX_CLANG_TIDY "") ${libcaosdb_TEST_SRC}")
target_link_libraries(${test_case_name} PRIVATE caosdb
caosdb_info_v1alpha1 ${CONAN_LIBS_GTEST} ${CONAN_LIBS_BOOST})
if(_LINTING)
set_target_properties(${test_case_name}
PROPERTIES
CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY};${_CMAKE_CXX_CLANG_TIDY_TEST_CHECKS}"
CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}")
endif()
gtest_discover_tests(${test_case_name} gtest_discover_tests(${test_case_name}
PROPERTIES PROPERTIES
LABELS "caosdb-cpplib-unit-tests") LABELS "caosdb-cpplib-unit-tests")
endforeach () endforeach ()
# code coverage report
###################################################
### Set up test coverage repor (Gcov + Lcov)
###################################################
include(CodeCoverage) include(CodeCoverage)
if (LCOV_PATH) if (LCOV_PATH)
target_link_libraries(caosdb gcov)
setup_target_for_coverage_lcov( setup_target_for_coverage_lcov(
NAME unit_test_coverage NAME unit_test_coverage
EXECUTABLE ctest -L caosdb-cpplib-unit-tests EXECUTABLE ctest -L caosdb-cpplib-unit-tests
...@@ -55,4 +83,3 @@ if (LCOV_PATH) ...@@ -55,4 +83,3 @@ if (LCOV_PATH)
else () else ()
message(WARNING "Could not generate code coverage report. Please install lcov.") message(WARNING "Could not generate code coverage report. Please install lcov.")
endif () endif ()
...@@ -20,32 +20,36 @@ ...@@ -20,32 +20,36 @@
* *
*/ */
#include "caosdb/connection.h" #include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-message.h> #include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver
#include <gtest/gtest-test-part.h> #include <memory> // for allocator, operator!=, shared_ptr
#include <memory> #include "caosdb/connection.h" // for PemCACertProvider, InsecureCaosDB...
#include <type_traits> #include "gtest/gtest_pred_impl.h" // for Test, AssertionResult, EXPECT_EQ
#include "gtest/gtest_pred_impl.h" namespace grpc {
class ChannelCredentials;
}
namespace caosdb::connection {
TEST(test_connection, configure_insecure_localhost_8080) { TEST(test_connection, configure_insecure_localhost_8080) {
caosdb::connection::InsecureCaosDBConnectionConfig config("localhost", 8000); InsecureCaosDBConnectionConfig config("localhost", 8000);
ASSERT_EQ("localhost", config.getHost()); EXPECT_EQ("localhost", config.getHost());
ASSERT_EQ(8000, config.getPort()); EXPECT_EQ(8000, config.getPort());
std::shared_ptr<grpc::ChannelCredentials> icc = std::shared_ptr<grpc::ChannelCredentials> icc =
config.getChannelCredentials(); config.getChannelCredentials();
ASSERT_TRUE(icc != nullptr); EXPECT_TRUE(icc != nullptr);
} }
TEST(test_connection, configure_ssl_localhost_8080) { TEST(test_connection, configure_ssl_localhost_8080) {
auto cacert = auto cacert = std::make_shared<PemCACertProvider>("ca chain");
std::make_shared<caosdb::connection::PemCACertProvider>("ca chain"); SslCaosDBConnectionConfig config("localhost", 44300, cacert);
caosdb::connection::SslCaosDBConnectionConfig config("localhost", 44300,
cacert);
ASSERT_EQ("localhost", config.getHost()); EXPECT_EQ("localhost", config.getHost());
ASSERT_EQ(44300, config.getPort()); EXPECT_EQ(44300, config.getPort());
std::shared_ptr<grpc::ChannelCredentials> sslcc = std::shared_ptr<grpc::ChannelCredentials> sslcc =
config.getChannelCredentials(); config.getChannelCredentials();
ASSERT_TRUE(sslcc != nullptr); EXPECT_TRUE(sslcc != nullptr);
} }
} // namespace caosdb::connection
/*
*
* This file is a part of the CaosDB Project.
*
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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, see <https://www.gnu.org/licenses/>.
*
*/
#include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiRe...
#include <memory> // for allocator
#include "caosdb/info.h" // for VersionInfo
#include "caosdb/info/v1alpha1/main.pb.h" // for VersionInfo
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST
namespace caosdb::info {
using ProtoVersionInfo = caosdb::info::v1alpha1::VersionInfo;
TEST(test_info, create_info_from_proto_info) {
auto *origial = new ProtoVersionInfo();
origial->set_major(12);
origial->set_pre_release("SNAPSHOT");
VersionInfo wrapper(origial);
EXPECT_EQ(12, wrapper.GetMajor());
EXPECT_EQ("SNAPSHOT", wrapper.GetPreRelease());
// default value.
EXPECT_EQ(0, wrapper.GetMinor());
}
} // namespace caosdb::info
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.1)
project(PackageTest CXX) project(PackageTest CXX)
#######################################################################
### append test cases here (file name without the ".cpp" suffix)
#######################################################################
set(test_cases
test_info
)
###################################################
### Set up tests using GoogleTest (GTest)
###################################################
# dependencies
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup() conan_basic_setup()
add_executable(example example.cpp) # supress warnings during build of gtest
target_link_libraries(example ${CONAN_LIBS}) cmake_policy(SET CMP0054 NEW)
enable_testing()
# add special cmake functions for gtest
include(GoogleTest REQUIRED)
# CTest is a testing tool that can be used to test your project. # loop over all test cases and add them to the test runner
# enable_testing() list(LENGTH test_cases len_test_cases)
# add_test(NAME example math(EXPR len_test_cases "${len_test_cases} - 1")
# WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin foreach (i RANGE "${len_test_cases}")
# COMMAND example) list(GET test_cases ${i} test_case_name)
add_executable(${test_case_name} ${test_case_name}.cpp)
target_link_libraries(${test_case_name} PRIVATE ${CONAN_LIBS_CAOSDB}
${CONAN_LIBS_GTEST} ${CONAN_LIBS_GRPC} ${CONAN_LIBS_ABSEIL}
${CONAN_LIBS_OPENSSL} ${CONAN_LIBS_C-ARES} ${CONAN_LIBS_BZIP2}
${CONAN_LIBS_PROTOBUF} ${CONAN_LIBS_ZLIB})
target_include_directories(${test_case_name} PUBLIC ${CONAN_INCLUDE_DIRS})
set_target_properties(${test_case_name}
PROPERTIES
CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY}"
CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}")
gtest_discover_tests(${test_case_name}
PROPERTIES
LABELS "caosdb-cpplib-int-tests")
endforeach ()
...@@ -21,5 +21,5 @@ class LibcaosdbTestConan(ConanFile): ...@@ -21,5 +21,5 @@ class LibcaosdbTestConan(ConanFile):
def test(self): def test(self):
if not tools.cross_building(self): if not tools.cross_building(self):
os.chdir("bin") cmake = CMake(self)
self.run(".%sexample" % os.sep) cmake.test()
#include <iostream>
#include "caosdb/connection.h"
int main() {
caosdb::connection::InsecureCaosDBConnectionConfig config("localhost", 8443);
std::cout << config << "\n";
}
../test/test_info.cpp
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment