2017-11-13 10:49:45 +00:00
|
|
|
from tests import test_data
|
2017-08-31 13:39:41 +00:00
|
|
|
import requests
|
|
|
|
import re
|
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-01-26 11:07:09 +00:00
|
|
|
from hashlib import md5
|
|
|
|
import hmac
|
2017-08-31 13:39:41 +00:00
|
|
|
|
|
|
|
storage = 'http://artifacts.status.im:8081/artifactory/nightlies-local/'
|
|
|
|
|
|
|
|
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)
|
2017-08-31 13:39:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_latest_apk():
|
|
|
|
raw_data = requests.request('GET', storage).text
|
|
|
|
dates = re.findall("\d{2}-[a-zA-Z]{3}-\d{4} \d{2}:\d{2}", raw_data)
|
|
|
|
dates.sort(key=lambda date: datetime.strptime(date, "%d-%b-%Y %H:%M"), reverse=True)
|
|
|
|
return re.findall('>(.*k)</a>\s*%s' % dates[0], raw_data)[0]
|
|
|
|
|
2018-01-03 09:34:40 +00:00
|
|
|
|
2017-11-13 10:49:45 +00:00
|
|
|
latest_nightly_apk = dict()
|
|
|
|
latest_nightly_apk['name'] = get_latest_apk()
|
|
|
|
latest_nightly_apk['url'] = storage + latest_nightly_apk['name']
|
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",
|
2017-11-13 10:49:45 +00:00
|
|
|
default='build_' + latest_nightly_apk['name'],
|
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',
|
2017-11-13 10:49:45 +00:00
|
|
|
default=latest_nightly_apk['url'],
|
|
|
|
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'])):
|
2017-11-13 10:49:45 +00:00
|
|
|
if stored_files['files'][i]['name'] == test_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)
|
|
|
|
test_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/'
|
|
|
|
+ sauce_username + '/' + test_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'))
|
|
|
|
|
|
|
|
|
|
|
|
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')))
|
|
|
|
with open('github_comment.txt', 'r') as comment:
|
|
|
|
pull.create_issue_comment('# Automated test results: \n' + comment.read())
|
|
|
|
|
|
|
|
|
|
|
|
def get_public_url(job_id):
|
|
|
|
token = hmac.new(bytes(sauce_username + ":" + sauce_access_key, 'latin-1'),
|
|
|
|
bytes(job_id, 'latin-1'), md5).hexdigest()
|
|
|
|
return "https://saucelabs.com/jobs/%s?auth=%s" % (job_id, token)
|
|
|
|
|
|
|
|
|
|
|
|
def make_github_report(error=None):
|
|
|
|
if pytest.config.getoption('pr_number'):
|
|
|
|
title = '### %s' % test_data.test_name
|
|
|
|
outcome = '%s' % ':x:' if error else ':white_check_mark:' + ':\n'
|
|
|
|
title += outcome
|
|
|
|
steps = '\n\n <details>\n<summary>Test Steps & Error message:</summary>\n\n ```%s ```%s\n\n</details>\n' % \
|
|
|
|
(test_data.test_info[test_data.test_name]['steps'], '\n```' + error + '```' if error else '')
|
|
|
|
sessions = str()
|
|
|
|
|
|
|
|
for job_id in test_data.test_info[test_data.test_name]['jobs']:
|
|
|
|
sessions += ' - [Android Device Session](%s) \n' % get_public_url(job_id)
|
|
|
|
with open('github_comment.txt', 'a') as comment:
|
|
|
|
comment.write(title + '\n' + steps + '\n' + sessions + '---\n')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.hookwrapper
|
|
|
|
def pytest_runtest_makereport(item, call):
|
|
|
|
outcome = yield
|
|
|
|
report = outcome.get_result()
|
|
|
|
if pytest.config.getoption('env') == 'sauce':
|
|
|
|
if report.when == 'call':
|
|
|
|
if report.passed:
|
|
|
|
for job_id in test_data.test_info[test_data.test_name]['jobs']:
|
|
|
|
sauce.jobs.update_job(job_id, name=test_data.test_name, passed=True)
|
|
|
|
make_github_report()
|
|
|
|
if report.failed:
|
|
|
|
for job_id in test_data.test_info[test_data.test_name]['jobs']:
|
|
|
|
sauce.jobs.update_job(job_id, name=test_data.test_name, passed=False)
|
|
|
|
make_github_report(error=report.longreprtext)
|
2017-11-13 10:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_runtest_setup(item):
|
2018-01-26 11:07:09 +00:00
|
|
|
test_data.test_name = item.name
|