Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add http api ft #1686

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ jobs:
- name: function test
run: |
if [ "${{ matrix.plugin }}" = "core" ]; then
pytest -s -v tests/ft --ignore=tests/ft/test_modbus.py --ignore=tests/ft/test_metrics.py
pytest -s -v tests/ft --ignore=tests/ft/driver/test_modbus.py --ignore=tests/ft/metrics/test_metrics.py
elif [ "${{ matrix.plugin }}" = "modbus" ]; then
pytest -s -v tests/ft/driver/"test_modbus.py"
pytest -s -v tests/ft/driver/test_modbus.py
else
pytest -s -v tests/ft/metrics/"test_metrics.py"
pytest -s -v tests/ft/metrics/test_metrics.py
fi

- name: create cov report
Expand Down
9 changes: 0 additions & 9 deletions plugins/restful/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ static struct neu_http_handler cors_handler[] = {
{
.url = "/api/v2/password",
},
{
.url = "/api/v2/logs",
},
{
.url = "/api/v2/log/level",
},
Expand Down Expand Up @@ -366,12 +363,6 @@ static struct neu_http_handler rest_handlers[] = {
.url = "/api/v2/node/state",
.value.handler = handle_get_node_state,
},
{
.method = NEU_HTTP_METHOD_GET,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/logs",
.value.handler = handle_logs_files,
},
{
.method = NEU_HTTP_METHOD_PUT,
.type = NEU_HTTP_HANDLER_FUNCTION,
Expand Down
64 changes: 0 additions & 64 deletions plugins/restful/log_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,70 +42,6 @@
#include "log_handle.h"
#include "utils/utarray.h"

void handle_logs_files(nng_aio *aio)
{
void * data = NULL;
size_t len = 0;
int rv = 0;

NEU_VALIDATE_JWT(aio);

/* check whether the neuron directory exists */
rv = access("../neuron", F_OK);
if (0 != rv) {
nlog_error("The neuron directory does not exists");
NEU_JSON_RESPONSE_ERROR(NEU_ERR_FILE_NOT_EXIST, {
neu_http_response(aio, error_code.error, result_error);
});
return;
}

rv = system("rm -rf /tmp/neuron_debug.tar.gz");
nlog_warn("remove old neuron_debug.tar.gz, rv: %d", rv);
/* tar the neuron directory */
rv = system("tar -zcvf /tmp/neuron_debug.tar.gz "
"--exclude='persistence/neuron.lic' ../neuron");
if (rv == -1) {
nlog_error("failed to create neuron_debug.tar.gz, rv: %d", rv);
NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, {
neu_http_response(aio, error_code.error, result_error);
});
return;
} else {
if (WIFEXITED(rv)) {
if (WEXITSTATUS(rv) < 0) {
nlog_error("failed to create neuron_debug.tar.gz, rv: %d",
WEXITSTATUS(rv));
NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, {
neu_http_response(aio, error_code.error, result_error);
});
return;
}
} else {
nlog_error("failed to create neuron_debug.tar.gz, rv: %d",
WEXITSTATUS(rv));
NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, {
neu_http_response(aio, error_code.error, result_error);
});
return;
}
}

rv = read_file("/tmp/neuron_debug.tar.gz", &data, &len);
if (0 != rv) {
NEU_JSON_RESPONSE_ERROR(
rv, { neu_http_response(aio, error_code.error, result_error); });
return;
}

/* handle http response */
rv = neu_http_response_file(aio, data, len,
"attachment; filename=neuron_debug.tar.gz");

free(data);
nlog_notice("download neuron_debug.tar.gz, ret: %d", rv);
}

