|  | @@ -0,0 +1,113 @@
 | 
	
		
			
				|  |  | +#!/usr/bin/env python
 | 
	
		
			
				|  |  | +# Copyright 2017, Google Inc.
 | 
	
		
			
				|  |  | +# All rights reserved.
 | 
	
		
			
				|  |  | +#
 | 
	
		
			
				|  |  | +# Redistribution and use in source and binary forms, with or without
 | 
	
		
			
				|  |  | +# modification, are permitted provided that the following conditions are
 | 
	
		
			
				|  |  | +# met:
 | 
	
		
			
				|  |  | +#
 | 
	
		
			
				|  |  | +#     * Redistributions of source code must retain the above copyright
 | 
	
		
			
				|  |  | +# notice, this list of conditions and the following disclaimer.
 | 
	
		
			
				|  |  | +#     * Redistributions in binary form must reproduce the above
 | 
	
		
			
				|  |  | +# copyright notice, this list of conditions and the following disclaimer
 | 
	
		
			
				|  |  | +# in the documentation and/or other materials provided with the
 | 
	
		
			
				|  |  | +# distribution.
 | 
	
		
			
				|  |  | +#     * Neither the name of Google Inc. nor the names of its
 | 
	
		
			
				|  |  | +# contributors may be used to endorse or promote products derived from
 | 
	
		
			
				|  |  | +# this software without specific prior written permission.
 | 
	
		
			
				|  |  | +#
 | 
	
		
			
				|  |  | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 | 
	
		
			
				|  |  | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 | 
	
		
			
				|  |  | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 | 
	
		
			
				|  |  | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 | 
	
		
			
				|  |  | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 | 
	
		
			
				|  |  | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 | 
	
		
			
				|  |  | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
	
		
			
				|  |  | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
	
		
			
				|  |  | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
	
		
			
				|  |  | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 | 
	
		
			
				|  |  | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +"""Helper to upload Jenkins test results to BQ"""
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +from __future__ import print_function
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import os
 | 
	
		
			
				|  |  | +import six
 | 
	
		
			
				|  |  | +import sys
 | 
	
		
			
				|  |  | +import time
 | 
	
		
			
				|  |  | +import uuid
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +gcp_utils_dir = os.path.abspath(os.path.join(
 | 
	
		
			
				|  |  | +    os.path.dirname(__file__), '../../gcp/utils'))
 | 
	
		
			
				|  |  | +sys.path.append(gcp_utils_dir)
 | 
	
		
			
				|  |  | +import big_query_utils
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +_DATASET_ID = 'jenkins_test_results'
 | 
	
		
			
				|  |  | +_DESCRIPTION = 'Test results from master job run on Jenkins'
 | 
	
		
			
				|  |  | +_PROJECT_ID = 'grpc-testing'
 | 
	
		
			
				|  |  | +_RESULTS_SCHEMA = [
 | 
	
		
			
				|  |  | +  ('job_name', 'STRING', 'Name of Jenkins job'),
 | 
	
		
			
				|  |  | +  ('build_id', 'INTEGER', 'Build ID of Jenkins job'),
 | 
	
		
			
				|  |  | +  ('build_url', 'STRING', 'URL of Jenkins job'),
 | 
	
		
			
				|  |  | +  ('test_name', 'STRING', 'Individual test name'),
 | 
	
		
			
				|  |  | +  ('language', 'STRING', 'Language of test'),
 | 
	
		
			
				|  |  | +  ('platform', 'STRING', 'Platform used for test'),
 | 
	
		
			
				|  |  | +  ('config', 'STRING', 'Config used for test'),
 | 
	
		
			
				|  |  | +  ('compiler', 'STRING', 'Compiler used for test'),
 | 
	
		
			
				|  |  | +  ('iomgr_platform', 'STRING', 'Iomgr used for test'),
 | 
	
		
			
				|  |  | +  ('result', 'STRING', 'Test result: PASSED, TIMEOUT, FAILED, or SKIPPED'),
 | 
	
		
			
				|  |  | +  ('timestamp', 'TIMESTAMP', 'Timestamp of test run'),
 | 
	
		
			
				|  |  | +  ('elapsed_time', 'FLOAT', 'How long test took to run'),
 | 
	
		
			
				|  |  | +  ('cpu_estimated', 'FLOAT', 'Estimated CPU usage of test'),
 | 
	
		
			
				|  |  | +  ('cpu_measured', 'FLOAT', 'Actual CPU usage of test'),
 | 
	
		
			
				|  |  | +]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def _get_build_metadata(test_results):
 | 
	
		
			
				|  |  | +  """Add Jenkins build metadata to test_results based on environment variables set by Jenkins."""
 | 
	
		
			
				|  |  | +  build_id = os.getenv('BUILD_ID')
 | 
	
		
			
				|  |  | +  build_url = os.getenv('BUILD_URL')
 | 
	
		
			
				|  |  | +  job_name = os.getenv('JOB_BASE_NAME')
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  if build_id:
 | 
	
		
			
				|  |  | +    test_results['build_id'] = build_id
 | 
	
		
			
				|  |  | +  if build_url:
 | 
	
		
			
				|  |  | +    test_results['build_url'] = build_url
 | 
	
		
			
				|  |  | +  if job_name:
 | 
	
		
			
				|  |  | +    test_results['job_name'] = job_name
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def upload_results_to_bq(resultset, bq_table, args, platform):
 | 
	
		
			
				|  |  | +  """Upload test results to a BQ table.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  Args:
 | 
	
		
			
				|  |  | +      resultset: dictionary generated by jobset.run
 | 
	
		
			
				|  |  | +      bq_table: string name of table to create/upload results to in BQ
 | 
	
		
			
				|  |  | +      args: args in run_tests.py, generated by argparse
 | 
	
		
			
				|  |  | +      platform: string name of platform tests were run on
 | 
	
		
			
				|  |  | +  """
 | 
	
		
			
				|  |  | +  bq = big_query_utils.create_big_query()
 | 
	
		
			
				|  |  | +  big_query_utils.create_table(bq, _PROJECT_ID, _DATASET_ID, bq_table, _RESULTS_SCHEMA, _DESCRIPTION)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  for shortname, results in six.iteritems(resultset):
 | 
	
		
			
				|  |  | +    for result in results:
 | 
	
		
			
				|  |  | +      test_results = {}
 | 
	
		
			
				|  |  | +      _get_build_metadata(test_results)
 | 
	
		
			
				|  |  | +      test_results['compiler'] = args.compiler
 | 
	
		
			
				|  |  | +      test_results['config'] = args.config
 | 
	
		
			
				|  |  | +      test_results['cpu_estimated'] = result.cpu_estimated
 | 
	
		
			
				|  |  | +      test_results['cpu_measured'] = result.cpu_measured
 | 
	
		
			
				|  |  | +      test_results['elapsed_time'] = '%.2f' % result.elapsed_time
 | 
	
		
			
				|  |  | +      test_results['iomgr_platform'] = args.iomgr_platform
 | 
	
		
			
				|  |  | +      # args.language is a list, but will always have one element in the contexts
 | 
	
		
			
				|  |  | +      # this function is used.
 | 
	
		
			
				|  |  | +      test_results['language'] = args.language[0]
 | 
	
		
			
				|  |  | +      test_results['platform'] = platform
 | 
	
		
			
				|  |  | +      test_results['result'] = result.state
 | 
	
		
			
				|  |  | +      test_results['test_name'] = shortname
 | 
	
		
			
				|  |  | +      test_results['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S')
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +      row = big_query_utils.make_row(str(uuid.uuid4()), test_results)
 | 
	
		
			
				|  |  | +      if not big_query_utils.insert_rows(bq, _PROJECT_ID, _DATASET_ID, bq_table, [row]):
 | 
	
		
			
				|  |  | +        print('Error uploading result to bigquery.')
 | 
	
		
			
				|  |  | +        sys.exit(1)
 |