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 json | 8 import json |
9 import os | 9 import os |
10 import StringIO | 10 import StringIO |
(...skipping 1894 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1905 ((['DoGetTryMasters'], ), None), | 1905 ((['DoGetTryMasters'], ), None), |
1906 ((['SetCQState', git_cl._CQState.DRY_RUN], ), None), | 1906 ((['SetCQState', git_cl._CQState.DRY_RUN], ), None), |
1907 ] | 1907 ] |
1908 out = StringIO.StringIO() | 1908 out = StringIO.StringIO() |
1909 self.mock(git_cl.sys, 'stdout', out) | 1909 self.mock(git_cl.sys, 'stdout', out) |
1910 self.assertEqual(0, git_cl.main(['try'])) | 1910 self.assertEqual(0, git_cl.main(['try'])) |
1911 self.assertEqual( | 1911 self.assertEqual( |
1912 out.getvalue(), | 1912 out.getvalue(), |
1913 'scheduled CQ Dry Run on https://codereview.chromium.org/123\n') | 1913 'scheduled CQ Dry Run on https://codereview.chromium.org/123\n') |
1914 | 1914 |
1915 def test_git_cl_try_buildbucket_with_properties(self): | 1915 def test_git_cl_try_default_cq_dry_run_gerrit(self): |
1916 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 20001) | 1916 self.mock(git_cl.Changelist, 'GetChange', |
1917 self.mock(git_cl.Changelist, 'GetIssueOwner', lambda _: 'owner@e.mail') | 1917 lambda _, *a: ( |
1918 self.mock(git_cl.Changelist, 'GetIssueProject', lambda _: 'depot_tools') | 1918 self._mocked_call(['GetChange']+list(a)))) |
| 1919 self.mock(git_cl.presubmit_support, 'DoGetTryMasters', |
| 1920 lambda *_, **__: ( |
| 1921 self._mocked_call(['DoGetTryMasters']))) |
| 1922 self.mock(git_cl._GerritChangelistImpl, 'SetCQState', |
| 1923 lambda _, s: self._mocked_call(['SetCQState', s])) |
| 1924 |
| 1925 def _GetChangeDetail(gerrit_change_list_impl, opts=None): |
| 1926 # Get realistic expectations. |
| 1927 gerrit_change_list_impl._GetGerritHost() |
| 1928 return self._mocked_call(['_GetChangeDetail', opts or []]) |
| 1929 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', |
| 1930 _GetChangeDetail) |
| 1931 |
| 1932 self.calls = [ |
| 1933 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 1934 ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 1935 ((['git', 'config', 'branch.feature.gerritissue'],), '123456'), |
| 1936 ((['git', 'config', 'branch.feature.gerritserver'],), |
| 1937 'https://chromium-review.googlesource.com'), |
| 1938 ((['_GetChangeDetail', []],), {'status': 'OPEN'}), |
| 1939 ((['git', 'config', 'branch.feature.merge'],), 'feature'), |
| 1940 ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 1941 ((['get_or_create_merge_base', 'feature', 'feature'],), |
| 1942 'fake_ancestor_sha'), |
| 1943 ((['GetChange', 'fake_ancestor_sha', None], ), |
| 1944 git_cl.presubmit_support.GitChange( |
| 1945 '', '', '', '', '', '', '', '')), |
| 1946 ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 1947 ((['DoGetTryMasters'], ), None), |
| 1948 ((['SetCQState', git_cl._CQState.DRY_RUN], ), None), |
| 1949 ] |
| 1950 out = StringIO.StringIO() |
| 1951 self.mock(git_cl.sys, 'stdout', out) |
| 1952 self.assertEqual(0, git_cl.main(['try'])) |
| 1953 self.assertEqual( |
| 1954 out.getvalue(), |
| 1955 'scheduled CQ Dry Run on ' |
| 1956 'https://chromium-review.googlesource.com/123456\n') |
| 1957 |
| 1958 def test_git_cl_try_buildbucket_with_properties_rietveld(self): |
| 1959 self.mock(git_cl._RietveldChangelistImpl, 'GetIssueProperties', |
| 1960 lambda _: { |
| 1961 'owner_email': 'owner@e.mail', |
| 1962 'private': False, |
| 1963 'closed': False, |
| 1964 'project': 'depot_tools', |
| 1965 'patchsets': [20001], |
| 1966 }) |
1919 self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') | 1967 self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
1920 self.calls = [ | 1968 self.calls = [ |
1921 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), | 1969 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
1922 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), | 1970 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
1923 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), | 1971 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
1924 ((['git', 'config', 'rietveld.server'],), | 1972 ((['git', 'config', 'rietveld.server'],), |
1925 'https://codereview.chromium.org'), | 1973 'https://codereview.chromium.org'), |
| 1974 ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
1926 ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), | 1975 ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
1927 ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), | |
1928 ] | 1976 ] |
1929 | 1977 |
1930 def _buildbucket_retry(*_, **kw): | 1978 def _buildbucket_retry(*_, **kw): |
1931 # self.maxDiff = 10000 | 1979 # self.maxDiff = 10000 |
1932 body = json.loads(kw['body']) | 1980 body = json.loads(kw['body']) |
1933 self.assertEqual(len(body['builds']), 1) | 1981 self.assertEqual(len(body['builds']), 1) |
1934 build = body['builds'][0] | 1982 build = body['builds'][0] |
1935 params = json.loads(build.pop('parameters_json')) | 1983 params = json.loads(build.pop('parameters_json')) |
1936 self.assertEqual(params, { | 1984 self.assertEqual(params, { |
1937 u'builder_name': u'win', | 1985 u'builder_name': u'win', |
(...skipping 23 matching lines...) Expand all Loading... |
1961 self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) | 2009 self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
1962 | 2010 |
1963 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | 2011 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
1964 self.assertEqual(0, git_cl.main([ | 2012 self.assertEqual(0, git_cl.main([ |
1965 'try', '-m', 'tryserver.chromium', '-b', 'win', | 2013 'try', '-m', 'tryserver.chromium', '-b', 'win', |
1966 '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) | 2014 '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) |
1967 self.assertRegexpMatches( | 2015 self.assertRegexpMatches( |
1968 git_cl.sys.stdout.getvalue(), | 2016 git_cl.sys.stdout.getvalue(), |
1969 'Tried jobs on:\nBucket: master.tryserver.chromium') | 2017 'Tried jobs on:\nBucket: master.tryserver.chromium') |
1970 | 2018 |
| 2019 def test_git_cl_try_buildbucket_with_properties_gerrit(self): |
| 2020 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 7) |
| 2021 self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2022 |
| 2023 def _GetChangeDetail(gerrit_change_list_impl, opts=None): |
| 2024 # Get realistic expectations. |
| 2025 gerrit_change_list_impl._GetGerritHost() |
| 2026 return self._mocked_call(['_GetChangeDetail', opts or []]) |
| 2027 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', |
| 2028 _GetChangeDetail) |
| 2029 |
| 2030 self.calls = [ |
| 2031 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2032 ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2033 ((['git', 'config', 'branch.feature.gerritissue'],), '123456'), |
| 2034 ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2035 'https://chromium-review.googlesource.com'), |
| 2036 ((['_GetChangeDetail', []],), {'status': 'OPEN'}), |
| 2037 ((['git', 'config', 'branch.feature.gerritpatchset'],), '7'), |
| 2038 ((['_GetChangeDetail', ['DETAILED_ACCOUNTS']],), |
| 2039 {'owner': {'email': 'owner@e.mail'}}), |
| 2040 ((['_GetChangeDetail', ['ALL_REVISIONS']],), { |
| 2041 'project': 'depot_tools', |
| 2042 'revisions': { |
| 2043 'deadbeaf': { |
| 2044 '_number': 6, |
| 2045 }, |
| 2046 'beeeeeef': { |
| 2047 '_number': 7, |
| 2048 'fetch': {'http': { |
| 2049 'url': 'https://chromium.googlesource.com/depot_tools', |
| 2050 'ref': 'refs/changes/56/123456/7' |
| 2051 }}, |
| 2052 }, |
| 2053 }, |
| 2054 }), |
| 2055 ] |
| 2056 |
| 2057 def _buildbucket_retry(*_, **kw): |
| 2058 # self.maxDiff = 10000 |
| 2059 body = json.loads(kw['body']) |
| 2060 self.assertEqual(len(body['builds']), 1) |
| 2061 build = body['builds'][0] |
| 2062 params = json.loads(build.pop('parameters_json')) |
| 2063 self.assertEqual(params, { |
| 2064 u'builder_name': u'win', |
| 2065 u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2066 u'revision': None}], |
| 2067 u'properties': { |
| 2068 u'category': u'git_cl_try', |
| 2069 u'key': u'val', |
| 2070 u'json': [{u'a': 1}, None], |
| 2071 u'master': u'tryserver.chromium', |
| 2072 |
| 2073 u'patch_gerrit_url': |
| 2074 u'https://chromium-review.googlesource.com', |
| 2075 u'patch_issue': 123456, |
| 2076 u'patch_project': u'depot_tools', |
| 2077 u'patch_ref': u'refs/changes/56/123456/7', |
| 2078 u'patch_repository_url': |
| 2079 u'https://chromium.googlesource.com/depot_tools', |
| 2080 u'patch_set': 7, |
| 2081 u'patch_storage': u'gerrit', |
| 2082 } |
| 2083 }) |
| 2084 self.assertEqual(build, { |
| 2085 u'bucket': u'master.tryserver.chromium', |
| 2086 u'client_operation_id': u'uuid4', |
| 2087 u'tags': [ |
| 2088 u'builder:win', |
| 2089 u'buildset:patch/gerrit/chromium-review.googlesource.com/123456/7', |
| 2090 u'user_agent:git_cl_try', |
| 2091 u'master:tryserver.chromium'], |
| 2092 }) |
| 2093 |
| 2094 self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2095 |
| 2096 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2097 self.assertEqual(0, git_cl.main([ |
| 2098 'try', '-m', 'tryserver.chromium', '-b', 'win', |
| 2099 '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) |
| 2100 self.assertRegexpMatches( |
| 2101 git_cl.sys.stdout.getvalue(), |
| 2102 'Tried jobs on:\nBucket: master.tryserver.chromium') |
| 2103 |
1971 def test_git_cl_try_buildbucket_bucket_flag(self): | 2104 def test_git_cl_try_buildbucket_bucket_flag(self): |
1972 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 20001) | 2105 self.mock(git_cl._RietveldChangelistImpl, 'GetIssueProperties', |
1973 self.mock(git_cl.Changelist, 'GetIssueOwner', lambda _: 'owner@e.mail') | 2106 lambda _: { |
1974 self.mock(git_cl.Changelist, 'GetIssueProject', lambda _: 'depot_tools') | 2107 'owner_email': 'owner@e.mail', |
| 2108 'private': False, |
| 2109 'closed': False, |
| 2110 'project': 'depot_tools', |
| 2111 'patchsets': [20001], |
| 2112 }) |
1975 self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') | 2113 self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
1976 self.calls = [ | 2114 self.calls = [ |
1977 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), | 2115 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
1978 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), | 2116 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
1979 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), | 2117 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
1980 ((['git', 'config', 'rietveld.server'],), | 2118 ((['git', 'config', 'rietveld.server'],), |
1981 'https://codereview.chromium.org'), | 2119 'https://codereview.chromium.org'), |
| 2120 ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
1982 ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), | 2121 ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
1983 ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), | |
1984 ] | 2122 ] |
1985 | 2123 |
1986 def _buildbucket_retry(*_, **kw): | 2124 def _buildbucket_retry(*_, **kw): |
1987 body = json.loads(kw['body']) | 2125 body = json.loads(kw['body']) |
1988 self.assertEqual(len(body['builds']), 1) | 2126 self.assertEqual(len(body['builds']), 1) |
1989 build = body['builds'][0] | 2127 build = body['builds'][0] |
1990 params = json.loads(build.pop('parameters_json')) | 2128 params = json.loads(build.pop('parameters_json')) |
1991 self.assertEqual(params, { | 2129 self.assertEqual(params, { |
1992 u'builder_name': u'win', | 2130 u'builder_name': u'win', |
1993 u'changes': [{u'author': {u'email': u'owner@e.mail'}, | 2131 u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
(...skipping 24 matching lines...) Expand all Loading... |
2018 git_cl.sys.stdout.getvalue(), | 2156 git_cl.sys.stdout.getvalue(), |
2019 'Tried jobs on:\nBucket: test.bucket') | 2157 'Tried jobs on:\nBucket: test.bucket') |
2020 | 2158 |
2021 def test_git_cl_try_bots_on_multiple_masters(self): | 2159 def test_git_cl_try_bots_on_multiple_masters(self): |
2022 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 20001) | 2160 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 20001) |
2023 self.calls = [ | 2161 self.calls = [ |
2024 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), | 2162 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
2025 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), | 2163 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
2026 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), | 2164 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
2027 ((['git', 'config', 'rietveld.server'],), | 2165 ((['git', 'config', 'rietveld.server'],), |
2028 'https://codereview.chromium.org'), | 2166 'https://codereview.chromium.org'), |
2029 ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), | 2167 ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
2030 ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), | 2168 ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
2031 ] | 2169 ] |
2032 | 2170 |
2033 def _buildbucket_retry(*_, **kw): | 2171 def _buildbucket_retry(*_, **kw): |
2034 body = json.loads(kw['body']) | 2172 body = json.loads(kw['body']) |
2035 self.assertEqual(len(body['builds']), 2) | 2173 self.assertEqual(len(body['builds']), 2) |
2036 | 2174 |
2037 first_build_params = json.loads(body['builds'][0]['parameters_json']) | 2175 first_build_params = json.loads(body['builds'][0]['parameters_json']) |
2038 self.assertEqual(first_build_params['builder_name'], 'builder1') | 2176 self.assertEqual(first_build_params['builder_name'], 'builder1') |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2256 self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') | 2394 self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') |
2257 self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') | 2395 self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
2258 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') | 2396 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
2259 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') | 2397 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
2260 | 2398 |
2261 | 2399 |
2262 if __name__ == '__main__': | 2400 if __name__ == '__main__': |
2263 git_cl.logging.basicConfig( | 2401 git_cl.logging.basicConfig( |
2264 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 2402 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
2265 unittest.main() | 2403 unittest.main() |
OLD | NEW |