This commit is contained in:
vagrant 2014-01-31 19:35:46 +00:00
Родитель 5fad6b8b4b
Коммит 35be73269c
8 изменённых файлов: 94 добавлений и 46 удалений

Просмотреть файл

@ -20,4 +20,4 @@ oauth2==1.5.211
httplib2==0.7.4 httplib2==0.7.4
git+git://github.com/jeads/datasource@143ac08d11 git+git://github.com/jeads/datasource@143ac08d11
git+git://github.com/mozilla/treeherder-client@7c71e756cf git+git://github.com/mozilla/treeherder-client@abe6a85180

Просмотреть файл

@ -5,7 +5,6 @@ import sys
from django.core.management import call_command from django.core.management import call_command
import pytest import pytest
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addoption( parser.addoption(
"--runslow", "--runslow",
@ -101,7 +100,6 @@ def initial_data():
call_command('load_initial_data') call_command('load_initial_data')
@pytest.fixture() @pytest.fixture()
def jm(): def jm():
""" Give a test access to a JobsModel instance. """ """ Give a test access to a JobsModel instance. """

Просмотреть файл

@ -1,14 +1,38 @@
import pytest import pytest
from webtest.app import TestApp from webtest.app import TestApp
from treeherder.etl.mixins import JsonLoaderMixin from treeherder.etl.mixins import JsonLoaderMixin, OAuthLoaderMixin
from treeherder.webapp.wsgi import application from treeherder.webapp.wsgi import application
from treeherder.etl import common from treeherder.etl import common
from tests.sampledata import SampleData
@pytest.fixture @pytest.fixture
def mock_post_json_data(monkeypatch): def mock_post_json_data(monkeypatch):
"""mock the urllib call replacing it with a webtest call""" """mock the urllib call replacing it with a webtest call"""
def _post_json_data(adapter, url, data): def _post_json_data(adapter, url, data):
response = TestApp(application).post_json(url, params=data)
tjc = TreeherderJobCollection()
tj = tjc.get_job(data)
tjc.add(tj)
OAuthLoaderMixin.set_credentials( SampleData.get_credentials() )
credentials = OAuthLoaderMixin.get_credentials('test_treeherder')
tr = TreeherderRequest(
protocol='http',
host='localhost',
project=jm.project,
oauth_key=credentials['consumer_key'],
oauth_secret=credentials['consumer_secret']
)
signed_uri = tr.get_signed_uri(tjc.to_json(), tr.get_uri(tjc))
#response = TestApp(application).post_json(url, params=data)
response = TestApp(application).post_json(
str(signed_uri), params=tjc.get_collection_data()
)
response.getcode = lambda: response.status_int response.getcode = lambda: response.status_int
return response return response

Просмотреть файл

@ -5,6 +5,18 @@ from django.conf import settings
class SampleData(object): class SampleData(object):
@classmethod
def get_credentials(cls):
credentials = {
'test_treeherder': {
'consumer_key':'8de17836-4a9b-45f5-824c-5ada76713334',
'consumer_secret':'0f71d011-d773-4831-9f1c-17b237207467'
}
}
return credentials
def __init__(self): def __init__(self):
self.job_data_file = "{0}/sample_data/job_data.txt".format( self.job_data_file = "{0}/sample_data/job_data.txt".format(
@ -60,4 +72,5 @@ class SampleData(object):
def get_log_path(self, name): def get_log_path(self, name):
"""Returns the full path to a log file""" """Returns the full path to a log file"""
return "{0}/{1}".format(self.logs_dir, name) return "{0}/{1}".format(self.logs_dir, name)

Просмотреть файл

@ -556,20 +556,28 @@ class Builds4hAnalyzer(JsonExtractorMixin, Builds4hTransformerMixin):
# found in the analysis file # found in the analysis file
with open(self.builds4h_analysis_file_path) as f: with open(self.builds4h_analysis_file_path) as f:
data = f.read() data = f.read()
deserialized_data = json.loads(data)
deserialized_data = {}
if data:
deserialized_data = json.loads(data)
self.report_obj['guids'] = deserialized_data.get('guids', {}) self.report_obj['guids'] = deserialized_data.get('guids', {})
for analysis_type in deserialized_data['analyzers']: if 'analyzers' in deserialized_data:
self.report_obj['analyzers'][analysis_type]['data'] = \ for analysis_type in deserialized_data['analyzers']:
deserialized_data['analyzers'][analysis_type] self.report_obj['analyzers'][analysis_type]['data'] = \
deserialized_data['analyzers'][analysis_type]
def get_blacklist(self): def get_blacklist(self):
if os.path.isfile(self.builds4h_blacklist_file_path): if os.path.isfile(self.builds4h_blacklist_file_path):
with open(self.builds4h_blacklist_file_path) as f: with open(self.builds4h_blacklist_file_path) as f:
data = f.read() data = f.read()
deserialized_data = json.loads(data)
deserialized_data = []
if data:
deserialized_data = json.loads(data)
self.blacklist = set(deserialized_data) self.blacklist = set(deserialized_data)
def write_report(self): def write_report(self):

Просмотреть файл

@ -134,10 +134,12 @@ class OAuthLoaderMixin(object):
return parameters return parameters
@classmethod @classmethod
def set_credentials(cls): def set_credentials(cls, credentials={}):
# Only get the credentials once # Only get the credentials once
if not cls.credentials: if not cls.credentials and not credentials:
cls.credentials = TreeherderModelBase.get_oauth_credentials() cls.credentials = TreeherderModelBase.get_oauth_credentials()
else:
cls.credentials = credentials
@classmethod @classmethod
def get_credentials(cls, project): def get_credentials(cls, project):

Просмотреть файл

@ -75,7 +75,6 @@ def oauth_required(func):
) )
server = oauth.Server() server = oauth.Server()
token = oauth.Token(key='', secret='') token = oauth.Token(key='', secret='')
#Get the consumer object #Get the consumer object

68
vendor/thclient/client.py поставляемый
Просмотреть файл

@ -580,45 +580,18 @@ class TreeherderRequest(object):
collection_inst.validate() collection_inst.validate()
uri = self.get_uri(collection_inst)
# Build the header # Build the header
headers = {'Content-Type': 'application/json'} headers = {'Content-Type': 'application/json'}
use_oauth = bool(self.oauth_key and self.oauth_secret) use_oauth = bool(self.oauth_key and self.oauth_secret)
body = collection_inst.get_collection_data() serialized_body = collection_inst.to_json()
uri = self.get_uri(collection_inst)
if use_oauth: if use_oauth:
uri = self.get_signed_uri(serialized_body, uri)
# There is no requirement for the token in two-legged
# OAuth but we still need the token object.
token = oauth.Token(key='', secret='')
consumer = oauth.Consumer(key=self.oauth_key, secret=self.oauth_secret)
parameters = {
'user':self.project,
'oauth_version':'1.0',
'oauth_nonce':oauth.generate_nonce(),
'oauth_timestamp':int(time.time())
}
try:
req = oauth.Request(
method='POST',
body=json.dumps(body),
url=uri,
parameters=parameters
)
except AssertionError, e:
print 'uri: %s' % uri
print 'body: %s' % body
raise
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, token)
uri = req.to_url()
# Make the POST request # Make the POST request
conn = None conn = None
@ -627,10 +600,41 @@ class TreeherderRequest(object):
else: else:
conn = httplib.HTTPSConnection(self.host) conn = httplib.HTTPSConnection(self.host)
conn.request('POST', uri, json.dumps(body), headers) conn.request('POST', uri, serialized_body, headers)
return conn.getresponse() return conn.getresponse()
def get_signed_uri(self, serialized_body, uri):
# There is no requirement for the token in two-legged
# OAuth but we still need the token object.
token = oauth.Token(key='', secret='')
consumer = oauth.Consumer(key=self.oauth_key, secret=self.oauth_secret)
parameters = {
'user':self.project,
'oauth_version':'1.0',
'oauth_nonce':oauth.generate_nonce(),
'oauth_timestamp':int(time.time())
}
try:
req = oauth.Request(
method='POST',
body=serialized_body,
url=uri,
parameters=parameters
)
except AssertionError, e:
print 'uri: %s' % uri
print 'body: %s' % body
raise
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, token)
return req.to_url()
def get_uri(self, collection_inst): def get_uri(self, collection_inst):
uri = '{0}://{1}/api/project/{2}/{3}/'.format( uri = '{0}://{1}/api/project/{2}/{3}/'.format(