From 5b08678f30bbd24fd4df80ad147b93fb90c1c7e2 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 22 Dec 2021 18:39:32 +0800 Subject: [PATCH] TD-10431 refact dnode test --- source/dnode/mgmt/impl/test/CMakeLists.txt | 10 +- .../dnode/mgmt/impl/test/dnode/CMakeLists.txt | 22 +- source/dnode/mgmt/impl/test/dnode/dnode.cpp | 334 +++++------------ .../mgmt/impl/test/profile/CMakeLists.txt | 22 +- .../dnode/mgmt/impl/test/profile/profile.cpp | 344 ++++-------------- .../dnode/mgmt/impl/test/stb/CMakeLists.txt | 22 +- source/dnode/mgmt/impl/test/stb/stb.cpp | 246 ++----------- .../dnode/mgmt/impl/test/user/CMakeLists.txt | 22 +- source/dnode/mgmt/impl/test/user/user.cpp | 253 +++---------- .../mgmt/impl/test/vgroup/CMakeLists.txt | 22 +- source/dnode/mgmt/impl/test/vgroup/vgroup.cpp | 192 +--------- 11 files changed, 296 insertions(+), 1193 deletions(-) diff --git a/source/dnode/mgmt/impl/test/CMakeLists.txt b/source/dnode/mgmt/impl/test/CMakeLists.txt index aabc4e7f4e..a29926c802 100644 --- a/source/dnode/mgmt/impl/test/CMakeLists.txt +++ b/source/dnode/mgmt/impl/test/CMakeLists.txt @@ -5,16 +5,16 @@ add_subdirectory(acct) # add_subdirectory(balance) add_subdirectory(cluster) add_subdirectory(db) -#add_subdirectory(dnode) +add_subdirectory(dnode) # add_subdirectory(func) # add_subdirectory(mnode) -#add_subdirectory(profile) +add_subdirectory(profile) add_subdirectory(show) -#add_subdirectory(stb) +add_subdirectory(stb) # add_subdirectory(sync) # add_subdirectory(telem) # add_subdirectory(trans) -#add_subdirectory(user) -#add_subdirectory(vgroup) +add_subdirectory(user) +add_subdirectory(vgroup) add_subdirectory(sut) diff --git a/source/dnode/mgmt/impl/test/dnode/CMakeLists.txt b/source/dnode/mgmt/impl/test/dnode/CMakeLists.txt index 48b74a4eff..5796590865 100644 --- a/source/dnode/mgmt/impl/test/dnode/CMakeLists.txt +++ b/source/dnode/mgmt/impl/test/dnode/CMakeLists.txt @@ -1,24 +1,8 @@ -add_executable(dnode_test_dnode "") - -target_sources(dnode_test_dnode - PRIVATE - "dnode.cpp" - "../sut/deploy.cpp" -) - +aux_source_directory(. DTEST_SRC) +add_executable(dnode_test_dnode ${DTEST_SRC}) target_link_libraries( dnode_test_dnode - PUBLIC dnode - PUBLIC util - PUBLIC os - PUBLIC gtest_main -) - -target_include_directories(dnode_test_dnode - PUBLIC - "${CMAKE_SOURCE_DIR}/include/server/dnode/mgmt" - "${CMAKE_CURRENT_SOURCE_DIR}/../../inc" - "${CMAKE_CURRENT_SOURCE_DIR}/../sut" + PUBLIC sut ) add_test( diff --git a/source/dnode/mgmt/impl/test/dnode/dnode.cpp b/source/dnode/mgmt/impl/test/dnode/dnode.cpp index 1fa91f26cb..c9708ec1cf 100644 --- a/source/dnode/mgmt/impl/test/dnode/dnode.cpp +++ b/source/dnode/mgmt/impl/test/dnode/dnode.cpp @@ -9,189 +9,62 @@ * */ -#include "deploy.h" +#include "base.h" class DndTestDnode : public ::testing::Test { - public: - static SServer* CreateServer(const char* path, const char* fqdn, uint16_t port, const char* firstEp) { - SServer* pServer = createServer(path, fqdn, port, firstEp); - ASSERT(pServer); - return pServer; - } - - static void SetUpTestSuite() { - initLog("/tmp/tdlog"); - - const char* fqdn = "localhost"; - const char* firstEp = "localhost:9041"; - pServer1 = CreateServer("/tmp/dnode_test_dnode1", fqdn, 9041, firstEp); - pServer2 = CreateServer("/tmp/dnode_test_dnode2", fqdn, 9042, firstEp); - pServer3 = CreateServer("/tmp/dnode_test_dnode3", fqdn, 9043, firstEp); - pServer4 = CreateServer("/tmp/dnode_test_dnode4", fqdn, 9044, firstEp); - pServer5 = CreateServer("/tmp/dnode_test_dnode5", fqdn, 9045, firstEp); - pClient = createClient("root", "taosdata", fqdn, 9041); - taosMsleep(300); - } - - static void TearDownTestSuite() { - stopServer(pServer1); - stopServer(pServer2); - stopServer(pServer3); - stopServer(pServer4); - stopServer(pServer5); - dropClient(pClient); - pServer1 = NULL; - pServer2 = NULL; - pServer3 = NULL; - pServer4 = NULL; - pServer5 = NULL; - pClient = NULL; - } - - static SServer* pServer1; - static SServer* pServer2; - static SServer* pServer3; - static SServer* pServer4; - static SServer* pServer5; - static SClient* pClient; - public: void SetUp() override {} void TearDown() override {} - void SendTheCheckShowMetaMsg(int8_t showType, const char* showName, int32_t columns) { - SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg)); - pShow->type = showType; - strcpy(pShow->db, ""); + public: + static void SetUpTestSuite() { + test.Init("/tmp/dnode_test_dnode1", 9041); + const char* fqdn = "localhost"; + const char* firstEp = "localhost:9041"; - SRpcMsg showRpcMsg = {0}; - showRpcMsg.pCont = pShow; - showRpcMsg.contLen = sizeof(SShowMsg); - showRpcMsg.msgType = TSDB_MSG_TYPE_SHOW; - - sendMsg(pClient, &showRpcMsg); - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - SShowRsp* pShowRsp = (SShowRsp*)pClient->pRsp->pCont; - ASSERT_NE(pShowRsp, nullptr); - pShowRsp->showId = htonl(pShowRsp->showId); - pMeta = &pShowRsp->tableMeta; - pMeta->numOfTags = htonl(pMeta->numOfTags); - pMeta->numOfColumns = htonl(pMeta->numOfColumns); - pMeta->sversion = htonl(pMeta->sversion); - pMeta->tversion = htonl(pMeta->tversion); - pMeta->tuid = htobe64(pMeta->tuid); - pMeta->suid = htobe64(pMeta->suid); - - showId = pShowRsp->showId; - - EXPECT_NE(pShowRsp->showId, 0); - EXPECT_STREQ(pMeta->tbFname, showName); - EXPECT_EQ(pMeta->numOfTags, 0); - EXPECT_EQ(pMeta->numOfColumns, columns); - EXPECT_EQ(pMeta->precision, 0); - EXPECT_EQ(pMeta->tableType, 0); - EXPECT_EQ(pMeta->update, 0); - EXPECT_EQ(pMeta->sversion, 0); - EXPECT_EQ(pMeta->tversion, 0); - EXPECT_EQ(pMeta->tuid, 0); - EXPECT_EQ(pMeta->suid, 0); + server2.Start("/tmp/dnode_test_dnode2", fqdn, 9042, firstEp); + server3.Start("/tmp/dnode_test_dnode3", fqdn, 9043, firstEp); + server4.Start("/tmp/dnode_test_dnode4", fqdn, 9044, firstEp); + server5.Start("/tmp/dnode_test_dnode5", fqdn, 9045, firstEp); + taosMsleep(300); } - void CheckSchema(int32_t index, int8_t type, int32_t bytes, const char* name) { - SSchema* pSchema = &pMeta->pSchema[index]; - pSchema->bytes = htonl(pSchema->bytes); - EXPECT_EQ(pSchema->colId, 0); - EXPECT_EQ(pSchema->type, type); - EXPECT_EQ(pSchema->bytes, bytes); - EXPECT_STREQ(pSchema->name, name); + static void TearDownTestSuite() { + server2.Stop(); + server3.Stop(); + server4.Stop(); + server5.Stop(); + test.Cleanup(); } - void SendThenCheckShowRetrieveMsg(int32_t rows) { - SRetrieveTableMsg* pRetrieve = (SRetrieveTableMsg*)rpcMallocCont(sizeof(SRetrieveTableMsg)); - pRetrieve->showId = htonl(showId); - pRetrieve->free = 0; - - SRpcMsg retrieveRpcMsg = {0}; - retrieveRpcMsg.pCont = pRetrieve; - retrieveRpcMsg.contLen = sizeof(SRetrieveTableMsg); - retrieveRpcMsg.msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE; - - sendMsg(pClient, &retrieveRpcMsg); - - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - pRetrieveRsp = (SRetrieveTableRsp*)pClient->pRsp->pCont; - ASSERT_NE(pRetrieveRsp, nullptr); - pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows); - pRetrieveRsp->useconds = htobe64(pRetrieveRsp->useconds); - pRetrieveRsp->compLen = htonl(pRetrieveRsp->compLen); - - EXPECT_EQ(pRetrieveRsp->numOfRows, rows); - EXPECT_EQ(pRetrieveRsp->useconds, 0); - // EXPECT_EQ(pRetrieveRsp->completed, completed); - EXPECT_EQ(pRetrieveRsp->precision, TSDB_TIME_PRECISION_MILLI); - EXPECT_EQ(pRetrieveRsp->compressed, 0); - EXPECT_EQ(pRetrieveRsp->compLen, 0); - - pData = pRetrieveRsp->data; - pos = 0; - } - - void CheckInt16(int16_t val) { - int16_t data = *((int16_t*)(pData + pos)); - pos += sizeof(int16_t); - EXPECT_EQ(data, val); - } - - void CheckInt64(int64_t val) { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_EQ(data, val); - } - - void CheckTimestamp() { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_GT(data, 0); - } - - void CheckBinary(const char* val, int32_t len) { - pos += sizeof(VarDataLenT); - char* data = (char*)(pData + pos); - pos += len; - EXPECT_STREQ(data, val); - } - - int32_t showId; - STableMetaMsg* pMeta; - SRetrieveTableRsp* pRetrieveRsp; - char* pData; - int32_t pos; + static Testbase test; + static TestServer server2; + static TestServer server3; + static TestServer server4; + static TestServer server5; }; -SServer* DndTestDnode::pServer1; -SServer* DndTestDnode::pServer2; -SServer* DndTestDnode::pServer3; -SServer* DndTestDnode::pServer4; -SServer* DndTestDnode::pServer5; -SClient* DndTestDnode::pClient; +Testbase DndTestDnode::test; +TestServer DndTestDnode::server2; +TestServer DndTestDnode::server3; +TestServer DndTestDnode::server4; +TestServer DndTestDnode::server5; TEST_F(DndTestDnode, 01_ShowDnode) { - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_DNODE, "show dnodes", 7); - CheckSchema(0, TSDB_DATA_TYPE_SMALLINT, 2, "id"); - CheckSchema(1, TSDB_DATA_TYPE_BINARY, TSDB_EP_LEN + VARSTR_HEADER_SIZE, "endpoint"); - CheckSchema(2, TSDB_DATA_TYPE_SMALLINT, 2, "vnodes"); - CheckSchema(3, TSDB_DATA_TYPE_SMALLINT, 2, "max_vnodes"); - CheckSchema(4, TSDB_DATA_TYPE_BINARY, 10 + VARSTR_HEADER_SIZE, "status"); - CheckSchema(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time"); - CheckSchema(6, TSDB_DATA_TYPE_BINARY, 24 + VARSTR_HEADER_SIZE, "offline_reason"); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_DNODE, ""); + CHECK_META("show dnodes", 7); + + CHECK_SCHEMA(0, TSDB_DATA_TYPE_SMALLINT, 2, "id"); + CHECK_SCHEMA(1, TSDB_DATA_TYPE_BINARY, TSDB_EP_LEN + VARSTR_HEADER_SIZE, "endpoint"); + CHECK_SCHEMA(2, TSDB_DATA_TYPE_SMALLINT, 2, "vnodes"); + CHECK_SCHEMA(3, TSDB_DATA_TYPE_SMALLINT, 2, "max_vnodes"); + CHECK_SCHEMA(4, TSDB_DATA_TYPE_BINARY, 10 + VARSTR_HEADER_SIZE, "status"); + CHECK_SCHEMA(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time"); + CHECK_SCHEMA(6, TSDB_DATA_TYPE_BINARY, 24 + VARSTR_HEADER_SIZE, "offline_reason"); + + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 1); - SendThenCheckShowRetrieveMsg(1); CheckInt16(1); CheckBinary("localhost:9041", TSDB_EP_LEN); CheckInt16(0); @@ -202,40 +75,36 @@ TEST_F(DndTestDnode, 01_ShowDnode) { } TEST_F(DndTestDnode, 02_ConfigDnode) { - SCfgDnodeMsg* pReq = (SCfgDnodeMsg*)rpcMallocCont(sizeof(SCfgDnodeMsg)); + int32_t contLen = sizeof(SCfgDnodeMsg); + + SCfgDnodeMsg* pReq = (SCfgDnodeMsg*)rpcMallocCont(contLen); pReq->dnodeId = htonl(1); strcpy(pReq->config, "ddebugflag 131"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCfgDnodeMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CONFIG_DNODE; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CONFIG_DNODE, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } TEST_F(DndTestDnode, 03_Create_Drop_Restart_Dnode) { { - SCreateDnodeMsg* pReq = (SCreateDnodeMsg*)rpcMallocCont(sizeof(SCreateDnodeMsg)); + int32_t contLen = sizeof(SCreateDnodeMsg); + + SCreateDnodeMsg* pReq = (SCreateDnodeMsg*)rpcMallocCont(contLen); strcpy(pReq->ep, "localhost:9042"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateDnodeMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_DNODE; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_DNODE, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } taosMsleep(1300); - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_DNODE, "show dnodes", 7); - SendThenCheckShowRetrieveMsg(2); + + test.SendShowMetaMsg(TSDB_MGMT_TABLE_DNODE, ""); + CHECK_META("show dnodes", 7); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 2); + CheckInt16(1); CheckInt16(2); CheckBinary("localhost:9041", TSDB_EP_LEN); @@ -252,22 +121,21 @@ TEST_F(DndTestDnode, 03_Create_Drop_Restart_Dnode) { CheckBinary("", 24); { - SDropDnodeMsg* pReq = (SDropDnodeMsg*)rpcMallocCont(sizeof(SDropDnodeMsg)); + int32_t contLen = sizeof(SDropDnodeMsg); + + SDropDnodeMsg* pReq = (SDropDnodeMsg*)rpcMallocCont(contLen); pReq->dnodeId = htonl(2); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SDropDnodeMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_DROP_DNODE; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_DROP_DNODE, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_DNODE, "show dnodes", 7); - SendThenCheckShowRetrieveMsg(1); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_DNODE, ""); + CHECK_META("show dnodes", 7); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 1); + CheckInt16(1); CheckBinary("localhost:9041", TSDB_EP_LEN); CheckInt16(0); @@ -277,53 +145,44 @@ TEST_F(DndTestDnode, 03_Create_Drop_Restart_Dnode) { CheckBinary("", 24); { - SCreateDnodeMsg* pReq = (SCreateDnodeMsg*)rpcMallocCont(sizeof(SCreateDnodeMsg)); + int32_t contLen = sizeof(SCreateDnodeMsg); + + SCreateDnodeMsg* pReq = (SCreateDnodeMsg*)rpcMallocCont(contLen); strcpy(pReq->ep, "localhost:9043"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateDnodeMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_DNODE; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_DNODE, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } { - SCreateDnodeMsg* pReq = (SCreateDnodeMsg*)rpcMallocCont(sizeof(SCreateDnodeMsg)); + int32_t contLen = sizeof(SCreateDnodeMsg); + + SCreateDnodeMsg* pReq = (SCreateDnodeMsg*)rpcMallocCont(contLen); strcpy(pReq->ep, "localhost:9044"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateDnodeMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_DNODE; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_DNODE, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } { - SCreateDnodeMsg* pReq = (SCreateDnodeMsg*)rpcMallocCont(sizeof(SCreateDnodeMsg)); + int32_t contLen = sizeof(SCreateDnodeMsg); + + SCreateDnodeMsg* pReq = (SCreateDnodeMsg*)rpcMallocCont(contLen); strcpy(pReq->ep, "localhost:9045"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateDnodeMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_DNODE; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_DNODE, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } taosMsleep(1300); - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_DNODE, "show dnodes", 7); - SendThenCheckShowRetrieveMsg(4); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_DNODE, ""); + CHECK_META("show dnodes", 7); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 4); + CheckInt16(1); CheckInt16(3); CheckInt16(4); @@ -355,31 +214,18 @@ TEST_F(DndTestDnode, 03_Create_Drop_Restart_Dnode) { // restart uInfo("stop all server"); - stopServer(pServer1); - stopServer(pServer2); - stopServer(pServer3); - stopServer(pServer4); - stopServer(pServer5); - pServer1 = NULL; - pServer2 = NULL; - pServer3 = NULL; - pServer4 = NULL; - pServer5 = NULL; - - uInfo("start all server"); - - const char* fqdn = "localhost"; - const char* firstEp = "localhost:9041"; - pServer1 = startServer("/tmp/dnode_test_dnode1", fqdn, 9041, firstEp); - pServer3 = startServer("/tmp/dnode_test_dnode3", fqdn, 9043, firstEp); - pServer4 = startServer("/tmp/dnode_test_dnode4", fqdn, 9044, firstEp); - pServer5 = startServer("/tmp/dnode_test_dnode5", fqdn, 9045, firstEp); - - uInfo("all server is running"); + test.Restart(); + server2.Restart(); + server3.Restart(); + server4.Restart(); + server5.Restart(); taosMsleep(1300); - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_DNODE, "show dnodes", 7); - SendThenCheckShowRetrieveMsg(4); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_DNODE, ""); + CHECK_META("show dnodes", 7); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 4); + CheckInt16(1); CheckInt16(3); CheckInt16(4); diff --git a/source/dnode/mgmt/impl/test/profile/CMakeLists.txt b/source/dnode/mgmt/impl/test/profile/CMakeLists.txt index b74709b775..0edd631720 100644 --- a/source/dnode/mgmt/impl/test/profile/CMakeLists.txt +++ b/source/dnode/mgmt/impl/test/profile/CMakeLists.txt @@ -1,24 +1,8 @@ -add_executable(dnode_test_profile "") - -target_sources(dnode_test_profile - PRIVATE - "profile.cpp" - "../sut/deploy.cpp" -) - +aux_source_directory(. PROFILE_SRC) +add_executable(dnode_test_profile ${PROFILE_SRC}) target_link_libraries( dnode_test_profile - PUBLIC dnode - PUBLIC util - PUBLIC os - PUBLIC gtest_main -) - -target_include_directories(dnode_test_profile - PUBLIC - "${CMAKE_SOURCE_DIR}/include/server/dnode/mgmt" - "${CMAKE_CURRENT_SOURCE_DIR}/../../inc" - "${CMAKE_CURRENT_SOURCE_DIR}/../sut" + PUBLIC sut ) add_test( diff --git a/source/dnode/mgmt/impl/test/profile/profile.cpp b/source/dnode/mgmt/impl/test/profile/profile.cpp index c21bd93765..2e82d4d1aa 100644 --- a/source/dnode/mgmt/impl/test/profile/profile.cpp +++ b/source/dnode/mgmt/impl/test/profile/profile.cpp @@ -9,188 +9,35 @@ * */ -#include "deploy.h" +#include "base.h" class DndTestProfile : public ::testing::Test { protected: - static SServer* CreateServer(const char* path, const char* fqdn, uint16_t port, const char* firstEp) { - SServer* pServer = createServer(path, fqdn, port, firstEp); - ASSERT(pServer); - return pServer; - } + static void SetUpTestSuite() { test.Init("/tmp/dnode_test_profile", 9080); } + static void TearDownTestSuite() { test.Cleanup(); } - static void SetUpTestSuite() { - initLog("/tmp/tdlog"); - - const char* fqdn = "localhost"; - const char* firstEp = "localhost:9080"; - pServer = CreateServer("/tmp/dnode_test_profile", fqdn, 9080, firstEp); - pClient = createClient("root", "taosdata", fqdn, 9080); - taosMsleep(300); - } - - static void TearDownTestSuite() { - stopServer(pServer); - dropClient(pClient); - pServer = NULL; - pClient = NULL; - } - - static SServer* pServer; - static SClient* pClient; - static int32_t connId; + static Testbase test; public: void SetUp() override {} void TearDown() override {} - void SendTheCheckShowMetaMsg(int8_t showType, const char* showName, int32_t columns) { - SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg)); - pShow->type = showType; - strcpy(pShow->db, ""); - - SRpcMsg showRpcMsg = {0}; - showRpcMsg.pCont = pShow; - showRpcMsg.contLen = sizeof(SShowMsg); - showRpcMsg.msgType = TSDB_MSG_TYPE_SHOW; - - sendMsg(pClient, &showRpcMsg); - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - SShowRsp* pShowRsp = (SShowRsp*)pClient->pRsp->pCont; - ASSERT_NE(pShowRsp, nullptr); - pShowRsp->showId = htonl(pShowRsp->showId); - pMeta = &pShowRsp->tableMeta; - pMeta->numOfTags = htonl(pMeta->numOfTags); - pMeta->numOfColumns = htonl(pMeta->numOfColumns); - pMeta->sversion = htonl(pMeta->sversion); - pMeta->tversion = htonl(pMeta->tversion); - pMeta->tuid = htobe64(pMeta->tuid); - pMeta->suid = htobe64(pMeta->suid); - - showId = pShowRsp->showId; - - EXPECT_NE(pShowRsp->showId, 0); - EXPECT_STREQ(pMeta->tbFname, showName); - EXPECT_EQ(pMeta->numOfTags, 0); - EXPECT_EQ(pMeta->numOfColumns, columns); - EXPECT_EQ(pMeta->precision, 0); - EXPECT_EQ(pMeta->tableType, 0); - EXPECT_EQ(pMeta->update, 0); - EXPECT_EQ(pMeta->sversion, 0); - EXPECT_EQ(pMeta->tversion, 0); - EXPECT_EQ(pMeta->tuid, 0); - EXPECT_EQ(pMeta->suid, 0); - } - - void CheckSchema(int32_t index, int8_t type, int32_t bytes, const char* name) { - SSchema* pSchema = &pMeta->pSchema[index]; - pSchema->bytes = htonl(pSchema->bytes); - EXPECT_EQ(pSchema->colId, 0); - EXPECT_EQ(pSchema->type, type); - EXPECT_EQ(pSchema->bytes, bytes); - EXPECT_STREQ(pSchema->name, name); - } - - void SendThenCheckShowRetrieveMsg(int32_t rows) { - SRetrieveTableMsg* pRetrieve = (SRetrieveTableMsg*)rpcMallocCont(sizeof(SRetrieveTableMsg)); - pRetrieve->showId = htonl(showId); - pRetrieve->free = 0; - - SRpcMsg retrieveRpcMsg = {0}; - retrieveRpcMsg.pCont = pRetrieve; - retrieveRpcMsg.contLen = sizeof(SRetrieveTableMsg); - retrieveRpcMsg.msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE; - - sendMsg(pClient, &retrieveRpcMsg); - - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - pRetrieveRsp = (SRetrieveTableRsp*)pClient->pRsp->pCont; - ASSERT_NE(pRetrieveRsp, nullptr); - pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows); - pRetrieveRsp->useconds = htobe64(pRetrieveRsp->useconds); - pRetrieveRsp->compLen = htonl(pRetrieveRsp->compLen); - - EXPECT_EQ(pRetrieveRsp->numOfRows, rows); - EXPECT_EQ(pRetrieveRsp->useconds, 0); - // EXPECT_EQ(pRetrieveRsp->completed, completed); - EXPECT_EQ(pRetrieveRsp->precision, TSDB_TIME_PRECISION_MILLI); - EXPECT_EQ(pRetrieveRsp->compressed, 0); - EXPECT_EQ(pRetrieveRsp->compLen, 0); - - pData = pRetrieveRsp->data; - pos = 0; - } - - void CheckInt16(int16_t val) { - int16_t data = *((int16_t*)(pData + pos)); - pos += sizeof(int16_t); - EXPECT_EQ(data, val); - } - - void CheckInt32(int32_t val) { - int32_t data = *((int32_t*)(pData + pos)); - pos += sizeof(int32_t); - EXPECT_EQ(data, val); - } - - void CheckInt64(int64_t val) { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_EQ(data, val); - } - - void CheckTimestamp() { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_GT(data, 0); - } - - void CheckBinary(const char* val, int32_t len) { - pos += sizeof(VarDataLenT); - char* data = (char*)(pData + pos); - pos += len; - EXPECT_STREQ(data, val); - } - - void IgnoreBinary(int32_t len) { - pos += sizeof(VarDataLenT); - char* data = (char*)(pData + pos); - pos += len; - } - - int32_t showId; - STableMetaMsg* pMeta; - SRetrieveTableRsp* pRetrieveRsp; - char* pData; - int32_t pos; + int32_t connId; }; -SServer* DndTestProfile::pServer; -SClient* DndTestProfile::pClient; -int32_t DndTestProfile::connId; +Testbase DndTestProfile::test; TEST_F(DndTestProfile, 01_ConnectMsg) { - ASSERT_NE(pClient, nullptr); + int32_t contLen = sizeof(SConnectMsg); - SConnectMsg* pReq = (SConnectMsg*)rpcMallocCont(sizeof(SConnectMsg)); + SConnectMsg* pReq = (SConnectMsg*)rpcMallocCont(contLen); pReq->pid = htonl(1234); strcpy(pReq->app, "dnode_test_profile"); strcpy(pReq->db, ""); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SConnectMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CONNECT; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CONNECT, pReq, contLen); ASSERT_NE(pMsg, nullptr); + ASSERT_EQ(pMsg->code, 0); SConnectRsp* pRsp = (SConnectRsp*)pMsg->pCont; ASSERT_NE(pRsp, nullptr); @@ -213,37 +60,32 @@ TEST_F(DndTestProfile, 01_ConnectMsg) { } TEST_F(DndTestProfile, 02_ConnectMsg_InvalidDB) { - ASSERT_NE(pClient, nullptr); + int32_t contLen = sizeof(SConnectMsg); - SConnectMsg* pReq = (SConnectMsg*)rpcMallocCont(sizeof(SConnectMsg)); + SConnectMsg* pReq = (SConnectMsg*)rpcMallocCont(contLen); pReq->pid = htonl(1234); strcpy(pReq->app, "dnode_test_profile"); strcpy(pReq->db, "invalid_db"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SConnectMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CONNECT; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CONNECT, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_DB); ASSERT_EQ(pMsg->contLen, 0); } TEST_F(DndTestProfile, 03_ConnectMsg_Show) { - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_CONNS, "show connections", 7); - CheckSchema(0, TSDB_DATA_TYPE_INT, 4, "connId"); - CheckSchema(1, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "user"); - CheckSchema(2, TSDB_DATA_TYPE_BINARY, TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE, "program"); - CheckSchema(3, TSDB_DATA_TYPE_INT, 4, "pid"); - CheckSchema(4, TSDB_DATA_TYPE_BINARY, TSDB_IPv4ADDR_LEN + 6 + VARSTR_HEADER_SIZE, "ip:port"); - CheckSchema(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "login_time"); - CheckSchema(6, TSDB_DATA_TYPE_TIMESTAMP, 8, "last_access"); - - SendThenCheckShowRetrieveMsg(1); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_CONNS, ""); + CHECK_META("show connections", 7); + CHECK_SCHEMA(0, TSDB_DATA_TYPE_INT, 4, "connId"); + CHECK_SCHEMA(1, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "user"); + CHECK_SCHEMA(2, TSDB_DATA_TYPE_BINARY, TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE, "program"); + CHECK_SCHEMA(3, TSDB_DATA_TYPE_INT, 4, "pid"); + CHECK_SCHEMA(4, TSDB_DATA_TYPE_BINARY, TSDB_IPv4ADDR_LEN + 6 + VARSTR_HEADER_SIZE, "ip:port"); + CHECK_SCHEMA(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "login_time"); + CHECK_SCHEMA(6, TSDB_DATA_TYPE_TIMESTAMP, 8, "last_access"); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 1); CheckInt32(1); CheckBinary("root", TSDB_USER_LEN); CheckBinary("dnode_test_profile", TSDB_APP_NAME_LEN); @@ -254,23 +96,18 @@ TEST_F(DndTestProfile, 03_ConnectMsg_Show) { } TEST_F(DndTestProfile, 04_HeartBeatMsg) { - ASSERT_NE(pClient, nullptr); + int32_t contLen = sizeof(SHeartBeatMsg); - SHeartBeatMsg* pReq = (SHeartBeatMsg*)rpcMallocCont(sizeof(SHeartBeatMsg)); + SHeartBeatMsg* pReq = (SHeartBeatMsg*)rpcMallocCont(contLen); pReq->connId = htonl(connId); pReq->pid = htonl(1234); pReq->numOfQueries = htonl(0); pReq->numOfStreams = htonl(0); strcpy(pReq->app, "dnode_test_profile"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SHeartBeatMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_HEARTBEAT; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_HEARTBEAT, pReq, contLen); ASSERT_NE(pMsg, nullptr); + ASSERT_EQ(pMsg->code, 0); SHeartBeatRsp* pRsp = (SHeartBeatRsp*)pMsg->pCont; ASSERT_NE(pRsp, nullptr); @@ -295,57 +132,44 @@ TEST_F(DndTestProfile, 04_HeartBeatMsg) { } TEST_F(DndTestProfile, 05_KillConnMsg) { - ASSERT_NE(pClient, nullptr); - { - SKillConnMsg* pReq = (SKillConnMsg*)rpcMallocCont(sizeof(SKillConnMsg)); + int32_t contLen = sizeof(SKillConnMsg); + + SKillConnMsg* pReq = (SKillConnMsg*)rpcMallocCont(contLen); pReq->connId = htonl(connId); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SKillConnMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_KILL_CONN; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_KILL_CONN, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } { - SHeartBeatMsg* pReq = (SHeartBeatMsg*)rpcMallocCont(sizeof(SHeartBeatMsg)); + int32_t contLen = sizeof(SHeartBeatMsg); + + SHeartBeatMsg* pReq = (SHeartBeatMsg*)rpcMallocCont(contLen); pReq->connId = htonl(connId); pReq->pid = htonl(1234); pReq->numOfQueries = htonl(0); pReq->numOfStreams = htonl(0); strcpy(pReq->app, "dnode_test_profile"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SHeartBeatMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_HEARTBEAT; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_HEARTBEAT, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_CONNECTION); ASSERT_EQ(pMsg->contLen, 0); } { - SConnectMsg* pReq = (SConnectMsg*)rpcMallocCont(sizeof(SConnectMsg)); + int32_t contLen = sizeof(SConnectMsg); + + SConnectMsg* pReq = (SConnectMsg*)rpcMallocCont(contLen); pReq->pid = htonl(1234); strcpy(pReq->app, "dnode_test_profile"); strcpy(pReq->db, ""); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SConnectMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CONNECT; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CONNECT, pReq, contLen); ASSERT_NE(pMsg, nullptr); + ASSERT_EQ(pMsg->code, 0); SConnectRsp* pRsp = (SConnectRsp*)pMsg->pCont; ASSERT_NE(pRsp, nullptr); @@ -369,58 +193,43 @@ TEST_F(DndTestProfile, 05_KillConnMsg) { } TEST_F(DndTestProfile, 06_KillConnMsg_InvalidConn) { - ASSERT_NE(pClient, nullptr); + int32_t contLen = sizeof(SKillConnMsg); - SKillConnMsg* pReq = (SKillConnMsg*)rpcMallocCont(sizeof(SKillConnMsg)); + SKillConnMsg* pReq = (SKillConnMsg*)rpcMallocCont(contLen); pReq->connId = htonl(2345); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SKillConnMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_KILL_CONN; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_KILL_CONN, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_CONN_ID); } TEST_F(DndTestProfile, 07_KillQueryMsg) { - ASSERT_NE(pClient, nullptr); - { - SKillQueryMsg* pReq = (SKillQueryMsg*)rpcMallocCont(sizeof(SKillQueryMsg)); + int32_t contLen = sizeof(SKillQueryMsg); + + SKillQueryMsg* pReq = (SKillQueryMsg*)rpcMallocCont(contLen); pReq->connId = htonl(connId); pReq->queryId = htonl(1234); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SKillQueryMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_KILL_QUERY; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_KILL_QUERY, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); ASSERT_EQ(pMsg->contLen, 0); } { - SHeartBeatMsg* pReq = (SHeartBeatMsg*)rpcMallocCont(sizeof(SHeartBeatMsg)); + int32_t contLen = sizeof(SHeartBeatMsg); + + SHeartBeatMsg* pReq = (SHeartBeatMsg*)rpcMallocCont(contLen); pReq->connId = htonl(connId); pReq->pid = htonl(1234); pReq->numOfQueries = htonl(0); pReq->numOfStreams = htonl(0); strcpy(pReq->app, "dnode_test_profile"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SHeartBeatMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_HEARTBEAT; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_HEARTBEAT, pReq, contLen); ASSERT_NE(pMsg, nullptr); + ASSERT_EQ(pMsg->code, 0); SHeartBeatRsp* pRsp = (SHeartBeatRsp*)pMsg->pCont; ASSERT_NE(pRsp, nullptr); @@ -446,39 +255,36 @@ TEST_F(DndTestProfile, 07_KillQueryMsg) { } TEST_F(DndTestProfile, 08_KillQueryMsg_InvalidConn) { - ASSERT_NE(pClient, nullptr); + int32_t contLen = sizeof(SKillQueryMsg); - SKillQueryMsg* pReq = (SKillQueryMsg*)rpcMallocCont(sizeof(SKillQueryMsg)); + SKillQueryMsg* pReq = (SKillQueryMsg*)rpcMallocCont(contLen); pReq->connId = htonl(2345); pReq->queryId = htonl(1234); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SKillQueryMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_KILL_QUERY; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_KILL_QUERY, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_CONN_ID); } TEST_F(DndTestProfile, 09_KillQueryMsg) { - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_QUERIES, "show queries", 14); - CheckSchema(0, TSDB_DATA_TYPE_INT, 4, "queryId"); - CheckSchema(1, TSDB_DATA_TYPE_INT, 4, "connId"); - CheckSchema(2, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "user"); - CheckSchema(3, TSDB_DATA_TYPE_BINARY, TSDB_IPv4ADDR_LEN + 6 + VARSTR_HEADER_SIZE, "ip:port"); - CheckSchema(4, TSDB_DATA_TYPE_BINARY, 22 + VARSTR_HEADER_SIZE, "qid"); - CheckSchema(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "created_time"); - CheckSchema(6, TSDB_DATA_TYPE_BIGINT, 8, "time"); - CheckSchema(7, TSDB_DATA_TYPE_BINARY, 18 + VARSTR_HEADER_SIZE, "sql_obj_id"); - CheckSchema(8, TSDB_DATA_TYPE_INT, 4, "pid"); - CheckSchema(9, TSDB_DATA_TYPE_BINARY, TSDB_EP_LEN + VARSTR_HEADER_SIZE, "ep"); - CheckSchema(10, TSDB_DATA_TYPE_BOOL, 1, "stable_query"); - CheckSchema(11, TSDB_DATA_TYPE_INT, 4, "sub_queries"); - CheckSchema(12, TSDB_DATA_TYPE_BINARY, TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE, "sub_query_info"); - CheckSchema(13, TSDB_DATA_TYPE_BINARY, TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE, "sql"); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_QUERIES, ""); + CHECK_META("show queries", 14); - SendThenCheckShowRetrieveMsg(0); + CHECK_SCHEMA(0, TSDB_DATA_TYPE_INT, 4, "queryId"); + CHECK_SCHEMA(1, TSDB_DATA_TYPE_INT, 4, "connId"); + CHECK_SCHEMA(2, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "user"); + CHECK_SCHEMA(3, TSDB_DATA_TYPE_BINARY, TSDB_IPv4ADDR_LEN + 6 + VARSTR_HEADER_SIZE, "ip:port"); + CHECK_SCHEMA(4, TSDB_DATA_TYPE_BINARY, 22 + VARSTR_HEADER_SIZE, "qid"); + CHECK_SCHEMA(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "created_time"); + CHECK_SCHEMA(6, TSDB_DATA_TYPE_BIGINT, 8, "time"); + CHECK_SCHEMA(7, TSDB_DATA_TYPE_BINARY, 18 + VARSTR_HEADER_SIZE, "sql_obj_id"); + CHECK_SCHEMA(8, TSDB_DATA_TYPE_INT, 4, "pid"); + CHECK_SCHEMA(9, TSDB_DATA_TYPE_BINARY, TSDB_EP_LEN + VARSTR_HEADER_SIZE, "ep"); + CHECK_SCHEMA(10, TSDB_DATA_TYPE_BOOL, 1, "stable_query"); + CHECK_SCHEMA(11, TSDB_DATA_TYPE_INT, 4, "sub_queries"); + CHECK_SCHEMA(12, TSDB_DATA_TYPE_BINARY, TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE, "sub_query_info"); + CHECK_SCHEMA(13, TSDB_DATA_TYPE_BINARY, TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE, "sql"); + + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 0); } diff --git a/source/dnode/mgmt/impl/test/stb/CMakeLists.txt b/source/dnode/mgmt/impl/test/stb/CMakeLists.txt index 524e3e5c7e..68e4dfb07f 100644 --- a/source/dnode/mgmt/impl/test/stb/CMakeLists.txt +++ b/source/dnode/mgmt/impl/test/stb/CMakeLists.txt @@ -1,24 +1,8 @@ -add_executable(dnode_test_stb "") - -target_sources(dnode_test_stb - PRIVATE - "stb.cpp" - "../sut/deploy.cpp" -) - +aux_source_directory(. STB_SRC) +add_executable(dnode_test_stb ${STB_SRC}) target_link_libraries( dnode_test_stb - PUBLIC dnode - PUBLIC util - PUBLIC os - PUBLIC gtest_main -) - -target_include_directories(dnode_test_stb - PUBLIC - "${CMAKE_SOURCE_DIR}/include/server/dnode/mgmt" - "${CMAKE_CURRENT_SOURCE_DIR}/../../inc" - "${CMAKE_CURRENT_SOURCE_DIR}/../sut" + PUBLIC sut ) add_test( diff --git a/source/dnode/mgmt/impl/test/stb/stb.cpp b/source/dnode/mgmt/impl/test/stb/stb.cpp index c12e8eadf4..992ebaf902 100644 --- a/source/dnode/mgmt/impl/test/stb/stb.cpp +++ b/source/dnode/mgmt/impl/test/stb/stb.cpp @@ -9,176 +9,27 @@ * */ -#include "deploy.h" +#include "base.h" class DndTestStb : public ::testing::Test { protected: - static SServer* CreateServer(const char* path, const char* fqdn, uint16_t port, const char* firstEp) { - SServer* pServer = createServer(path, fqdn, port, firstEp); - ASSERT(pServer); - return pServer; - } + static void SetUpTestSuite() { test.Init("/tmp/dnode_test_stb", 9101); } + static void TearDownTestSuite() { test.Cleanup(); } - static void SetUpTestSuite() { - initLog("/tmp/tdlog"); - - const char* fqdn = "localhost"; - const char* firstEp = "localhost:9101"; - pServer = CreateServer("/tmp/dnode_test_stb", fqdn, 9101, firstEp); - pClient = createClient("root", "taosdata", fqdn, 9101); - taosMsleep(1100); - } - - static void TearDownTestSuite() { - stopServer(pServer); - dropClient(pClient); - pServer = NULL; - pClient = NULL; - } - - static SServer* pServer; - static SClient* pClient; - static int32_t connId; + static Testbase test; public: void SetUp() override {} void TearDown() override {} - - void SendTheCheckShowMetaMsg(int8_t showType, const char* showName, int32_t columns, const char* db) { - SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg)); - pShow->type = showType; - if (db != NULL) { - strcpy(pShow->db, db); - } - SRpcMsg showRpcMsg = {0}; - showRpcMsg.pCont = pShow; - showRpcMsg.contLen = sizeof(SShowMsg); - showRpcMsg.msgType = TSDB_MSG_TYPE_SHOW; - - sendMsg(pClient, &showRpcMsg); - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - SShowRsp* pShowRsp = (SShowRsp*)pClient->pRsp->pCont; - ASSERT_NE(pShowRsp, nullptr); - pShowRsp->showId = htonl(pShowRsp->showId); - pMeta = &pShowRsp->tableMeta; - pMeta->numOfTags = htonl(pMeta->numOfTags); - pMeta->numOfColumns = htonl(pMeta->numOfColumns); - pMeta->sversion = htonl(pMeta->sversion); - pMeta->tversion = htonl(pMeta->tversion); - pMeta->tuid = htobe64(pMeta->tuid); - pMeta->suid = htobe64(pMeta->suid); - - showId = pShowRsp->showId; - - EXPECT_NE(pShowRsp->showId, 0); - EXPECT_STREQ(pMeta->tbFname, showName); - EXPECT_EQ(pMeta->numOfTags, 0); - EXPECT_EQ(pMeta->numOfColumns, columns); - EXPECT_EQ(pMeta->precision, 0); - EXPECT_EQ(pMeta->tableType, 0); - EXPECT_EQ(pMeta->update, 0); - EXPECT_EQ(pMeta->sversion, 0); - EXPECT_EQ(pMeta->tversion, 0); - EXPECT_EQ(pMeta->tuid, 0); - EXPECT_EQ(pMeta->suid, 0); - } - - void CheckSchema(int32_t index, int8_t type, int32_t bytes, const char* name) { - SSchema* pSchema = &pMeta->pSchema[index]; - pSchema->bytes = htonl(pSchema->bytes); - EXPECT_EQ(pSchema->colId, 0); - EXPECT_EQ(pSchema->type, type); - EXPECT_EQ(pSchema->bytes, bytes); - EXPECT_STREQ(pSchema->name, name); - } - - void SendThenCheckShowRetrieveMsg(int32_t rows) { - SRetrieveTableMsg* pRetrieve = (SRetrieveTableMsg*)rpcMallocCont(sizeof(SRetrieveTableMsg)); - pRetrieve->showId = htonl(showId); - pRetrieve->free = 0; - - SRpcMsg retrieveRpcMsg = {0}; - retrieveRpcMsg.pCont = pRetrieve; - retrieveRpcMsg.contLen = sizeof(SRetrieveTableMsg); - retrieveRpcMsg.msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE; - - sendMsg(pClient, &retrieveRpcMsg); - - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - pRetrieveRsp = (SRetrieveTableRsp*)pClient->pRsp->pCont; - ASSERT_NE(pRetrieveRsp, nullptr); - pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows); - pRetrieveRsp->useconds = htobe64(pRetrieveRsp->useconds); - pRetrieveRsp->compLen = htonl(pRetrieveRsp->compLen); - - EXPECT_EQ(pRetrieveRsp->numOfRows, rows); - EXPECT_EQ(pRetrieveRsp->useconds, 0); - // EXPECT_EQ(pRetrieveRsp->completed, completed); - EXPECT_EQ(pRetrieveRsp->precision, TSDB_TIME_PRECISION_MILLI); - EXPECT_EQ(pRetrieveRsp->compressed, 0); - EXPECT_EQ(pRetrieveRsp->compLen, 0); - - pData = pRetrieveRsp->data; - pos = 0; - } - - void CheckInt8(int8_t val) { - int8_t data = *((int8_t*)(pData + pos)); - pos += sizeof(int8_t); - EXPECT_EQ(data, val); - } - - void CheckInt16(int16_t val) { - int16_t data = *((int16_t*)(pData + pos)); - pos += sizeof(int16_t); - EXPECT_EQ(data, val); - } - - void CheckInt32(int32_t val) { - int32_t data = *((int32_t*)(pData + pos)); - pos += sizeof(int32_t); - EXPECT_EQ(data, val); - } - - void CheckInt64(int64_t val) { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_EQ(data, val); - } - - void CheckTimestamp() { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_GT(data, 0); - } - - void CheckBinary(const char* val, int32_t len) { - pos += sizeof(VarDataLenT); - char* data = (char*)(pData + pos); - pos += len; - EXPECT_STREQ(data, val); - } - - int32_t showId; - STableMetaMsg* pMeta; - SRetrieveTableRsp* pRetrieveRsp; - char* pData; - int32_t pos; }; -SServer* DndTestStb::pServer; -SClient* DndTestStb::pClient; -int32_t DndTestStb::connId; +Testbase DndTestStb::test; TEST_F(DndTestStb, 01_Create_Show_Meta_Drop_Restart_Stb) { { - SCreateDbMsg* pReq = (SCreateDbMsg*)rpcMallocCont(sizeof(SCreateDbMsg)); + int32_t contLen = sizeof(SCreateDbMsg); + + SCreateDbMsg* pReq = (SCreateDbMsg*)rpcMallocCont(contLen); strcpy(pReq->db, "1.d1"); pReq->numOfVgroups = htonl(2); pReq->cacheBlockSize = htonl(16); @@ -200,13 +51,7 @@ TEST_F(DndTestStb, 01_Create_Show_Meta_Drop_Restart_Stb) { pReq->cacheLastRow = 0; pReq->ignoreExist = 1; - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateDbMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_DB; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_DB, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } @@ -214,9 +59,9 @@ TEST_F(DndTestStb, 01_Create_Show_Meta_Drop_Restart_Stb) { { int32_t cols = 2; int32_t tags = 3; - int32_t size = (tags + cols) * sizeof(SSchema) + sizeof(SCreateStbMsg); + int32_t contLen = (tags + cols) * sizeof(SSchema) + sizeof(SCreateStbMsg); - SCreateStbMsg* pReq = (SCreateStbMsg*)rpcMallocCont(size); + SCreateStbMsg* pReq = (SCreateStbMsg*)rpcMallocCont(contLen); strcpy(pReq->name, "1.d1.stb"); pReq->numOfTags = htonl(tags); pReq->numOfColumns = htonl(cols); @@ -261,24 +106,21 @@ TEST_F(DndTestStb, 01_Create_Show_Meta_Drop_Restart_Stb) { strcpy(pSchema->name, "tag3"); } - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = size; - rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_STB; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_STB, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_STB, "show stables", 4, "1.d1"); - CheckSchema(0, TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE, "name"); - CheckSchema(1, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time"); - CheckSchema(2, TSDB_DATA_TYPE_INT, 4, "columns"); - CheckSchema(3, TSDB_DATA_TYPE_INT, 4, "tags"); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_STB, "1.d1"); + CHECK_META("show stables", 4); - SendThenCheckShowRetrieveMsg(1); + CHECK_SCHEMA(0, TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE, "name"); + CHECK_SCHEMA(1, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time"); + CHECK_SCHEMA(2, TSDB_DATA_TYPE_INT, 4, "columns"); + CHECK_SCHEMA(3, TSDB_DATA_TYPE_INT, 4, "tags"); + + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 1); CheckBinary("stb", TSDB_TABLE_NAME_LEN); CheckTimestamp(); CheckInt32(2); @@ -286,16 +128,12 @@ TEST_F(DndTestStb, 01_Create_Show_Meta_Drop_Restart_Stb) { // ----- meta ------ { - STableInfoMsg* pReq = (STableInfoMsg*)rpcMallocCont(sizeof(STableInfoMsg)); + int32_t contLen = sizeof(STableInfoMsg); + + STableInfoMsg* pReq = (STableInfoMsg*)rpcMallocCont(contLen); strcpy(pReq->tableFname, "1.d1.stb"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(STableInfoMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_TABLE_META; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_TABLE_META, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); @@ -336,39 +174,31 @@ TEST_F(DndTestStb, 01_Create_Show_Meta_Drop_Restart_Stb) { } // restart - stopServer(pServer); - pServer = NULL; + test.Restart(); - uInfo("start all server"); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_STB, "1.d1"); + CHECK_META("show stables", 4); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 1); - const char* fqdn = "localhost"; - const char* firstEp = "localhost:9101"; - pServer = startServer("/tmp/dnode_test_stb", fqdn, 9101, firstEp); - - uInfo("all server is running"); - - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_STB, "show stables", 4, "1.d1"); - SendThenCheckShowRetrieveMsg(1); CheckBinary("stb", TSDB_TABLE_NAME_LEN); CheckTimestamp(); CheckInt32(2); CheckInt32(3); { - SDropStbMsg* pReq = (SDropStbMsg*)rpcMallocCont(sizeof(SDropStbMsg)); + int32_t contLen = sizeof(SDropStbMsg); + + SDropStbMsg* pReq = (SDropStbMsg*)rpcMallocCont(contLen); strcpy(pReq->name, "1.d1.stb"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SDropStbMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_DROP_STB; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_DROP_STB, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_STB, "show stables", 4, "1.d1"); - SendThenCheckShowRetrieveMsg(0); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_STB, "1.d1"); + CHECK_META("show stables", 4); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 0); } diff --git a/source/dnode/mgmt/impl/test/user/CMakeLists.txt b/source/dnode/mgmt/impl/test/user/CMakeLists.txt index 0d3215a4d0..5068660eba 100644 --- a/source/dnode/mgmt/impl/test/user/CMakeLists.txt +++ b/source/dnode/mgmt/impl/test/user/CMakeLists.txt @@ -1,24 +1,8 @@ -add_executable(dnode_test_user "") - -target_sources(dnode_test_user - PRIVATE - "user.cpp" - "../sut/deploy.cpp" -) - +aux_source_directory(. USER_SRC) +add_executable(dnode_test_user ${USER_SRC}) target_link_libraries( dnode_test_user - PUBLIC dnode - PUBLIC util - PUBLIC os - PUBLIC gtest_main -) - -target_include_directories(dnode_test_user - PUBLIC - "${CMAKE_SOURCE_DIR}/include/server/dnode/mgmt" - "${CMAKE_CURRENT_SOURCE_DIR}/../../inc" - "${CMAKE_CURRENT_SOURCE_DIR}/../sut" + PUBLIC sut ) add_test( diff --git a/source/dnode/mgmt/impl/test/user/user.cpp b/source/dnode/mgmt/impl/test/user/user.cpp index 32309b106e..a1810e7ada 100644 --- a/source/dnode/mgmt/impl/test/user/user.cpp +++ b/source/dnode/mgmt/impl/test/user/user.cpp @@ -9,168 +9,34 @@ * */ -#include "deploy.h" +#include "base.h" class DndTestUser : public ::testing::Test { protected: - static SServer* CreateServer(const char* path, const char* fqdn, uint16_t port, const char* firstEp) { - SServer* pServer = createServer(path, fqdn, port, firstEp); - ASSERT(pServer); - return pServer; - } + static void SetUpTestSuite() { test.Init("/tmp/dnode_test_user", 9140); } + static void TearDownTestSuite() { test.Cleanup(); } - static void SetUpTestSuite() { - initLog("/tmp/tdlog"); - - const char* fqdn = "localhost"; - const char* firstEp = "localhost:9140"; - pServer = CreateServer("/tmp/dnode_test_user", fqdn, 9140, firstEp); - pClient = createClient("root", "taosdata", fqdn, 9140); - taosMsleep(300); - } - - static void TearDownTestSuite() { - stopServer(pServer); - dropClient(pClient); - pServer = NULL; - pClient = NULL; - } - - static SServer* pServer; - static SClient* pClient; - static int32_t connId; + static Testbase test; public: void SetUp() override {} void TearDown() override {} - - void SendTheCheckShowMetaMsg(int8_t showType, const char* showName, int32_t columns) { - SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg)); - pShow->type = showType; - strcpy(pShow->db, ""); - - SRpcMsg showRpcMsg = {0}; - showRpcMsg.pCont = pShow; - showRpcMsg.contLen = sizeof(SShowMsg); - showRpcMsg.msgType = TSDB_MSG_TYPE_SHOW; - - sendMsg(pClient, &showRpcMsg); - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - SShowRsp* pShowRsp = (SShowRsp*)pClient->pRsp->pCont; - ASSERT_NE(pShowRsp, nullptr); - pShowRsp->showId = htonl(pShowRsp->showId); - pMeta = &pShowRsp->tableMeta; - pMeta->numOfTags = htonl(pMeta->numOfTags); - pMeta->numOfColumns = htonl(pMeta->numOfColumns); - pMeta->sversion = htonl(pMeta->sversion); - pMeta->tversion = htonl(pMeta->tversion); - pMeta->tuid = htobe64(pMeta->tuid); - pMeta->suid = htobe64(pMeta->suid); - - showId = pShowRsp->showId; - - EXPECT_NE(pShowRsp->showId, 0); - EXPECT_STREQ(pMeta->tbFname, showName); - EXPECT_EQ(pMeta->numOfTags, 0); - EXPECT_EQ(pMeta->numOfColumns, columns); - EXPECT_EQ(pMeta->precision, 0); - EXPECT_EQ(pMeta->tableType, 0); - EXPECT_EQ(pMeta->update, 0); - EXPECT_EQ(pMeta->sversion, 0); - EXPECT_EQ(pMeta->tversion, 0); - EXPECT_EQ(pMeta->tuid, 0); - EXPECT_EQ(pMeta->suid, 0); - } - - void CheckSchema(int32_t index, int8_t type, int32_t bytes, const char* name) { - SSchema* pSchema = &pMeta->pSchema[index]; - pSchema->bytes = htonl(pSchema->bytes); - EXPECT_EQ(pSchema->colId, 0); - EXPECT_EQ(pSchema->type, type); - EXPECT_EQ(pSchema->bytes, bytes); - EXPECT_STREQ(pSchema->name, name); - } - - void SendThenCheckShowRetrieveMsg(int32_t rows) { - SRetrieveTableMsg* pRetrieve = (SRetrieveTableMsg*)rpcMallocCont(sizeof(SRetrieveTableMsg)); - pRetrieve->showId = htonl(showId); - pRetrieve->free = 0; - - SRpcMsg retrieveRpcMsg = {0}; - retrieveRpcMsg.pCont = pRetrieve; - retrieveRpcMsg.contLen = sizeof(SRetrieveTableMsg); - retrieveRpcMsg.msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE; - - sendMsg(pClient, &retrieveRpcMsg); - - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - pRetrieveRsp = (SRetrieveTableRsp*)pClient->pRsp->pCont; - ASSERT_NE(pRetrieveRsp, nullptr); - pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows); - pRetrieveRsp->useconds = htobe64(pRetrieveRsp->useconds); - pRetrieveRsp->compLen = htonl(pRetrieveRsp->compLen); - - EXPECT_EQ(pRetrieveRsp->numOfRows, rows); - EXPECT_EQ(pRetrieveRsp->useconds, 0); - // EXPECT_EQ(pRetrieveRsp->completed, completed); - EXPECT_EQ(pRetrieveRsp->precision, TSDB_TIME_PRECISION_MILLI); - EXPECT_EQ(pRetrieveRsp->compressed, 0); - EXPECT_EQ(pRetrieveRsp->compLen, 0); - - pData = pRetrieveRsp->data; - pos = 0; - } - - void CheckInt16(int16_t val) { - int16_t data = *((int16_t*)(pData + pos)); - pos += sizeof(int16_t); - EXPECT_EQ(data, val); - } - - void CheckInt64(int64_t val) { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_EQ(data, val); - } - - void CheckTimestamp() { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_GT(data, 0); - } - - void CheckBinary(const char* val, int32_t len) { - pos += sizeof(VarDataLenT); - char* data = (char*)(pData + pos); - pos += len; - EXPECT_STREQ(data, val); - } - - int32_t showId; - STableMetaMsg* pMeta; - SRetrieveTableRsp* pRetrieveRsp; - char* pData; - int32_t pos; }; -SServer* DndTestUser::pServer; -SClient* DndTestUser::pClient; -int32_t DndTestUser::connId; +Testbase DndTestUser::test; TEST_F(DndTestUser, 01_ShowUser) { - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4); - CheckSchema(0, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "name"); - CheckSchema(1, TSDB_DATA_TYPE_BINARY, 10 + VARSTR_HEADER_SIZE, "privilege"); - CheckSchema(2, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time"); - CheckSchema(3, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "account"); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, ""); + CHECK_META("show users", 4); + + CHECK_SCHEMA(0, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "name"); + CHECK_SCHEMA(1, TSDB_DATA_TYPE_BINARY, 10 + VARSTR_HEADER_SIZE, "privilege"); + CHECK_SCHEMA(2, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time"); + CHECK_SCHEMA(3, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "account"); + + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 1); - SendThenCheckShowRetrieveMsg(1); CheckBinary("root", TSDB_USER_LEN); CheckBinary("super", 10); CheckTimestamp(); @@ -179,39 +45,35 @@ TEST_F(DndTestUser, 01_ShowUser) { TEST_F(DndTestUser, 02_Create_Drop_Alter_User) { { - SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(sizeof(SCreateUserMsg)); + int32_t contLen = sizeof(SCreateUserMsg); + + SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen); strcpy(pReq->user, "u1"); strcpy(pReq->pass, "p1"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateUserMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_USER; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_USER, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } { - SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(sizeof(SCreateUserMsg)); + int32_t contLen = sizeof(SCreateUserMsg); + + SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen); strcpy(pReq->user, "u2"); strcpy(pReq->pass, "p2"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateUserMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_USER; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_USER, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4); - SendThenCheckShowRetrieveMsg(3); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, ""); + CHECK_META("show users", 4); + + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 3); + CheckBinary("u1", TSDB_USER_LEN); CheckBinary("root", TSDB_USER_LEN); CheckBinary("u2", TSDB_USER_LEN); @@ -226,22 +88,23 @@ TEST_F(DndTestUser, 02_Create_Drop_Alter_User) { CheckBinary("root", TSDB_USER_LEN); { - SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(sizeof(SAlterUserMsg)); + int32_t contLen = sizeof(SAlterUserMsg); + + SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen); strcpy(pReq->user, "u1"); strcpy(pReq->pass, "p2"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SAlterUserMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_ALTER_USER; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_ALTER_USER, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4); - SendThenCheckShowRetrieveMsg(3); + + test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, ""); + CHECK_META("show users", 4); + + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 3); + CheckBinary("u1", TSDB_USER_LEN); CheckBinary("root", TSDB_USER_LEN); CheckBinary("u2", TSDB_USER_LEN); @@ -256,21 +119,22 @@ TEST_F(DndTestUser, 02_Create_Drop_Alter_User) { CheckBinary("root", TSDB_USER_LEN); { - SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(sizeof(SDropUserMsg)); + int32_t contLen = sizeof(SDropUserMsg); + + SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(contLen); strcpy(pReq->user, "u1"); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SDropUserMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_DROP_USER; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_DROP_USER, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4); - SendThenCheckShowRetrieveMsg(2); + + test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, ""); + CHECK_META("show users", 4); + + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 2); + CheckBinary("root", TSDB_USER_LEN); CheckBinary("u2", TSDB_USER_LEN); CheckBinary("super", 10); @@ -281,19 +145,14 @@ TEST_F(DndTestUser, 02_Create_Drop_Alter_User) { CheckBinary("root", TSDB_USER_LEN); // restart - stopServer(pServer); - pServer = NULL; + test.Restart(); - uInfo("start all server"); + test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, ""); + CHECK_META("show users", 4); - const char* fqdn = "localhost"; - const char* firstEp = "localhost:9140"; - pServer = startServer("/tmp/dnode_test_user", fqdn, 9140, firstEp); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 2); - uInfo("all server is running"); - - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4); - SendThenCheckShowRetrieveMsg(2); CheckBinary("root", TSDB_USER_LEN); CheckBinary("u2", TSDB_USER_LEN); CheckBinary("super", 10); diff --git a/source/dnode/mgmt/impl/test/vgroup/CMakeLists.txt b/source/dnode/mgmt/impl/test/vgroup/CMakeLists.txt index 5670f9dbf2..b864b0593c 100644 --- a/source/dnode/mgmt/impl/test/vgroup/CMakeLists.txt +++ b/source/dnode/mgmt/impl/test/vgroup/CMakeLists.txt @@ -1,24 +1,8 @@ -add_executable(dnode_test_vgroup "") - -target_sources(dnode_test_vgroup - PRIVATE - "vgroup.cpp" - "../sut/deploy.cpp" -) - +aux_source_directory(. VGROUP_SRC) +add_executable(dnode_test_vgroup ${VGROUP_SRC}) target_link_libraries( dnode_test_vgroup - PUBLIC dnode - PUBLIC util - PUBLIC os - PUBLIC gtest_main -) - -target_include_directories(dnode_test_vgroup - PUBLIC - "${CMAKE_SOURCE_DIR}/include/server/dnode/mgmt" - "${CMAKE_CURRENT_SOURCE_DIR}/../../inc" - "${CMAKE_CURRENT_SOURCE_DIR}/../sut" + PUBLIC sut ) add_test( diff --git a/source/dnode/mgmt/impl/test/vgroup/vgroup.cpp b/source/dnode/mgmt/impl/test/vgroup/vgroup.cpp index 9149be9cd8..c534fcbabe 100644 --- a/source/dnode/mgmt/impl/test/vgroup/vgroup.cpp +++ b/source/dnode/mgmt/impl/test/vgroup/vgroup.cpp @@ -9,177 +9,28 @@ * */ -#include "deploy.h" +#include "base.h" class DndTestVgroup : public ::testing::Test { protected: - static SServer* CreateServer(const char* path, const char* fqdn, uint16_t port, const char* firstEp) { - SServer* pServer = createServer(path, fqdn, port, firstEp); - ASSERT(pServer); - return pServer; - } + static void SetUpTestSuite() { test.Init("/tmp/dnode_test_vgroup", 9150); } + static void TearDownTestSuite() { test.Cleanup(); } - static void SetUpTestSuite() { - initLog("/tmp/tdlog"); - - const char* fqdn = "localhost"; - const char* firstEp = "localhost:9150"; - pServer = CreateServer("/tmp/dnode_test_vgroup", fqdn, 9150, firstEp); - pClient = createClient("root", "taosdata", fqdn, 9150); - taosMsleep(1100); - } - - static void TearDownTestSuite() { - stopServer(pServer); - dropClient(pClient); - pServer = NULL; - pClient = NULL; - } - - static SServer* pServer; - static SClient* pClient; - static int32_t connId; + static Testbase test; public: void SetUp() override {} void TearDown() override {} - - void SendTheCheckShowMetaMsg(int8_t showType, const char* showName, int32_t columns, const char* db) { - SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg)); - pShow->type = showType; - if (db != NULL) { - strcpy(pShow->db, db); - } - SRpcMsg showRpcMsg = {0}; - showRpcMsg.pCont = pShow; - showRpcMsg.contLen = sizeof(SShowMsg); - showRpcMsg.msgType = TSDB_MSG_TYPE_SHOW; - - sendMsg(pClient, &showRpcMsg); - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - SShowRsp* pShowRsp = (SShowRsp*)pClient->pRsp->pCont; - ASSERT_NE(pShowRsp, nullptr); - pShowRsp->showId = htonl(pShowRsp->showId); - pMeta = &pShowRsp->tableMeta; - pMeta->numOfTags = htonl(pMeta->numOfTags); - pMeta->numOfColumns = htonl(pMeta->numOfColumns); - pMeta->sversion = htonl(pMeta->sversion); - pMeta->tversion = htonl(pMeta->tversion); - pMeta->tuid = htobe64(pMeta->tuid); - pMeta->suid = htobe64(pMeta->suid); - - showId = pShowRsp->showId; - - EXPECT_NE(pShowRsp->showId, 0); - EXPECT_STREQ(pMeta->tbFname, showName); - EXPECT_EQ(pMeta->numOfTags, 0); - EXPECT_EQ(pMeta->numOfColumns, columns); - EXPECT_EQ(pMeta->precision, 0); - EXPECT_EQ(pMeta->tableType, 0); - EXPECT_EQ(pMeta->update, 0); - EXPECT_EQ(pMeta->sversion, 0); - EXPECT_EQ(pMeta->tversion, 0); - EXPECT_EQ(pMeta->tuid, 0); - EXPECT_EQ(pMeta->suid, 0); - } - - void CheckSchema(int32_t index, int8_t type, int32_t bytes, const char* name) { - SSchema* pSchema = &pMeta->pSchema[index]; - pSchema->bytes = htonl(pSchema->bytes); - EXPECT_EQ(pSchema->colId, 0); - EXPECT_EQ(pSchema->type, type); - EXPECT_EQ(pSchema->bytes, bytes); - EXPECT_STREQ(pSchema->name, name); - } - - void SendThenCheckShowRetrieveMsg(int32_t rows) { - SRetrieveTableMsg* pRetrieve = (SRetrieveTableMsg*)rpcMallocCont(sizeof(SRetrieveTableMsg)); - pRetrieve->showId = htonl(showId); - pRetrieve->free = 0; - - SRpcMsg retrieveRpcMsg = {0}; - retrieveRpcMsg.pCont = pRetrieve; - retrieveRpcMsg.contLen = sizeof(SRetrieveTableMsg); - retrieveRpcMsg.msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE; - - sendMsg(pClient, &retrieveRpcMsg); - - ASSERT_NE(pClient->pRsp, nullptr); - ASSERT_EQ(pClient->pRsp->code, 0); - ASSERT_NE(pClient->pRsp->pCont, nullptr); - - pRetrieveRsp = (SRetrieveTableRsp*)pClient->pRsp->pCont; - ASSERT_NE(pRetrieveRsp, nullptr); - pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows); - pRetrieveRsp->useconds = htobe64(pRetrieveRsp->useconds); - pRetrieveRsp->compLen = htonl(pRetrieveRsp->compLen); - - EXPECT_EQ(pRetrieveRsp->numOfRows, rows); - EXPECT_EQ(pRetrieveRsp->useconds, 0); - // EXPECT_EQ(pRetrieveRsp->completed, completed); - EXPECT_EQ(pRetrieveRsp->precision, TSDB_TIME_PRECISION_MILLI); - EXPECT_EQ(pRetrieveRsp->compressed, 0); - EXPECT_EQ(pRetrieveRsp->compLen, 0); - - pData = pRetrieveRsp->data; - pos = 0; - } - - void CheckInt8(int8_t val) { - int8_t data = *((int8_t*)(pData + pos)); - pos += sizeof(int8_t); - EXPECT_EQ(data, val); - } - - void CheckInt16(int16_t val) { - int16_t data = *((int16_t*)(pData + pos)); - pos += sizeof(int16_t); - EXPECT_EQ(data, val); - } - - void CheckInt32(int32_t val) { - int32_t data = *((int32_t*)(pData + pos)); - pos += sizeof(int32_t); - EXPECT_EQ(data, val); - } - - void CheckInt64(int64_t val) { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_EQ(data, val); - } - - void CheckTimestamp() { - int64_t data = *((int64_t*)(pData + pos)); - pos += sizeof(int64_t); - EXPECT_GT(data, 0); - } - - void CheckBinary(const char* val, int32_t len) { - pos += sizeof(VarDataLenT); - char* data = (char*)(pData + pos); - pos += len; - EXPECT_STREQ(data, val); - } - - int32_t showId; - STableMetaMsg* pMeta; - SRetrieveTableRsp* pRetrieveRsp; - char* pData; - int32_t pos; }; -SServer* DndTestVgroup::pServer; -SClient* DndTestVgroup::pClient; -int32_t DndTestVgroup::connId; +Testbase DndTestVgroup::test; TEST_F(DndTestVgroup, 01_Create_Restart_Drop_Vnode) { { for (int i = 0; i < 3; ++i) { - SCreateVnodeMsg* pReq = (SCreateVnodeMsg*)rpcMallocCont(sizeof(SCreateVnodeMsg)); + int32_t contLen = sizeof(SCreateVnodeMsg); + + SCreateVnodeMsg* pReq = (SCreateVnodeMsg*)rpcMallocCont(contLen); pReq->vgId = htonl(2); pReq->dnodeId = htonl(1); strcpy(pReq->db, "1.d1"); @@ -209,13 +60,7 @@ TEST_F(DndTestVgroup, 01_Create_Restart_Drop_Vnode) { pReplica->port = htons(9150); } - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateVnodeMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_VNODE_IN; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_VNODE_IN, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } @@ -223,7 +68,9 @@ TEST_F(DndTestVgroup, 01_Create_Restart_Drop_Vnode) { { for (int i = 0; i < 3; ++i) { - SAlterVnodeMsg* pReq = (SAlterVnodeMsg*)rpcMallocCont(sizeof(SAlterVnodeMsg)); + int32_t contLen = sizeof(SAlterVnodeMsg); + + SAlterVnodeMsg* pReq = (SAlterVnodeMsg*)rpcMallocCont(contLen); pReq->vgId = htonl(2); pReq->dnodeId = htonl(1); strcpy(pReq->db, "1.d1"); @@ -253,13 +100,7 @@ TEST_F(DndTestVgroup, 01_Create_Restart_Drop_Vnode) { pReplica->port = htons(9150); } - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SAlterVnodeMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_ALTER_VNODE_IN; - - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_ALTER_VNODE_IN, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); } @@ -267,7 +108,9 @@ TEST_F(DndTestVgroup, 01_Create_Restart_Drop_Vnode) { { for (int i = 0; i < 3; ++i) { - SDropVnodeMsg* pReq = (SDropVnodeMsg*)rpcMallocCont(sizeof(SDropVnodeMsg)); + int32_t contLen = sizeof(SDropVnodeMsg); + + SDropVnodeMsg* pReq = (SDropVnodeMsg*)rpcMallocCont(contLen); pReq->vgId = htonl(2); pReq->dnodeId = htonl(1); strcpy(pReq->db, "1.d1"); @@ -278,8 +121,7 @@ TEST_F(DndTestVgroup, 01_Create_Restart_Drop_Vnode) { rpcMsg.contLen = sizeof(SDropVnodeMsg); rpcMsg.msgType = TSDB_MSG_TYPE_DROP_VNODE_IN; - sendMsg(pClient, &rpcMsg); - SRpcMsg* pMsg = pClient->pRsp; + SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_DROP_VNODE_IN, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); }