test_wrapper.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_get_gitc_manifest_dir_no_gitc(self):
  54. """
  55. Test reading a missing gitc config file
  56. """
  57. self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
  58. val = self.wrapper.get_gitc_manifest_dir()
  59. self.assertEqual(val, '')
  60. def test_get_gitc_manifest_dir(self):
  61. """
  62. Test reading the gitc config file and parsing the directory
  63. """
  64. self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
  65. val = self.wrapper.get_gitc_manifest_dir()
  66. self.assertEqual(val, '/test/usr/local/google/gitc')
  67. def test_gitc_parse_clientdir_no_gitc(self):
  68. """
  69. Test parsing the gitc clientdir without gitc running
  70. """
  71. self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
  72. self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
  73. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
  74. def test_gitc_parse_clientdir(self):
  75. """
  76. Test parsing the gitc clientdir
  77. """
  78. self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
  79. self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
  80. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
  81. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test')
  82. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test')
  83. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test')
  84. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test')
  85. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'),
  86. 'test')
  87. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None)
  88. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None)
  89. class SetGitTrace2ParentSid(RepoWrapperTestCase):
  90. """Check SetGitTrace2ParentSid behavior."""
  91. KEY = 'GIT_TRACE2_PARENT_SID'
  92. VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$')
  93. def test_first_set(self):
  94. """Test env var not yet set."""
  95. env = {}
  96. self.wrapper.SetGitTrace2ParentSid(env)
  97. self.assertIn(self.KEY, env)
  98. value = env[self.KEY]
  99. self.assertRegex(value, self.VALID_FORMAT)
  100. def test_append(self):
  101. """Test env var is appended."""
  102. env = {self.KEY: 'pfx'}
  103. self.wrapper.SetGitTrace2ParentSid(env)
  104. self.assertIn(self.KEY, env)
  105. value = env[self.KEY]
  106. self.assertTrue(value.startswith('pfx/'))
  107. self.assertRegex(value[4:], self.VALID_FORMAT)
  108. def test_global_context(self):
  109. """Check os.environ gets updated by default."""
  110. os.environ.pop(self.KEY, None)
  111. self.wrapper.SetGitTrace2ParentSid()
  112. self.assertIn(self.KEY, os.environ)
  113. value = os.environ[self.KEY]
  114. self.assertRegex(value, self.VALID_FORMAT)
  115. if __name__ == '__main__':
  116. unittest.main()