Skip to content

Commit

Permalink
Merge pull request #39 from skalenetwork/enhancement/SKALE-1602-setup…
Browse files Browse the repository at this point in the history
…-tests

Enhancement/skale 1602 setup tests
  • Loading branch information
dmytrotkk authored Oct 16, 2019
2 parents a312fd7 + 57cd1ed commit ccc7b43
Show file tree
Hide file tree
Showing 24 changed files with 962 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 100
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist,venv
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist,venv
21 changes: 13 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
language: python
python:
- '3.6'
os: linux
dist: bionic
python: '3.6'
cache: pip
install:
- pip install -r requirements.txt
- pip install -r requirements-dev.txt
before_script:
- "flake8 ."
- pip install -e .
- pip install -e .[dev]
- export ENV=dev
- bash scripts/build.sh 1.0.0 test-branch
jobs:
include:
#- stage: test
- stage: test
before_script:
- flake8 .
script:
- bash scripts/run-tests.sh
- stage: deploy
if: branch IN (develop, beta, stable, master)
script:
Expand Down Expand Up @@ -60,4 +65,4 @@ jobs:
- master
- stable
- develop
- beta
- beta
12 changes: 6 additions & 6 deletions cli/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ def metrics():
@login_required
def first():
print('Please wait - collecting metrics from blockchain...')
bounty_list = get('first-bounties')
if not bounty_list:
bounty_data = get('first-bounties')
if not bounty_data.get('bounties'):
print('No bounties found')
return
print_metrics(bounty_list)
print_metrics(bounty_data)


@metrics.command(help="List of bounties and metrics for the last year")
@login_required
def last():
print('Please wait - collecting metrics from blockchain...')
bounty_list = get('last-bounties')
if not bounty_list:
bounty_data = get('last-bounties')
if not bounty_data.get('bounties'):
print('No bounties found')
return
print_metrics(bounty_list)
print_metrics(bounty_data)
10 changes: 7 additions & 3 deletions cli/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from urllib.parse import urlparse

import click
from readsettings import ReadSettings

from skale.utils.random_names.generator import generate_random_node_name

Expand All @@ -30,9 +29,11 @@
from core.host import install_host_dependencies
from core.helper import (abort_if_false, local_only,
login_required, safe_load_texts)
from configs import CONFIG_FILEPATH, DEFAULT_DB_USER, DEFAULT_DB_PORT, DEFAULT_NODE_BASE_PORT
from configs import DEFAULT_DB_USER, DEFAULT_DB_PORT, DEFAULT_NODE_BASE_PORT
from tools.helper import session_config

config = ReadSettings(CONFIG_FILEPATH)

config = session_config()
TEXTS = safe_load_texts()


Expand Down Expand Up @@ -80,13 +81,15 @@ def node():
@click.option('--format', '-f', type=click.Choice(['json', 'text']))
@login_required
def node_info(format):
config = session_config()
get_node_info(config, format)


@node.command('about', help="Get service info about SKALE node")
@click.option('--format', '-f', type=click.Choice(['json', 'text']))
@login_required
def node_about(format):
config = session_config()
get_node_about(config, format)


Expand Down Expand Up @@ -123,6 +126,7 @@ def node_about(format):
@login_required
# def register_node(name, p2p_ip, public_ip, port):
def register_node(name, ip, port):
config = session_config()
create_node(config, name, ip, ip, port)


Expand Down
5 changes: 3 additions & 2 deletions configs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@

SKALE_DIR = os.path.join(HOME_DIR, '.skale')
PROJECT_PATH = os.path.join(SKALE_DIR, '.skale-node')
NODE_DATA_PATH = os.path.join(SKALE_DIR, 'node_data')
CONFIG_FILEPATH = os.path.join(SKALE_DIR, '.skale-cli.yaml')
NODE_DATA_PATH = os.path.join(SKALE_DIR, 'NODE_DATA_PATH')
CONFIG_FILEPATH = os.environ.get('CONFIG_FILEPATH') or \
os.path.join(SKALE_DIR, '.skale-cli.yaml')

TOKENS_FILEPATH = os.path.join(NODE_DATA_PATH, 'tokens.json')
LOCAL_WALLET_FILEPATH = os.path.join(NODE_DATA_PATH, 'local_wallet.json')
Expand Down
25 changes: 16 additions & 9 deletions core/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
import logging.handlers as py_handlers
from logging import Formatter

from readsettings import ReadSettings
from configs import CONFIG_FILEPATH, TEXT_FILE, SKALE_NODE_UI_LOCALHOST, SKALE_NODE_UI_PORT, \
from configs import TEXT_FILE, SKALE_NODE_UI_LOCALHOST, SKALE_NODE_UI_PORT, \
LONG_LINE, HOST_OS, MAC_OS_SYSTEM_NAME, ROUTES
from configs.cli_logger import LOG_FORMAT, LOG_BACKUP_COUNT, LOG_FILE_SIZE_BYTES, LOG_FILEPATH, \
DEBUG_LOG_FILEPATH
from tools.helper import session_config

config = ReadSettings(CONFIG_FILEPATH)

