github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/gubernator/testgrid.py (about) 1 #!/usr/bin/env python 2 # Copyright 2016 The Kubernetes 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 16 import logging 17 18 import cloudstorage as gcs 19 20 import pb_glance 21 22 23 CONFIG_PROTO_SCHEMA = { 24 1: { 25 'name': 'test_groups', 26 1: 'name', 27 2: 'query', 28 9: {}, 29 }, 30 2: { 31 'name': 'dashboards', 32 1: { 33 'name': 'dashboard_tab', 34 1: 'name', 35 2: 'test_group_name', 36 6: 'base_options', 37 7: {}, 38 8: {2: {}}, 39 9: {}, 40 11: {}, 41 12: {}, 42 }, 43 2: 'name', 44 } 45 } 46 47 _testgrid_config = None 48 49 50 def get_config(): 51 ''' 52 Load the testgrid config loaded from a proto stored on GCS. 53 It will be cached locally in memory for the life of this process. 54 55 Returns: 56 dict: { 57 'test_groups': [{'name': ..., 'query': ...}], 58 'dashboards': [{ 59 'name': ..., 60 'dashboard_tab': [{'name': ..., 'test_group_name': ...}] 61 }] 62 } 63 ''' 64 global _testgrid_config # pylint: disable=global-statement 65 if not _testgrid_config: 66 data = gcs.open('/k8s-testgrid/config').read() 67 _testgrid_config = pb_glance.parse_protobuf(data, CONFIG_PROTO_SCHEMA) 68 return _testgrid_config 69 70 71 def path_to_group_name(path): 72 ''' 73 Args: 74 path: a job directory like "/kubernetes-jenkins/jobs/e2e-gce" 75 Returns: 76 test_group_name: the group name in the config, or None if not found 77 ''' 78 path = path.strip('/') # the config doesn't have leading/trailing slashes 79 try: 80 config = get_config() 81 except gcs.errors.Error: 82 logging.exception('unable to load testgrid config') 83 return None 84 for test_group in config.get('test_groups', []): 85 if path in test_group['query']: 86 return test_group['name'][0] 87 88 89 def path_to_query(path): 90 ''' 91 Convert a GCS job directory to the testgrid path for its results. 92 93 Args: 94 path: a job directory like "/kubernetes-jenkins/jobs/e2e-gce" 95 Returns: 96 query: the url for the job, like "k8s#gce", or "" if not found. 97 ''' 98 group = path_to_group_name(path) 99 if not group: 100 return '' 101 102 # Tabs can appear on multiple dashboards. Favor selecting 'k8s' over others, 103 # otherwise pick a random tab. 104 105 options = {} 106 for dashboard in get_config().get('dashboards', []): 107 dashboard_name = dashboard['name'][0] 108 tabs = dashboard['dashboard_tab'] 109 for tab in tabs: 110 if 'base_options' in tab: 111 continue 112 if group in tab['test_group_name']: 113 query = '%s#%s' % (dashboard_name, tab['name'][0]) 114 options[dashboard_name] = (-len(tabs), query) 115 if 'k8s' in options: 116 return options['k8s'][1] 117 elif len(options) > 1: 118 logging.warning('ambiguous testgrid options: %s', options) 119 elif len(options) == 0: 120 return '' 121 return sorted(options.values())[0][1]