From c65a769b147537fcc47fea70f943d808c4c803e6 Mon Sep 17 00:00:00 2001 From: Ji Bin Date: Wed, 22 Mar 2023 15:20:06 +0800 Subject: [PATCH] Support GetVersion Fixes: #220 Signed-off-by: Ji Bin --- cmake/CPM.cmake | 33 --------------- cmake/ThirdPartyPackages.cmake | 62 +++++++++++++++++++---------- src/impl/MilvusClientImpl.cpp | 13 ++++++ src/impl/MilvusClientImpl.h | 3 ++ src/impl/MilvusConnection.cpp | 6 +++ src/impl/MilvusConnection.h | 3 ++ src/include/milvus/MilvusClient.h | 10 +++++ test/CMakeLists.txt | 15 +++---- test/it/TestGetVersion.cpp | 42 +++++++++++++++++++ test/it/mocks/MilvusMockedService.h | 3 ++ test/st/TestGeneric.cpp | 34 ++++++++++++++++ 11 files changed, 162 insertions(+), 62 deletions(-) delete mode 100644 cmake/CPM.cmake create mode 100644 test/it/TestGetVersion.cpp create mode 100644 test/st/TestGeneric.cpp diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake deleted file mode 100644 index a3086b7..0000000 --- a/cmake/CPM.cmake +++ /dev/null @@ -1,33 +0,0 @@ -set(CPM_DOWNLOAD_VERSION 0.38.1) - -if(CPM_SOURCE_CACHE) - set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") -elseif(DEFINED ENV{CPM_SOURCE_CACHE}) - set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") -else() - set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") -endif() - -# Expand relative path. This is important if the provided path contains a tilde (~) -get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) - -function(download_cpm) - message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") - file(DOWNLOAD - https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake - ${CPM_DOWNLOAD_LOCATION} - ) -endfunction() - -if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) - download_cpm() -else() - # resume download if it previously failed - file(READ ${CPM_DOWNLOAD_LOCATION} check) - if("${check}" STREQUAL "") - download_cpm() - endif() - unset(check) -endif() - -include(${CPM_DOWNLOAD_LOCATION}) diff --git a/cmake/ThirdPartyPackages.cmake b/cmake/ThirdPartyPackages.cmake index 085a969..596f2a4 100644 --- a/cmake/ThirdPartyPackages.cmake +++ b/cmake/ThirdPartyPackages.cmake @@ -16,7 +16,33 @@ include_guard(GLOBAL) -include(CPM) +include(FetchContent) + +set(GRPC_VERSION 1.49.1) +set(NLOHMANN_JSON_VERSION 3.11.2) +set(GOOGLETEST_VERSION 1.12.1) + +# grpc +FetchContent_Declare( + grpc + GIT_REPOSITORY https://github.com/grpc/grpc.git + GIT_TAG v${GRPC_VERSION} +) + +# nlohmann_json +FetchContent_Declare( + nlohmann_json + GIT_REPOSITORY https://github.com/nlohmann/json.git + GIT_TAG v${NLOHMANN_JSON_VERSION} +) + +# googletest +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-${GOOGLETEST_VERSION} +) + # grpc if ("${MILVUS_WITH_GRPC}" STREQUAL "pakcage") @@ -27,20 +53,17 @@ else () else () set(OPENSSL_NO_ASM_TXT "NO") endif () - CPMAddPackage( - NAME grpc - VERSION 1.49.1 - GITHUB_REPOSITORY grpc/grpc - EXCLUDE_FROM_ALL YES - OPTIONS - "gRPC_SSL_PROVIDER module" - "gRPC_PROTOBUF_PROVIDER module" - "gRPC_BUILD_TESTS OFF" - "RE2_BUILD_TESTING OFF" - "ABSL_PROPAGATE_CXX_STD ON" - "OPENSSL_NO_ASM ${OPENSSL_NO_ASM_TXT}" - ) - if (grpc_ADDED) + if (NOT grpc_POPULATED) + FetchContent_Populate(grpc) + if (WIN32) + set(OPENSSL_NO_ASM YES CACHE INTERNAL "") + endif() + set(gRPC_SSL_PROVIDER "module" CACHE INTERNAL "") + set(gRPC_PROTOBUF_PROVIDER "module" CACHE INTERNAL "") + set(gRPC_BUILD_TESTS OFF CACHE INTERNAL "") + set(RE2_BUILD_TESTING OFF CACHE INTERNAL "") + set(ABSL_PROPAGATE_CXX_STD ON CACHE INTERNAL "") + add_subdirectory(${grpc_SOURCE_DIR} ${grpc_BINARY_DIR} EXCLUDE_FROM_ALL) add_library(gRPC::grpc++ ALIAS grpc++) endif () endif () @@ -50,9 +73,8 @@ endif () if ("${MILVUS_WITH_NLOHMANN_JSON}" STREQUAL "package") find_package(nlohmann_json REQUIRED) else () - CPMAddPackage( - NAME nlohmann_json - VERSION 3.11.2 - GITHUB_REPOSITORY nlohmann/json - ) + if (NOT nlohmann_json_POPULATED) + FetchContent_Populate(nlohmann_json) + add_subdirectory(${nlohmann_json_SOURCE_DIR} ${nlohmann_json_BINARY_DIR} EXCLUDE_FROM_ALL) + endif () endif () diff --git a/src/impl/MilvusClientImpl.cpp b/src/impl/MilvusClientImpl.cpp index 8a0e904..b23d53b 100644 --- a/src/impl/MilvusClientImpl.cpp +++ b/src/impl/MilvusClientImpl.cpp @@ -57,6 +57,19 @@ MilvusClientImpl::Disconnect() { return Status::OK(); } +Status +MilvusClientImpl::GetVersion(std::string& version) { + auto pre = []() { + proto::milvus::GetVersionRequest rpc_request; + return rpc_request; + }; + + auto post = [&version](const proto::milvus::GetVersionResponse& response) { version = response.version(); }; + + return apiHandler( + pre, &MilvusConnection::GetVersion, post); +} + Status MilvusClientImpl::CreateCollection(const CollectionSchema& schema) { auto pre = [&schema]() { diff --git a/src/impl/MilvusClientImpl.h b/src/impl/MilvusClientImpl.h index c6be0a4..2ebe01e 100644 --- a/src/impl/MilvusClientImpl.h +++ b/src/impl/MilvusClientImpl.h @@ -37,6 +37,9 @@ class MilvusClientImpl : public MilvusClient { Status Disconnect() final; + Status + GetVersion(std::string& version) final; + Status CreateCollection(const CollectionSchema& schema) final; diff --git a/src/impl/MilvusConnection.cpp b/src/impl/MilvusConnection.cpp index 189dbda..c7b232c 100644 --- a/src/impl/MilvusConnection.cpp +++ b/src/impl/MilvusConnection.cpp @@ -98,6 +98,12 @@ MilvusConnection::Disconnect() { return Status::OK(); } +Status +MilvusConnection::GetVersion(const proto::milvus::GetVersionRequest& request, + proto::milvus::GetVersionResponse& response, const GrpcContextOptions& options) { + return grpcCall("GetVersion", &Stub::GetVersion, request, response, options); +} + Status MilvusConnection::CreateCollection(const proto::milvus::CreateCollectionRequest& request, proto::common::Status& response, const GrpcContextOptions& options) { diff --git a/src/impl/MilvusConnection.h b/src/impl/MilvusConnection.h index 988af65..c63345c 100644 --- a/src/impl/MilvusConnection.h +++ b/src/impl/MilvusConnection.h @@ -60,6 +60,9 @@ class MilvusConnection { Status Disconnect(); + Status + GetVersion(const proto::milvus::GetVersionRequest& request, proto::milvus::GetVersionResponse& response, + const GrpcContextOptions& options); Status CreateCollection(const proto::milvus::CreateCollectionRequest& request, proto::common::Status& response, const GrpcContextOptions& options); diff --git a/src/include/milvus/MilvusClient.h b/src/include/milvus/MilvusClient.h index abd82f1..3d1d458 100644 --- a/src/include/milvus/MilvusClient.h +++ b/src/include/milvus/MilvusClient.h @@ -77,6 +77,16 @@ class MilvusClient { virtual Status Disconnect() = 0; + /** + * Get milvus server version + * + * @param [out] version version string + * @return Status operation successfully or not + * + */ + virtual Status + GetVersion(std::string& version) = 0; + /** * Create a collection with schema. * diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e32f7aa..6eff3ad 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -22,15 +22,12 @@ include_directories(${milvus_proto_BINARY_DIR}) if ("${MILVUS_WITH_GTEST}" STREQUAL "pakcage") find_package(GTest REQUIRED) else () - CPMAddPackage( - NAME googletest - GIT_TAG release-1.12.1 - VERSION 1.12.1 - GITHUB_REPOSITORY google/googletest - OPTIONS - "INSTALL_GTEST OFF" - "gtest_force_shared_crt ON" - ) + if (NOT googletest_POPULATED) + FetchContent_Populate(googletest) + set(gtest_force_shared_crt ON CACHE INTERNAL "") + set(INSTALL_GTEST OFF CACHE INTERNAL "") + add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL) + endif () endif () set(GTEST_LIBRARIES gtest) diff --git a/test/it/TestGetVersion.cpp b/test/it/TestGetVersion.cpp new file mode 100644 index 0000000..df50d24 --- /dev/null +++ b/test/it/TestGetVersion.cpp @@ -0,0 +1,42 @@ +// Licensed to the LF AI & Data foundation under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "milvus.pb.h" +#include "mocks/MilvusMockedTest.h" + +using ::milvus::StatusCode; +using ::milvus::proto::milvus::GetVersionRequest; +using ::testing::_; +using ::testing::Property; + +TEST_F(MilvusMockedTest, GetVersionFoo) { + milvus::ConnectParam connect_param{"127.0.0.1", server_.ListenPort()}; + client_->Connect(connect_param); + + EXPECT_CALL(service_, GetVersion(_, _, _)) + .WillOnce([](::grpc::ServerContext*, const GetVersionRequest*, + ::milvus::proto::milvus::GetVersionResponse* response) { + response->set_version("2.0.0"); + return ::grpc::Status{}; + }); + std::string version; + auto status = client_->GetVersion(version); + + EXPECT_TRUE(status.IsOk()); + EXPECT_EQ(version, "2.0.0"); +} diff --git a/test/it/mocks/MilvusMockedService.h b/test/it/mocks/MilvusMockedService.h index c24db9a..8a63089 100644 --- a/test/it/mocks/MilvusMockedService.h +++ b/test/it/mocks/MilvusMockedService.h @@ -17,10 +17,13 @@ #include #include "milvus.grpc.pb.h" +#include "milvus.pb.h" namespace milvus { class MilvusMockedService : public ::milvus::proto::milvus::MilvusService::Service { public: + MOCK_METHOD3(GetVersion, ::grpc::Status(::grpc::ServerContext*, const ::milvus::proto::milvus::GetVersionRequest*, + ::milvus::proto::milvus::GetVersionResponse*)); MOCK_METHOD3(CreateCollection, ::grpc::Status(::grpc::ServerContext*, const ::milvus::proto::milvus::CreateCollectionRequest*, ::milvus::proto::common::Status*)); diff --git a/test/st/TestGeneric.cpp b/test/st/TestGeneric.cpp new file mode 100644 index 0000000..6432397 --- /dev/null +++ b/test/st/TestGeneric.cpp @@ -0,0 +1,34 @@ +// Licensed to the LF AI & Data foundation under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "MilvusServerTest.h" +#include "gmock/gmock.h" + +using milvus::test::MilvusServerTest; + +class MilvusServerTestGeneric : public MilvusServerTest {}; + +TEST_F(MilvusServerTestGeneric, GetVersion) { + milvus::ConnectParam connect_param{"127.0.0.1", server_.ListenPort()}; + client_->Connect(connect_param); + + std::string version; + auto status = client_->GetVersion(version); + EXPECT_TRUE(status.IsOk()); + EXPECT_THAT(version, testing::StartsWith("v2.")); +}