cluster.cpp 5.0 KB
Newer Older
S
Shengliang Guan 已提交
1
/**
S
Shengliang Guan 已提交
2
 * @file cluster.cpp
S
Shengliang Guan 已提交
3 4 5 6
 * @author slguan (slguan@taosdata.com)
 * @brief DNODE module cluster-msg tests
 * @version 0.1
 * @date 2021-12-15
S
Shengliang Guan 已提交
7
 *
S
Shengliang Guan 已提交
8
 * @copyright Copyright (c) 2021
S
Shengliang Guan 已提交
9 10 11 12 13 14 15
 *
 */

#include "deploy.h"

class DndTestCluster : public ::testing::Test {
 protected:
S
Shengliang Guan 已提交
16 17 18 19 20
  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;
  }
S
Shengliang Guan 已提交
21 22

  static void SetUpTestSuite() {
S
Shengliang Guan 已提交
23
    initLog("/tmp/tdlog");
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25 26 27 28 29
    const char* fqdn = "localhost";
    const char* firstEp = "localhost:9030";
    pServer = CreateServer("/tmp/dnode_test_cluster", fqdn, 9030, firstEp);
    pClient = createClient("root", "taosdata", fqdn, 9030);
    taosMsleep(1100);
S
Shengliang Guan 已提交
30 31 32
  }

  static void TearDownTestSuite() {
S
Shengliang Guan 已提交
33
    stopServer(pServer);
S
Shengliang Guan 已提交
34
    dropClient(pClient);
S
Shengliang Guan 已提交
35 36
    pServer = NULL;
    pClient = NULL;
S
Shengliang Guan 已提交
37 38 39 40 41 42
  }

  static SServer* pServer;
  static SClient* pClient;
  static int32_t  connId;

S
Shengliang Guan 已提交
43 44 45
 public:
  void SetUp() override {}
  void TearDown() override {}
S
Shengliang Guan 已提交
46

S
Shengliang Guan 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  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);
S
Shengliang Guan 已提交
72 73
    pMeta->suid = htobe64(pMeta->suid);

S
Shengliang Guan 已提交
74
    showId = pShowRsp->showId;
S
Shengliang Guan 已提交
75

S
Shengliang Guan 已提交
76 77
    EXPECT_NE(pShowRsp->showId, 0);
    EXPECT_STREQ(pMeta->tbFname, showName);
S
Shengliang Guan 已提交
78
    EXPECT_EQ(pMeta->numOfTags, 0);
S
Shengliang Guan 已提交
79
    EXPECT_EQ(pMeta->numOfColumns, columns);
S
Shengliang Guan 已提交
80 81
    EXPECT_EQ(pMeta->precision, 0);
    EXPECT_EQ(pMeta->tableType, 0);
S
Shengliang Guan 已提交
82
    EXPECT_EQ(pMeta->update, 0);
S
Shengliang Guan 已提交
83 84
    EXPECT_EQ(pMeta->sversion, 0);
    EXPECT_EQ(pMeta->tversion, 0);
S
Shengliang Guan 已提交
85
    EXPECT_EQ(pMeta->tuid, 0);
S
Shengliang Guan 已提交
86
    EXPECT_EQ(pMeta->suid, 0);
S
Shengliang Guan 已提交
87
  }
S
Shengliang Guan 已提交
88

S
Shengliang Guan 已提交
89 90
  void CheckSchema(int32_t index, int8_t type, int32_t bytes, const char* name) {
    SSchema* pSchema = &pMeta->pSchema[index];
91
    pSchema->bytes = htonl(pSchema->bytes);
S
Shengliang Guan 已提交
92
    EXPECT_EQ(pSchema->colId, 0);
S
Shengliang Guan 已提交
93 94 95 96
    EXPECT_EQ(pSchema->type, type);
    EXPECT_EQ(pSchema->bytes, bytes);
    EXPECT_STREQ(pSchema->name, name);
  }
S
Shengliang Guan 已提交
97

S
Shengliang Guan 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  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;
S
Shengliang Guan 已提交
129 130
  }

S
Shengliang Guan 已提交
131 132
  void CheckInt32() {
    int32_t data = *((int32_t*)(pData + pos));
S
Shengliang Guan 已提交
133
    pos += sizeof(int32_t);
S
Shengliang Guan 已提交
134 135 136 137 138 139 140 141
    EXPECT_GT(data, 0);
  }

  void CheckTimestamp() {
    int64_t data = *((int64_t*)(pData + pos));
    pos += sizeof(int64_t);
    EXPECT_GT(data, 0);
  }
S
Shengliang Guan 已提交
142

S
Shengliang Guan 已提交
143
  void CheckBinary(int32_t len) {
S
Shengliang Guan 已提交
144
    pos += sizeof(VarDataLenT);
S
Shengliang Guan 已提交
145 146 147
    char* data = (char*)(pData + pos);
    pos += len;
  }
S
Shengliang Guan 已提交
148

S
Shengliang Guan 已提交
149 150 151 152 153 154
  int32_t            showId;
  STableMetaMsg*     pMeta;
  SRetrieveTableRsp* pRetrieveRsp;
  char*              pData;
  int32_t            pos;
};
S
Shengliang Guan 已提交
155

S
Shengliang Guan 已提交
156 157 158
SServer* DndTestCluster::pServer;
SClient* DndTestCluster::pClient;
int32_t  DndTestCluster::connId;
S
Shengliang Guan 已提交
159

S
Shengliang Guan 已提交
160 161 162 163
TEST_F(DndTestCluster, 01_ShowCluster) {
  SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_CLUSTER, "show cluster", 3, NULL);
  CheckSchema(0, TSDB_DATA_TYPE_INT, 4, "id");
  CheckSchema(1, TSDB_DATA_TYPE_BINARY, TSDB_CLUSTER_ID_LEN + VARSTR_HEADER_SIZE, "name");
164
  CheckSchema(2, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time");
S
Shengliang Guan 已提交
165 166 167 168 169

  SendThenCheckShowRetrieveMsg(1);
  CheckInt32();
  CheckBinary(TSDB_CLUSTER_ID_LEN);
  CheckTimestamp();
S
Shengliang Guan 已提交
170
}