2018-02-26 23:38:56 +00:00
|
|
|
from tests import test_suite_data, SingleTestData
|
2017-08-31 13:39:41 +00:00
|
|
|
import requests
|
2018-01-26 11:07:09 +00:00
|
|
|
import pytest
|
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-02-26 23:38:56 +00:00
|
|
|
from support.github_test_report import GithubHtmlReport
|
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)
|
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',
|
2017-11-13 10:49:45 +00:00
|
|
|
help='Specify environment: local/sauce')
|
|
|
|
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')
|
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-02-26 23:38:56 +00:00
|
|
|
test_suite_data.apk_name = ([i for i in [i for i in config.getoption('apk').split('/')
|
|
|
|
if '.apk' in i]])[0]
|
2017-10-11 20:10:57 +00:00
|
|
|
if is_master(config) and config.getoption('env') == 'sauce':
|
2018-01-26 11:07:09 +00:00
|
|
|
if config.getoption('pr_number'):
|
|
|
|
with open('github_comment.txt', 'w') as _:
|
|
|
|
pass
|
2017-08-31 13:39:41 +00:00
|
|
|
if not is_uploaded():
|
2018-01-26 11:07:09 +00:00
|
|
|
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/'
|
2018-02-26 23:38:56 +00:00
|
|
|
+ sauce_username + '/' + test_suite_data.apk_name + '?overwrite=true',
|
2018-01-26 11:07:09 +00:00
|
|
|
auth=(sauce_username, sauce_access_key),
|
|
|
|
data=file,
|
|
|
|
headers={'Content-Type': 'application/octet-stream'})
|
|
|
|
else:
|
|
|
|
sauce.storage.upload_file(config.getoption('apk'))
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_unconfigure(config):
|
|
|
|
if is_master(config) and 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')))
|
2018-02-26 23:38:56 +00:00
|
|
|
pull.create_issue_comment(github_report.build_html_report())
|
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
|
|
|
is_sauce_env = pytest.config.getoption('env') == 'sauce'
|
|
|
|
current_test = test_suite_data.current_test
|
|
|
|
if report.when == 'call':
|
|
|
|
if report.failed:
|
|
|
|
current_test.error = report.longreprtext
|
|
|
|
if is_sauce_env:
|
|
|
|
update_sauce_jobs(current_test.name, current_test.jobs, report.passed)
|
|
|
|
github_report.save_test(current_test)
|
|
|
|
|
|
|
|
|
|
|
|
def update_sauce_jobs(test_name, job_ids, passed):
|
|
|
|
for job_id in job_ids:
|
|
|
|
sauce.jobs.update_job(job_id, name=test_name, passed=passed)
|
2017-11-13 10:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_runtest_setup(item):
|
2018-04-30 09:58:20 +00:00
|
|
|
test_suite_data.add_test(SingleTestData(item.name))
|