bot_update_coverage_test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/usr/bin/env vpython3
  2. # Copyright (c) 2015 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import codecs
  6. import copy
  7. import os
  8. import sys
  9. import unittest
  10. sys.path.insert(
  11. 0,
  12. os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
  13. 'recipes', 'recipe_modules', 'bot_update', 'resources'))
  14. import bot_update
  15. # TODO: Should fix these warnings.
  16. # pylint: disable=line-too-long
  17. class MockedPopen(object):
  18. """A fake instance of a called subprocess.
  19. This is meant to be used in conjunction with MockedCall.
  20. """
  21. def __init__(self, args=None, kwargs=None):
  22. self.args = args or []
  23. self.kwargs = kwargs or {}
  24. self.return_value = None
  25. self.fails = False
  26. def returns(self, rv):
  27. """Set the return value when this popen is called.
  28. rv can be a string, or a callable (eg function).
  29. """
  30. self.return_value = rv
  31. return self
  32. def check(self, args, kwargs):
  33. """Check to see if the given args/kwargs call match this instance.
  34. This does a partial match, so that a call to "git clone foo" will match
  35. this instance if this instance was recorded as "git clone"
  36. """
  37. if any(input_arg != expected_arg
  38. for (input_arg, expected_arg) in zip(args, self.args)):
  39. return False
  40. return self.return_value
  41. def __call__(self, args, kwargs):
  42. """Actually call this popen instance."""
  43. if hasattr(self.return_value, '__call__'):
  44. return self.return_value(*args, **kwargs)
  45. return self.return_value
  46. class MockedCall(object):
  47. """A fake instance of bot_update.call().
  48. This object is pre-seeded with "answers" in self.expectations. The type
  49. is a MockedPopen object, or any object with a __call__() and check() method.
  50. The check() method is used to check to see if the correct popen object is
  51. chosen (can be a partial match, eg a "git clone" popen module would match
  52. a "git clone foo" call).
  53. By default, if no answers have been pre-seeded, the call() returns successful
  54. with an empty string.
  55. """
  56. def __init__(self, fake_filesystem):
  57. self.expectations = []
  58. self.records = []
  59. def expect(self, args=None, kwargs=None):
  60. args = args or []
  61. kwargs = kwargs or {}
  62. popen = MockedPopen(args, kwargs)
  63. self.expectations.append(popen)
  64. return popen
  65. def __call__(self, *args, **kwargs):
  66. self.records.append((args, kwargs))
  67. for popen in self.expectations:
  68. if popen.check(args, kwargs):
  69. self.expectations.remove(popen)
  70. return popen(args, kwargs)
  71. return ''
  72. class MockedGclientSync():
  73. """A class producing a callable instance of gclient sync."""
  74. def __init__(self, fake_filesystem):
  75. self.records = []
  76. def __call__(self, *args, **_):
  77. self.records.append(args)
  78. class FakeFile():
  79. def __init__(self):
  80. self.contents = ''
  81. def write(self, buf):
  82. self.contents += buf
  83. def read(self):
  84. return self.contents
  85. def __enter__(self):
  86. return self
  87. def __exit__(self, _, __, ___):
  88. pass
  89. class FakeFilesystem():
  90. def __init__(self):
  91. self.files = {}
  92. def open(self, target, mode='r', encoding=None):
  93. if 'w' in mode:
  94. self.files[target] = FakeFile()
  95. return self.files[target]
  96. return self.files[target]
  97. def fake_git(*args, **kwargs):
  98. return bot_update.call('git', *args, **kwargs)
  99. class BotUpdateUnittests(unittest.TestCase):
  100. DEFAULT_PARAMS = {
  101. 'solutions': [{
  102. 'name': 'somename',
  103. 'url': 'https://fake.com'
  104. }],
  105. 'revisions': {},
  106. 'first_sln': 'somename',
  107. 'target_os': None,
  108. 'target_os_only': None,
  109. 'target_cpu': None,
  110. 'patch_root': None,
  111. 'patch_refs': [],
  112. 'gerrit_rebase_patch_ref': None,
  113. 'no_fetch_tags': False,
  114. 'refs': [],
  115. 'git_cache_dir': '',
  116. 'cleanup_dir': None,
  117. 'gerrit_reset': None,
  118. 'enforce_fetch': False,
  119. 'experiments': [],
  120. }
  121. def setUp(self):
  122. sys.platform = 'linux2' # For consistency, ya know?
  123. self.filesystem = FakeFilesystem()
  124. self.call = MockedCall(self.filesystem)
  125. self.gclient = MockedGclientSync(self.filesystem)
  126. self.call.expect((sys.executable, '-u', bot_update.GCLIENT_PATH,
  127. 'sync')).returns(self.gclient)
  128. self.old_call = getattr(bot_update, 'call')
  129. self.params = copy.deepcopy(self.DEFAULT_PARAMS)
  130. setattr(bot_update, 'call', self.call)
  131. setattr(bot_update, 'git', fake_git)
  132. self.old_os_cwd = os.getcwd
  133. setattr(os, 'getcwd', lambda: '/b/build/foo/build')
  134. setattr(bot_update, 'open', self.filesystem.open)
  135. self.old_codecs_open = codecs.open
  136. setattr(codecs, 'open', self.filesystem.open)
  137. def tearDown(self):
  138. setattr(bot_update, 'call', self.old_call)
  139. setattr(os, 'getcwd', self.old_os_cwd)
  140. delattr(bot_update, 'open')
  141. setattr(codecs, 'open', self.old_codecs_open)
  142. def overrideSetupForWindows(self):
  143. sys.platform = 'win'
  144. self.call.expect((sys.executable, '-u', bot_update.GCLIENT_PATH,
  145. 'sync')).returns(self.gclient)
  146. def testBasic(self):
  147. bot_update.ensure_checkout(**self.params)
  148. return self.call.records
  149. def testBasicCachepackOffloading(self):
  150. os.environ['PACKFILE_OFFLOADING'] = '1'
  151. bot_update.ensure_checkout(**self.params)
  152. os.environ.pop('PACKFILE_OFFLOADING')
  153. return self.call.records
  154. def testBasicRevision(self):
  155. self.params['revisions'] = {
  156. 'src': 'HEAD',
  157. 'src/v8': 'deadbeef',
  158. 'somename': 'DNE'
  159. }
  160. bot_update.ensure_checkout(**self.params)
  161. args = self.gclient.records[0]
  162. idx_first_revision = args.index('--revision')
  163. idx_second_revision = args.index('--revision', idx_first_revision + 1)
  164. idx_third_revision = args.index('--revision', idx_second_revision + 1)
  165. self.assertEqual(args[idx_first_revision + 1], 'somename@unmanaged')
  166. self.assertEqual(args[idx_second_revision + 1],
  167. 'src@refs/remotes/origin/main')
  168. self.assertEqual(args[idx_third_revision + 1], 'src/v8@deadbeef')
  169. return self.call.records
  170. def testTagsByDefault(self):
  171. bot_update.ensure_checkout(**self.params)
  172. found = False
  173. for record in self.call.records:
  174. args = record[0]
  175. if args[:3] == ('git', 'cache', 'populate'):
  176. self.assertFalse('--no-fetch-tags' in args)
  177. found = True
  178. self.assertTrue(found)
  179. return self.call.records
  180. def testNoTags(self):
  181. params = self.params
  182. params['no_fetch_tags'] = True
  183. bot_update.ensure_checkout(**params)
  184. found = False
  185. for record in self.call.records:
  186. args = record[0]
  187. if args[:3] == ('git', 'cache', 'populate'):
  188. self.assertTrue('--no-fetch-tags' in args)
  189. found = True
  190. self.assertTrue(found)
  191. return self.call.records
  192. def testGclientNoSyncExperiment(self):
  193. ref = 'refs/changes/12/345/6'
  194. repo = 'https://chromium.googlesource.com/v8/v8'
  195. self.params['patch_refs'] = ['%s@%s' % (repo, ref)]
  196. self.params['experiments'] = bot_update.EXP_NO_SYNC
  197. bot_update.ensure_checkout(**self.params)
  198. args = self.gclient.records[0]
  199. idx = args.index('--experiment')
  200. self.assertEqual(args[idx + 1], bot_update.EXP_NO_SYNC)
  201. def testApplyPatchOnGclient(self):
  202. ref = 'refs/changes/12/345/6'
  203. repo = 'https://chromium.googlesource.com/v8/v8'
  204. self.params['patch_refs'] = ['%s@%s' % (repo, ref)]
  205. bot_update.ensure_checkout(**self.params)
  206. args = self.gclient.records[0]
  207. idx = args.index('--patch-ref')
  208. self.assertEqual(args[idx + 1], self.params['patch_refs'][0])
  209. self.assertNotIn('--patch-ref', args[idx + 1:])
  210. # Assert we're not patching in bot_update.py
  211. for record in self.call.records:
  212. self.assertNotIn('git fetch ' + repo, ' '.join(record[0]))
  213. def testPatchRefs(self):
  214. self.params['patch_refs'] = [
  215. 'https://chromium.googlesource.com/chromium/src@refs/changes/12/345/6',
  216. 'https://chromium.googlesource.com/v8/v8@refs/changes/1/234/56'
  217. ]
  218. bot_update.ensure_checkout(**self.params)
  219. args = self.gclient.records[0]
  220. patch_refs = set(args[i + 1] for i in range(len(args))
  221. if args[i] == '--patch-ref' and i + 1 < len(args))
  222. self.assertIn(self.params['patch_refs'][0], patch_refs)
  223. self.assertIn(self.params['patch_refs'][1], patch_refs)
  224. def testGitCheckoutBreaksLocks(self):
  225. self.overrideSetupForWindows()
  226. path = '/b/build/foo/build/.git'
  227. lockfile = 'index.lock'
  228. removed = []
  229. old_os_walk = os.walk
  230. old_os_remove = os.remove
  231. setattr(os, 'walk', lambda _: [(path, None, [lockfile])])
  232. setattr(os, 'remove', removed.append)
  233. bot_update.ensure_checkout(**self.params)
  234. setattr(os, 'walk', old_os_walk)
  235. setattr(os, 'remove', old_os_remove)
  236. self.assertTrue(os.path.join(path, lockfile) in removed)
  237. def testParsesRevisions(self):
  238. revisions = [
  239. 'f671d3baeb64d9dba628ad582e867cf1aebc0207',
  240. 'src@deadbeef',
  241. 'https://foo.googlesource.com/bar@12345',
  242. 'bar@refs/experimental/test@example.com/test',
  243. ]
  244. expected_results = {
  245. 'root': 'f671d3baeb64d9dba628ad582e867cf1aebc0207',
  246. 'src': 'deadbeef',
  247. 'https://foo.googlesource.com/bar.git': '12345',
  248. 'bar': 'refs/experimental/test@example.com/test',
  249. }
  250. actual_results = bot_update.parse_revisions(revisions, 'root')
  251. self.assertEqual(expected_results, actual_results)
  252. class CallUnitTest(unittest.TestCase):
  253. def testCall(self):
  254. ret = bot_update.call(sys.executable, '-c', 'print(1)')
  255. self.assertEqual(u'1\n', ret)
  256. if __name__ == '__main__':
  257. unittest.main()