From bb52ef5d5fbcf3b6adc32fb129a27a1eec26a5e7 Mon Sep 17 00:00:00 2001 From: fengzero Date: Mon, 9 Oct 2023 08:41:09 +0000 Subject: [PATCH] ci: pyft --- .github/workflows/check.yml | 32 ++--------------------------- src/core/manager.c | 1 - src/core/manager_internal.h | 3 +-- tests/ft/login/__init__.py | 3 +++ tests/ft/login/test_login.py | 32 +++++++++++++++++++++++++++++ tests/ft/neuron/__init__.py | 0 tests/ft/neuron/api.py | 10 +++++++++ tests/ft/neuron/common.py | 40 ++++++++++++++++++++++++++++++++++++ tests/ft/neuron/config.py | 2 ++ tests/ft/neuron/process.py | 23 +++++++++++++++++++++ tests/ft/pytest.ini | 3 +++ tests/ft/setup.py | 4 ++++ 12 files changed, 120 insertions(+), 33 deletions(-) create mode 100644 tests/ft/login/__init__.py create mode 100644 tests/ft/login/test_login.py create mode 100644 tests/ft/neuron/__init__.py create mode 100644 tests/ft/neuron/api.py create mode 100644 tests/ft/neuron/common.py create mode 100644 tests/ft/neuron/config.py create mode 100644 tests/ft/neuron/process.py create mode 100644 tests/ft/pytest.ini create mode 100644 tests/ft/setup.py diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index bcdd0858..fa3d54cf 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -121,7 +121,7 @@ jobs: gcov_include: include plugins src gcov_ignore: tests simulator - pyft: + pft: runs-on: ubuntu-20.04 container: ghcr.io/neugates/build:x86_64-main @@ -129,7 +129,6 @@ jobs: - uses: actions/checkout@v4 with: submodules: recursive - fetch-depth: 0 - name: compile run: | @@ -138,37 +137,11 @@ jobs: cmake -DUSE_GCOV=1 .. -DCMAKE_TOOLCHAIN_FILE=../cmake/x86_64-linux-gnu.cmake make -j4 - - name: get neuron current branch - shell: bash - run: | - if [ '${{ github.event_name }}' == 'push' ]; then - branch=${{ github.ref_name }} - elif [ '${{ github.event_name }}' == 'pull_request' ]; then - branch=${{ github.event.pull_request.base.ref }} - elif [ '${{ github.event_name }}' == 'release' ]; then - branch=${{ github.event.release.target_commitish }} - fi - echo "BRANCH=$branch" >> $GITHUB_ENV - echo "neuron current branch is $branch" - - - name: fetch neu-ft - uses: actions/checkout@v4 - with: - repository: neugates/neu-ft - path: neu-ft - ref: ${{ env.BRANCH }} - - name: function test run: | pip install -U pytest pip install requests - pytest -s neu-ft/neuron - - - uses: actions/upload-artifact@v3 - with: - name: pyft_result - path: | - neu-ft/neuron/report + pytest -s tests/ft - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 @@ -176,4 +149,3 @@ jobs: gcov: true gcov_include: include plugins src gcov_ignore: tests simulator - diff --git a/src/core/manager.c b/src/core/manager.c index d3f39890..7eb08ff2 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -164,7 +164,6 @@ void neu_manager_destroy(neu_manager_t *manager) UT_array *addrs = neu_node_manager_get_addrs_all(manager->node_manager); - neu_event_del_timer(manager->events, manager->timer_lev); neu_event_del_timer(manager->events, manager->timer_timestamp); header->type = NEU_REQ_NODE_UNINIT; diff --git a/src/core/manager_internal.h b/src/core/manager_internal.h index 0f5aef00..874ce1d1 100644 --- a/src/core/manager_internal.h +++ b/src/core/manager_internal.h @@ -42,8 +42,7 @@ typedef struct neu_manager { neu_event_timer_t *timer_timestamp; - neu_event_timer_t *timer_lev; - int64_t timestamp_lev_manager; + int64_t timestamp_lev_manager; } neu_manager_t; int neu_manager_add_plugin(neu_manager_t *manager, const char *library); diff --git a/tests/ft/login/__init__.py b/tests/ft/login/__init__.py new file mode 100644 index 00000000..6da12771 --- /dev/null +++ b/tests/ft/login/__init__.py @@ -0,0 +1,3 @@ +import sys + +sys.path.append("tests/ft") diff --git a/tests/ft/login/test_login.py b/tests/ft/login/test_login.py new file mode 100644 index 00000000..26fa77dc --- /dev/null +++ b/tests/ft/login/test_login.py @@ -0,0 +1,32 @@ +import os +import sys + +import neuron.api as api +from neuron.common import * + + +class TestLogin: + + @description(given="name and password", when="login", then="login success with token") + def test_login_success(self): + response = api.login() + assert 200 == response.status_code + + @description(given="invalid user", when="login", then="login failed") + def test_login_invalid_user(self): + response = api.login('invalid_user') + assert 401 == response.status_code + + @description(given="invalid password", when="login", then="login failed") + def test_login_invalid_password(self): + response = api.login(password='invalid_password') + assert 401 == response.status_code + + @description(given="user, old and new password", when="change password", then="change success") + def test_change_password(self): + response = api.change_password(new_password='invalid_password') + assert 200 == response.status_code + assert 0 == response.json()['error'] + + response = api.login(password='invalid_password') + assert 200 == response.status_code diff --git a/tests/ft/neuron/__init__.py b/tests/ft/neuron/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/ft/neuron/api.py b/tests/ft/neuron/api.py new file mode 100644 index 00000000..1449725e --- /dev/null +++ b/tests/ft/neuron/api.py @@ -0,0 +1,10 @@ +import requests +import neuron.config as config + + +def login(user='admin', password='0000'): + return requests.post(url=config.BASE_URL + '/api/v2/login', json={"name": user, "pass": password}) + + +def change_password(new_password, user='admin', old_password='0000', jwt=config.default_jwt): + return requests.post(url=config.BASE_URL + '/api/v2/password', headers={"Authorization": jwt}, json={"name": user, "old_pass": old_password, "new_pass": new_password}) diff --git a/tests/ft/neuron/common.py b/tests/ft/neuron/common.py new file mode 100644 index 00000000..b369b46a --- /dev/null +++ b/tests/ft/neuron/common.py @@ -0,0 +1,40 @@ +import time +import sys +from functools import wraps + +import pytest + +import neuron.process as process + + +class description(object): + def __init__(self, given, when, then): + self.given = given + self.when = when + self.then = then + + def __call__(self, func): + @wraps(func) + def wrapper(*args, **kwargs): + info = "Given " + self.given + ", when " + \ + self.when + ", then " + self.then + "." + print('\033[1;33;40m') + print("\n" + info) + print('\033[0m', end=' Result: ') + return func(*args, **kwargs) + return wrapper + + +@pytest.fixture(autouse=True, scope='class') +def class_setup_and_teardown(): + process.remove_persistence() + p = process.start_neuron() + yield + process.stop_neuron(p) + + +@pytest.fixture(autouse=True, scope='function') +def case_time(): + start = time.time() + yield + print(' Time cost: {:.3f}s'.format(time.time() - start), end='') diff --git a/tests/ft/neuron/config.py b/tests/ft/neuron/config.py new file mode 100644 index 00000000..569fe6d1 --- /dev/null +++ b/tests/ft/neuron/config.py @@ -0,0 +1,2 @@ +BASE_URL = "http://127.0.0.1:7000" +default_jwt = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJuZXVyb24iLCJib2R5RW5jb2RlIjowLCJleHAiOjE3MTc4MzUyNjIsImlhdCI6MTY4NjI5OTI2MiwiaXNzIjoibmV1cm9uIn0.tJ8Z12S5lzzyqvJedFr11w0FmQ8l9inFHuzcuA_3uKoNBJSZlQAkcWyDAO2XELyuZFtHPrk8pJFgEqTzwphtBAybVXbeHBRWED5XuHvYqznzwk1J2UCOuw_ehmHnLszZHRAcMoeLWIRIx0KM7BXvZAyFA2J1axJntxfRrurNMn6MbfnGGtza6hJfuQ6pG-h4cHhrBOogf8nw3f8RLfd16vbWobhEp8KBUrwsfn2Iv4hMXvZMekv_PXboLO74f7oMkEL6pdM9g6G6rDBTjTU8FssfeyJcAP7Xb9mS2G7GNW5B8twtPVLtpOQH9VNbLa4OQxdMb0WN86i9vSDiCOoN6A" diff --git a/tests/ft/neuron/process.py b/tests/ft/neuron/process.py new file mode 100644 index 00000000..d3925423 --- /dev/null +++ b/tests/ft/neuron/process.py @@ -0,0 +1,23 @@ +import os +import time +import pytest +import subprocess + + +def remove_persistence(dir='build/'): + os.system("rm -rf " + dir + "/persistence/sqlite.db") + os.system("mkdir -p " + dir + "/persistence") + + +def start_neuron(dir='build/'): + process = subprocess.Popen( + ['./neuron'], stderr=subprocess.PIPE, cwd=dir) + time.sleep(1) + assert process.poll() is None + return process + + +def stop_neuron(process): + result = process.terminate() + stderr = process.stderr.read().decode() + assert stderr == '', "Neuron process has errors: " + stderr diff --git a/tests/ft/pytest.ini b/tests/ft/pytest.ini new file mode 100644 index 00000000..261d996b --- /dev/null +++ b/tests/ft/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +python_files = test_*.py +python_functions = test_* \ No newline at end of file diff --git a/tests/ft/setup.py b/tests/ft/setup.py new file mode 100644 index 00000000..ebc03e8d --- /dev/null +++ b/tests/ft/setup.py @@ -0,0 +1,4 @@ +from distutils.core import setup + +setup(name='neuronft', version="0.0.1", + description='ft module', author='neuron', py_modules=['neuron', 'neuron.api', 'neuron.common', 'neuron.config', 'neuron.process'])