| # Copyright 2019 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| from google.protobuf import text_format as textpb |
| |
| from PB.go.chromium.org.luci.buildbucket.proto import build as build_pb2 |
| from PB.go.chromium.org.luci.buildbucket.proto import common as common_pb2 |
| from recipe_engine.recipe_api import Property |
| |
| DEPS = [ |
| 'fuchsia/build_input_resolver', |
| 'fuchsia/gitiles', |
| 'fuchsia/status_check', |
| 'recipe_engine/buildbucket', |
| 'recipe_engine/json', |
| 'recipe_engine/properties', |
| ] |
| |
| PROPERTIES = { |
| 'test': Property(kind=str, help='the name of the test case to run'), |
| } |
| |
| |
| class TestBed(object): |
| """A Helper class for unit testing BuildInputApi methods""" |
| |
| def __init__(self): |
| self._expected_output = {} |
| |
| def register(self, |
| api, |
| name, |
| buildbucket_build, |
| expected_output, |
| gitiles_refs=None, |
| status='success', |
| branch=None): |
| """Registers a test case |
| |
| Args: |
| name (string): The name of the test. |
| buildbucket_build: The input build. |
| expected_output: The expected resolved Buildbucket build input. |
| gitiles_refs (List(string)): A list with two elements: The first is a ref |
| in gitiles, the second is that ref's resolved HEAD revision. Used as a |
| mock when fetching from gitiles. |
| branch: The gerrit branch the CL should pretend to be part of. |
| """ |
| self._expected_output[name] = expected_output |
| ret = api.status_check.test(name, status=status) + api.properties(test=name) |
| if branch: |
| ret += api.build_input_resolver.set_gerrit_branch(branch) |
| ret += api.buildbucket.build(buildbucket_build) |
| if gitiles_refs: |
| ret += api.gitiles.refs('refs', gitiles_refs) |
| return ret |
| |
| def run(self, api, name): |
| """Executes the test with the given name with its registered inputs""" |
| expected = self._expected_output[name] |
| actual = api.build_input_resolver.resolve( |
| api.buildbucket.build.input, |
| default_project_url='https://fuchsia.googlesource.com/fuchsia', |
| ) |
| |
| diff_message = self._diff_message(expected, actual) |
| assert (actual.gerrit_changes == expected.gerrit_changes and |
| actual.gitiles_commit == expected.gitiles_commit), diff_message |
| |
| def _diff_message(self, expected, actual): |
| """Produces a string diff of expected and actual""" |
| |
| def _str(build_input): |
| gerrit_change = '' |
| if build_input.gerrit_changes: |
| gerrit_change = textpb.MessageToString(build_input.gerrit_changes[0]) |
| gitiles_commit = textpb.MessageToString(build_input.gitiles_commit) |
| |
| return 'gerrit_change:\n{}\ngitiles_commit:\n{}'.format( |
| gerrit_change, |
| gitiles_commit, |
| ) |
| |
| return 'wanted\n{}\ngot\n{}'.format(_str(expected), _str(actual)) |
| |
| |
| # Global Test bed for all cases in this example. |
| testbed = TestBed() |
| |
| |
| def RunSteps(api, test): |
| testbed.run(api, test) |
| |
| |
| def GenTests(api): |
| #yapf:disable |
| yield testbed.register( |
| api, |
| name='single gerrit change', |
| gitiles_refs=['refs/heads/master', 'deadbeef'], |
| buildbucket_build=build_pb2.Build( |
| input=build_pb2.Build.Input( |
| gerrit_changes=[ |
| common_pb2.GerritChange( |
| host='fuchsia-review.googlesource.com', |
| project='fuchsia', |
| change=456789, |
| patchset=1, |
| ), |
| ], |
| ), |
| ), |
| expected_output=build_pb2.Build.Input( |
| gerrit_changes=[ |
| common_pb2.GerritChange( |
| host='fuchsia-review.googlesource.com', |
| project='fuchsia', |
| change=456789, |
| patchset=1, |
| ), |
| ], |
| gitiles_commit=common_pb2.GitilesCommit( |
| host='fuchsia.googlesource.com', |
| project='fuchsia', |
| ref='refs/heads/master', |
| id='deadbeef', |
| ), |
| ), |
| branch='master', |
| ) |
| yield testbed.register( |
| api, |
| name='non-master gerrit change', |
| gitiles_refs=['refs/heads/branch', 'deadbeef'], |
| buildbucket_build=build_pb2.Build( |
| input=build_pb2.Build.Input( |
| gerrit_changes=[ |
| common_pb2.GerritChange( |
| host='fuchsia-review.googlesource.com', |
| project='fuchsia', |
| change=456789, |
| patchset=1, |
| ), |
| ], |
| ), |
| ), |
| expected_output=build_pb2.Build.Input( |
| gerrit_changes=[ |
| common_pb2.GerritChange( |
| host='fuchsia-review.googlesource.com', |
| project='fuchsia', |
| change=456789, |
| patchset=1, |
| ), |
| ], |
| gitiles_commit=common_pb2.GitilesCommit( |
| host='fuchsia.googlesource.com', |
| project='fuchsia', |
| ref='refs/heads/branch', |
| id='deadbeef', |
| ), |
| ), |
| branch='branch', |
| ) |
| yield testbed.register( |
| api, |
| name='single resolved gitiles commit', |
| buildbucket_build=build_pb2.Build( |
| input=build_pb2.Build.Input( |
| gitiles_commit=common_pb2.GitilesCommit( |
| host='fuchsia.googlesource.com', |
| project='fuchsia', |
| ref='refs/heads/feature', |
| id='deadbeef', |
| ), |
| ), |
| ), |
| expected_output=build_pb2.Build.Input( |
| gitiles_commit=common_pb2.GitilesCommit( |
| host='fuchsia.googlesource.com', |
| project='fuchsia', |
| ref='refs/heads/feature', |
| id='deadbeef', |
| ), |
| ), |
| ) |
| yield testbed.register( |
| api, |
| name='single unresolved gitiles commit', |
| gitiles_refs=['refs/heads/bugfix', 'abcdefg'], |
| buildbucket_build=build_pb2.Build( |
| input=build_pb2.Build.Input( |
| gitiles_commit=common_pb2.GitilesCommit( |
| host='fuchsia.googlesource.com', |
| project='fuchsia', |
| ref='refs/heads/bugfix', |
| id='HEAD', |
| ), |
| ), |
| ), |
| expected_output=build_pb2.Build.Input( |
| gitiles_commit=common_pb2.GitilesCommit( |
| host='fuchsia.googlesource.com', |
| project='fuchsia', |
| ref='refs/heads/bugfix', |
| id='abcdefg', |
| ), |
| ), |
| ) |
| yield testbed.register( |
| api, |
| name='no changes in build input', |
| gitiles_refs=['refs/heads/master', 'deadbeef'], |
| buildbucket_build=build_pb2.Build( |
| input=build_pb2.Build.Input(), |
| ), |
| expected_output=build_pb2.Build.Input( |
| gitiles_commit=common_pb2.GitilesCommit( |
| host='fuchsia.googlesource.com', |
| project='fuchsia', |
| ref='refs/heads/master', |
| id='deadbeef', |
| ), |
| ), |
| ) |
| #yapf:enable |