| import logging |
| import json |
| |
| from helpers import git_utils |
| from slave import base_recipe |
| from slave import base_step |
| |
| |
| CONFIG_TAG = 'cq_e2e_config:' |
| PASS = 'PASSES' |
| FAIL = 'FAILS' |
| _DEFAULT_CONFIG = {} |
| |
| |
| def GetValidBuildNames(): |
| return [ |
| 'cq-e2e-mock-1', |
| 'cq-e2e-mock-2', |
| 'cq-e2e-mock-experimental', |
| 'cq-e2e-mock-legacy', |
| 'cq-e2e-mock-recipe', |
| ] |
| |
| |
| def CreateRecipe(build_name: str, **kwargs): |
| """Defines how this recipe gets built.""" |
| return CqE2EMockRecipe(build_name, **kwargs) |
| |
| |
| class CqE2EMockRecipe(base_recipe.BaseRecipe): |
| """Recipe to execute e2e fake build.""" |
| |
| def __init__(self, builder_name, **kwargs): |
| base_recipe.BaseRecipe.__init__(self, **kwargs) |
| self._builder_name = builder_name |
| |
| def get_steps(self): |
| return [ |
| CqE2EMockStep(builder_name=self._builder_name, **self._step_kwargs), |
| ] |
| |
| |
| class CqE2EMockStep(base_step.BaseStep): |
| |
| def __init__(self, builder_name, **kwargs): |
| base_step.BaseStep.__init__(self, name="e2e_mock", **kwargs) |
| self._builder_name = builder_name |
| |
| def run(self): |
| for directory in self._get_patch_dirs(): |
| build = self._get_build_config(directory) |
| if build: |
| break |
| else: |
| return True |
| |
| if build.get('print_patch_order', False): |
| self._print_patch_order() |
| |
| if build.get('review'): |
| self.add_review(build['review']) |
| |
| outcome = build.get('outcome', PASS) |
| if outcome == PASS: |
| return True |
| if outcome == FAIL: |
| return False |
| raise ValueError('status must be set in the build config.') |
| |
| def _get_build_config(self, directory): |
| logging.info('Checking for config from latest commit in %s', directory) |
| commit_message = git_utils.commit_message(self, directory) |
| lines = commit_message.split('\n') |
| build_config = None |
| for line in lines: |
| if line.startswith(CONFIG_TAG): |
| build_config = line |
| if build_config: |
| build_config = build_config[len(CONFIG_TAG):] |
| build_config = build_config.strip() |
| build_config = json.loads(build_config) |
| logging.info('Build config is %s', build_config) |
| return build_config.get(self._builder_name, _DEFAULT_CONFIG) |
| return _DEFAULT_CONFIG |
| |
| def _get_patch_dirs(self): |
| dirs = list() |
| for patch in [{'project': self.patch_project}] + self.depends_on_list: |
| try: |
| new_dir = self.get_project_path(patch['project']) |
| if new_dir not in dirs: |
| dirs.append(new_dir) |
| except KeyError: |
| logging.info( |
| 'Ignoring KeyError since %s is not in the manifest', |
| patch['project'] |
| ) |
| logging.info('patch dirs are: %s', dirs) |
| return dirs |
| |
| def _print_patch_order(self): |
| main_patch = { |
| 'change_number': self.get_property('issue', 0), |
| 'gob_host': self.get_property('gerrit'), |
| 'patchset_number': self.patchset, |
| 'project': self.patch_project, |
| } |
| patches = self.depends_on_list |
| patches.append(main_patch) |
| msg = 'Patch Order: ' + ', '.join([ |
| '{}/{}/{}'.format( |
| p['gob_host'], int(p['change_number']), int(p['patchset_number'])) |
| for p in patches |
| ]) |
| self.add_review({'message': msg}) |