OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for git_cl.py.""" | 6 """Unit tests for git_cl.py.""" |
7 | 7 |
8 import os | 8 import os |
9 import StringIO | 9 import StringIO |
10 import stat | 10 import stat |
(...skipping 1952 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1963 'builder_name': 'my-builder', | 1963 'builder_name': 'my-builder', |
1964 'status': 'STARTED', | 1964 'status': 'STARTED', |
1965 'result': None, | 1965 'result': None, |
1966 'failure_reason': None, | 1966 'failure_reason': None, |
1967 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/2', | 1967 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/2', |
1968 } | 1968 } |
1969 ] | 1969 ] |
1970 self.calls = [(('write_json', 'output.json', expected_output), '')] | 1970 self.calls = [(('write_json', 'output.json', expected_output), '')] |
1971 git_cl.write_try_results_json('output.json', self.BUILDBUCKET_BUILDS_MAP) | 1971 git_cl.write_try_results_json('output.json', self.BUILDBUCKET_BUILDS_MAP) |
1972 | 1972 |
1973 def _setup_fetch_try_jobs_rietveld(self, *request_results): | 1973 def _setup_fetch_try_jobs(self, most_recent_patchset=20001): |
1974 out = StringIO.StringIO() | 1974 out = StringIO.StringIO() |
1975 self.mock(sys, 'stdout', out) | 1975 self.mock(sys, 'stdout', out) |
1976 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda *args: 20001) | 1976 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', |
| 1977 lambda *args: most_recent_patchset) |
1977 self.mock(git_cl.auth, 'get_authenticator_for_host', lambda host, _cfg: | 1978 self.mock(git_cl.auth, 'get_authenticator_for_host', lambda host, _cfg: |
1978 self._mocked_call(['get_authenticator_for_host', host])) | 1979 self._mocked_call(['get_authenticator_for_host', host])) |
1979 self.mock(git_cl, '_buildbucket_retry', lambda *_, **__: | 1980 self.mock(git_cl, '_buildbucket_retry', lambda *_, **__: |
1980 self._mocked_call(['_buildbucket_retry'])) | 1981 self._mocked_call(['_buildbucket_retry'])) |
| 1982 |
| 1983 def _setup_fetch_try_jobs_rietveld(self, *request_results): |
| 1984 self._setup_fetch_try_jobs(most_recent_patchset=20001) |
1981 self.calls += [ | 1985 self.calls += [ |
1982 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), | 1986 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
1983 ((['git', 'config', 'branch.feature.rietveldissue'],), '1'), | 1987 ((['git', 'config', 'branch.feature.rietveldissue'],), '1'), |
1984 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), | 1988 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
1985 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), | 1989 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
1986 ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), | 1990 ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
1987 ((['git', 'config', 'branch.feature.rietveldserver'],), | 1991 ((['git', 'config', 'branch.feature.rietveldserver'],), |
1988 'codereview.example.com'), | 1992 'codereview.example.com'), |
1989 ((['get_authenticator_for_host', 'codereview.example.com'],), | 1993 ((['get_authenticator_for_host', 'codereview.example.com'],), |
1990 AuthenticatorMock()), | 1994 AuthenticatorMock()), |
1991 ] + [((['_buildbucket_retry'],), r) for r in request_results] | 1995 ] + [((['_buildbucket_retry'],), r) for r in request_results] |
1992 | 1996 |
1993 def test_fetch_try_jobs_none_rietveld(self): | 1997 def test_fetch_try_jobs_none_rietveld(self): |
1994 self._setup_fetch_try_jobs_rietveld({}) | 1998 self._setup_fetch_try_jobs_rietveld({}) |
| 1999 # Simulate that user isn't logged in. |
| 2000 self.mock(AuthenticatorMock, 'has_cached_credentials', lambda _: False) |
1995 self.assertEqual(0, git_cl.main(['try-results'])) | 2001 self.assertEqual(0, git_cl.main(['try-results'])) |
| 2002 self.assertRegexpMatches(sys.stdout.getvalue(), |
| 2003 'Warning: Some results might be missing') |
1996 self.assertRegexpMatches(sys.stdout.getvalue(), 'No try jobs') | 2004 self.assertRegexpMatches(sys.stdout.getvalue(), 'No try jobs') |
1997 | 2005 |
1998 def test_fetch_try_jobs_some_rietveld(self): | 2006 def test_fetch_try_jobs_some_rietveld(self): |
1999 self._setup_fetch_try_jobs_rietveld({ | 2007 self._setup_fetch_try_jobs_rietveld({ |
2000 'builds': self.BUILDBUCKET_BUILDS_MAP.values(), | 2008 'builds': self.BUILDBUCKET_BUILDS_MAP.values(), |
2001 }) | 2009 }) |
2002 self.assertEqual(0, git_cl.main(['try-results'])) | 2010 self.assertEqual(0, git_cl.main(['try-results'])) |
2003 self.assertRegexpMatches(sys.stdout.getvalue(), 'Failures:') | 2011 self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
2004 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') | 2012 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
2005 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') | 2013 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
2006 | 2014 |
| 2015 def _setup_fetch_try_jobs_gerrit(self, *request_results): |
| 2016 self._setup_fetch_try_jobs(most_recent_patchset=13) |
| 2017 self.calls += [ |
| 2018 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2019 ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2020 ((['git', 'config', 'branch.feature.gerritissue'],), '1'), |
| 2021 # Simulate that Gerrit has more patchsets than local. |
| 2022 ((['git', 'config', 'branch.feature.gerritpatchset'],), '12'), |
| 2023 ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2024 'https://x-review.googlesource.com'), |
| 2025 ((['get_authenticator_for_host', 'x-review.googlesource.com'],), |
| 2026 AuthenticatorMock()), |
| 2027 ] + [((['_buildbucket_retry'],), r) for r in request_results] |
| 2028 |
| 2029 def test_fetch_try_jobs_none_gerrit(self): |
| 2030 self._setup_fetch_try_jobs_gerrit({}) |
| 2031 self.assertEqual(0, git_cl.main(['try-results'])) |
| 2032 self.assertRegexpMatches( |
| 2033 sys.stdout.getvalue(), |
| 2034 r'Warning: Codereview server has newer patchsets \(13\)') |
| 2035 self.assertRegexpMatches(sys.stdout.getvalue(), 'No try jobs') |
| 2036 |
| 2037 def test_fetch_try_jobs_some_gerrit(self): |
| 2038 self._setup_fetch_try_jobs_gerrit({ |
| 2039 'builds': self.BUILDBUCKET_BUILDS_MAP.values(), |
| 2040 }) |
| 2041 # Explicit --patchset means actual local patchset doesn't matter. |
| 2042 self.calls.remove( |
| 2043 ((['git', 'config', 'branch.feature.gerritpatchset'],), '12')) |
| 2044 self.assertEqual(0, git_cl.main(['try-results', '--patchset', '5'])) |
| 2045 |
| 2046 # ... and doesn't result in warning. |
| 2047 self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') |
| 2048 self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
| 2049 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
| 2050 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
| 2051 |
2007 | 2052 |
2008 if __name__ == '__main__': | 2053 if __name__ == '__main__': |
2009 git_cl.logging.basicConfig( | 2054 git_cl.logging.basicConfig( |
2010 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 2055 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
2011 unittest.main() | 2056 unittest.main() |
OLD | NEW |