Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
badrogger committed Oct 8, 2024
1 parent 3a0c3ff commit fb32776
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 72 deletions.
20 changes: 16 additions & 4 deletions tests/cli/node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
_turn_on,
_set_domain_name
)
from node_cli.utils.exit_codes import CLIExitCodes
from node_cli.utils.helper import init_default_logger

from tests.helper import (
response_mock,
run_command,
run_command_mock,
safe_update_api_response,
subprocess_run_mock
)
from tests.resources_test import BIG_DISK_SIZE
Expand Down Expand Up @@ -123,7 +125,6 @@ def test_register_with_no_alloc(mocked_g_config):
register_node,
['--name', 'test-node', '-d', 'skale.test'], input='0.0.0.0\n')
assert result.exit_code == 8
print(repr(result.output))
assert result.output == f'Enter node public IP: 0.0.0.0\nCommand failed with following errors:\n--------------------------------------------------\nNode hasn\'t been inited before.\nYou should run < skale node init >\n--------------------------------------------------\nYou can find more info in {G_CONF_HOME}.skale/.skale-cli-log/debug-node-cli.log\n' # noqa


Expand Down Expand Up @@ -375,7 +376,18 @@ def test_turn_off_maintenance_on(mocked_g_config):
)
with mock.patch('subprocess.run', new=subprocess_run_mock), \
mock.patch('node_cli.core.node.turn_off_op'), \
mock.patch('node_cli.core.node.is_node_inited', return_value=True):
mock.patch('node_cli.utils.decorators.is_node_inited', return_value=True):
with mock.patch('node_cli.utils.helper.requests.get', return_value=safe_update_api_response()):
result = run_command_mock(
'node_cli.utils.helper.requests.post',
resp_mock,
_turn_off,
[
'--maintenance-on',
'--yes'
])
assert result.output == 'Setting maintenance mode on...\nNode is successfully set in maintenance mode\n' # noqa
assert result.exit_code == 0
result = run_command_mock(
'node_cli.utils.helper.requests.post',
resp_mock,
Expand All @@ -384,8 +396,8 @@ def test_turn_off_maintenance_on(mocked_g_config):
'--maintenance-on',
'--yes'
])
assert result.exit_code == 0
assert result.output == 'Setting maintenance mode on...\nNode is successfully set in maintenance mode\n' # noqa
assert 'Cannot turn off safetly' in result.output
assert result.exit_code == CLIExitCodes.UNSAFE_UPDATE


def test_turn_on_maintenance_off(mocked_g_config):
Expand Down
120 changes: 64 additions & 56 deletions tests/cli/sync_node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
import logging

from node_cli.configs import SKALE_DIR, NODE_DATA_PATH
from node_cli.core.node_options import NodeOptions
from node_cli.cli.sync_node import _init_sync, _update_sync
from node_cli.utils.exit_codes import CLIExitCodes
from node_cli.utils.helper import init_default_logger
from node_cli.core.node_options import NodeOptions

from tests.helper import (
run_command, subprocess_run_mock
)
from tests.helper import run_command, safe_update_api_response, subprocess_run_mock
from tests.resources_test import BIG_DISK_SIZE

logger = logging.getLogger(__name__)
Expand All @@ -38,43 +37,41 @@

def test_init_sync(mocked_g_config):
pathlib.Path(SKALE_DIR).mkdir(parents=True, exist_ok=True)
with mock.patch('subprocess.run', new=subprocess_run_mock), \
mock.patch('node_cli.core.node.init_sync_op'), \
mock.patch('node_cli.core.node.is_base_containers_alive', return_value=True), \
mock.patch('node_cli.core.resources.get_disk_size', return_value=BIG_DISK_SIZE), \
mock.patch('node_cli.core.node.configure_firewall_rules'), \
mock.patch('node_cli.utils.decorators.is_node_inited', return_value=False):
result = run_command(
_init_sync,
['./tests/test-env']
)
with mock.patch('subprocess.run', new=subprocess_run_mock), mock.patch(
'node_cli.core.node.init_sync_op'
), mock.patch('node_cli.core.node.is_base_containers_alive', return_value=True), mock.patch(
'node_cli.core.resources.get_disk_size', return_value=BIG_DISK_SIZE
), mock.patch('node_cli.core.node.configure_firewall_rules'), mock.patch(
'node_cli.utils.decorators.is_node_inited', return_value=False
):
result = run_command(_init_sync, ['./tests/test-env'])
assert result.exit_code == 0


