test_wrapper.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2015 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Unittests for the wrapper.py module."""
  17. from __future__ import print_function
  18. import os
  19. import re
  20. import unittest
  21. from pyversion import is_python3
  22. import wrapper
  23. if is_python3():
  24. from unittest import mock
  25. from io import StringIO
  26. else:
  27. import mock
  28. from StringIO import StringIO
  29. def fixture(*paths):
  30. """Return a path relative to tests/fixtures.
  31. """
  32. return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
  33. class RepoWrapperTestCase(unittest.TestCase):
  34. """TestCase for the wrapper module."""
  35. def setUp(self):
  36. """Load the wrapper module every time."""
  37. wrapper._wrapper_module = None
  38. self.wrapper = wrapper.Wrapper()
  39. if not is_python3():
  40. self.assertRegex = self.assertRegexpMatches
  41. class RepoWrapperUnitTest(RepoWrapperTestCase):
  42. """Tests helper functions in the repo wrapper
  43. """
  44. def test_version(self):
  45. """Make sure _Version works."""
  46. with self.assertRaises(SystemExit) as e:
  47. with mock.patch('sys.stdout', new_callable=StringIO) as stdout:
  48. with mock.patch('sys.stderr', new_callable=StringIO) as stderr:
  49. self.wrapper._Version()
  50. self.assertEqual(0, e.exception.code)
  51. self.assertEqual('', stderr.getvalue())
  52. self.assertIn('repo launcher version', stdout.getvalue())
  53. def test_init_parser(self):
  54. """Make sure 'init' GetParser works."""
  55. parser = self.wrapper.GetParser(gitc_init=False)
  56. opts, args = parser.parse_args([])
  57. self.assertEqual([], args)
  58. self.assertIsNone(opts.manifest_url)
  59. def test_gitc_init_parser(self):
  60. """Make sure 'gitc-init' GetParser works."""
  61. parser = self.wrapper.GetParser(gitc_init=True)
  62. opts, args = parser.parse_args([])
  63. self.assertEqual([], args)
  64. self.assertIsNone(opts.manifest_file)
  65. def test_get_gitc_manifest_dir_no_gitc(self):
  66. """
  67. Test reading a missing gitc config file
  68. """
  69. self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
  70. val = self.wrapper.get_gitc_manifest_dir()
  71. self.assertEqual(val, '')
  72. def test_get_gitc_manifest_dir(self):
  73. """
  74. Test reading the gitc config file and parsing the directory
  75. """
  76. self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
  77. val = self.wrapper.get_gitc_manifest_dir()
  78. self.assertEqual(val, '/test/usr/local/google/gitc')
  79. def test_gitc_parse_clientdir_no_gitc(self):
  80. """
  81. Test parsing the gitc clientdir without gitc running
  82. """
  83. self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
  84. self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
  85. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
  86. def test_gitc_parse_clientdir(self):
  87. """
  88. Test parsing the gitc clientdir
  89. """
  90. self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
  91. self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
  92. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
  93. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test')
  94. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test')
  95. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test')
  96. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test')
  97. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'),
  98. 'test')
  99. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None)
  100. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None)
  101. class SetGitTrace2ParentSid(RepoWrapperTestCase):
  102. """Check SetGitTrace2ParentSid behavior."""
  103. KEY = 'GIT_TRACE2_PARENT_SID'
  104. VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$')
  105. def test_first_set(self):
  106. """Test env var not yet set."""
  107. env = {}
  108. self.wrapper.SetGitTrace2ParentSid(env)
  109. self.assertIn(self.KEY, env)
  110. value = env[self.KEY]
  111. self.assertRegex(value, self.VALID_FORMAT)
  112. def test_append(self):
  113. """Test env var is appended."""
  114. env = {self.KEY: 'pfx'}
  115. self.wrapper.SetGitTrace2ParentSid(env)
  116. self.assertIn(self.KEY, env)
  117. value = env[self.KEY]
  118. self.assertTrue(value.startswith('pfx/'))
  119. self.assertRegex(value[4:], self.VALID_FORMAT)
  120. def test_global_context(self):
  121. """Check os.environ gets updated by default."""
  122. os.environ.pop(self.KEY, None)
  123. self.wrapper.SetGitTrace2ParentSid()
  124. self.assertIn(self.KEY, os.environ)
  125. value = os.environ[self.KEY]
  126. self.assertRegex(value, self.VALID_FORMAT)
  127. class RunCommand(RepoWrapperTestCase):
  128. """Check run_command behavior."""
  129. def test_capture(self):
  130. """Check capture_output handling."""
  131. ret = self.wrapper.run_command(['echo', 'hi'], capture_output=True)
  132. self.assertEqual(ret.stdout, 'hi\n')
  133. def test_check(self):
  134. """Check check handling."""
  135. self.wrapper.run_command(['true'], check=False)
  136. self.wrapper.run_command(['true'], check=True)
  137. self.wrapper.run_command(['false'], check=False)
  138. with self.assertRaises(self.wrapper.RunError):
  139. self.wrapper.run_command(['false'], check=True)
  140. class RunGit(RepoWrapperTestCase):
  141. """Check run_git behavior."""
  142. def test_capture(self):
  143. """Check capture_output handling."""
  144. ret = self.wrapper.run_git('--version')
  145. self.assertIn('git', ret.stdout)
  146. def test_check(self):
  147. """Check check handling."""
  148. with self.assertRaises(self.wrapper.CloneFailure):
  149. self.wrapper.run_git('--version-asdfasdf')
  150. self.wrapper.run_git('--version-asdfasdf', check=False)
  151. class ParseGitVersion(RepoWrapperTestCase):
  152. """Check ParseGitVersion behavior."""
  153. def test_autoload(self):
  154. """Check we can load the version from the live git."""
  155. ret = self.wrapper.ParseGitVersion()
  156. self.assertIsNotNone(ret)
  157. def test_bad_ver(self):
  158. """Check handling of bad git versions."""
  159. ret = self.wrapper.ParseGitVersion(ver_str='asdf')
  160. self.assertIsNone(ret)
  161. def test_normal_ver(self):
  162. """Check handling of normal git versions."""
  163. ret = self.wrapper.ParseGitVersion(ver_str='git version 2.25.1')
  164. self.assertEqual(2, ret.major)
  165. self.assertEqual(25, ret.minor)
  166. self.assertEqual(1, ret.micro)
  167. self.assertEqual('2.25.1', ret.full)
  168. def test_extended_ver(self):
  169. """Check handling of extended distro git versions."""
  170. ret = self.wrapper.ParseGitVersion(
  171. ver_str='git version 1.30.50.696.g5e7596f4ac-goog')
  172. self.assertEqual(1, ret.major)
  173. self.assertEqual(30, ret.minor)
  174. self.assertEqual(50, ret.micro)
  175. self.assertEqual('1.30.50.696.g5e7596f4ac-goog', ret.full)
  176. class CheckGitVersion(RepoWrapperTestCase):
  177. """Check _CheckGitVersion behavior."""
  178. def test_unknown(self):
  179. """Unknown versions should abort."""
  180. with mock.patch.object(self.wrapper, 'ParseGitVersion', return_value=None):
  181. with self.assertRaises(self.wrapper.CloneFailure):
  182. self.wrapper._CheckGitVersion()
  183. def test_old(self):
  184. """Old versions should abort."""
  185. with mock.patch.object(
  186. self.wrapper, 'ParseGitVersion',
  187. return_value=self.wrapper.GitVersion(1, 0, 0, '1.0.0')):
  188. with self.assertRaises(self.wrapper.CloneFailure):
  189. self.wrapper._CheckGitVersion()
  190. def test_new(self):
  191. """Newer versions should run fine."""
  192. with mock.patch.object(
  193. self.wrapper, 'ParseGitVersion',
  194. return_value=self.wrapper.GitVersion(100, 0, 0, '100.0.0')):
  195. self.wrapper._CheckGitVersion()
  196. if __name__ == '__main__':
  197. unittest.main()