| """Recipe for running LUCI recipe tests.""" |
| import logging |
| import os |
| |
| from slave import base_recipe |
| from slave import base_step |
| from slave.step import cipd_upload_step |
| from slave.step import luci_recipe_step |
| from slave.step import shell_step |
| |
| from slave.test import test_generator |
| |
| PROJECT_CONFIGS = { |
| 'test': { |
| # Path to the desired recipes.py script. |
| 'script': 'swarming/recipes/recipes.py', |
| # Path to the recipes.cfg file for the recipe package. |
| 'package': 'infra/config/recipes.cfg', |
| # luci_recipe_bundle only. Desired out dir for generated bundle. |
| 'bundle': 'swarming/recipes/bundle', |
| # luci_recipe_bundle only. Path to the cipd def for the recipe package. |
| 'cipd_cfg': 'swarming/recipes/cipd.yaml', |
| 'cipd_refs': ['staging'], |
| 'test_retries': 0, |
| }, |
| # Gazoo recipes. |
| # nest-internal |
| 'gazoo/infra': { |
| 'script': 'recipes/recipes.py', |
| 'package': 'infra/config/recipes.cfg', |
| 'bundle': 'recipes/bundle', |
| 'cipd_cfg': 'recipes/cipd.yaml', |
| 'cipd_refs': ['gazoo-staging'], |
| # Adding retries for flaky coverage report (b/145263501). |
| 'test_retries': 4, |
| }, |
| } |
| |
| BUILD_CONFIGS = { |
| 'luci_recipe_test': { |
| 'project': None, # Project provided by CQ via `patch_project`. |
| 'upload_to_cipd': False, |
| }, |
| 'gazoo_recipes_upload': { |
| 'project': 'gazoo/infra', |
| 'upload_to_cipd': True, |
| }, |
| } |
| |
| |
| def GetValidBuildNames(): |
| return list(BUILD_CONFIGS.keys()) |
| |
| |
| def CreateRecipe(build_name: str, **kwargs): |
| return LUCIRecipeRecipe(build_name, **kwargs) |
| |
| |
| class DoNothingStep(base_step.BaseStep): |
| """Does nothing.""" |
| def __init__(self, msg, **kwargs): |
| """Creates a JsonCheckStep instance. |
| Args: |
| **kwargs: Any additional args to pass to BaseStep. |
| """ |
| base_step.BaseStep.__init__(self, name='nothing to do here.', **kwargs) |
| self._msg = msg |
| |
| def run(self): |
| """Print a message and return.""" |
| logging.info(self._msg) |
| return True |
| |
| |
| class LUCIRecipeRecipe(base_recipe.BaseRecipe): |
| """Recipe to run LUCI recipe functions.""" |
| |
| def __init__(self, build_name, **kwargs): |
| super(LUCIRecipeRecipe, self).__init__(**kwargs) |
| self._build_name = build_name |
| |
| def get_steps(self): |
| build_config = BUILD_CONFIGS[self._build_name] |
| if build_config.get('project'): |
| # Running on CB. Set patch_project via BUILD_CONFIGS. |
| self._properties['patch_project'] = build_config['project'] |
| if not PROJECT_CONFIGS.get(self._properties['patch_project']): |
| return [DoNothingStep('Project {} not in PROJECT_CONFIGS.', |
| **self._step_kwargs)] |
| project_config = PROJECT_CONFIGS[self._properties['patch_project']] |
| script_path = project_config['script'] |
| package_path = project_config['package'] |
| test_retries = project_config['test_retries'] |
| steps = [ |
| luci_recipe_step.LUCIRecipeTestStep( |
| script_path, |
| package_path, |
| test_retries, |
| name='Test recipes', |
| **self._step_kwargs), |
| ] |
| |
| if build_config.get('upload_to_cipd'): |
| bundle_path = project_config['bundle'] |
| cipd_path = project_config['cipd_cfg'] |
| cipd_refs = project_config['cipd_refs'] |
| cleanup_cmd = ['rm', '-rf', bundle_path] |
| steps.extend([ |
| shell_step.ShellStep( |
| name='Ensure bundle dir does not exist', |
| command=cleanup_cmd, |
| **self._step_kwargs), |
| luci_recipe_step.LUCIRecipeBundleStep( |
| script_path, |
| package_path, |
| bundle_path, |
| name='Bundle recipes', |
| **self._step_kwargs), |
| cipd_upload_step.CIPDUploadStep( |
| os.path.join(self._properties['patch_project'], cipd_path), |
| refs=cipd_refs, |
| **self._step_kwargs), |
| shell_step.ShellStep( |
| name='Cleanup bundle dir', |
| command=cleanup_cmd, |
| **self._step_kwargs) |
| ]) |
| |
| return steps |
| |