提交 2f97bd24 编写于 作者: S Shengliang Guan

unitest for config

上级 48a2d415
......@@ -25,12 +25,12 @@ extern "C" {
typedef enum {
CFG_TYPE_DEFAULT,
CFG_TYPE_TAOS_CFG,
CFG_TYPE_CFG_FILE,
CFG_TYPE_DOT_ENV,
CFG_TYPE_ENV_VAR,
CFG_TYPE_APOLLO_URL,
CFG_TYPE_CONSOLE_PARA
} ECfgType;
} ECfgSrcType;
typedef enum {
CFG_DTYPE_NONE,
......@@ -63,7 +63,7 @@ typedef enum {
} ECfgUnitType;
typedef struct SConfigItem {
ECfgType stype;
ECfgSrcType stype;
ECfgUnitType utype;
ECfgDataType dtype;
char *name;
......@@ -90,7 +90,7 @@ typedef struct SConfigItem {
typedef struct SConfig SConfig;
SConfig *cfgInit();
int32_t cfgLoad(SConfig *pConfig, ECfgType cfgType, const char *sourceStr);
int32_t cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr);
void cfgCleanup(SConfig *pConfig);
int32_t cfgGetSize(SConfig *pConfig);
......@@ -115,6 +115,10 @@ int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal,
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
int32_t cfgAddFile(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
const char *cfgStypeStr(ECfgSrcType type);
const char *cfgDtypeStr(ECfgDataType type);
const char *cfgUtypeStr(ECfgUnitType type);
#ifdef __cplusplus
}
#endif
......
......@@ -27,7 +27,7 @@ extern "C" {
#endif
typedef struct SConfig {
ECfgType stype;
ECfgSrcType stype;
SHashObj *hash;
} SConfig;
......
......@@ -33,9 +33,9 @@ SConfig *cfgInit() {
return pConfig;
}
int32_t cfgLoad(SConfig *pConfig, ECfgType cfgType, const char *sourceStr) {
int32_t cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr) {
switch (cfgType) {
case CFG_TYPE_TAOS_CFG:
case CFG_TYPE_CFG_FILE:
return cfgLoadFromTaosFile(pConfig, sourceStr);
case CFG_TYPE_DOT_ENV:
return cfgLoadFromDotEnvFile(pConfig, sourceStr);
......@@ -49,10 +49,12 @@ int32_t cfgLoad(SConfig *pConfig, ECfgType cfgType, const char *sourceStr) {
}
void cfgCleanup(SConfig *pConfig) {
if (pConfig == NULL) return;
if (pConfig->hash != NULL) {
taosHashCleanup(pConfig->hash);
pConfig->hash == NULL;
if (pConfig != NULL) {
if (pConfig->hash != NULL) {
taosHashCleanup(pConfig->hash);
pConfig->hash == NULL;
}
free(pConfig);
}
}
......@@ -68,7 +70,7 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name
pItem->stype = CFG_TYPE_DEFAULT;
pItem->utype = utype;
pItem->name = strdup(name);
if (pItem->name != NULL) {
if (pItem->name == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
......@@ -152,7 +154,7 @@ int32_t cfgAddDouble(SConfig *pConfig, const char *name, double defaultVal, ECfg
int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_STRING};
item.strVal = strdup(defaultVal);
if (item.strVal != NULL) {
if (item.strVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
......@@ -162,7 +164,7 @@ int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal,
int32_t cfgAddFqdn(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_FQDN};
item.fqdnVal = strdup(defaultVal);
if (item.fqdnVal != NULL) {
if (item.fqdnVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
......@@ -172,7 +174,7 @@ int32_t cfgAddFqdn(SConfig *pConfig, const char *name, const char *defaultVal, E
int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_IPSTR};
item.ipstrVal = strdup(defaultVal);
if (item.ipstrVal != NULL) {
if (item.ipstrVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
......@@ -182,7 +184,7 @@ int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal,
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_DIR};
item.dirVal = strdup(defaultVal);
if (item.dirVal != NULL) {
if (item.dirVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
......@@ -192,9 +194,88 @@ int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, EC
int32_t cfgAddFile(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_FILE};
item.fileVal = strdup(defaultVal);
if (item.fileVal != NULL) {
if (item.fileVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
return cfgAddItem(pConfig, &item, name, utype);
}
const char *cfgStypeStr(ECfgSrcType type) {
switch (type) {
case CFG_TYPE_DEFAULT:
return "default";
case CFG_TYPE_CFG_FILE:
return "cfg";
case CFG_TYPE_DOT_ENV:
return ".env";
case CFG_TYPE_ENV_VAR:
return "env";
case CFG_TYPE_APOLLO_URL:
return "apollo";
default:
return "invalid";
}
}
const char *cfgDtypeStr(ECfgDataType type) {
switch (type) {
case CFG_DTYPE_NONE:
return "none";
case CFG_DTYPE_BOOL:
return "bool";
case CFG_DTYPE_INT8:
return "int8";
case CFG_DTYPE_UINT8:
return "uint8";
case CFG_DTYPE_INT16:
return "int16";
case CFG_DTYPE_UINT16:
return "uint16";
case CFG_DTYPE_INT32:
return "int32";
case CFG_DTYPE_UINT32:
return "uint32";
case CFG_DTYPE_INT64:
return "int64";
case CFG_DTYPE_UINT64:
return "uint64";
case CFG_DTYPE_FLOAT:
return "float";
case CFG_DTYPE_DOUBLE:
return "double";
case CFG_DTYPE_STRING:
return "string";
case CFG_DTYPE_FQDN:
return "fqdn";
case CFG_DTYPE_IPSTR:
return "ipstr";
case CFG_DTYPE_DIR:
return "dir";
case CFG_DTYPE_FILE:
return "file";
default:
return "invalid";
}
}
const char *cfgUtypeStr(ECfgUnitType type) {
switch (type) {
case CFG_UTYPE_NONE:
return "";
case CFG_UTYPE_PERCENT:
return "(%)";
case CFG_UTYPE_GB:
return "(GB)";
case CFG_UTYPE_MB:
return "(Mb)";
case CFG_UTYPE_BYTE:
return "(byte)";
case CFG_UTYPE_SECOND:
return "(s)";
case CFG_UTYPE_MS:
return "(ms)";
default:
return "invalid";
}
}
\ No newline at end of file
......@@ -28,84 +28,124 @@ class CfgTest : public ::testing::Test {
const char *CfgTest::pConfig;
TEST_F(CfgTest, 01_Basic) {
TEST_F(CfgTest, 02_Str) {
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_DEFAULT), "default");
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_CFG_FILE), "cfg");
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_DOT_ENV), ".env");
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_ENV_VAR), "env");
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_APOLLO_URL), "apollo");
EXPECT_STREQ(cfgStypeStr(ECfgSrcType(1024)), "invalid");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_NONE), "none");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_BOOL), "bool");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT8), "int8");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_UINT8), "uint8");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT16), "int16");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_UINT16), "uint16");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT32), "int32");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_UINT32), "uint32");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT64), "int64");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_UINT64), "uint64");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_FLOAT), "float");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DOUBLE), "double");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_STRING), "string");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_FQDN), "fqdn");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_IPSTR), "ipstr");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DIR), "dir");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_FILE), "file");
EXPECT_STREQ(cfgDtypeStr(ECfgDataType(1024)), "invalid");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_NONE), "");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_PERCENT), "(%)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_GB), "(GB)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_MB), "(Mb)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_BYTE), "(byte)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_SECOND), "(s)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_MS), "(ms)");
EXPECT_STREQ(cfgUtypeStr(ECfgUnitType(1024)), "invalid");
}
TEST_F(CfgTest, 02_Basic) {
SConfig *pConfig = cfgInit();
cfgAddBool(pConfig, "test_bool", 0, CFG_UTYPE_NONE);
cfgAddInt8(pConfig, "test_int8", 1, CFG_UTYPE_GB);
cfgAddUInt8(pConfig, "test_uint8", 2, CFG_UTYPE_MB);
cfgAddInt16(pConfig, "test_int16", 3, CFG_UTYPE_BYTE);
cfgAddUInt16(pConfig, "test_uint16", 4, CFG_UTYPE_SECOND);
cfgAddInt32(pConfig, "test_int32", 5, CFG_UTYPE_MS);
cfgAddUInt32(pConfig, "test_uint32", 6, CFG_UTYPE_PERCENT);
cfgAddInt64(pConfig, "test_int64", 7, CFG_UTYPE_NONE);
cfgAddUInt64(pConfig, "test_uint64", 8, CFG_UTYPE_NONE);
cfgAddFloat(pConfig, "test_float", 9, CFG_UTYPE_NONE);
cfgAddDouble(pConfig, "test_double", 10, CFG_UTYPE_NONE);
cfgAddString(pConfig, "test_string", "11", CFG_UTYPE_NONE);
cfgAddFqdn(pConfig, "test_fqdn", "localhost", CFG_UTYPE_NONE);
cfgAddIpStr(pConfig, "test_ipstr", "192.168.0.1", CFG_UTYPE_NONE);
cfgAddDir(pConfig, "test_dir", "/tmp", CFG_UTYPE_NONE);
cfgAddFile(pConfig, "test_file", "/tmp/file1", CFG_UTYPE_NONE);
ASSERT_NE(pConfig, nullptr);
EXPECT_EQ(cfgAddBool(pConfig, "test_bool", 0, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddInt8(pConfig, "test_int8", 1, CFG_UTYPE_GB), 0);
EXPECT_EQ(cfgAddUInt8(pConfig, "test_uint8", 2, CFG_UTYPE_MB), 0);
EXPECT_EQ(cfgAddInt16(pConfig, "test_int16", 3, CFG_UTYPE_BYTE), 0);
EXPECT_EQ(cfgAddUInt16(pConfig, "test_uint16", 4, CFG_UTYPE_SECOND), 0);
EXPECT_EQ(cfgAddInt32(pConfig, "test_int32", 5, CFG_UTYPE_MS), 0);
EXPECT_EQ(cfgAddUInt32(pConfig, "test_uint32", 6, CFG_UTYPE_PERCENT), 0);
EXPECT_EQ(cfgAddInt64(pConfig, "test_int64", 7, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddUInt64(pConfig, "test_uint64", 8, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddFloat(pConfig, "test_float", 9, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddDouble(pConfig, "test_double", 10, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddString(pConfig, "test_string", "11", CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddFqdn(pConfig, "test_fqdn", "localhost", CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddIpStr(pConfig, "test_ipstr", "192.168.0.1", CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddDir(pConfig, "test_dir", "/tmp", CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddFile(pConfig, "test_file", "/tmp/file1", CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgGetSize(pConfig), 16);
int32_t size = 0;
SConfigItem *pItem = cfgIterate(pConfig, NULL);
while (pItem != NULL) {
pItem = cfgIterate(pConfig, pItem);
switch (pItem->dtype) {
case CFG_DTYPE_BOOL:
printf("cfg:%s, value:%d\n", pItem->name, pItem->boolVal);
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->boolVal);
break;
CFG_DTYPE_INT8:
printf("cfg:%s, value:%d\n", pItem->name, pItem->uint8Val);
case CFG_DTYPE_INT8:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint8Val);
break;
CFG_DTYPE_UINT8:
printf("cfg:%s, value:%d\n", pItem->name, pItem->int8Val);
case CFG_DTYPE_UINT8:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int8Val);
break;
CFG_DTYPE_INT16:
printf("cfg:%s, value:%d\n", pItem->name, pItem->uint16Val);
case CFG_DTYPE_INT16:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint16Val);
break;
CFG_DTYPE_UINT16:
printf("cfg:%s, value:%d\n", pItem->name, pItem->int16Val);
case CFG_DTYPE_UINT16:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int16Val);
break;
CFG_DTYPE_INT32:
printf("cfg:%s, value:%d\n", pItem->name, pItem->uint32Val);
case CFG_DTYPE_INT32:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint32Val);
break;
CFG_DTYPE_UINT32:
printf("cfg:%s, value:%d\n", pItem->name, pItem->int32Val);
case CFG_DTYPE_UINT32:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int32Val);
break;
CFG_DTYPE_INT64:
printf("cfg:%s, value:%" PRIu64, pItem->name, pItem->uint64Val);
case CFG_DTYPE_INT64:
printf("index:%d, cfg:%s value:%" PRIu64 "\n", size, pItem->name, pItem->uint64Val);
break;
CFG_DTYPE_UINT64:
printf("cfg:%s, value:%" PRId64, pItem->name, pItem->int64Val);
case CFG_DTYPE_UINT64:
printf("index:%d, cfg:%s value:%" PRId64 "\n", size, pItem->name, pItem->int64Val);
break;
CFG_DTYPE_FLOAT:
printf("cfg:%s, value:%f\n", pItem->name, pItem->floatVal);
case CFG_DTYPE_FLOAT:
printf("index:%d, cfg:%s value:%f\n", size, pItem->name, pItem->floatVal);
break;
CFG_DTYPE_DOUBLE:
printf("cfg:%s, value:%f\n", pItem->name, pItem->doubleVal);
case CFG_DTYPE_DOUBLE:
printf("index:%d, cfg:%s value:%f\n", size, pItem->name, pItem->doubleVal);
break;
CFG_DTYPE_STRING:
printf("cfg:%s, value:%s\n", pItem->name, pItem->strVal);
case CFG_DTYPE_STRING:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->strVal);
break;
CFG_DTYPE_FQDN:
printf("cfg:%s, value:%s\n", pItem->name, pItem->fqdnVal);
case CFG_DTYPE_FQDN:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->fqdnVal);
break;
CFG_DTYPE_IPSTR:
printf("cfg:%s, value:%s\n", pItem->name, pItem->ipstrVal);
case CFG_DTYPE_IPSTR:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->ipstrVal);
break;
CFG_DTYPE_DIR:
printf("cfg:%s, value:%s\n", pItem->name, pItem->dirVal);
case CFG_DTYPE_DIR:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->dirVal);
break;
CFG_DTYPE_FILE:
printf("cfg:%s, value:%s\n", pItem->name, pItem->fileVal);
case CFG_DTYPE_FILE:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->fileVal);
break;
default:
printf("index:%d, cfg:%s invalid cfg dtype:%d\n", size, pItem->name, pItem->dtype);
break;
}
size++;
pItem = cfgIterate(pConfig, pItem);
}
cfgCancelIterate(pConfig, pItem);
......@@ -162,8 +202,8 @@ TEST_F(CfgTest, 01_Basic) {
pItem = cfgGetItem(pConfig, "test_int64");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_SECOND);
EXPECT_EQ(pItem->dtype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT64);
EXPECT_STREQ(pItem->name, "test_int64");
EXPECT_EQ(pItem->int64Val, 7);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册