def test_init_sync_archive_catchup(mocked_g_config, clean_node_options):
pathlib.Path(NODE_DATA_PATH).mkdir(parents=True, exist_ok=True)
# with mock.patch('subprocess.run', new=subprocess_run_mock), \
with mock.patch('node_cli.core.node.is_base_containers_alive', return_value=True), \
mock.patch('node_cli.operations.base.cleanup_volume_artifacts'), \
mock.patch('node_cli.operations.base.download_skale_node'), \
mock.patch('node_cli.operations.base.sync_skale_node'), \
mock.patch('node_cli.operations.base.configure_docker'), \
mock.patch('node_cli.operations.base.prepare_host'), \
mock.patch('node_cli.operations.base.ensure_filestorage_mapping'), \
mock.patch('node_cli.operations.base.link_env_file'), \
mock.patch('node_cli.operations.base.download_contracts'), \
mock.patch('node_cli.operations.base.generate_nginx_config'), \
mock.patch('node_cli.operations.base.prepare_block_device'), \
mock.patch('node_cli.operations.base.update_meta'), \
mock.patch('node_cli.operations.base.update_resource_allocation'), \
mock.patch('node_cli.operations.base.update_images'), \
mock.patch('node_cli.operations.base.compose_up'), \
mock.patch('node_cli.core.resources.get_disk_size', return_value=BIG_DISK_SIZE), \
mock.patch('node_cli.core.node.configure_firewall_rules'), \
mock.patch('node_cli.utils.decorators.is_node_inited', return_value=False):
# with mock.patch('subprocess.run', new=subprocess_run_mock), \
with mock.patch('node_cli.core.node.is_base_containers_alive', return_value=True), mock.patch(
'node_cli.operations.base.cleanup_volume_artifacts'
), mock.patch('node_cli.operations.base.download_skale_node'), mock.patch(
'node_cli.operations.base.sync_skale_node'
), mock.patch('node_cli.operations.base.configure_docker'), mock.patch(
'node_cli.operations.base.prepare_host'
), mock.patch('node_cli.operations.base.ensure_filestorage_mapping'), mock.patch(
'node_cli.operations.base.link_env_file'
), mock.patch('node_cli.operations.base.download_contracts'), mock.patch(
'node_cli.operations.base.generate_nginx_config'
), mock.patch('node_cli.operations.base.prepare_block_device'), mock.patch(
'node_cli.operations.base.update_meta'
), mock.patch('node_cli.operations.base.update_resource_allocation'), mock.patch(
'node_cli.operations.base.update_images'
), mock.patch('node_cli.operations.base.compose_up'), mock.patch(
'node_cli.core.resources.get_disk_size', return_value=BIG_DISK_SIZE
), mock.patch('node_cli.core.node.configure_firewall_rules'), mock.patch(
'node_cli.utils.decorators.is_node_inited', return_value=False
):
result = run_command(
_init_sync,
['./tests/test-env', '--archive', '--catchup', '--historic-state']
_init_sync, ['./tests/test-env', '--archive', '--catchup', '--historic-state']
)
node_options = NodeOptions()

Expand All @@ -87,30 +84,41 @@ def test_init_sync_archive_catchup(mocked_g_config, clean_node_options):

def test_init_sync_historic_state_fail(mocked_g_config, clean_node_options):
pathlib.Path(SKALE_DIR).mkdir(parents=True, exist_ok=True)
with mock.patch('subprocess.run', new=subprocess_run_mock), \
mock.patch('node_cli.core.node.init_sync_op'), \
mock.patch('node_cli.core.node.is_base_containers_alive', return_value=True), \
mock.patch('node_cli.core.resources.get_disk_size', return_value=BIG_DISK_SIZE), \
mock.patch('node_cli.core.node.configure_firewall_rules'), \
mock.patch('node_cli.utils.decorators.is_node_inited', return_value=False):
result = run_command(
_init_sync,
['./tests/test-env', '--historic-state']
)
with mock.patch('subprocess.run', new=subprocess_run_mock), mock.patch(
'node_cli.core.node.init_sync_op'
), mock.patch('node_cli.core.node.is_base_containers_alive', return_value=True), mock.patch(
'node_cli.core.resources.get_disk_size', return_value=BIG_DISK_SIZE
), mock.patch('node_cli.core.node.configure_firewall_rules'), mock.patch(
'node_cli.utils.decorators.is_node_inited', return_value=False
):
result = run_command(_init_sync, ['./tests/test-env', '--historic-state'])
assert result.exit_code == 1
assert '--historic-state can be used only' in result.output


