mirror of
https://github.com/emqx/neuron.git
synced 2026-10-17 04:40:42 +08:00
add function test for group config api
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
class GrpConfig(object):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def Get_Interval_In_Group_Config(self, gconfigs, gname):
|
||||
for gconfig in gconfigs[0]:
|
||||
if gconfig['name'] == gname:
|
||||
return gconfig['interval']
|
||||
return 0
|
||||
|
||||
def Group_Config_Size(self, gconfigs):
|
||||
size = 0
|
||||
for gconfig in gconfigs[0]:
|
||||
size = size + 1
|
||||
return size
|
||||
@@ -1,7 +1,7 @@
|
||||
import subprocess
|
||||
|
||||
|
||||
class NeuronLib(object):
|
||||
class Neuron(object):
|
||||
process = 0
|
||||
|
||||
def __init__(self):
|
||||
@@ -1,8 +1,4 @@
|
||||
import json
|
||||
|
||||
|
||||
class NodeLib(object):
|
||||
process = 0
|
||||
class Node(object):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
@@ -7,3 +7,4 @@ ${err_plugin_name_not_found} -2000
|
||||
${err_node_type_invalid} -2001
|
||||
${err_node_exist} -2002
|
||||
${err_node_not_exist} -2003
|
||||
${err_grpconfig_not_exist} -2004
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
*** Settings ***
|
||||
Library Keyword.Neuron.Neuron
|
||||
Library Keyword.Node.Node
|
||||
Library Keyword.GrpConfig.GrpConfig
|
||||
Resource error.robot
|
||||
Resource neuron.robot
|
||||
Library REST http://127.0.0.1:7001
|
||||
Suite Setup Neuron Context Ready
|
||||
Suite Teardown Stop Neuron
|
||||
|
||||
*** Variables ***
|
||||
${test_gconfig} "test_gconfig"
|
||||
|
||||
*** Test Cases ***
|
||||
Add group config under non-existent node, it should return failure
|
||||
POST /api/v2/gconfig {"name": ${test_gconfig}, "node_id": 1234, "interval": 1000}
|
||||
Integer response status 400
|
||||
Integer response body error ${err_node_not_exist}
|
||||
|
||||
Add group config under existent node, it should return success
|
||||
${test_node_id} Get Test Node ID
|
||||
POST /api/v2/gconfig {"name": ${test_gconfig}, "node_id": ${test_node_id}, "interval": 1000}
|
||||
Integer response status 201
|
||||
|
||||
Delete a non-existent group config, it should return failure
|
||||
${test_node_id} Get Test Node ID
|
||||
DELETE /api/v2/gconfig {"node_id": ${test_node_id}, "name": "xxxx"}
|
||||
Integer response status 404
|
||||
Integer response body error ${err_grpconfig_not_exist}
|
||||
|
||||
Delete a group config from non-existent node, it should return failure
|
||||
DELETE /api/v2/gconfig {"node_id": 1234, "name": ${test_gconfig}}
|
||||
Integer response status 404
|
||||
Integer response body error ${err_grpconfig_not_exist}
|
||||
|
||||
Delete a group config, it should return success
|
||||
${test_node_id} Get Test Node ID
|
||||
DELETE /api/v2/gconfig {"node_id": ${test_node_id}, "name": ${test_gconfig}}
|
||||
Integer response status 200
|
||||
|
||||
Update a non-existent group config, it should return failure
|
||||
${test_node_id} Get Test Node ID
|
||||
PUT /api/v2/gconfig {"node_id": ${test_node_id}, "name": "xxxx", "interval": 2000}
|
||||
Integer response status 404
|
||||
Integer response body error ${err_grpconfig_not_exist}
|
||||
|
||||
Update a group config from non-existent node, it should return failure
|
||||
PUT /api/v2/gconfig {"node_id": 1234, "name": ${test_gconfig}, "interval": 2000}
|
||||
Integer response status 404
|
||||
Integer response body error ${err_grpconfig_not_exist}
|
||||
|
||||
Add a group config to the node, then get the group config from this node, it should return the group config
|
||||
${test_node_id} Get Test Node ID
|
||||
POST /api/v2/gconfig {"name": ${test_gconfig}, "node_id": ${test_node_id}, "interval": 1000}
|
||||
Integer response status 201
|
||||
GET /api/v2/gconfig?node_id=${test_node_id}
|
||||
${tmp} Array $.group_configs
|
||||
${size} Group Config Size ${tmp}
|
||||
should be equal as integers ${size} 1
|
||||
${interval} Get Interval In Group Config ${tmp} test_gconfig
|
||||
should be equal as integers ${interval} 1000
|
||||
|
||||
Update the interval of the test group config, it should return success
|
||||
${test_node_id} Get Test Node ID
|
||||
${test_node_id} Get Test Node ID
|
||||
PUT /api/v2/gconfig {"node_id": ${test_node_id}, "name": ${test_gconfig}, "interval": 2000}
|
||||
Integer response status 200
|
||||
GET /api/v2/gconfig?node_id=${test_node_id}
|
||||
${tmp} Array $.group_configs
|
||||
${interval} Get Interval In Group Config ${tmp} test_gconfig
|
||||
should be equal as integers ${interval} 2000
|
||||
|
||||
|
||||
|
||||
*** Keywords ***
|
||||
Get Test Node ID
|
||||
GET /api/v2/node?type=1
|
||||
Integer response status 200
|
||||
${tmp} Array $.nodes
|
||||
${test_node_id} Get Node By Name ${tmp} test-node
|
||||
[RETURN] ${test_node_id}
|
||||
Add Test Node
|
||||
POST /api/v2/node {"type": 1, "name": "test-node", "plugin_name": "modbus-tcp-plugin"}
|
||||
Integer response status 201
|
||||
Integer response body error ${err_success}
|
||||
${test_node_id} Get Test Node ID
|
||||
should not be equal ${test_node_id} 0
|
||||
Neuron Context Ready
|
||||
Neuron Ready
|
||||
Add Test Node
|
||||
@@ -1,6 +1,6 @@
|
||||
*** Settings ***
|
||||
Library Keyword.NeuronLib.NeuronLib
|
||||
Library Keyword.NodeLib.NodeLib
|
||||
Library Keyword.Neuron.Neuron
|
||||
Library Keyword.Node.Node
|
||||
Resource error.robot
|
||||
Resource neuron.robot
|
||||
Library REST http://127.0.0.1:7001
|
||||
@@ -68,6 +68,7 @@ typedef enum {
|
||||
NEU_ERR_NODE_TYPE_INVALID = -2001,
|
||||
NEU_ERR_NODE_EXIST = -2002,
|
||||
NEU_ERR_NODE_NOT_EXIST = -2003,
|
||||
NEU_ERR_GRP_CONFIG_NOT_EXIST = -2004,
|
||||
} neu_err_code_e;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -36,17 +36,21 @@ void handle_add_group_config(nng_aio *aio)
|
||||
{
|
||||
neu_plugin_t *plugin = neu_rest_get_plugin();
|
||||
|
||||
REST_PROCESS_HTTP_REQUEST(
|
||||
REST_PROCESS_HTTP_REQUEST_WITH_ERROR(
|
||||
aio, neu_parse_add_group_config_req_t,
|
||||
neu_parse_decode_add_group_config, {
|
||||
neu_taggrp_config_t *gconfig = neu_taggrp_cfg_new(req->name);
|
||||
neu_taggrp_cfg_set_interval(gconfig, req->interval);
|
||||
intptr_t err =
|
||||
error.error =
|
||||
neu_system_add_group_config(plugin, req->node_id, gconfig);
|
||||
if (err != 0) {
|
||||
http_bad_request(aio, "{\"error\": 1}");
|
||||
|
||||
neu_json_encode_by_fn(&error, neu_parse_encode_error,
|
||||
&result_error);
|
||||
if (error.error != 0) {
|
||||
http_bad_request(aio, result_error);
|
||||
neu_taggrp_cfg_free(gconfig);
|
||||
} else {
|
||||
http_ok(aio, "{\"error\": 0}");
|
||||
http_created(aio, result_error);
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -55,17 +59,19 @@ void handle_del_group_config(nng_aio *aio)
|
||||
{
|
||||
neu_plugin_t *plugin = neu_rest_get_plugin();
|
||||
|
||||
REST_PROCESS_HTTP_REQUEST(
|
||||
REST_PROCESS_HTTP_REQUEST_WITH_ERROR(
|
||||
aio, neu_parse_del_group_config_req_t,
|
||||
neu_parse_decode_del_group_config, {
|
||||
neu_taggrp_config_t *config =
|
||||
neu_system_find_group_config(plugin, req->node_id, req->name);
|
||||
neu_datatag_table_t *table =
|
||||
neu_system_get_datatags_table(plugin, req->node_id);
|
||||
intptr_t err = 0;
|
||||
|
||||
if (config == NULL || table == NULL) {
|
||||
http_not_found(aio, "{\"error\" : 1}");
|
||||
error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST;
|
||||
neu_json_encode_by_fn(&error, neu_parse_encode_error,
|
||||
&result_error);
|
||||
http_not_found(aio, result_error);
|
||||
} else {
|
||||
vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config);
|
||||
|
||||
@@ -76,12 +82,14 @@ void handle_del_group_config(nng_aio *aio)
|
||||
neu_datatag_tbl_remove(table, *id);
|
||||
}
|
||||
|
||||
err = neu_system_del_group_config(plugin, req->node_id,
|
||||
req->name);
|
||||
if (err != 0) {
|
||||
http_bad_request(aio, "{\"error\": 1}");
|
||||
error.error = neu_system_del_group_config(plugin, req->node_id,
|
||||
req->name);
|
||||
neu_json_encode_by_fn(&error, neu_parse_encode_error,
|
||||
&result_error);
|
||||
if (error.error != 0) {
|
||||
http_bad_request(aio, result_error);
|
||||
} else {
|
||||
http_ok(aio, "{\"error\": 0}");
|
||||
http_ok(aio, result_error);
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -91,22 +99,27 @@ void handle_update_group_config(nng_aio *aio)
|
||||
{
|
||||
neu_plugin_t *plugin = neu_rest_get_plugin();
|
||||
|
||||
REST_PROCESS_HTTP_REQUEST(
|
||||
REST_PROCESS_HTTP_REQUEST_WITH_ERROR(
|
||||
aio, neu_parse_update_group_config_req_t,
|
||||
neu_parse_decode_update_group_config, {
|
||||
neu_taggrp_config_t *config =
|
||||
neu_system_find_group_config(plugin, req->node_id, req->name);
|
||||
|
||||
if (config == NULL) {
|
||||
http_not_found(aio, "{\"error\": 1}");
|
||||
error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST;
|
||||
neu_json_encode_by_fn(&error, neu_parse_encode_error,
|
||||
&result_error);
|
||||
http_not_found(aio, result_error);
|
||||
} else {
|
||||
neu_taggrp_cfg_set_interval(config, req->interval);
|
||||
intptr_t err = neu_system_update_group_config(
|
||||
error.error = neu_system_update_group_config(
|
||||
plugin, req->node_id, config);
|
||||
if (err != 0) {
|
||||
http_bad_request(aio, "{\"error\": 1}");
|
||||
neu_json_encode_by_fn(&error, neu_parse_encode_error,
|
||||
&result_error);
|
||||
if (error.error != 0) {
|
||||
http_bad_request(aio, result_error);
|
||||
} else {
|
||||
http_ok(aio, "{\"error\": 0}");
|
||||
http_ok(aio, result_error);
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -114,12 +127,15 @@ void handle_update_group_config(nng_aio *aio)
|
||||
|
||||
void handle_get_group_config(nng_aio *aio)
|
||||
{
|
||||
neu_plugin_t *plugin = neu_rest_get_plugin();
|
||||
char * result = NULL;
|
||||
char * s_node_id = http_get_param(aio, "id");
|
||||
neu_plugin_t * plugin = neu_rest_get_plugin();
|
||||
char * result = NULL;
|
||||
char * s_node_id = http_get_param(aio, "node_id");
|
||||
neu_parse_error_t error = { 0 };
|
||||
|
||||
if (s_node_id == NULL) {
|
||||
http_bad_request(aio, "{\"error\": 1}");
|
||||
error.error = NEU_ERR_PARAM_IS_WRONG;
|
||||
neu_json_encode_by_fn(&error, neu_parse_encode_error, &result);
|
||||
http_bad_request(aio, result);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+6
-19
@@ -527,28 +527,19 @@ static int adapter_command(neu_adapter_t *adapter, neu_request_t *cmd,
|
||||
}
|
||||
|
||||
case NEU_REQRESP_ADD_GRP_CONFIG: {
|
||||
ADAPTER_RESP_CODE(adapter, cmd, intptr_t, neu_cmd_add_grp_config_t, rv,
|
||||
NEU_REQRESP_ERR_CODE, p_result, {
|
||||
ret = NEU_ERR_SUCCESS;
|
||||
rv = neu_manager_add_grp_config(adapter->manager,
|
||||
req_cmd);
|
||||
if (rv != 0) {
|
||||
ret = NEU_ERR_FAILURE;
|
||||
}
|
||||
});
|
||||
ADAPTER_RESP_CODE(
|
||||
adapter, cmd, intptr_t, neu_cmd_add_grp_config_t, rv,
|
||||
NEU_REQRESP_ERR_CODE, p_result,
|
||||
{ ret = neu_manager_add_grp_config(adapter->manager, req_cmd); });
|
||||
break;
|
||||
}
|
||||
|
||||
case NEU_REQRESP_DEL_GRP_CONFIG: {
|
||||
ADAPTER_RESP_CODE(adapter, cmd, intptr_t, neu_cmd_del_grp_config_t, rv,
|
||||
NEU_REQRESP_ERR_CODE, p_result, {
|
||||
ret = NEU_ERR_SUCCESS;
|
||||
rv = neu_manager_del_grp_config(
|
||||
ret = neu_manager_del_grp_config(
|
||||
adapter->manager, req_cmd->node_id,
|
||||
req_cmd->config_name);
|
||||
if (rv != 0) {
|
||||
ret = NEU_ERR_FAILURE;
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
@@ -556,12 +547,8 @@ static int adapter_command(neu_adapter_t *adapter, neu_request_t *cmd,
|
||||
case NEU_REQRESP_UPDATE_GRP_CONFIG: {
|
||||
ADAPTER_RESP_CODE(adapter, cmd, intptr_t, neu_cmd_update_grp_config_t,
|
||||
rv, NEU_REQRESP_ERR_CODE, p_result, {
|
||||
ret = NEU_ERR_SUCCESS;
|
||||
rv = neu_manager_update_grp_config(
|
||||
ret = neu_manager_update_grp_config(
|
||||
adapter->manager, req_cmd);
|
||||
if (rv != 0) {
|
||||
ret = NEU_ERR_FAILURE;
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
+10
-8
@@ -1587,7 +1587,7 @@ int neu_manager_add_grp_config(neu_manager_t * manager,
|
||||
reg_entity = find_reg_adapter_by_id(&manager->reg_adapters, adapter_id);
|
||||
if (reg_entity == NULL) {
|
||||
log_error("Can't find matched src registered adapter");
|
||||
rv = -1;
|
||||
rv = NEU_ERR_NODE_NOT_EXIST;
|
||||
goto add_grp_config_exit;
|
||||
}
|
||||
|
||||
@@ -1600,13 +1600,13 @@ add_grp_config_exit:
|
||||
int neu_manager_del_grp_config(neu_manager_t *manager, neu_node_id_t node_id,
|
||||
const char *config_name)
|
||||
{
|
||||
int rv = 0;
|
||||
int rv = NEU_ERR_SUCCESS;
|
||||
adapter_id_t adapter_id;
|
||||
adapter_reg_entity_t *reg_entity;
|
||||
|
||||
if (manager == NULL || config_name == NULL) {
|
||||
log_error("Delete group config NULL manager or config_name");
|
||||
return -1;
|
||||
return NEU_ERR_EINTERNAL;
|
||||
}
|
||||
|
||||
nng_mtx_lock(manager->adapters_mtx);
|
||||
@@ -1614,7 +1614,7 @@ int neu_manager_del_grp_config(neu_manager_t *manager, neu_node_id_t node_id,
|
||||
reg_entity = find_reg_adapter_by_id(&manager->reg_adapters, adapter_id);
|
||||
if (reg_entity == NULL) {
|
||||
log_error("Can't find matched registered adapter");
|
||||
rv = -1;
|
||||
rv = NEU_ERR_NODE_NOT_EXIST;
|
||||
goto del_grp_config_exit;
|
||||
}
|
||||
|
||||
@@ -1622,6 +1622,7 @@ int neu_manager_del_grp_config(neu_manager_t *manager, neu_node_id_t node_id,
|
||||
config_name);
|
||||
if (rv != 0) {
|
||||
log_error("Failed to delete datatag group config: %s", config_name);
|
||||
rv = NEU_ERR_EINTERNAL;
|
||||
}
|
||||
del_grp_config_exit:
|
||||
nng_mtx_unlock(manager->adapters_mtx);
|
||||
@@ -1631,18 +1632,18 @@ del_grp_config_exit:
|
||||
int neu_manager_update_grp_config(neu_manager_t * manager,
|
||||
neu_cmd_update_grp_config_t *cmd)
|
||||
{
|
||||
int rv = 0;
|
||||
int rv = NEU_ERR_SUCCESS;
|
||||
adapter_id_t adapter_id;
|
||||
adapter_reg_entity_t *reg_entity;
|
||||
|
||||
if (manager == NULL || cmd == NULL) {
|
||||
log_error("Update group config with NULL manager or command");
|
||||
return -1;
|
||||
return NEU_ERR_EINTERNAL;
|
||||
}
|
||||
|
||||
if (cmd->grp_config == NULL) {
|
||||
log_error("group config is NULL");
|
||||
return -1;
|
||||
return NEU_ERR_EINTERNAL;
|
||||
}
|
||||
|
||||
nng_mtx_lock(manager->adapters_mtx);
|
||||
@@ -1650,7 +1651,7 @@ int neu_manager_update_grp_config(neu_manager_t * manager,
|
||||
reg_entity = find_reg_adapter_by_id(&manager->reg_adapters, adapter_id);
|
||||
if (reg_entity == NULL) {
|
||||
log_error("Can't find matched src or dst registered adapter");
|
||||
rv = -1;
|
||||
rv = NEU_ERR_NODE_NOT_EXIST;
|
||||
goto update_grp_config_exit;
|
||||
}
|
||||
|
||||
@@ -1660,6 +1661,7 @@ int neu_manager_update_grp_config(neu_manager_t * manager,
|
||||
log_error("Failed to update datatag group config: %s",
|
||||
neu_taggrp_cfg_get_name(cmd->grp_config));
|
||||
neu_taggrp_cfg_free(cmd->grp_config);
|
||||
rv = NEU_ERR_EINTERNAL;
|
||||
}
|
||||
|
||||
update_grp_config_exit:
|
||||
|
||||
Reference in New Issue
Block a user