config = session_config()
logger = logging.getLogger(__name__)


Expand All @@ -49,24 +50,30 @@ def safe_get_config(config, key):
return None


def cookies_exists():
return safe_get_config(config, 'cookies') is not None


def host_exists():
return safe_get_config(config, 'host') is not None


def login_required(f):
@wraps(f)
def inner(*args, **kwargs):
cookies_text = safe_get_config(config, 'cookies')
if not cookies_text:
if cookies_exists():
return f(*args, **kwargs)
else:
TEXTS = safe_load_texts()
print(TEXTS['service']['unauthorized'])
else:
return f(*args, **kwargs)

return inner


def local_only(f):
@wraps(f)
def inner(*args, **kwargs):
host = safe_get_config(config, 'host')
if host:
if host_exists():
print('This command couldn\'t be executed on the remote SKALE host.')
else:
if HOST_OS == MAC_OS_SYSTEM_NAME:
Expand Down
1 change: 1 addition & 0 deletions core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def create_node(config, name, p2p_ip, public_ip, port):
response = post_request(url, data, cookies)

if response is None:
print('Your request returned nothing. Something went wrong. Try again')
return None
if response.status_code == requests.codes.created:
msg = 'Node registered in SKALE manager. For more info run: skale node info'
Expand Down
23 changes: 15 additions & 8 deletions core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def register_user(config, username, password, token):
response = post_request(url, data)
if response is None:
return None

if response.status_code == requests.codes.ok:
cookies_text = pickle.dumps(response.cookies)
config['cookies'] = cookies_text
Expand Down Expand Up @@ -84,19 +83,27 @@ def logout_user(config):
clean_cookies(config)
print('Cookies removed')
else:
print('Logout failed')
print('Logout failed:')
print(response.text)


def show_registration_token(short):
def get_registration_token_data():
try:
with open(TOKENS_FILEPATH, encoding='utf-8') as data_file:
config = json.loads(data_file.read())
return json.loads(data_file.read())
except FileNotFoundError:
return None


def show_registration_token(short):
token_data = get_registration_token_data()
if token_data is not None:
if short:
print(config["token"])
print(token_data["token"])
else:
print(f'User registration token: {config["token"]}')
except FileNotFoundError:
err_msg = "Couldn't find registration tokens file. Check that node inited on this machine."
print(f'User registration token: {token_data["token"]}')
else:
err_msg = ("Couldn't find registration tokens file. "
"Check that node inited on this machine.")
logger.error(err_msg)
print(err_msg)
5 changes: 2 additions & 3 deletions core/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ def get_wallet_info(config, format):
if response is None:
return None

json = response.json()
data = json['data']

json_data = response.json()
data = json_data['data']
if format == 'json':
print(data)
else:
Expand Down
12 changes: 9 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import sys
import logging
import inspect
from readsettings import ReadSettings

from cli import __version__
from cli.info import BUILD_DATETIME, COMMIT, BRANCH, OS, VERSION
Expand All @@ -33,14 +32,15 @@

from core.helper import (login_required, safe_load_texts, local_only,
no_node, init_default_logger)
from configs import CONFIG_FILEPATH, LONG_LINE
from configs import LONG_LINE
from core.wallet import get_wallet_info, set_wallet_by_pk
from core.user import (register_user, login_user, logout_user,
show_registration_token)
from core.host import (test_host, show_host, fix_url, reset_host,
init_logs_dir)
from tools.helper import session_config


config = ReadSettings(CONFIG_FILEPATH)
TEXTS = safe_load_texts()

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -78,6 +78,7 @@ def info():
@click.argument('host')
@click.option('--skip-check', is_flag=True)
def attach(host, skip_check):
config = session_config()
host = fix_url(host)
if not host:
return
Expand All @@ -92,6 +93,7 @@ def attach(host, skip_check):
@cli.command('host', help="Get SKALE node endpoint")
@click.option('--reset', is_flag=True)
def host(reset):
config = session_config()
if reset:
reset_host(config)
return
Expand Down Expand Up @@ -131,6 +133,7 @@ def user_token(short):
hide_input=True
)
def register(username, password, token):
config = session_config()
register_user(config, username, password, token)


Expand All @@ -147,11 +150,13 @@ def register(username, password, token):
hide_input=True
)
def login(username, password):
config = session_config()
login_user(config, username, password)


@user.command('logout', help="Logout from SKALE node")
def logout():
config = session_config()
logout_user(config)


Expand All @@ -164,6 +169,7 @@ def wallet():
@click.option('--format', '-f', type=click.Choice(['json', 'text']))
@login_required
def wallet_info(format):
config = session_config()
get_wallet_info(config, format)


Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PyInstaller==3.5
boto3==1.9.233
flake8==3.7.8
bumpversion==0.5.3
bumpversion==0.5.3
pytest==5.2.1
6 changes: 6 additions & 0 deletions scripts/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
PROJECT_DIR=$(dirname $DIR)

CONFIG_FILEPATH='tests/test-skale-cli.yaml' py.test tests/ $@
Loading

0 comments on commit ccc7b43

Please sign in to comment.