monTest.cpp 6.7 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * @file monTest.cpp
 * @author slguan (slguan@taosdata.com)
 * @brief monitor module tests
 * @version 1.0
 * @date 2022-03-05
 *
 * @copyright Copyright (c) 2022
 *
 */

#include <gtest/gtest.h>
#include "os.h"

#include "monitor.h"
S
monitor  
Shengliang Guan 已提交
16
#include "tglobal.h"
S
Shengliang Guan 已提交
17 18 19

class MonitorTest : public ::testing::Test {
 protected:
S
monitor  
Shengliang Guan 已提交
20 21 22 23 24 25 26 27 28
  static void SetUpTestSuite() {
    SMonCfg cfg;
    cfg.maxLogs = 2;
    cfg.port = 80;
    cfg.server = "localhost";
    monInit(&cfg);
  }

  static void TearDownTestSuite() { monCleanup(); }
S
Shengliang Guan 已提交
29 30 31 32 33

 public:
  void SetUp() override {}
  void TearDown() override {}

S
monitor  
Shengliang Guan 已提交
34 35 36 37 38 39 40 41
  void GetBasicInfo(SMonInfo *pMonitor, SMonBasicInfo *pInfo);
  void GetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo);
  void GetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo);
  void GetGrantInfo(SMonInfo *pMonitor, SMonGrantInfo *pInfo);
  void GetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo);
  void GetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo);
  void AddLogInfo1();
  void AddLogInfo2();
S
Shengliang Guan 已提交
42 43
};

S
monitor  
Shengliang Guan 已提交
44 45 46 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
void MonitorTest::GetBasicInfo(SMonInfo *pMonitor, SMonBasicInfo *pInfo) {
  pInfo->dnode_id = 1;
  strcpy(pInfo->dnode_ep, "localhost");
}

void MonitorTest::GetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo) {
  strcpy(pInfo->first_ep, "localhost:6030");
  pInfo->first_ep_dnode_id = 1;
  strcpy(pInfo->version, "3.0.0.0");
  pInfo->master_uptime = 1;
  pInfo->monitor_interval = 2;
  pInfo->vgroups_total = 3;
  pInfo->vgroups_alive = 43;
  pInfo->vnodes_total = 5;
  pInfo->vnodes_alive = 6;
  pInfo->connections_total = 7;

  pInfo->dnodes = taosArrayInit(4, sizeof(SMonDnodeDesc));
  SMonDnodeDesc d1 = {0};
  d1.dnode_id = 1;
  strcpy(d1.dnode_ep, "localhost:6030");
  strcpy(d1.status, "ready");
  taosArrayPush(pInfo->dnodes, &d1);
  SMonDnodeDesc d2 = {0};
  d2.dnode_id = 2;
  strcpy(d2.dnode_ep, "localhost:7030");
  strcpy(d2.status, "offline");
  taosArrayPush(pInfo->dnodes, &d2);

  pInfo->mnodes = taosArrayInit(4, sizeof(SMonMnodeDesc));
  SMonMnodeDesc m1 = {0};
  m1.mnode_id = 1;
  strcpy(m1.mnode_ep, "localhost:6030");
  strcpy(m1.role, "master");
  taosArrayPush(pInfo->mnodes, &m1);
  SMonMnodeDesc m2 = {0};
  m2.mnode_id = 2;
  strcpy(m2.mnode_ep, "localhost:7030");
  strcpy(m2.role, "unsynced");
  taosArrayPush(pInfo->mnodes, &m2);
}

void MonitorTest::GetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo) {
  pInfo->vgroups = taosArrayInit(4, sizeof(SMonVgroupDesc));

  SMonVgroupDesc vg1 = {0};
  vg1.vgroup_id = 1;
  strcpy(vg1.database_name, "d1");
  vg1.tables_num = 4;
  strcpy(vg1.status, "ready");
  vg1.vnodes[0].dnode_id = 1;
  strcpy(vg1.vnodes[0].vnode_role, "master");
  vg1.vnodes[1].dnode_id = 2;
  strcpy(vg1.vnodes[1].vnode_role, "slave");
  taosArrayPush(pInfo->vgroups, &vg1);

  SMonVgroupDesc vg2 = {0};
  vg2.vgroup_id = 2;
  strcpy(vg2.database_name, "d2");
  vg2.tables_num = 5;
  strcpy(vg2.status, "offline");
  vg2.vnodes[0].dnode_id = 1;
  strcpy(vg2.vnodes[0].vnode_role, "master");
  vg2.vnodes[1].dnode_id = 2;
  strcpy(vg2.vnodes[1].vnode_role, "unsynced");
  taosArrayPush(pInfo->vgroups, &vg2);

  SMonVgroupDesc vg3 = {0};
  vg3.vgroup_id = 3;
  strcpy(vg3.database_name, "d3");
  vg3.tables_num = 6;
  strcpy(vg3.status, "ready");
  vg3.vnodes[0].dnode_id = 1;
  strcpy(vg3.vnodes[0].vnode_role, "master");
  taosArrayPush(pInfo->vgroups, &vg3);
}

void MonitorTest::GetGrantInfo(SMonInfo *pMonitor, SMonGrantInfo *pInfo) {
  pInfo->expire_time = 1234567;
  pInfo->timeseries_total = 234567;
  pInfo->timeseries_used = 34567;
}

