2018-08-10 10:09:19 +00:00
|
|
|
import requests
|
|
|
|
import pytest
|
|
|
|
import re
|
2018-04-28 09:02:39 +00:00
|
|
|
from _pytest.runner import runtestprotocol
|
|
|
|
from support.test_rerun import should_rerun_test
|
2018-03-31 20:05:11 +00:00
|
|
|
from tests import test_suite_data
|
2017-08-31 13:39:41 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from os import environ
|
2017-09-26 10:50:34 +00:00
|
|
|
from io import BytesIO
|
2017-08-31 13:39:41 +00:00
|
|
|
from sauceclient import SauceClient
|
2018-09-17 08:50:01 +00:00
|
|
|
from support.api.network_api import NetworkApi
|
2018-04-26 12:34:15 +00:00
|
|
|
from support.github_report import GithubHtmlReport
|
|
|
|
from support.testrail_report import TestrailReport
|
2018-09-17 08:50:01 +00:00
|
|
|
from tests.users import transaction_senders
|
2017-08-31 13:39:41 +00:00
|
|
|
|
|
|
|
sauce_username = environ.get('SAUCE_USERNAME')
|
|
|
|
sauce_access_key = environ.get('SAUCE_ACCESS_KEY')
|
2018-01-26 11:07:09 +00:00
|
|
|
github_token = environ.get('GIT_HUB_TOKEN')
|
|
|
|
|
|
|
|
sauce = SauceClient(sauce_username, sauce_access_key)
|
2018-02-26 23:38:56 +00:00
|
|
|
github_report = GithubHtmlReport(sauce_username, sauce_access_key)
|
2018-04-26 12:34:15 +00:00
|
|
|
testrail_report = TestrailReport(sauce_username, sauce_access_key)
|
2017-08-31 13:39:41 +00:00
|
|
|
|
|
|
|
|
2017-08-28 10:02:20 +00:00
|
|
|
def pytest_addoption(parser):
|
2017-08-31 13:39:41 +00:00
|
|
|
parser.addoption("--build",
|
|
|
|
action="store",
|
2018-04-25 17:02:03 +00:00
|
|
|
default=datetime.now().strftime('%Y-%m-%d-%H-%M'),
|
2017-08-28 10:02:20 +00:00
|
|
|
help="Specify build name")
|
2017-08-31 13:39:41 +00:00
|
|
|
parser.addoption('--apk',
|
|
|
|
action='store',
|
2018-04-30 09:58:20 +00:00
|
|
|
default=None,
|
2017-11-13 10:49:45 +00:00
|
|
|
help='Url or local path to apk')
|
2017-10-05 19:41:17 +00:00
|
|
|
parser.addoption('--env',
|
|
|
|
action='store',
|
|
|
|
default='sauce',
|
2018-06-07 15:13:37 +00:00
|
|
|
help='Specify environment: local/sauce/api')
|
2017-11-13 10:49:45 +00:00
|
|
|
parser.addoption('--log',
|
|
|
|
action='store',
|
|
|
|
default=False,
|
|
|
|
help='Display each test step in terminal as plain text: True/False')
|
2018-01-26 11:07:09 +00:00
|
|
|
parser.addoption('--pr_number',
|
|
|
|
action='store',
|
|
|
|
default=None,
|
|
|
|
help='Pull Request number')
|
2018-05-23 13:02:45 +00:00
|
|
|
parser.addoption('--testrail_report',
|
2018-04-26 12:34:15 +00:00
|
|
|
action='store',
|
|
|
|
default=False,
|
2018-05-23 13:02:45 +00:00
|
|
|
help='boolean; For creating testrail report per run')
|
2018-08-07 09:08:54 +00:00
|
|
|
parser.addoption('--network',
|
|
|
|
action='store',
|
|
|
|
default='ropsten',
|
|
|
|
help='string; ropsten or rinkeby')
|
|
|
|
|
|
|
|
# message reliability
|
2018-09-03 21:18:18 +00:00
|
|
|
|
2018-04-28 09:02:39 +00:00
|
|
|
parser.addoption('--rerun_count',
|
|
|
|
action='store',
|
|
|
|
default=0,
|
|
|
|
help='How many times tests should be re-run if failed')
|
2018-03-31 20:05:11 +00:00
|
|
|
parser.addoption('--messages_number',
|
|
|
|
action='store',
|
|
|
|
default=20,
|
|
|
|
help='Messages number')
|
|
|
|
parser.addoption('--message_wait_time',
|
|
|
|
action='store',
|
|
|
|
default=20,
|
|
|
|
help='Max time to wait for a message to be received')
|
|
|
|
parser.addoption('--participants_number',
|
|
|
|
action='store',
|
|
|
|
default=5,
|
|
|
|
help='Public chat participants number')
|
2018-06-05 15:12:15 +00:00
|
|
|
parser.addoption('--chat_name',
|
|
|
|
action='store',
|
|
|
|
default=None,
|
|
|
|
help='Public chat name')
|
|
|
|
parser.addoption('--user_public_key',
|
|
|
|
action='store',
|
|
|
|
default=None,
|
|
|
|
help='Public key of user for 1-1 chat')
|
2018-04-28 09:02:39 +00:00
|
|
|
|
2018-09-03 21:18:18 +00:00
|
|
|
# chat bot
|
|
|
|
|
|
|
|
parser.addoption('--public_keys',
|
|
|
|
action='store',
|
2018-09-28 12:31:02 +00:00
|
|
|
default='',
|
2018-09-03 21:18:18 +00:00
|
|
|
help='List of public keys for one-to-one chats')
|
|
|
|
parser.addoption('--running_time',
|
|
|
|
action='store',
|
2018-09-14 13:35:37 +00:00
|
|
|
default=600,
|
2018-09-03 21:18:18 +00:00
|
|
|
help='Running time in seconds')
|
|
|
|
|
2018-04-28 09:02:39 +00:00
|
|
|
|
|
|
|
def get_rerun_count():
|
|
|
|
return int(pytest.config.getoption('rerun_count'))
|
2017-08-31 13:39:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_master(config):
|
|
|
|
return not hasattr(config, 'slaveinput')
|
|
|
|
|
|
|
|
|
|
|
|
def is_uploaded():
|
2018-01-26 11:07:09 +00:00
|
|
|
stored_files = sauce.storage.get_stored_files()
|
2017-08-31 13:39:41 +00:00
|
|
|
for i in range(len(stored_files['files'])):
|
2018-02-26 23:38:56 +00:00
|
|
|
if stored_files['files'][i]['name'] == test_suite_data.apk_name:
|
2017-08-31 13:39:41 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
2017-11-13 10:49:45 +00:00
|
|
|
if config.getoption('log'):
|
|
|
|
import logging
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
2018-06-07 15:13:37 +00:00
|
|
|
if config.getoption('env') != 'api':
|
|
|
|
test_suite_data.apk_name = ([i for i in [i for i in config.getoption('apk').split('/')
|
|
|
|
if '.apk' in i]])[0]
|
|
|
|
if is_master(config):
|
|
|
|
if config.getoption('testrail_report'):
|
2018-08-07 16:38:12 +00:00
|
|
|
pr_number = config.getoption('pr_number')
|
|
|
|
if pr_number:
|
|
|
|
run_number = len(testrail_report.get_runs(pr_number)) + 1
|
|
|
|
run_name = 'PR-%s run #%s' % (pr_number, run_number)
|
|
|
|
else:
|
|
|
|
run_name = test_suite_data.apk_name
|
|
|
|
testrail_report.add_run(run_name)
|
2018-06-07 15:13:37 +00:00
|
|
|
if config.getoption('env') == 'sauce':
|
|
|
|
if not is_uploaded():
|
|
|
|
if 'http' in config.getoption('apk'):
|
|
|
|
response = requests.get(config.getoption('apk'), stream=True)
|
|
|
|
response.raise_for_status()
|
|
|
|
file = BytesIO(response.content)
|
|
|
|
del response
|
|
|
|
requests.post('http://saucelabs.com/rest/v1/storage/'
|
|
|
|
+ sauce_username + '/' + test_suite_data.apk_name + '?overwrite=true',
|
|
|
|
auth=(sauce_username, sauce_access_key),
|
|
|
|
data=file,
|
|
|
|
headers={'Content-Type': 'application/octet-stream'})
|
|
|
|
else:
|
|
|
|
sauce.storage.upload_file(config.getoption('apk'))
|
2018-01-26 11:07:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_unconfigure(config):
|
2018-04-26 12:34:15 +00:00
|
|
|
if is_master(config):
|
|
|
|
if config.getoption('pr_number'):
|
|
|
|
from github import Github
|
|
|
|
repo = Github(github_token).get_user('status-im').get_repo('status-react')
|
|
|
|
pull = repo.get_pull(int(config.getoption('pr_number')))
|
|
|
|
pull.create_issue_comment(github_report.build_html_report())
|
2018-05-23 13:02:45 +00:00
|
|
|
if config.getoption('testrail_report'):
|
2018-04-26 12:34:15 +00:00
|
|
|
testrail_report.add_results()
|
2018-01-26 11:07:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.hookwrapper
|
|
|
|
def pytest_runtest_makereport(item, call):
|
|
|
|
outcome = yield
|
|
|
|
report = outcome.get_result()
|
2018-02-26 23:38:56 +00:00
|
|
|
if report.when == 'call':
|
2018-04-28 09:02:39 +00:00
|
|
|
is_sauce_env = pytest.config.getoption('env') == 'sauce'
|
|
|
|
current_test = test_suite_data.current_test
|
2018-02-26 23:38:56 +00:00
|
|
|
if report.failed:
|
2018-08-10 10:09:19 +00:00
|
|
|
error = report.longreprtext
|
2018-08-29 14:04:12 +00:00
|
|
|
exception = re.findall('E.*Message:|E.*Error:|E.*Failed:', error)
|
2018-08-10 10:09:19 +00:00
|
|
|
if exception:
|
2018-08-15 12:51:52 +00:00
|
|
|
error = error.replace(re.findall('E.*Message:|E.*Error:|E.*Failed:', report.longreprtext)[0], '')
|
2018-08-10 10:09:19 +00:00
|
|
|
current_test.testruns[-1].error = error
|
2018-02-26 23:38:56 +00:00
|
|
|
if is_sauce_env:
|
2018-04-28 09:02:39 +00:00
|
|
|
update_sauce_jobs(current_test.name, current_test.testruns[-1].jobs, report.passed)
|
2018-02-26 23:38:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_sauce_jobs(test_name, job_ids, passed):
|
2018-08-15 12:51:52 +00:00
|
|
|
for job_id in job_ids.keys():
|
2018-02-26 23:38:56 +00:00
|
|
|
sauce.jobs.update_job(job_id, name=test_name, passed=passed)
|
2017-11-13 10:49:45 +00:00
|
|
|
|
|
|
|
|
2018-04-26 12:34:15 +00:00
|
|
|
def get_testrail_case_id(obj):
|
2018-06-30 12:17:38 +00:00
|
|
|
if 'testrail_id' in obj.keywords._markers:
|
|
|
|
return obj.keywords._markers['testrail_id'].args[0]
|
2018-04-26 12:34:15 +00:00
|
|
|
|
|
|
|
|
2017-11-13 10:49:45 +00:00
|
|
|
def pytest_runtest_setup(item):
|
2018-04-28 09:02:39 +00:00
|
|
|
test_suite_data.set_current_test(item.name, testrail_case_id=get_testrail_case_id(item))
|
|
|
|
test_suite_data.current_test.create_new_testrun()
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_runtest_protocol(item, nextitem):
|
|
|
|
for i in range(get_rerun_count()):
|
|
|
|
reports = runtestprotocol(item, nextitem=nextitem)
|
|
|
|
for report in reports:
|
|
|
|
if report.failed and should_rerun_test(report.longreprtext):
|
|
|
|
break # rerun
|
|
|
|
else:
|
|
|
|
return True # no need to rerun
|
2018-03-31 20:05:11 +00:00
|
|
|
|
|
|
|
|
2018-09-17 08:50:01 +00:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def faucet_for_senders():
|
|
|
|
network_api = NetworkApi()
|
|
|
|
for user in transaction_senders.values():
|
|
|
|
network_api.faucet(address=user['address'])
|
|
|
|
|
|
|
|
|
2018-03-31 20:05:11 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def messages_number():
|
|
|
|
return int(pytest.config.getoption('messages_number'))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def message_wait_time():
|
|
|
|
return int(pytest.config.getoption('message_wait_time'))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def participants_number():
|
|
|
|
return int(pytest.config.getoption('participants_number'))
|
2018-06-05 15:12:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def chat_name():
|
|
|
|
return pytest.config.getoption('chat_name')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def user_public_key():
|
|
|
|
return pytest.config.getoption('user_public_key')
|