def test_send(self, mock_HTTPConnection, mock_time, mock_generate_nonce): """Can send data to the server.""" mock_time.return_value = 1342229050 mock_generate_nonce.return_value = "46810593" host = "datazilla.mozilla.org" project = "project" key = "oauth-key" secret = "oauth-secret" req = DatazillaRequest(host, project, key, secret) mock_conn = mock_HTTPConnection.return_value mock_request = mock_conn.request mock_response = mock_conn.getresponse.return_value response = req.send({"some": "data"}) self.assertEqual(mock_HTTPConnection.call_count, 1) self.assertEqual(mock_HTTPConnection.call_args[0][0], host) self.assertEqual(mock_response, response) self.assertEqual(mock_request.call_count, 1) method, path, data, header = mock_request.call_args[0] self.assertEqual(method, "POST") self.assertEqual(path, "/project/api/load_test") self.assertEqual(data, 'oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D&oauth_nonce=46810593&oauth_timestamp=1342229050&oauth_consumer_key=oauth-key&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=&user=project&oauth_signature=mKpovMfgWJqlcVKSdcTCbw4gfaM%3D&data=%257B%2522some%2522%253A%2520%2522data%2522%257D') self.assertEqual( header['Content-type'], 'application/x-www-form-urlencoded', )
def test_send_without_oauth(self, mock_HTTPConnection): """Can send data to the server without oauth""" protocol = 'http' host = "datazilla.mozilla.org" project = "project" key = None secret = None req = DatazillaRequest(protocol, host, project, key, secret, branch='mozilla-try') mock_conn = mock_HTTPConnection.return_value mock_request = mock_conn.request mock_response = mock_conn.getresponse.return_value response = req.send({"some": "data"}) self.assertEqual(mock_HTTPConnection.call_count, 1) self.assertEqual(mock_HTTPConnection.call_args[0][0], host) self.assertEqual(mock_response, response) self.assertEqual(mock_request.call_count, 1) method, path, data, header = mock_request.call_args[0] self.assertEqual(method, "POST") self.assertEqual(path, "/project/api/load_test") self.assertEqual(data, 'data=%257B%2522some%2522%253A%2520%2522data%2522%257D') self.assertEqual( header['Content-type'], 'application/x-www-form-urlencoded', )
def test_send(self, mock_HTTPConnection, mock_time, mock_generate_nonce): """Can send data to the server.""" mock_time.return_value = 1342229050 mock_generate_nonce.return_value = "46810593" protocol = 'http' host = "datazilla.mozilla.org" project = "project" key = "oauth-key" secret = "oauth-secret" req = DatazillaRequest(protocol, host, project, key, secret, branch='mozilla-try') mock_conn = mock_HTTPConnection.return_value mock_request = mock_conn.request mock_response = mock_conn.getresponse.return_value response = req.send({"some": "data"}) self.assertEqual(mock_HTTPConnection.call_count, 1) self.assertEqual(mock_HTTPConnection.call_args[0][0], host) self.assertEqual(mock_response, response) self.assertEqual(mock_request.call_count, 1) method, path, data, header = mock_request.call_args[0] self.assertEqual(method, "POST") self.assertEqual(path, "/project/api/load_test") self.assertEqual( data, 'oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D&oauth_nonce=46810593&oauth_timestamp=1342229050&oauth_consumer_key=oauth-key&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=&user=project&oauth_signature=mKpovMfgWJqlcVKSdcTCbw4gfaM%3D&data=%257B%2522some%2522%253A%2520%2522data%2522%257D' ) self.assertEqual( header['Content-type'], 'application/x-www-form-urlencoded', )
def test_send_without_oauth(self, mock_HTTPConnection): """Can send data to the server without oauth""" protocol = 'http' host = "datazilla.mozilla.org" project = "project" key = None secret = None req = DatazillaRequest(protocol, host, project, key, secret, branch='mozilla-try') mock_conn = mock_HTTPConnection.return_value mock_request = mock_conn.request mock_response = mock_conn.getresponse.return_value response = req.send({"some": "data"}) self.assertEqual(mock_HTTPConnection.call_count, 1) self.assertEqual(mock_HTTPConnection.call_args[0][0], host) self.assertEqual(mock_response, response) self.assertEqual(mock_request.call_count, 1) method, path, data, header = mock_request.call_args[0] self.assertEqual(method, "POST") self.assertEqual(path, "/project/api/load_test") self.assertEqual( data, 'data=%257B%2522some%2522%253A%2520%2522data%2522%257D') self.assertEqual( header['Content-type'], 'application/x-www-form-urlencoded', )
def post_to_datazilla(self, test_result): """ take test_results (json) and upload them to datazilla """ # We will attach wpt_data to the datazilla result as a top # level attribute to store out of band data about the test. wpt_data = { "url": "", "firstView": {}, "repeatView": {} } wpt_data["label"] = test_result["data"]["label"] submit_results = False if self.job.datazilla == "on": # Do not short circuit the function but collect # additional data for use in emailing the user # before returning. submit_results = True self.logger.debug('Submit results to datazilla: %s' % self.job.datazilla) wpt_data["connectivity"] = test_result["data"]["connectivity"] wpt_data["location"] = test_result["data"]["location"] wpt_data["url"] = test_result["data"]["url"] runs = test_result["data"]["runs"] # runs[0] is a dummy entry # runs[1]["firstView"]["SpeedIndex"] # runs[1]["repeatView"]["SpeedIndex"] # runs[1]["firstView"]["requests"][0]["headers"]["request"][2] # "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:26.0) Gecko/20100101 Firefox/26.0 PTST/125" wpt_metric_keys = ['TTFB', 'render', 'docTime', 'fullyLoaded', 'SpeedIndex', 'SpeedIndexDT', 'bytesInDoc', 'requestsDoc', 'domContentLoadedEventStart', 'visualComplete'] for wpt_key in wpt_metric_keys: for view in "firstView", "repeatView": wpt_data[view][wpt_key] = [] if len(runs) == 1: raise Exception("post_to_datazilla: no runs") os_version = "unknown" os_name = "unknown" platform = "x86" reUserAgent = re.compile('User-Agent: Mozilla/5.0 \(Windows NT ([^;]*);.*') for run in runs: for wpt_key in wpt_metric_keys: for view in "firstView", "repeatView": if not run[view]: continue if wpt_key in run[view]: if run[view][wpt_key]: wpt_data[view][wpt_key].append(run[view][wpt_key]) if os_name == "unknown": try: requests = run[view]["requests"] if requests and len(requests) > 0: request = requests[0] if request: headers = request["headers"] if headers: request_headers = headers["request"] if request_headers: for request_header in request_headers: if "User-Agent" in request_header: match = re.match(reUserAgent, request_header) if match: os_name = "WINNT" os_version = match.group(1) break except KeyError: pass machine_name = wpt_data["location"].split(":")[0] # limit suite name to 128 characters to match mysql column size suite_name = (wpt_data["location"] + "." + wpt_data["connectivity"])[:128] # limit {first,repeat}_name, to 255 characters to match mysql column size # leave protocol in the url in order to distinguish http vs https. first_name = wpt_data["url"][:252] + ":fv" repeat_name = wpt_data["url"][:252] + ":rv" result = DatazillaResult() result.add_testsuite(suite_name) result.add_test_results(suite_name, first_name, wpt_data["firstView"]["SpeedIndex"]) result.add_test_results(suite_name, repeat_name, wpt_data["repeatView"]["SpeedIndex"]) request = DatazillaRequest("https", "datazilla.mozilla.org", "webpagetest", self.oauth_key, self.oauth_secret, machine_name=machine_name, os=os_name, os_version=os_version, platform=platform, build_name=self.build_name, version=self.build_version, revision=self.build_revision, branch=self.build_branch, id=self.build_id) request.add_datazilla_result(result) datasets = request.datasets() for dataset in datasets: dataset["wpt_data"] = wpt_data if not submit_results: continue response = request.send(dataset) # print error responses if response.status != 200: # use lower-case string because buildbot is sensitive to upper case error # as in 'INTERNAL SERVER ERROR' # https://bugzilla.mozilla.org/show_bug.cgi?id=799576 reason = response.reason.lower() self.logger.debug("Error posting to %s %s %s: %s %s" % ( wpt_data["url"], wpt_data["location"], wpt_data["connectivity"], response.status, reason)) else: res = response.read() self.logger.debug("Datazilla response for %s %s %s is: %s" % ( wpt_data["url"], wpt_data["location"], wpt_data["connectivity"], res.lower())) return datasets