From e1ec5edd2772751ef68f6156a33464257c48d3e5 Mon Sep 17 00:00:00 2001 From: badrogger Date: Fri, 9 Aug 2024 11:43:50 +0000 Subject: [PATCH 1/8] Fetch telegraf user from node-cli --- core/monitoring.py | 5 +++-- tests/monitoring_test.py | 2 ++ tools/docker_utils.py | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/monitoring.py b/core/monitoring.py index 9aa87dcf..0884ba55 100644 --- a/core/monitoring.py +++ b/core/monitoring.py @@ -21,7 +21,7 @@ from typing import Optional from tools.helper import process_template -from tools.docker_utils import DockerUtils +from tools.docker_utils import DockerUtils, get_docker_group_id from tools.configs import SKALE_DIR_HOST from tools.configs.monitoring import ( @@ -68,11 +68,12 @@ def ensure_telegraf_running(dutils: Optional[DockerUtils] = None) -> None: if dutils.is_container_exists(TELEGRAF_CONTAINER_NAME): dutils.restart(TELEGRAF_CONTAINER_NAME) else: + group_id = get_docker_group_id() dutils.run_container( image_name=TELEGRAF_IMAGE, name=TELEGRAF_CONTAINER_NAME, network_mode='host', - user='telegraf:998', + user=f'telegraf:{group_id}', restart_policy={'name': 'on-failure'}, environment={'HOST_PROC': '/host/proc'}, volumes={ diff --git a/tests/monitoring_test.py b/tests/monitoring_test.py index c5eb74f1..0bc1a85d 100644 --- a/tests/monitoring_test.py +++ b/tests/monitoring_test.py @@ -65,3 +65,5 @@ def test_update_telegraf_service(telegraf_template, cleanup_container, dutils): config = config.read() assert config == '\n[agent]\n interval = "60s"\n hostname = "1.1.1.1"\n omit_hostname = false\n\n[global_tags]\n node_id = "1"\n\n[[outputs.db]]\n alias = "db"\n urls = ["http://127.0.0.1:1231"]\n' # noqa assert dutils.is_container_running('skale_telegraf') + print(dutils.get_info('skale_telegraf')['stats']) + assert dutils.get_info('skale_telegraf')['stats']['Config']['User'] == 'telegraf:998' diff --git a/tools/docker_utils.py b/tools/docker_utils.py index 00378fdb..4d501e33 100644 --- a/tools/docker_utils.py +++ b/tools/docker_utils.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import grp import io import itertools import logging @@ -52,6 +53,7 @@ MAX_RETRIES = 12 CONTAINER_CREATION_TIMEOUT = 10 +DOCKER_GROUPNAME = 'docker' class ContainerCreationTimeoutError(Exception): @@ -81,6 +83,10 @@ def inner(*args, **kwargs) -> list: return inner +def get_docker_group_id() -> int: + return grp.getgrnam('docker').gr_gid + + class DockerUtils: docker_lock = multiprocessing.Lock() From cd678dc1b7201b4b710f6108e48ff0f30d463c5c Mon Sep 17 00:00:00 2001 From: badrogger Date: Wed, 11 Sep 2024 17:28:18 +0000 Subject: [PATCH 2/8] Fetch docker user from file --- tools/configs/__init__.py | 2 ++ tools/docker_utils.py | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/configs/__init__.py b/tools/configs/__init__.py index 0fa95de3..35cfbcb1 100644 --- a/tools/configs/__init__.py +++ b/tools/configs/__init__.py @@ -103,3 +103,5 @@ STATSD_HOST = '127.0.0.1' STATSD_PORT = 8125 SYNC_NODE = os.getenv('SYNC_NODE') == 'True' + +DOCKER_NODE_CONFIG_FILEPATH = os.path.join(NODE_DATA_PATH, 'docker.json') diff --git a/tools/docker_utils.py b/tools/docker_utils.py index 4d501e33..911ab4f5 100644 --- a/tools/docker_utils.py +++ b/tools/docker_utils.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import grp import io import itertools import logging @@ -35,6 +34,7 @@ from docker.models.containers import Container from docker.models.volumes import Volume +from tools.configs import DOCKER_NODE_CONFIG_FILEPATH from tools.configs.containers import ( CONTAINER_NOT_FOUND, CREATED_STATUS, @@ -47,6 +47,7 @@ CONTAINER_LOGS_SEPARATOR ) from tools.configs.logs import REMOVED_CONTAINERS_FOLDER_PATH +from tools.helper import read_json logger = logging.getLogger(__name__) @@ -84,7 +85,7 @@ def inner(*args, **kwargs) -> list: def get_docker_group_id() -> int: - return grp.getgrnam('docker').gr_gid + return read_json(DOCKER_NODE_CONFIG_FILEPATH)['docker_group_id'] class DockerUtils: From f8783a71f43871e4cbf26acb547ba9195341e0af Mon Sep 17 00:00:00 2001 From: badrogger Date: Fri, 6 Sep 2024 12:29:45 +0000 Subject: [PATCH 3/8] Switch to the new GA runner --- .github/workflows/test.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6f7720c2..d9ef6e8a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,6 +17,9 @@ jobs: ENDPOINT: http://127.0.0.1:8545 CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} SCHAIN_TYPE: ${{ secrets.SCHAIN_TYPE }} + defaults: + run: + working-directory: ~/admin steps: - uses: actions/checkout@v2 with: @@ -28,7 +31,10 @@ jobs: python-version: ${{ env.PYTHON_VERSION }} - name: Install python dependencies - run: bash ./scripts/install_python_dependencies.sh + run: | + source ~/admin/.venv/bin/activate + python --version + bash ./scripts/install_python_dependencies.sh - name: Lint with flake8 run: flake8 . From 5cf8bc9656c35cecca4459d446ded32cde4dc11f Mon Sep 17 00:00:00 2001 From: badrogger Date: Fri, 6 Sep 2024 14:42:57 +0000 Subject: [PATCH 4/8] Use python3.11 --- .github/workflows/test.yml | 7 +++++-- scripts/run_core_tests.sh | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d9ef6e8a..1c71e3b1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,9 +32,10 @@ jobs: - name: Install python dependencies run: | - source ~/admin/.venv/bin/activate + source .venv/bin/activate python --version - bash ./scripts/install_python_dependencies.sh + uv pip install -r requirements.txt --prerelease=allow + uv pip install -r requirements-dev.txt - name: Lint with flake8 run: flake8 . @@ -59,6 +60,8 @@ jobs: - name: Run core tests run: | + source .venv/bin/activate + python --version bash ./scripts/run_core_tests.sh - name: Cleanup docker artifacts diff --git a/scripts/run_core_tests.sh b/scripts/run_core_tests.sh index ae867c12..e63b3064 100755 --- a/scripts/run_core_tests.sh +++ b/scripts/run_core_tests.sh @@ -13,5 +13,5 @@ export_test_env run_sgx_simulator $SGX_WALLET_TAG bash scripts/run_redis.sh -py.test --cov-config=.coveragerc --cov=. tests/ --ignore=tests/firewall $@ +python -m py.test --cov-config=.coveragerc --cov=. tests --ignore=tests/firewall $@ tests_cleanup From 9e9af9dbab6078a38d882268b06ec30b54827efc Mon Sep 17 00:00:00 2001 From: badrogger Date: Wed, 11 Sep 2024 17:58:21 +0000 Subject: [PATCH 5/8] Switch to self-hosted github runner --- .github/workflows/test.yml | 41 +++++++++++++++++--------------------- scripts/run_redis.sh | 2 +- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1c71e3b1..1f63da04 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,34 +11,28 @@ env: jobs: test_core: - runs-on: ubuntu-latest + runs-on: self-hosted env: ETH_PRIVATE_KEY: ${{ secrets.ETH_PRIVATE_KEY }} ENDPOINT: http://127.0.0.1:8545 CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} SCHAIN_TYPE: ${{ secrets.SCHAIN_TYPE }} - defaults: - run: - working-directory: ~/admin steps: - uses: actions/checkout@v2 with: submodules: true - - name: Set up Python ${{ env.PYTHON_VERSION }} - uses: actions/setup-python@v1 - with: - python-version: ${{ env.PYTHON_VERSION }} - - name: Install python dependencies run: | - source .venv/bin/activate + pwd + ls -altr ../.venv + source ../.venv/bin/activate python --version uv pip install -r requirements.txt --prerelease=allow uv pip install -r requirements-dev.txt - name: Lint with flake8 - run: flake8 . + run: uv run flake8 . - name: Launch anvil node run: | @@ -48,32 +42,27 @@ jobs: run: | bash ./helper-scripts/deploy_test_ima.sh - - name: Cleanup skale-manager image - run: | - docker rmi -f skalenetwork/skale-manager:${{ env.MANAGER_TAG }} - - name: Show stats before tests if: always() run: | - sudo lsblk -f - sudo free -h + lsblk -f + free -h - name: Run core tests run: | - source .venv/bin/activate + source ../.venv/bin/activate python --version bash ./scripts/run_core_tests.sh - name: Cleanup docker artifacts run: | docker rm -f $(docker ps -aq) - docker rmi -f $(docker images -q) - name: Show stats after core tests if: always() run: | - sudo lsblk -f - sudo free -h + lsblk -f + free -h - name: Run firewall tests run: | @@ -82,9 +71,15 @@ jobs: - name: Show stats after firewall tests if: always() run: | - sudo lsblk -f - sudo free -h + lsblk -f + free -h + + - name: Cleanup docker artifacts + if: always() + run: | + docker rm -f $(docker ps -aq) - name: Run codecov run: | + source ../.venv/bin/activate codecov -t $CODECOV_TOKEN diff --git a/scripts/run_redis.sh b/scripts/run_redis.sh index cf066fa7..c1d37db8 100755 --- a/scripts/run_redis.sh +++ b/scripts/run_redis.sh @@ -2,4 +2,4 @@ set -e docker rm -f redis || true export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -docker run -v $DIR/../tests/redis-conf:/config:Z -p 6379:6379 --name=redis -d redis:6.0-alpine +docker run -v $DIR/../tests/redis-conf:/config:Z --network=host --name=redis -d redis:6.0-alpine From 0a4e8600ac4db594543ad3040ed539c2984f4bf9 Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 12 Sep 2024 11:50:13 +0000 Subject: [PATCH 6/8] Fix tests --- scripts/run_redis.sh | 6 +--- tests/monitoring_test.py | 40 +++++++++++++----------- tests/schains/monitor/containers_test.py | 6 +++- tests/schains/monitor/rpc_test.py | 12 ++++--- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/scripts/run_redis.sh b/scripts/run_redis.sh index 087023bb..b3591b97 100755 --- a/scripts/run_redis.sh +++ b/scripts/run_redis.sh @@ -2,8 +2,4 @@ set -e docker rm -f redis || true export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -<<<<<<< HEAD -docker run -v $DIR/../tests/redis-conf:/config:Z --network=host --name=redis -d redis:6.0-alpine -======= -docker run -v $DIR/../tests/redis-conf:/config -p 6381:6381 --name=redis -d redis:6.0-alpine ->>>>>>> develop +docker run -v $DIR/../tests/redis-conf:/config --network=host --name=redis -d redis:6.0-alpine diff --git a/tests/monitoring_test.py b/tests/monitoring_test.py index 0bc1a85d..79c1dab3 100644 --- a/tests/monitoring_test.py +++ b/tests/monitoring_test.py @@ -1,12 +1,11 @@ +import json import os import pytest from core.monitoring import TelegrafNotConfiguredError, update_telegraf_service -from tools.configs.monitoring import ( - TELEGRAF_TEMPLATE_PATH, - TELEGRAF_CONFIG_PATH -) +from tools.configs import DOCKER_NODE_CONFIG_FILEPATH +from tools.configs.monitoring import TELEGRAF_TEMPLATE_PATH, TELEGRAF_CONFIG_PATH CONFIG_TEMPLATE = """ @@ -24,6 +23,8 @@ """ +DOCKER_GROUP_ID = 1023 + @pytest.fixture def cleanup_container(dutils): @@ -44,26 +45,29 @@ def telegraf_template(): os.remove(TELEGRAF_CONFIG_PATH) -def test_update_telegraf_service(telegraf_template, cleanup_container, dutils): +@pytest.fixture +def docker_node_config(): + try: + with open(DOCKER_NODE_CONFIG_FILEPATH, 'w') as docker_config: + json.dump({'docker_group_id': DOCKER_GROUP_ID}, docker_config) + yield DOCKER_NODE_CONFIG_FILEPATH + finally: + os.remove(DOCKER_NODE_CONFIG_FILEPATH) + + +def test_update_telegraf_service(docker_node_config, telegraf_template, cleanup_container, dutils): node_id = 1 node_ip = '1.1.1.1' with pytest.raises(TelegrafNotConfiguredError): update_telegraf_service( - node_id=node_id, - node_ip='', - url='http://127.0.0.1:1231', - dutils=dutils + node_id=node_id, node_ip='', url='http://127.0.0.1:1231', dutils=dutils ) - update_telegraf_service( - node_ip, - node_id, - url='http://127.0.0.1:1231', - dutils=dutils - ) + update_telegraf_service(node_ip, node_id, url='http://127.0.0.1:1231', dutils=dutils) with open(TELEGRAF_CONFIG_PATH) as config: config = config.read() - assert config == '\n[agent]\n interval = "60s"\n hostname = "1.1.1.1"\n omit_hostname = false\n\n[global_tags]\n node_id = "1"\n\n[[outputs.db]]\n alias = "db"\n urls = ["http://127.0.0.1:1231"]\n' # noqa + assert ( + config == '\n[agent]\n interval = "60s"\n hostname = "1.1.1.1"\n omit_hostname = false\n\n[global_tags]\n node_id = "1"\n\n[[outputs.db]]\n alias = "db"\n urls = ["http://127.0.0.1:1231"]\n') # noqa assert dutils.is_container_running('skale_telegraf') - print(dutils.get_info('skale_telegraf')['stats']) - assert dutils.get_info('skale_telegraf')['stats']['Config']['User'] == 'telegraf:998' + user_info = dutils.get_info('skale_telegraf')['stats']['Config']['User'] + assert user_info == f'telegraf:{DOCKER_GROUP_ID}' diff --git a/tests/schains/monitor/containers_test.py b/tests/schains/monitor/containers_test.py index b8e806f0..eb0922e1 100644 --- a/tests/schains/monitor/containers_test.py +++ b/tests/schains/monitor/containers_test.py @@ -1,4 +1,6 @@ -import mock +import time + +from unittest import mock from core.schains.monitor.containers import monitor_schain_container from core.schains.runner import is_container_exists @@ -75,6 +77,8 @@ def test_monitor_schain_container_ec( schain_name = schain_db run_custom_schain_container(dutils, schain_name, entrypoint=['sh', 'exit', '1']) + # To make sure container initializaed + time.sleep(2) with mock.patch('core.schains.monitor.containers.is_volume_exists', return_value=True): schain_record.set_failed_rpc_count(100) schain_record.set_restart_count(0) diff --git a/tests/schains/monitor/rpc_test.py b/tests/schains/monitor/rpc_test.py index 5445ef88..65c26ea0 100644 --- a/tests/schains/monitor/rpc_test.py +++ b/tests/schains/monitor/rpc_test.py @@ -1,7 +1,7 @@ import datetime +import time import json -import mock -from time import sleep +from unittest import mock import freezegun import requests @@ -37,7 +37,7 @@ def test_handle_failed_schain_rpc_exit_time_reached( image_name, container_name, _, _ = get_container_info(SCHAIN_CONTAINER, schain_db) dutils.run_container(image_name=image_name, name=container_name, entrypoint='bash -c "exit 0"') - sleep(7) + time.sleep(7) schain_record.set_failed_rpc_count(100) container_info = dutils.get_info(container_name) @@ -65,7 +65,7 @@ def test_monitor_schain_downloading_snapshot( dutils.run_container( image_name=image_name, name=container_name, entrypoint='bash -c "sleep 100"' ) - sleep(7) + time.sleep(7) schain_record.set_failed_rpc_count(100) container_info = dutils.get_info(container_name) @@ -112,6 +112,8 @@ def test_monitor_container_exited(schain_db, dutils, cleanup_schain_containers, dutils.run_container( image_name=image_name, name=container_name, entrypoint='bash -c "exit 100;"' ) + # Wait for container initialization + time.sleep(2) schain_record.set_failed_rpc_count(100) schain_record.set_restart_count(0) @@ -126,6 +128,8 @@ def test_monitor_container_exited(schain_db, dutils, cleanup_schain_containers, skaled_status=skaled_status, dutils=dutils, ) + # Wait for container initialization + time.sleep(2) assert schain_record.restart_count == 0 container_info = dutils.get_info(container_name) assert container_info['stats']['State']['FinishedAt'] == finished_at From 7a707bd462e4d8b7ab9555b3312556326c828e4c Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 12 Sep 2024 14:10:42 +0000 Subject: [PATCH 7/8] Bump version --- .github/workflows/test.yml | 6 ------ VERSION | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b8f0f2de..f5cc084e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -75,12 +75,6 @@ jobs: run: | docker rm -f $(docker ps -aq) - - name: Cleanup docker artifacts - if: always() - run: | - docker rm -f $(docker ps -aq) - docker rmi -f $(docker images -q) - - name: Run codecov run: | source ../.venv/bin/activate diff --git a/VERSION b/VERSION index 860487ca..37c2961c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.7.1 +2.7.2 From 7bd9d9e0765d1d4370a85b637e6a13166471ae41 Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 12 Sep 2024 16:02:31 +0000 Subject: [PATCH 8/8] Remove unused DOCKER_GROUPNAME --- tools/docker_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/docker_utils.py b/tools/docker_utils.py index 911ab4f5..009c807f 100644 --- a/tools/docker_utils.py +++ b/tools/docker_utils.py @@ -54,7 +54,6 @@ MAX_RETRIES = 12 CONTAINER_CREATION_TIMEOUT = 10 -DOCKER_GROUPNAME = 'docker' class ContainerCreationTimeoutError(Exception):