void handle_log_level(nng_aio *aio)
{
neu_plugin_t *plugin = neu_rest_get_plugin();
Expand Down
1 change: 0 additions & 1 deletion plugins/restful/log_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <nng/nng.h>

// void handle_get_log(nng_aio *aio);
void handle_logs_files(nng_aio *aio);
void handle_log_level(nng_aio *aio);

int read_file(const char *file_name, void **datap, size_t *lenp);
Expand Down
3 changes: 3 additions & 0 deletions tests/ft/http_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sys

sys.path.append("tests/ft")
146 changes: 146 additions & 0 deletions tests/ft/http_api/test_http_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import neuron.api as api
import neuron.error as error
import neuron.config as config
from neuron.common import *

hold_int16 = [{"name": "hold_int16", "address": "1!400001",
"attribute": config.NEU_TAG_ATTRIBUTE_RW, "type": config.NEU_TYPE_INT16}]

global_config = {
"nodes": [
{
"plugin": "MQTT",
"name": "mqtt"
},
{
"plugin": "Modbus TCP",
"name": "modbus"
}
],
"groups": [
{
"driver": "modbus",
"group": "group",
"tag_count": 1,
"interval": 100
}
],
"tags": [
{
"tags": [
{
"type": 3,
"name": "hold_int16",
"attribute": 3,
"precision": 0,
"decimal": 0.0,
"address": "1!400001",
"description": ""
}
],
"driver": "modbus",
"group": "group"
}
],
"subscriptions": [
{
"groups": [
{
"driver": "modbus",
"group": "group"
}
],
"app": "mqtt"
}
],
"settings": [
{
"node": "mqtt",
"params": {
"client-id": "neuron_aBcDeF",
"qos": 0,
"format": 0,
"write-req-topic": "/neuron/mqtt/write/req",
"write-resp-topic": "/neuron/mqtt/write/resp",
"offline-cache": False,
"cache-sync-interval": 100,
"host": "broker.emqx.io",
"port": 1883,
"username": "",
"password": "",
"ssl": False
}
},
{
"node": "modbus",
"params": {
"connection_mode": 0,
"transport_mode": 0,
"interval": 0,
"host": "127.0.0.1",
"port": 502,
"timeout": 3000,
"max_retries": 2
}
}
]
}

class TestHttp:

@description(given="running neuron", when="add driver/app node/group/tag, app subscribes driver group, and get global config", then="success")
def test_get_global_config(self):
response = api.put_global_config(json=global_config)
assert 200 == response.status_code
assert error.NEU_ERR_SUCCESS == response.json()['error']

response = api.get_global_config()
assert 200 == response.status_code

config_data = response.json()

assert 'nodes' in config_data
assert len(config_data['nodes']) == 2
assert config_data['nodes'][0]['plugin'] == 'MQTT'
assert config_data['nodes'][1]['plugin'] == 'Modbus TCP'

assert 'groups' in config_data
assert len(config_data['groups']) == 1
assert config_data['groups'][0]['driver'] == 'modbus'

assert 'tags' in config_data
assert len(config_data['tags']) == 1
assert config_data['tags'][0]['driver'] == 'modbus'
assert len(config_data['tags'][0]['tags']) == 1

assert 'subscriptions' in config_data
assert len(config_data['subscriptions']) == 1
assert config_data['subscriptions'][0]['app'] == 'mqtt'

assert 'settings' in config_data
assert len(config_data['settings']) == 2
assert config_data['settings'][0]['node'] == 'mqtt'

response = api.del_node_check(node='modbus')

response = api.get_global_config()
assert 200 == response.status_code

config_data = response.json()

assert 'nodes' in config_data
assert len(config_data['nodes']) == 1
assert config_data['nodes'][0]['plugin'] == 'MQTT'

assert 'groups' in config_data
assert len(config_data['groups']) == 0

assert 'tags' in config_data
assert len(config_data['tags']) == 0

assert 'subscriptions' in config_data
assert len(config_data['subscriptions']) == 0

assert 'settings' in config_data
assert len(config_data['settings']) == 1
assert config_data['settings'][0]['node'] == 'mqtt'
6 changes: 6 additions & 0 deletions tests/ft/neuron/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ def unsubscribe_group(app, driver, group):
def get_metrics(category="global", node=""):
return requests.get(url=config.BASE_URL + "/api/v2/metrics?category=" + category + "&node=" + node, headers={"Authorization": config.default_jwt})

def get_global_config():
return requests.get(url=config.BASE_URL + "/api/v2/global/config", headers={"Authorization": config.default_jwt})

def put_global_config(json):
return requests.put(url=config.BASE_URL + "/api/v2/global/config", headers={"Authorization": config.default_jwt}, json=json)

# plugin setting


Expand Down