• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright 2017 gRPC authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Uploads RBE results to BigQuery"""
16
17import argparse
18import os
19import json
20import sys
21import urllib2
22import uuid
23
24gcp_utils_dir = os.path.abspath(
25    os.path.join(os.path.dirname(__file__), '../../gcp/utils'))
26sys.path.append(gcp_utils_dir)
27import big_query_utils
28
29_DATASET_ID = 'jenkins_test_results'
30_DESCRIPTION = 'Test results from master RBE builds on Kokoro'
31# 365 days in milliseconds
32_EXPIRATION_MS = 365 * 24 * 60 * 60 * 1000
33_PARTITION_TYPE = 'DAY'
34_PROJECT_ID = 'grpc-testing'
35_RESULTS_SCHEMA = [
36    ('job_name', 'STRING', 'Name of Kokoro job'),
37    ('build_id', 'INTEGER', 'Build ID of Kokoro job'),
38    ('build_url', 'STRING', 'URL of Kokoro build'),
39    ('test_target', 'STRING', 'Bazel target path'),
40    ('test_class_name', 'STRING', 'Name of test class'),
41    ('test_case', 'STRING', 'Name of test case'),
42    ('result', 'STRING', 'Test or build result'),
43    ('timestamp', 'TIMESTAMP', 'Timestamp of test run'),
44    ('duration', 'FLOAT', 'Duration of the test run'),
45]
46_TABLE_ID = 'rbe_test_results'
47
48
49def _get_api_key():
50    """Returns string with API key to access ResultStore.
51	Intended to be used in Kokoro environment."""
52    api_key_directory = os.getenv('KOKORO_GFILE_DIR')
53    api_key_file = os.path.join(api_key_directory, 'resultstore_api_key')
54    assert os.path.isfile(api_key_file), 'Must add --api_key arg if not on ' \
55     'Kokoro or Kokoro environment is not set up properly.'
56    with open(api_key_file, 'r') as f:
57        return f.read().replace('\n', '')
58
59
60def _get_invocation_id():
61    """Returns String of Bazel invocation ID. Intended to be used in
62	Kokoro environment."""
63    bazel_id_directory = os.getenv('KOKORO_ARTIFACTS_DIR')
64    bazel_id_file = os.path.join(bazel_id_directory, 'bazel_invocation_ids')
65    assert os.path.isfile(bazel_id_file), 'bazel_invocation_ids file, written ' \
66     'by RBE initialization script, expected but not found.'
67    with open(bazel_id_file, 'r') as f:
68        return f.read().replace('\n', '')
69
70
71def _parse_test_duration(duration_str):
72    """Parse test duration string in '123.567s' format"""
73    try:
74        if duration_str.endswith('s'):
75            duration_str = duration_str[:-1]
76        return float(duration_str)
77    except:
78        return None
79
80
81def _upload_results_to_bq(rows):
82    """Upload test results to a BQ table.
83
84  Args:
85      rows: A list of dictionaries containing data for each row to insert
86  """
87    bq = big_query_utils.create_big_query()
88    big_query_utils.create_partitioned_table(bq,
89                                             _PROJECT_ID,
90                                             _DATASET_ID,
91                                             _TABLE_ID,
92                                             _RESULTS_SCHEMA,
93                                             _DESCRIPTION,
94                                             partition_type=_PARTITION_TYPE,
95                                             expiration_ms=_EXPIRATION_MS)
96
97    max_retries = 3
98    for attempt in range(max_retries):
99        if big_query_utils.insert_rows(bq, _PROJECT_ID, _DATASET_ID, _TABLE_ID,
100                                       rows):
101            break
102        else:
103            if attempt < max_retries - 1:
104                print('Error uploading result to bigquery, will retry.')
105            else:
106                print(
107                    'Error uploading result to bigquery, all attempts failed.')
108                sys.exit(1)
109
110
111def _get_resultstore_data(api_key, invocation_id):
112    """Returns dictionary of test results by querying ResultStore API.
113  Args:
114      api_key: String of ResultStore API key
115      invocation_id: String of ResultStore invocation ID to results from
116  """
117    all_actions = []
118    page_token = ''
119    # ResultStore's API returns data on a limited number of tests. When we exceed
120    # that limit, the 'nextPageToken' field is included in the request to get
121    # subsequent data, so keep requesting until 'nextPageToken' field is omitted.
122    while True:
123        req = urllib2.Request(
124            url=
125            'https://resultstore.googleapis.com/v2/invocations/%s/targets/-/configuredTargets/-/actions?key=%s&pageToken=%s&fields=next_page_token,actions.id,actions.status_attributes,actions.timing,actions.test_action'
126            % (invocation_id, api_key, page_token),
127            headers={'Content-Type': 'application/json'})
128        results = json.loads(urllib2.urlopen(req).read())
129        all_actions.extend(results['actions'])
130        if 'nextPageToken' not in results:
131            break
132        page_token = results['nextPageToken']
133    return all_actions
134
135
136if __name__ == "__main__":
137    # Arguments are necessary if running in a non-Kokoro environment.
138    argp = argparse.ArgumentParser(
139        description=
140        'Fetches results for given RBE invocation and uploads them to BigQuery table.'
141    )
142    argp.add_argument('--api_key',
143                      default='',
144                      type=str,
145                      help='The API key to read from ResultStore API')
146    argp.add_argument('--invocation_id',
147                      default='',
148                      type=str,
149                      help='UUID of bazel invocation to fetch.')
150    argp.add_argument('--bq_dump_file',
151                      default=None,
152                      type=str,
153                      help='Dump JSON data to file just before uploading')
154    argp.add_argument('--resultstore_dump_file',
155                      default=None,
156                      type=str,
157                      help='Dump JSON data as received from ResultStore API')
158    argp.add_argument('--skip_upload',
159                      default=False,
160                      action='store_const',
161                      const=True,
162                      help='Skip uploading to bigquery')
163    args = argp.parse_args()
164
165    api_key = args.api_key or _get_api_key()
166    invocation_id = args.invocation_id or _get_invocation_id()
167    resultstore_actions = _get_resultstore_data(api_key, invocation_id)
168
169    if args.resultstore_dump_file:
170        with open(args.resultstore_dump_file, 'w') as f:
171            json.dump(resultstore_actions, f, indent=4, sort_keys=True)
172        print('Dumped resultstore data to file %s' % args.resultstore_dump_file)
173
174    # google.devtools.resultstore.v2.Action schema:
175    # https://github.com/googleapis/googleapis/blob/master/google/devtools/resultstore/v2/action.proto
176    bq_rows = []
177    for index, action in enumerate(resultstore_actions):
178        # Filter out non-test related data, such as build results.
179        if 'testAction' not in action:
180            continue
181        # Some test results contain the fileProcessingErrors field, which indicates
182        # an issue with parsing results individual test cases.
183        if 'fileProcessingErrors' in action:
184            test_cases = [{
185                'testCase': {
186                    'caseName': str(action['id']['actionId']),
187                }
188            }]
189        # Test timeouts have a different dictionary structure compared to pass and
190        # fail results.
191        elif action['statusAttributes']['status'] == 'TIMED_OUT':
192            test_cases = [{
193                'testCase': {
194                    'caseName': str(action['id']['actionId']),
195                    'timedOut': True
196                }
197            }]
198        # When RBE believes its infrastructure is failing, it will abort and
199        # mark running tests as UNKNOWN. These infrastructure failures may be
200        # related to our tests, so we should investigate if specific tests are
201        # repeatedly being marked as UNKNOWN.
202        elif action['statusAttributes']['status'] == 'UNKNOWN':
203            test_cases = [{
204                'testCase': {
205                    'caseName': str(action['id']['actionId']),
206                    'unknown': True
207                }
208            }]
209            # Take the timestamp from the previous action, which should be
210            # a close approximation.
211            action['timing'] = {
212                'startTime':
213                    resultstore_actions[index - 1]['timing']['startTime']
214            }
215        elif 'testSuite' not in action['testAction']:
216            continue
217        elif 'tests' not in action['testAction']['testSuite']:
218            continue
219        else:
220            test_cases = []
221            for tests_item in action['testAction']['testSuite']['tests']:
222                test_cases += tests_item['testSuite']['tests']
223        for test_case in test_cases:
224            if any(s in test_case['testCase'] for s in ['errors', 'failures']):
225                result = 'FAILED'
226            elif 'timedOut' in test_case['testCase']:
227                result = 'TIMEOUT'
228            elif 'unknown' in test_case['testCase']:
229                result = 'UNKNOWN'
230            else:
231                result = 'PASSED'
232            try:
233                bq_rows.append({
234                    'insertId': str(uuid.uuid4()),
235                    'json': {
236                        'job_name':
237                            os.getenv('KOKORO_JOB_NAME'),
238                        'build_id':
239                            os.getenv('KOKORO_BUILD_NUMBER'),
240                        'build_url':
241                            'https://source.cloud.google.com/results/invocations/%s'
242                            % invocation_id,
243                        'test_target':
244                            action['id']['targetId'],
245                        'test_class_name':
246                            test_case['testCase'].get('className', ''),
247                        'test_case':
248                            test_case['testCase']['caseName'],
249                        'result':
250                            result,
251                        'timestamp':
252                            action['timing']['startTime'],
253                        'duration':
254                            _parse_test_duration(action['timing']['duration']),
255                    }
256                })
257            except Exception as e:
258                print('Failed to parse test result. Error: %s' % str(e))
259                print(json.dumps(test_case, indent=4))
260                bq_rows.append({
261                    'insertId': str(uuid.uuid4()),
262                    'json': {
263                        'job_name':
264                            os.getenv('KOKORO_JOB_NAME'),
265                        'build_id':
266                            os.getenv('KOKORO_BUILD_NUMBER'),
267                        'build_url':
268                            'https://source.cloud.google.com/results/invocations/%s'
269                            % invocation_id,
270                        'test_target':
271                            action['id']['targetId'],
272                        'test_class_name':
273                            'N/A',
274                        'test_case':
275                            'N/A',
276                        'result':
277                            'UNPARSEABLE',
278                        'timestamp':
279                            'N/A',
280                    }
281                })
282
283    if args.bq_dump_file:
284        with open(args.bq_dump_file, 'w') as f:
285            json.dump(bq_rows, f, indent=4, sort_keys=True)
286        print('Dumped BQ data to file %s' % args.bq_dump_file)
287
288    if not args.skip_upload:
289        # BigQuery sometimes fails with large uploads, so batch 1,000 rows at a time.
290        for i in range((len(bq_rows) / 1000) + 1):
291            _upload_results_to_bq(bq_rows[i * 1000:(i + 1) * 1000])
292    else:
293        print('Skipped upload to bigquery.')
294