From a0606115499ca5dd4281f15c7f04a5c53a8bf5d8 Mon Sep 17 00:00:00 2001 From: Garrett Beaty Date: Tue, 16 Mar 2021 17:17:45 +0000 Subject: [PATCH] Provide step_test_data in gerrit.get_revision_info. This will reduce the need to manually mock steps: calls to get revision info will now return CLs that match the requested change and patchset numbers. Manually mocking will only be required if specific values for other field are desired or if the test case should not have revision info that matches the change and patchset. Change-Id: Ibc6ec62100616680e36d9a842449ac9c6fb2440b Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2765162 Commit-Queue: Garrett Beaty Commit-Queue: Josip Sokcevic Auto-Submit: Garrett Beaty Reviewed-by: Josip Sokcevic --- recipes/README.recipes.md | 10 ++-- recipes/recipe_modules/gerrit/api.py | 21 +++++--- .../gerrit/examples/full.expected/basic.json | 23 +------- .../recipe_modules/gerrit/examples/full.py | 5 +- recipes/recipe_modules/gerrit/test_api.py | 54 +++++++++---------- 5 files changed, 51 insertions(+), 62 deletions(-) diff --git a/recipes/README.recipes.md b/recipes/README.recipes.md index 5fcebaa75..92e91fcf7 100644 --- a/recipes/README.recipes.md +++ b/recipes/README.recipes.md @@ -361,7 +361,7 @@ Module for interact with Gerrit endpoints Wrapper for easy calling of gerrit_utils steps. -— **def [abandon\_change](/recipes/recipe_modules/gerrit/api.py#157)(self, host, change, message=None, name=None, step_test_data=None):** +— **def [abandon\_change](/recipes/recipe_modules/gerrit/api.py#162)(self, host, change, message=None, name=None, step_test_data=None):** — **def [create\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#32)(self, host, project, branch, commit, \*\*kwargs):** @@ -370,7 +370,7 @@ Creates a new branch from given project and commit Returns: The ref of the branch created -— **def [get\_change\_description](/recipes/recipe_modules/gerrit/api.py#71)(self, host, change, patchset):** +— **def [get\_change\_description](/recipes/recipe_modules/gerrit/api.py#71)(self, host, change, patchset, step_test_data=None):** Gets the description for a given CL and patchset. @@ -382,7 +382,7 @@ Args: Returns: The description corresponding to given CL and patchset. -— **def [get\_changes](/recipes/recipe_modules/gerrit/api.py#115)(self, host, query_params, start=None, limit=None, o_params=None, step_test_data=None, \*\*kwargs):** +— **def [get\_changes](/recipes/recipe_modules/gerrit/api.py#120)(self, host, query_params, start=None, limit=None, o_params=None, step_test_data=None, \*\*kwargs):** Queries changes for the given host. @@ -408,7 +408,7 @@ Gets a branch from given project and commit Returns: The revision of the branch -— **def [get\_revision\_info](/recipes/recipe_modules/gerrit/api.py#85)(self, host, change, patchset):** +— **def [get\_revision\_info](/recipes/recipe_modules/gerrit/api.py#85)(self, host, change, patchset, step_test_data=None):** Returns the info for a given patchset of a given change. @@ -421,7 +421,7 @@ Returns: A dict for the target revision as documented here: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes -— **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#177)(self, host, project, from_branch, to_branch, step_test_data=None):** +— **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#182)(self, host, project, from_branch, to_branch, step_test_data=None):** ### *recipe_modules* / [git](/recipes/recipe_modules/git) [DEPS](/recipes/recipe_modules/git/__init__.py#1): [recipe\_engine/buildbucket][recipe_engine/recipe_modules/buildbucket], [recipe\_engine/context][recipe_engine/recipe_modules/context], [recipe\_engine/path][recipe_engine/recipe_modules/path], [recipe\_engine/platform][recipe_engine/recipe_modules/platform], [recipe\_engine/properties][recipe_engine/recipe_modules/properties], [recipe\_engine/python][recipe_engine/recipe_modules/python], [recipe\_engine/raw\_io][recipe_engine/recipe_modules/raw_io], [recipe\_engine/runtime][recipe_engine/recipe_modules/runtime], [recipe\_engine/step][recipe_engine/recipe_modules/step] diff --git a/recipes/recipe_modules/gerrit/api.py b/recipes/recipe_modules/gerrit/api.py index e076d599e..22584ea7f 100644 --- a/recipes/recipe_modules/gerrit/api.py +++ b/recipes/recipe_modules/gerrit/api.py @@ -68,7 +68,7 @@ class GerritApi(recipe_api.RecipeApi): revision = step_result.json.output.get('revision') return revision - def get_change_description(self, host, change, patchset): + def get_change_description(self, host, change, patchset, step_test_data=None): """Gets the description for a given CL and patchset. Args: @@ -79,10 +79,10 @@ class GerritApi(recipe_api.RecipeApi): Returns: The description corresponding to given CL and patchset. """ - ri = self.get_revision_info(host, change, patchset) + ri = self.get_revision_info(host, change, patchset, step_test_data) return ri['commit']['message'] - def get_revision_info(self, host, change, patchset): + def get_revision_info(self, host, change, patchset, step_test_data=None): """ Returns the info for a given patchset of a given change. @@ -97,11 +97,16 @@ class GerritApi(recipe_api.RecipeApi): """ assert int(change), change assert int(patchset), patchset - cls = self.get_changes( - host, - query_params=[('change', str(change))], - o_params=['ALL_REVISIONS', 'ALL_COMMITS'], - limit=1) + + step_test_data = step_test_data or ( + lambda: self.test_api.get_one_change_response_data(change_number=change, + patchset=patchset)) + + cls = self.get_changes(host, + query_params=[('change', str(change))], + o_params=['ALL_REVISIONS', 'ALL_COMMITS'], + limit=1, + step_test_data=step_test_data) cl = cls[0] if len(cls) == 1 else {'revisions': {}} for ri in cl['revisions'].values(): # TODO(tandrii): add support for patchset=='current'. diff --git a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json b/recipes/recipe_modules/gerrit/examples/full.expected/basic.json index 910ba3ce9..15e509104 100644 --- a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json +++ b/recipes/recipe_modules/gerrit/examples/full.expected/basic.json @@ -210,7 +210,7 @@ "~followup_annotations": [ "@@@STEP_LOG_LINE@json.output@[@@@", "@@@STEP_LOG_LINE@json.output@ {@@@", - "@@@STEP_LOG_LINE@json.output@ \"_number\": \"91827\", @@@", + "@@@STEP_LOG_LINE@json.output@ \"_number\": \"123\", @@@", "@@@STEP_LOG_LINE@json.output@ \"branch\": \"main\", @@@", "@@@STEP_LOG_LINE@json.output@ \"change_id\": \"Ideadbeef\", @@@", "@@@STEP_LOG_LINE@json.output@ \"created\": \"2017-01-30 13:11:20.000000000\", @@@", @@ -300,26 +300,7 @@ "infra_step": true, "name": "gerrit changes (3)", "~followup_annotations": [ - "@@@STEP_LOG_LINE@json.output@[@@@", - "@@@STEP_LOG_LINE@json.output@ {@@@", - "@@@STEP_LOG_LINE@json.output@ \"_number\": \"91827\", @@@", - "@@@STEP_LOG_LINE@json.output@ \"branch\": \"main\", @@@", - "@@@STEP_LOG_LINE@json.output@ \"change_id\": \"Ideadbeef\", @@@", - "@@@STEP_LOG_LINE@json.output@ \"created\": \"2017-01-30 13:11:20.000000000\", @@@", - "@@@STEP_LOG_LINE@json.output@ \"has_review_started\": false, @@@", - "@@@STEP_LOG_LINE@json.output@ \"project\": \"chromium/src\", @@@", - "@@@STEP_LOG_LINE@json.output@ \"revisions\": {@@@", - "@@@STEP_LOG_LINE@json.output@ \"184ebe53805e102605d11f6b143486d15c23a09c\": {@@@", - "@@@STEP_LOG_LINE@json.output@ \"_number\": \"1\", @@@", - "@@@STEP_LOG_LINE@json.output@ \"commit\": {@@@", - "@@@STEP_LOG_LINE@json.output@ \"message\": \"Change commit message\"@@@", - "@@@STEP_LOG_LINE@json.output@ }@@@", - "@@@STEP_LOG_LINE@json.output@ }@@@", - "@@@STEP_LOG_LINE@json.output@ }, @@@", - "@@@STEP_LOG_LINE@json.output@ \"status\": \"NEW\", @@@", - "@@@STEP_LOG_LINE@json.output@ \"subject\": \"Change title\"@@@", - "@@@STEP_LOG_LINE@json.output@ }@@@", - "@@@STEP_LOG_LINE@json.output@]@@@", + "@@@STEP_LOG_LINE@json.output@[]@@@", "@@@STEP_LOG_END@json.output@@@" ] }, diff --git a/recipes/recipe_modules/gerrit/examples/full.py b/recipes/recipe_modules/gerrit/examples/full.py index a9d5ead02..dfd4e6b67 100644 --- a/recipes/recipe_modules/gerrit/examples/full.py +++ b/recipes/recipe_modules/gerrit/examples/full.py @@ -54,7 +54,10 @@ def RunSteps(api): with api.step.defer_results(): api.gerrit.get_change_description( - host, change=122, patchset=3) + host, + change=122, + patchset=3, + step_test_data=api.gerrit.test_api.get_empty_changes_response_data) def GenTests(api): diff --git a/recipes/recipe_modules/gerrit/test_api.py b/recipes/recipe_modules/gerrit/test_api.py index 1896c2e82..e7e031de9 100644 --- a/recipes/recipe_modules/gerrit/test_api.py +++ b/recipes/recipe_modules/gerrit/test_api.py @@ -5,29 +5,33 @@ from recipe_engine import recipe_test_api -# Exemplary change. Note: This contains only a subset of the key/value pairs -# present in production to limit recipe simulation output. -EXAMPLE_CHANGE = { - 'status': 'NEW', - 'created': '2017-01-30 13:11:20.000000000', - '_number': '91827', - 'change_id': 'Ideadbeef', - 'project': 'chromium/src', - 'has_review_started': False, - 'branch': 'main', - 'subject': 'Change title', - 'revisions': { - '184ebe53805e102605d11f6b143486d15c23a09c': { - '_number': '1', - 'commit': { - 'message': 'Change commit message', - }, - }, - }, -} - class GerritTestApi(recipe_test_api.RecipeTestApi): + @staticmethod + def _gerrit_change_data(change_number=91827, patchset=1, **kwargs): + # Exemplary change. Note: This contains only a subset of the key/value pairs + # present in production to limit recipe simulation output. + data = { + 'status': 'NEW', + 'created': '2017-01-30 13:11:20.000000000', + '_number': str(change_number), + 'change_id': 'Ideadbeef', + 'project': 'chromium/src', + 'has_review_started': False, + 'branch': 'main', + 'subject': 'Change title', + 'revisions': { + '184ebe53805e102605d11f6b143486d15c23a09c': { + '_number': str(patchset), + 'commit': { + 'message': 'Change commit message', + }, + }, + }, + } + data.update(kwargs) + return data + def _make_gerrit_response_json(self, data): return self.m.json.output(data) @@ -45,14 +49,10 @@ class GerritTestApi(recipe_test_api.RecipeTestApi): }) def get_one_change_response_data(self, **kwargs): - change = EXAMPLE_CHANGE.copy() - change.update(kwargs) - return self._make_gerrit_response_json([change]) + return self._make_gerrit_response_json([self._gerrit_change_data(**kwargs)]) def get_empty_changes_response_data(self): return self._make_gerrit_response_json([]) def get_move_change_response_data(self, **kwargs): - change = EXAMPLE_CHANGE.copy() - change.update(kwargs) - return self._make_gerrit_response_json([change]) + return self._make_gerrit_response_json([self._gerrit_change_data(**kwargs)])