def test_update_sync(mocked_g_config):
pathlib.Path(SKALE_DIR).mkdir(parents=True, exist_ok=True)
with mock.patch('subprocess.run', new=subprocess_run_mock), \
mock.patch('node_cli.core.node.update_sync_op'), \
mock.patch('node_cli.core.node.is_base_containers_alive', return_value=True), \
mock.patch('node_cli.core.resources.get_disk_size', return_value=BIG_DISK_SIZE), \
mock.patch('node_cli.core.node.configure_firewall_rules'), \
mock.patch('node_cli.utils.decorators.is_node_inited', return_value=True):
result = run_command(
_update_sync,
['./tests/test-env', '--yes']
)
assert result.exit_code == 0

with mock.patch('subprocess.run', new=subprocess_run_mock), mock.patch(
'node_cli.core.node.update_sync_op'
), mock.patch('node_cli.core.node.is_base_containers_alive', return_value=True), mock.patch(
'node_cli.core.resources.get_disk_size', return_value=BIG_DISK_SIZE
), mock.patch('node_cli.core.node.configure_firewall_rules'), mock.patch(
'node_cli.utils.decorators.is_node_inited', return_value=True
):
result = run_command(_update_sync, ['./tests/test-env', '--yes'])
assert result.exit_code == CLIExitCodes.UNSAFE_UPDATE
assert 'Cannot update safetly' in result.output

with mock.patch(
'node_cli.utils.helper.requests.get', return_value=safe_update_api_response()
):
result = run_command(_update_sync, ['./tests/test-env', '--yes'])
assert result.exit_code == 0

with mock.patch(
'node_cli.utils.helper.requests.get', return_value=safe_update_api_response(False)
):
result = run_command(_update_sync, ['./tests/test-env', '--yes'])
assert result.exit_code == CLIExitCodes.UNSAFE_UPDATE
assert 'Cannot update safetly' in result.output
21 changes: 9 additions & 12 deletions tests/core/core_node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
from node_cli.configs.resource_allocation import RESOURCE_ALLOCATION_FILEPATH
from node_cli.core.node import BASE_CONTAINERS_AMOUNT, is_base_containers_alive
from node_cli.core.node import init, pack_dir, update, is_update_safe
from node_cli.utils.exit_codes import CLIExitCodes

from tests.helper import response_mock, subprocess_run_mock
from tests.helper import response_mock, safe_update_api_response, subprocess_run_mock
from tests.resources_test import BIG_DISK_SIZE

dclient = docker.from_env()
Expand Down Expand Up @@ -169,21 +170,17 @@ def test_update_node(mocked_g_config, resource_file):
), mock.patch('node_cli.core.resources.get_disk_size', return_value=BIG_DISK_SIZE), mock.patch(
'node_cli.core.host.init_data_dir'
):
update(env_filepath, pull_config_for_schain=None)
with mock.patch('node_cli.utils.helper.requests.get', return_value=safe_update_api_response()):
result = update(env_filepath, pull_config_for_schain=None)
assert result is None


def test_is_update_safe():
assert not is_update_safe()
safe_response = response_mock(
requests.codes.ok,
{'status': 'ok', 'payload': {'update_safe': True, 'unsafe_chains': []}},
)
with mock.patch('node_cli.utils.helper.requests.get', return_value=safe_response):
with mock.patch('node_cli.utils.helper.requests.get', return_value=safe_update_api_response()):
assert is_update_safe()

unsafe_response = response_mock(
requests.codes.ok,
{'status': 'ok', 'payload': {'update_safe': False, 'unsafe_chains': ['test_chain']}},
)
with mock.patch('node_cli.utils.helper.requests.get', return_value=unsafe_response):
with mock.patch(
'node_cli.utils.helper.requests.get', return_value=safe_update_api_response(safe=False)
):
assert not is_update_safe()
15 changes: 15 additions & 0 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import mock
import os


import requests
from click.testing import CliRunner
from mock import Mock, MagicMock

Expand Down Expand Up @@ -84,3 +86,16 @@ def subprocess_run_mock(*args, returncode=0, **kwargs):
result.stdout = MagicMock()
result.stderr = MagicMock()
return result


def safe_update_api_response(safe: bool = True) -> dict:
if safe:
return response_mock(
requests.codes.ok,
{'status': 'ok', 'payload': {'update_safe': True, 'unsafe_chains': []}},
)
else:
return response_mock(
requests.codes.ok,
{'status': 'ok', 'payload': {'update_safe': False, 'unsafe_chains': ['test_chain']}},
)
1 change: 1 addition & 0 deletions tests/routes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'/api/v1/node/exit/start',
'/api/v1/node/exit/status',
'/api/v1/node/set-domain-name',
'/api/v1/node/update-safe',

'/api/v1/health/containers',
'/api/v1/health/schains',
Expand Down

0 comments on commit fb32776

Please sign in to comment.