void MonitorTest::GetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo) {
  pInfo->uptime = 1.2;
  pInfo->cpu_engine = 2.1;
  pInfo->cpu_system = 2.1;
  pInfo->cpu_cores = 2;
  pInfo->mem_engine = 3.1;
  pInfo->mem_system = 3.2;
  pInfo->mem_total = 3.3;
  pInfo->disk_engine = 4.1;
  pInfo->disk_used = 4.2;
  pInfo->disk_total = 4.3;
  pInfo->net_in = 5.1;
  pInfo->net_out = 5.2;
  pInfo->io_read = 6.1;
  pInfo->io_write = 6.2;
  pInfo->io_read_disk = 7.1;
  pInfo->io_write_disk = 7.2;
  pInfo->req_select = 8;
  pInfo->req_insert = 9;
  pInfo->req_insert_success = 10;
  pInfo->req_insert_batch = 11;
  pInfo->req_insert_batch_success = 12;
  pInfo->errors = 4;
  pInfo->vnodes_num = 5;
  pInfo->masters = 6;
  pInfo->has_mnode = 1;
}

void MonitorTest::GetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) {
  pInfo->datadirs = taosArrayInit(2, sizeof(SMonDiskDesc));
  SMonDiskDesc d1 = {0};
  strcpy(d1.name, "/t1/d1/d");
  d1.level = 0;
  d1.size.avail = 11;
  d1.size.total = 12;
  d1.size.used = 13;
  taosArrayPush(pInfo->datadirs, &d1);

  SMonDiskDesc d2 = {0};
  strcpy(d2.name, "/t2d2/d");
  d2.level = 2;
  d2.size.avail = 21;
  d2.size.total = 22;
  d2.size.used = 23;
  taosArrayPush(pInfo->datadirs, &d2);

  SMonDiskDesc d3 = {0};
  strcpy(d3.name, "/t3/d3/d");
  d3.level = 3;
  d3.size.avail = 31;
  d3.size.total = 32;
  d3.size.used = 33;
  taosArrayPush(pInfo->datadirs, &d3);

  strcpy(pInfo->logdir.name, "/log/dir/d");
  pInfo->logdir.size.avail = 41;
  pInfo->logdir.size.total = 42;
  pInfo->logdir.size.used = 43;

  strcpy(pInfo->tempdir.name, "/data/dir/d");
  pInfo->tempdir.size.avail = 51;
  pInfo->tempdir.size.total = 52;
  pInfo->tempdir.size.used = 53;
}

void MonitorTest::AddLogInfo1() {
S
Shengliang Guan 已提交
193 194 195
  monRecordLog(taosGetTimestampMs(), DEBUG_INFO, "1 -------------------------- a");
  monRecordLog(taosGetTimestampMs(), DEBUG_ERROR, "1 ------------------------ b");
  monRecordLog(taosGetTimestampMs(), DEBUG_DEBUG, "1 ------- c");
S
monitor  
Shengliang Guan 已提交
196 197 198
}

void MonitorTest::AddLogInfo2() {
S
Shengliang Guan 已提交
199 200
  monRecordLog(taosGetTimestampMs(), DEBUG_ERROR, "2 ------- a");
  monRecordLog(taosGetTimestampMs(), DEBUG_ERROR, "2 ------- b");
S
monitor  
Shengliang Guan 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
}

TEST_F(MonitorTest, 01_Full) {
  AddLogInfo1();

  SMonInfo *pMonitor = monCreateMonitorInfo();
  if (pMonitor == NULL) return;

  SMonBasicInfo basicInfo = {0};
  GetBasicInfo(pMonitor, &basicInfo);
  monSetBasicInfo(pMonitor, &basicInfo);

  SMonClusterInfo clusterInfo = {0};
  SMonVgroupInfo  vgroupInfo = {0};
  SMonGrantInfo   grantInfo = {0};
  GetClusterInfo(pMonitor, &clusterInfo);
  GetVgroupInfo(pMonitor, &vgroupInfo);
  GetGrantInfo(pMonitor, &grantInfo);
  monSetClusterInfo(pMonitor, &clusterInfo);
  monSetVgroupInfo(pMonitor, &vgroupInfo);
  monSetGrantInfo(pMonitor, &grantInfo);

  SMonDnodeInfo dnodeInfo = {0};
  GetDnodeInfo(pMonitor, &dnodeInfo);
  monSetDnodeInfo(pMonitor, &dnodeInfo);

  SMonDiskInfo diskInfo = {0};
  GetDiskInfo(pMonitor, &diskInfo);
  monSetDiskInfo(pMonitor, &diskInfo);

  monSendReport(pMonitor);
  monCleanupMonitorInfo(pMonitor);

  taosArrayDestroy(clusterInfo.dnodes);
  taosArrayDestroy(clusterInfo.mnodes);
  taosArrayDestroy(vgroupInfo.vgroups);
  taosArrayDestroy(diskInfo.datadirs);
}

TEST_F(MonitorTest, 02_Log) {
  AddLogInfo2();

  SMonInfo *pMonitor = monCreateMonitorInfo();
  if (pMonitor == NULL) return;
S
Shengliang Guan 已提交
245

S
monitor  
Shengliang Guan 已提交
246 247
  monSendReport(pMonitor);
  monCleanupMonitorInfo(pMonitor);
S
Shengliang Guan 已提交
248
}