2
0

test_client.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 tempfile
  24. from dulwich import client
  25. from dulwich import errors
  26. from dulwich import file
  27. from dulwich import index
  28. from dulwich import protocol
  29. from dulwich import object_store
  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,
  40. )
  41. class DulwichClientTest(CompatTestCase):
  42. """Tests for client/server compatibility."""
  43. def setUp(self):
  44. if check_for_daemon(limit=1):
  45. raise TestSkipped('git-daemon was already running on port %s' %
  46. protocol.TCP_GIT_PORT)
  47. CompatTestCase.setUp(self)
  48. fd, self.pidfile = tempfile.mkstemp(prefix='dulwich-test-git-client',
  49. suffix=".pid")
  50. os.fdopen(fd).close()
  51. self.gitroot = os.path.dirname(import_repo_to_dir('server_new.export'))
  52. dest = os.path.join(self.gitroot, 'dest')
  53. file.ensure_dir_exists(dest)
  54. run_git(['init', '--bare'], cwd=dest)
  55. run_git(
  56. ['daemon', '--verbose', '--export-all',
  57. '--pid-file=%s' % self.pidfile, '--base-path=%s' % self.gitroot,
  58. '--detach', '--reuseaddr', '--enable=receive-pack',
  59. '--listen=localhost', self.gitroot], cwd=self.gitroot)
  60. if not check_for_daemon():
  61. raise TestSkipped('git-daemon failed to start')
  62. def tearDown(self):
  63. CompatTestCase.tearDown(self)
  64. try:
  65. os.kill(int(open(self.pidfile).read().strip()), signal.SIGKILL)
  66. os.unlink(self.pidfile)
  67. except (OSError, IOError):
  68. pass
  69. shutil.rmtree(self.gitroot)
  70. def assertDestEqualsSrc(self):
  71. src = repo.Repo(os.path.join(self.gitroot, 'server_new.export'))
  72. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  73. self.assertReposEqual(src, dest)
  74. def _do_send_pack(self):
  75. c = client.TCPGitClient('localhost')
  76. srcpath = os.path.join(self.gitroot, 'server_new.export')
  77. src = repo.Repo(srcpath)
  78. sendrefs = dict(src.get_refs())
  79. del sendrefs['HEAD']
  80. c.send_pack('/dest', lambda _: sendrefs,
  81. src.object_store.generate_pack_contents)
  82. def test_send_pack(self):
  83. self._do_send_pack()
  84. self.assertDestEqualsSrc()
  85. def test_send_pack_nothing_to_send(self):
  86. self._do_send_pack()
  87. self.assertDestEqualsSrc()
  88. # nothing to send, but shouldn't raise either.
  89. self._do_send_pack()
  90. def test_send_without_report_status(self):
  91. c = client.TCPGitClient('localhost')
  92. c._send_capabilities.remove('report-status')
  93. srcpath = os.path.join(self.gitroot, 'server_new.export')
  94. src = repo.Repo(srcpath)
  95. sendrefs = dict(src.get_refs())
  96. del sendrefs['HEAD']
  97. c.send_pack('/dest', lambda _: sendrefs,
  98. src.object_store.generate_pack_contents)
  99. self.assertDestEqualsSrc()
  100. def disable_ff_and_make_dummy_commit(self):
  101. # disable non-fast-forward pushes to the server
  102. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  103. run_git(['config', 'receive.denyNonFastForwards', 'true'], cwd=dest.path)
  104. b = objects.Blob.from_string('hi')
  105. dest.object_store.add_object(b)
  106. t = index.commit_tree(dest.object_store, [('hi', b.id, 0100644)])
  107. c = objects.Commit()
  108. c.author = c.committer = 'Foo Bar <foo@example.com>'
  109. c.author_time = c.commit_time = 0
  110. c.author_timezone = c.commit_timezone = 0
  111. c.message = 'hi'
  112. c.tree = t
  113. dest.object_store.add_object(c)
  114. return dest, c.id
  115. def compute_send(self):
  116. srcpath = os.path.join(self.gitroot, 'server_new.export')
  117. src = repo.Repo(srcpath)
  118. sendrefs = dict(src.get_refs())
  119. del sendrefs['HEAD']
  120. return sendrefs, src.object_store.generate_pack_contents
  121. def test_send_pack_one_error(self):
  122. dest, dummy_commit = self.disable_ff_and_make_dummy_commit()
  123. dest.refs['refs/heads/master'] = dummy_commit
  124. sendrefs, gen_pack = self.compute_send()
  125. c = client.TCPGitClient('localhost')
  126. try:
  127. c.send_pack('/dest', lambda _: sendrefs, gen_pack)
  128. except errors.UpdateRefsError, e:
  129. self.assertEqual('refs/heads/master failed to update', str(e))
  130. self.assertEqual({'refs/heads/branch': 'ok',
  131. 'refs/heads/master': 'non-fast-forward'},
  132. e.ref_status)
  133. def test_send_pack_multiple_errors(self):
  134. dest, dummy = self.disable_ff_and_make_dummy_commit()
  135. # set up for two non-ff errors
  136. dest.refs['refs/heads/branch'] = dest.refs['refs/heads/master'] = dummy
  137. sendrefs, gen_pack = self.compute_send()
  138. c = client.TCPGitClient('localhost')
  139. try:
  140. c.send_pack('/dest', lambda _: sendrefs, gen_pack)
  141. except errors.UpdateRefsError, e:
  142. self.assertEqual('refs/heads/branch, refs/heads/master failed to '
  143. 'update', str(e))
  144. self.assertEqual({'refs/heads/branch': 'non-fast-forward',
  145. 'refs/heads/master': 'non-fast-forward'},
  146. e.ref_status)
  147. def test_fetch_pack(self):
  148. c = client.TCPGitClient('localhost')
  149. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  150. refs = c.fetch('/server_new.export', dest)
  151. map(lambda r: dest.refs.set_if_equals(r[0], None, r[1]), refs.items())
  152. self.assertDestEqualsSrc()
  153. def test_incremental_fetch_pack(self):
  154. self.test_fetch_pack()
  155. dest, dummy = self.disable_ff_and_make_dummy_commit()
  156. dest.refs['refs/heads/master'] = dummy
  157. c = client.TCPGitClient('localhost')
  158. dest = repo.Repo(os.path.join(self.gitroot, 'server_new.export'))
  159. refs = c.fetch('/dest', dest)
  160. map(lambda r: dest.refs.set_if_equals(r[0], None, r[1]), refs.items())
  161. self.assertDestEqualsSrc()