From bc856fdd2ba43ff4519e4c9bb72d9c71e7005871 Mon Sep 17 00:00:00 2001 From: yhmo Date: Wed, 8 Jan 2025 17:45:02 +0800 Subject: [PATCH] Test ci Signed-off-by: yhmo --- .clang-tidy | 2 + scripts/run_clang_format.py | 2 +- src/impl/MilvusConnection.h | 1 - test/CMakeLists.txt | 2 +- test/st/Main.cpp | 77 +++++++++++++++++++++ test/st/MilvusServerTest.h | 33 ++------- test/st/PythonMilvusServer.cpp | 43 ++++++++++-- test/st/TestCollection.cpp | 2 - test/st/TestConnectWithTls.cpp | 91 +++---------------------- test/st/TestConnectWithUser.cpp | 31 +++------ test/st/TestGeneric.cpp | 3 - test/st/TestSearch.cpp | 12 ---- test/st/TestSearchWithBinaryVectors.cpp | 12 ---- 13 files changed, 146 insertions(+), 165 deletions(-) create mode 100644 test/st/Main.cpp diff --git a/.clang-tidy b/.clang-tidy index adf53df..931bb81 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -16,9 +16,11 @@ # under the License. # The checks defined here will be run and will display by default as warnings. +# Note: disable clang-analyzer-core.CallAndMessage, clang-analyzer-core.uninitialized.Assign, because the code could not pass the checker under Ubuntu 20.04 Checks: > -*, clang-diagnostic-*, -clang-diagnostic-error, clang-analyzer-*, -clang-analyzer-alpha*, -clang-diagnostic-deprecated*, + -clang-analyzer-core.CallAndMessage, -clang-analyzer-core.uninitialized.Assign, google-*, -google-runtime-references, -google-readability-todo, -google-default-arguments, modernize-*, -modernize-pass-by-value, -modernize-use-equals-default, -modernize-use-trailing-return-type, performance-faster-string-find, performance-for-range-copy, diff --git a/scripts/run_clang_format.py b/scripts/run_clang_format.py index ba0e87f..5cdd512 100644 --- a/scripts/run_clang_format.py +++ b/scripts/run_clang_format.py @@ -130,7 +130,7 @@ def _check_one_file(completed_processes, filename): print(file=sys.stderr) diff_out = [] for diff_str in diff: - diff_out.append(diff_str.encode('raw_unicode_escape')) + diff_out.append(diff_str) sys.stderr.writelines(diff_out) except Exception: error = True diff --git a/src/impl/MilvusConnection.h b/src/impl/MilvusConnection.h index 5f275df..4ccf4b9 100644 --- a/src/impl/MilvusConnection.h +++ b/src/impl/MilvusConnection.h @@ -290,5 +290,4 @@ class MilvusConnection { return StatusByProtoResponse(response); } }; - } // namespace milvus diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 71021bb..5f18c37 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -49,5 +49,5 @@ target_link_libraries(testing-it PRIVATE milvus_sdk ${GTEST_LIBRARIES} ${GMOCK_L if (CMAKE_SYSTEM_NAME MATCHES "(Linux|Darwin)") aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/st st_files) add_executable(testing-st ${st_files}) -target_link_libraries(testing-st PRIVATE milvus_sdk ${GTEST_LIBRARIES} ${GMOCK_LIBRARIES} ${GTEST_MAIN_LIBRARIES}) +target_link_libraries(testing-st PRIVATE milvus_sdk ${GTEST_LIBRARIES} ${GMOCK_LIBRARIES}) endif() diff --git a/test/st/Main.cpp b/test/st/Main.cpp new file mode 100644 index 0000000..07eb793 --- /dev/null +++ b/test/st/Main.cpp @@ -0,0 +1,77 @@ +#include + +#include "PythonMilvusServer.h" +#include "milvus/MilvusClient.h" +#include "milvus/Status.h" + +inline void +waitMilvusServerReady(const milvus::test::PythonMilvusServer& server) { + int max_retry = 60, retry = 0; + bool has; + + auto client = milvus::MilvusClient::Create(); + auto param = server.TestClientParam(); + client->Connect(*param); + auto status = client->HasCollection("no_such", has); + + while (!status.IsOk() && retry++ < max_retry) { + std::this_thread::sleep_for(std::chrono::seconds{5}); + client = milvus::MilvusClient::Create(); + client->Connect(*param); + status = client->HasCollection("no_such", has); + std::cout << "Wait milvus start done, try: " << retry << ", status: " << status.Message() << std::endl; + } + std::cout << "Wait milvus start done, status: " << status.Message() << std::endl; +} + +std::shared_ptr s_connectParam; + +int +main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + + // tls mode + { + std::cout << "======== Test TLS mode ========" << std::endl; + + std::array path{}; + getcwd(path.data(), path.size()); + std::string pwd = path.data(); + + milvus::test::PythonMilvusServer server_{}; + server_.SetTls(2, pwd + "/certs/server.crt", pwd + "/certs/server.key", pwd + "/certs/ca.crt"); + // server_.SetAuthorizationEnabled(true); + + server_.Start(); + waitMilvusServerReady(server_); + s_connectParam = server_.TestClientParam(); + + ::testing::GTEST_FLAG(filter) = "-MilvusServerTestWithAuth.*"; + int result = RUN_ALL_TESTS(); + if (result > 0) { + return result; + } + server_.Stop(); + } + + // auth mode + { + std::cout << "======== Test Auth mode ========" << std::endl; + + milvus::test::PythonMilvusServer server_{}; + server_.SetAuthorizationEnabled(true); + + server_.Start(); + waitMilvusServerReady(server_); + s_connectParam = server_.TestClientParam(); + + ::testing::GTEST_FLAG(filter) = "-MilvusServerTestWithTlsMode.*"; + int result = RUN_ALL_TESTS(); + if (result > 0) { + return result; + } + server_.Stop(); + } + + return 0; +} diff --git a/test/st/MilvusServerTest.h b/test/st/MilvusServerTest.h index 11d8f5f..9917804 100644 --- a/test/st/MilvusServerTest.h +++ b/test/st/MilvusServerTest.h @@ -24,63 +24,42 @@ #include "PythonMilvusServer.h" #include "milvus/MilvusClient.h" -#include "milvus/Status.h" + +extern std::shared_ptr s_connectParam; namespace milvus { namespace test { -inline void -waitMilvusServerReady(const PythonMilvusServer& server) { - int max_retry = 60, retry = 0; - bool has; - - auto client = milvus::MilvusClient::Create(); - auto param = server.TestClientParam(); - client->Connect(*param); - auto status = client->HasCollection("no_such", has); - - while (!status.IsOk() && retry++ < max_retry) { - std::this_thread::sleep_for(std::chrono::seconds{5}); - client = milvus::MilvusClient::Create(); - client->Connect(*param); - status = client->HasCollection("no_such", has); - std::cout << "Wait milvus start done, try: " << retry << ", status: " << status.Message() << std::endl; - } - std::cout << "Wait milvus start done, status: " << status.Message() << std::endl; -} - class MilvusServerTest : public ::testing::Test { protected: - PythonMilvusServer server_{}; std::shared_ptr client_{nullptr}; void SetUp() override { - server_.Start(); client_ = milvus::MilvusClient::Create(); - waitMilvusServerReady(server_); + client_->Connect(*s_connectParam); } void TearDown() override { + client_->Disconnect(); } }; template class MilvusServerTestWithParam : public ::testing::TestWithParam { protected: - PythonMilvusServer server_{}; std::shared_ptr client_{nullptr}; void SetUp() override { - server_.Start(); client_ = milvus::MilvusClient::Create(); - waitMilvusServerReady(server_); + client_->Connect(*s_connectParam); } void TearDown() override { + client_->Disconnect(); } }; } // namespace test diff --git a/test/st/PythonMilvusServer.cpp b/test/st/PythonMilvusServer.cpp index 78f356f..280dde6 100644 --- a/test/st/PythonMilvusServer.cpp +++ b/test/st/PythonMilvusServer.cpp @@ -29,11 +29,46 @@ namespace milvus { namespace test { + +static void +generate_certificates() { + std::system("mkdir -p certs"); + std::system("openssl genrsa -out certs/ca.key 2048"); + std::system( + "openssl req -new" + " -key certs/ca.key" + " -subj /C=CN/ST=Zhejiang/L=Hangzhou/O=Milvus/OU=CppSdk/CN=ca.test.com" + " -out certs/ca.csr"); + std::system( + "openssl x509 -req" + " -days 365" + " -in certs/ca.csr" + " -signkey certs/ca.key" + " -out certs/ca.crt"); + for (const auto& name : {"server", "client"}) { + std::system((std::string("openssl genrsa -out certs/") + name + ".key 2048").c_str()); + std::system((std::string("openssl req -new -key certs/") + name + + ".key" + " -subj /C=CN/ST=Zhejiang/L=Hangzhou/O=Milvus/OU=CppSdk/CN=" + + name + + ".test.com" + " -out certs/" + + name + ".csr") + .c_str()); + std::system((std::string("openssl x509 -req -days 365 -in certs/") + name + + ".csr" + " -CA certs/ca.crt -CAkey certs/ca.key -CAcreateserial" + " -out certs/" + + name + ".crt") + .c_str()); + } + std::system("echo generate certifications"); +} + // using 2.3.x latest const char* kPythonMilvusServerVersion = "milvus~=2.3.0"; PythonMilvusServer::~PythonMilvusServer() noexcept { - Stop(); } void @@ -86,6 +121,8 @@ PythonMilvusServer::run() { } if (tls_mode_ != 0) { + generate_certificates(); + cmd += " --tls-mode " + std::to_string(tls_mode_); cmd += " --server-pem-path " + server_cert_; cmd += " --server-key-path " + server_key_; @@ -120,9 +157,7 @@ PythonMilvusServer::TestClientParam() const { if (tls_mode_ == 1) { param->EnableTls(server, pwd + "/certs/ca.crt"); - } - - else if (tls_mode_ == 2) { + } else if (tls_mode_ == 2) { param->EnableTls(server, pwd + "/certs/client.crt", pwd + "/certs/client.key", pwd + "/certs/ca.crt"); } } diff --git a/test/st/TestCollection.cpp b/test/st/TestCollection.cpp index e960ad3..d46837b 100644 --- a/test/st/TestCollection.cpp +++ b/test/st/TestCollection.cpp @@ -22,8 +22,6 @@ using MilvusServerTestCollection = MilvusServerTestWithParam; TEST_P(MilvusServerTestCollection, CreateAndDeleteCollection) { auto using_string_primary_key = GetParam(); - milvus::ConnectParam connect_param{"127.0.0.1", server_.ListenPort()}; - client_->Connect(connect_param); milvus::CollectionSchema collection_schema("Foo"); if (using_string_primary_key) { diff --git a/test/st/TestConnectWithTls.cpp b/test/st/TestConnectWithTls.cpp index 134d3c1..a1044dc 100644 --- a/test/st/TestConnectWithTls.cpp +++ b/test/st/TestConnectWithTls.cpp @@ -26,94 +26,23 @@ using testing::UnorderedElementsAre; using testing::UnorderedElementsAreArray; -static void -generate_certificates() { - std::system("mkdir -p certs"); - std::system("openssl genrsa -out certs/ca.key 2048"); - std::system( - "openssl req -new" - " -key certs/ca.key" - " -subj /C=CN/ST=Zhejiang/L=Hangzhou/O=Milvus/OU=CppSdk/CN=ca.test.com" - " -out certs/ca.csr"); - std::system( - "openssl x509 -req" - " -days 365" - " -in certs/ca.csr" - " -signkey certs/ca.key" - " -out certs/ca.crt"); - for (const auto& name : {"server", "client"}) { - std::system((std::string("openssl genrsa -out certs/") + name + ".key 2048").c_str()); - std::system((std::string("openssl req -new -key certs/") + name + - ".key" - " -subj /C=CN/ST=Zhejiang/L=Hangzhou/O=Milvus/OU=CppSdk/CN=" + - name + - ".test.com" - " -out certs/" + - name + ".csr") - .c_str()); - std::system((std::string("openssl x509 -req -days 365 -in certs/") + name + - ".csr" - " -CA certs/ca.crt -CAkey certs/ca.key -CAcreateserial" - " -out certs/" + - name + ".crt") - .c_str()); - } -} - using milvus::test::MilvusServerTest; -template -class MilvusServerTestWithTlsMode : public MilvusServerTest { - protected: - std::shared_ptr ssl_client_; - - void - SetUp() override { - generate_certificates(); - std::array path{}; - getcwd(path.data(), path.size()); - std::string pwd = path.data(); - server_.SetTls(Mode, pwd + "/certs/server.crt", pwd + "/certs/server.key", pwd + "/certs/ca.crt"); - - MilvusServerTest::SetUp(); - - ssl_client_ = milvus::MilvusClient::Create(); - auto param = server_.TestClientParam(); - ssl_client_->Connect(*param); - } - - void - TearDown() override { - MilvusServerTest::TearDown(); - std::system("rm -fr certs/"); - } -}; - -class MilvusServerTestWithTlsMode1 : public MilvusServerTestWithTlsMode<1> {}; -// TODO: fix it with milvus2.3+tls2 -class DISABLED_MilvusServerTestWithTlsMode2 : public MilvusServerTestWithTlsMode<2> {}; - -TEST_F(MilvusServerTestWithTlsMode1, GenericTest) { - bool has; - auto status = ssl_client_->HasCollection("nosuchcollection", has); - EXPECT_TRUE(status.IsOk()); - status = client_->HasCollection("nosuchcollection", has); - EXPECT_FALSE(status.IsOk()); - EXPECT_EQ(status.Code(), milvus::StatusCode::NOT_CONNECTED); -} +class MilvusServerTestWithTlsMode : public MilvusServerTest {}; -TEST_F(DISABLED_MilvusServerTestWithTlsMode2, GenericTest) { +TEST_F(MilvusServerTestWithTlsMode, GenericTest) { bool has; - auto status = ssl_client_->HasCollection("nosuchcollection", has); + auto status = client_->HasCollection("nosuchcollection", has); EXPECT_TRUE(status.IsOk()); - status = client_->HasCollection("nosuchcollection", has); - EXPECT_FALSE(status.IsOk()); - EXPECT_EQ(status.Code(), milvus::StatusCode::NOT_CONNECTED); + EXPECT_FALSE(has); - milvus::ConnectParam param{"127.0.0.1", 300}; + // client without certifications + milvus::ConnectParam param{"127.0.0.1", 19530}; param.EnableTls(); - client_->Connect(param); - status = client_->HasCollection("nosuchcollection", has); + std::shared_ptr tempClient = milvus::MilvusClient::Create(); + status = tempClient->Connect(param); + EXPECT_FALSE(status.IsOk()); + status = tempClient->HasCollection("nosuchcollection", has); EXPECT_FALSE(status.IsOk()); EXPECT_EQ(status.Code(), milvus::StatusCode::NOT_CONNECTED); } diff --git a/test/st/TestConnectWithUser.cpp b/test/st/TestConnectWithUser.cpp index 9c63aeb..92dd535 100644 --- a/test/st/TestConnectWithUser.cpp +++ b/test/st/TestConnectWithUser.cpp @@ -24,30 +24,19 @@ using milvus::test::MilvusServerTest; using testing::UnorderedElementsAre; using testing::UnorderedElementsAreArray; -class MilvusServerTestWithAuth : public MilvusServerTest { - protected: - std::shared_ptr root_client_; - - void - SetUp() override { - server_.SetAuthorizationEnabled(true); - MilvusServerTest::SetUp(); - root_client_ = milvus::MilvusClient::Create(); - root_client_->Connect({"127.0.0.1", server_.ListenPort(), "root", "Milvus"}); - } - - void - TearDown() override { - MilvusServerTest::TearDown(); - } -}; +class MilvusServerTestWithAuth : public MilvusServerTest {}; TEST_F(MilvusServerTestWithAuth, GenericTest) { bool has; - auto status = root_client_->HasCollection("nosuchcollection", has); + auto status = client_->HasCollection("nosuchcollection", has); EXPECT_TRUE(status.IsOk()); - // orig client with out user/pass - status = client_->HasCollection("nosuchcollection", has); + EXPECT_FALSE(has); + + // client without user/pass + milvus::ConnectParam param{"127.0.0.1", 19530}; + std::shared_ptr tempClient = milvus::MilvusClient::Create(); + status = tempClient->Connect(param); + status = tempClient->HasCollection("nosuchcollection", has); EXPECT_FALSE(status.IsOk()); - EXPECT_EQ(status.Code(), milvus::StatusCode::NOT_CONNECTED); + EXPECT_NE(status.Code(), milvus::StatusCode::OK); } diff --git a/test/st/TestGeneric.cpp b/test/st/TestGeneric.cpp index 6432397..1399b33 100644 --- a/test/st/TestGeneric.cpp +++ b/test/st/TestGeneric.cpp @@ -24,9 +24,6 @@ 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()); diff --git a/test/st/TestSearch.cpp b/test/st/TestSearch.cpp index c41406a..b0a7f2a 100644 --- a/test/st/TestSearch.cpp +++ b/test/st/TestSearch.cpp @@ -27,18 +27,6 @@ class MilvusServerTestSearch : public MilvusServerTest { std::string collection_name{"Foo"}; std::string partition_name{"Bar"}; - void - SetUp() override { - MilvusServerTest::SetUp(); - milvus::ConnectParam connect_param{"127.0.0.1", server_.ListenPort()}; - client_->Connect(connect_param); - } - - void - TearDown() override { - MilvusServerTest::TearDown(); - } - void createCollectionAndPartitions(bool create_flat_index) { milvus::CollectionSchema collection_schema(collection_name); diff --git a/test/st/TestSearchWithBinaryVectors.cpp b/test/st/TestSearchWithBinaryVectors.cpp index 17a2d35..ac8c16f 100644 --- a/test/st/TestSearchWithBinaryVectors.cpp +++ b/test/st/TestSearchWithBinaryVectors.cpp @@ -27,18 +27,6 @@ class MilvusServerTestSearchWithBinaryVectors : public MilvusServerTest { std::string collection_name{"Foo"}; std::string partition_name{"Bar"}; - void - SetUp() override { - MilvusServerTest::SetUp(); - milvus::ConnectParam connect_param{"127.0.0.1", server_.ListenPort()}; - client_->Connect(connect_param); - } - - void - TearDown() override { - MilvusServerTest::TearDown(); - } - void createCollectionAndPartitions() { milvus::CollectionSchema collection_schema(collection_name);