test_client.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # test_client.py -- Compatibilty tests for git client.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) any later version of
  8. # the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Compatibilty tests between the Dulwich client and the cgit server."""
  20. import os
  21. import shutil
  22. import signal
  23. import subprocess
  24. import tempfile
  25. from dulwich import client
  26. from dulwich import errors
  27. from dulwich import file
  28. from dulwich import index
  29. from dulwich import protocol
  30. from dulwich import objects
  31. from dulwich import repo
  32. from dulwich.tests import (
  33. TestSkipped,
  34. )
  35. from utils import (
  36. CompatTestCase,
  37. check_for_daemon,
  38. import_repo_to_dir,
  39. run_git_or_fail,
  40. )
  41. class DulwichClientTestBase(object):
  42. """Tests for client/server compatibility."""
  43. def setUp(self):
  44. self.gitroot = os.path.dirname(import_repo_to_dir('server_new.export'))
  45. dest = os.path.join(self.gitroot, 'dest')
  46. file.ensure_dir_exists(dest)
  47. run_git_or_fail(['init', '--quiet', '--bare'], cwd=dest)
  48. def tearDown(self):
  49. shutil.rmtree(self.gitroot)
  50. def assertDestEqualsSrc(self):
  51. src = repo.Repo(os.path.join(self.gitroot, 'server_new.export'))
  52. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  53. self.assertReposEqual(src, dest)
  54. def _client(self):
  55. raise NotImplementedError()
  56. def _build_path(self):
  57. raise NotImplementedError()
  58. def _do_send_pack(self):
  59. c = self._client()
  60. srcpath = os.path.join(self.gitroot, 'server_new.export')
  61. src = repo.Repo(srcpath)
  62. sendrefs = dict(src.get_refs())
  63. del sendrefs['HEAD']
  64. c.send_pack(self._build_path('/dest'), lambda _: sendrefs,
  65. src.object_store.generate_pack_contents)
  66. def test_send_pack(self):
  67. self._do_send_pack()
  68. self.assertDestEqualsSrc()
  69. def test_send_pack_nothing_to_send(self):
  70. self._do_send_pack()
  71. self.assertDestEqualsSrc()
  72. # nothing to send, but shouldn't raise either.
  73. self._do_send_pack()
  74. def test_send_without_report_status(self):
  75. c = self._client()
  76. c._send_capabilities.remove('report-status')
  77. srcpath = os.path.join(self.gitroot, 'server_new.export')
  78. src = repo.Repo(srcpath)
  79. sendrefs = dict(src.get_refs())
  80. del sendrefs['HEAD']
  81. c.send_pack(self._build_path('/dest'), lambda _: sendrefs,
  82. src.object_store.generate_pack_contents)
  83. self.assertDestEqualsSrc()
  84. def disable_ff_and_make_dummy_commit(self):
  85. # disable non-fast-forward pushes to the server
  86. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  87. run_git_or_fail(['config', 'receive.denyNonFastForwards', 'true'],
  88. cwd=dest.path)
  89. b = objects.Blob.from_string('hi')
  90. dest.object_store.add_object(b)
  91. t = index.commit_tree(dest.object_store, [('hi', b.id, 0100644)])
  92. c = objects.Commit()
  93. c.author = c.committer = 'Foo Bar <foo@example.com>'
  94. c.author_time = c.commit_time = 0
  95. c.author_timezone = c.commit_timezone = 0
  96. c.message = 'hi'
  97. c.tree = t
  98. dest.object_store.add_object(c)
  99. return dest, c.id
  100. def compute_send(self):
  101. srcpath = os.path.join(self.gitroot, 'server_new.export')
  102. src = repo.Repo(srcpath)
  103. sendrefs = dict(src.get_refs())
  104. del sendrefs['HEAD']
  105. return sendrefs, src.object_store.generate_pack_contents
  106. def test_send_pack_one_error(self):
  107. dest, dummy_commit = self.disable_ff_and_make_dummy_commit()
  108. dest.refs['refs/heads/master'] = dummy_commit
  109. sendrefs, gen_pack = self.compute_send()
  110. c = self._client()
  111. try:
  112. c.send_pack(self._build_path('/dest'), lambda _: sendrefs, gen_pack)
  113. except errors.UpdateRefsError, e:
  114. self.assertEqual('refs/heads/master failed to update', str(e))
  115. self.assertEqual({'refs/heads/branch': 'ok',
  116. 'refs/heads/master': 'non-fast-forward'},
  117. e.ref_status)
  118. def test_send_pack_multiple_errors(self):
  119. dest, dummy = self.disable_ff_and_make_dummy_commit()
  120. # set up for two non-ff errors
  121. dest.refs['refs/heads/branch'] = dest.refs['refs/heads/master'] = dummy
  122. sendrefs, gen_pack = self.compute_send()
  123. c = self._client()
  124. try:
  125. c.send_pack(self._build_path('/dest'), lambda _: sendrefs, gen_pack)
  126. except errors.UpdateRefsError, e:
  127. self.assertEqual('refs/heads/branch, refs/heads/master failed to '
  128. 'update', str(e))
  129. self.assertEqual({'refs/heads/branch': 'non-fast-forward',
  130. 'refs/heads/master': 'non-fast-forward'},
  131. e.ref_status)
  132. def test_fetch_pack(self):
  133. c = self._client()
  134. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  135. refs = c.fetch(self._build_path('/server_new.export'), dest)
  136. map(lambda r: dest.refs.set_if_equals(r[0], None, r[1]), refs.items())
  137. self.assertDestEqualsSrc()
  138. def test_incremental_fetch_pack(self):
  139. self.test_fetch_pack()
  140. dest, dummy = self.disable_ff_and_make_dummy_commit()
  141. dest.refs['refs/heads/master'] = dummy
  142. c = self._client()
  143. dest = repo.Repo(os.path.join(self.gitroot, 'server_new.export'))
  144. refs = c.fetch(self._build_path('/dest'), dest)
  145. map(lambda r: dest.refs.set_if_equals(r[0], None, r[1]), refs.items())
  146. self.assertDestEqualsSrc()
  147. class DulwichTCPClientTest(CompatTestCase, DulwichClientTestBase):
  148. def setUp(self):
  149. CompatTestCase.setUp(self)
  150. DulwichClientTestBase.setUp(self)
  151. if check_for_daemon(limit=1):
  152. raise TestSkipped('git-daemon was already running on port %s' %
  153. protocol.TCP_GIT_PORT)
  154. fd, self.pidfile = tempfile.mkstemp(prefix='dulwich-test-git-client',
  155. suffix=".pid")
  156. os.fdopen(fd).close()
  157. run_git_or_fail(
  158. ['daemon', '--verbose', '--export-all',
  159. '--pid-file=%s' % self.pidfile, '--base-path=%s' % self.gitroot,
  160. '--detach', '--reuseaddr', '--enable=receive-pack',
  161. '--listen=localhost', self.gitroot], cwd=self.gitroot)
  162. if not check_for_daemon():
  163. raise TestSkipped('git-daemon failed to start')
  164. def tearDown(self):
  165. try:
  166. os.kill(int(open(self.pidfile).read().strip()), signal.SIGKILL)
  167. os.unlink(self.pidfile)
  168. except (OSError, IOError):
  169. pass
  170. DulwichClientTestBase.tearDown(self)
  171. CompatTestCase.tearDown(self)
  172. def _client(self):
  173. return client.TCPGitClient('localhost')
  174. def _build_path(self, path):
  175. return path
  176. class TestSSHVendor(object):
  177. @staticmethod
  178. def connect_ssh(host, command, username=None, port=None):
  179. cmd, path = command[0].replace("'", '').split(' ')
  180. cmd = cmd.split('-', 1)
  181. p = subprocess.Popen(cmd + [path], stdin=subprocess.PIPE,
  182. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  183. return client.SubprocessWrapper(p)
  184. class DulwichMockSSHClientTest(CompatTestCase, DulwichClientTestBase):
  185. def setUp(self):
  186. CompatTestCase.setUp(self)
  187. DulwichClientTestBase.setUp(self)
  188. self.real_vendor = client.get_ssh_vendor
  189. client.get_ssh_vendor = TestSSHVendor
  190. def tearDown(self):
  191. DulwichClientTestBase.tearDown(self)
  192. CompatTestCase.tearDown(self)
  193. client.get_ssh_vendor = self.real_vendor
  194. def _client(self):
  195. return client.SSHGitClient('localhost')
  196. def _build_path(self, path):
  197. return self.gitroot + path
  198. class DulwichSubprocessClientTest(CompatTestCase, DulwichClientTestBase):
  199. def setUp(self):
  200. CompatTestCase.setUp(self)
  201. DulwichClientTestBase.setUp(self)
  202. def tearDown(self):
  203. DulwichClientTestBase.tearDown(self)
  204. CompatTestCase.tearDown(self)
  205. def _client(self):
  206. return client.SubprocessGitClient()
  207. def _build_path(self, path):
  208. return self.gitroot + path