2
0

test_client.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # test_client.py -- Tests for the git protocol, client side
  2. # Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
  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. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. from cStringIO import StringIO
  19. from dulwich.client import (
  20. TraditionalGitClient,
  21. TCPGitClient,
  22. SubprocessGitClient,
  23. SSHGitClient,
  24. HttpGitClient,
  25. ReportStatusParser,
  26. SendPackError,
  27. UpdateRefsError,
  28. get_transport_and_path,
  29. )
  30. from dulwich.tests import (
  31. TestCase,
  32. )
  33. from dulwich.protocol import (
  34. TCP_GIT_PORT,
  35. Protocol,
  36. )
  37. class DummyClient(TraditionalGitClient):
  38. def __init__(self, can_read, read, write):
  39. self.can_read = can_read
  40. self.read = read
  41. self.write = write
  42. TraditionalGitClient.__init__(self)
  43. def _connect(self, service, path):
  44. return Protocol(self.read, self.write), self.can_read
  45. # TODO(durin42): add unit-level tests of GitClient
  46. class GitClientTests(TestCase):
  47. def setUp(self):
  48. super(GitClientTests, self).setUp()
  49. self.rout = StringIO()
  50. self.rin = StringIO()
  51. self.client = DummyClient(lambda x: True, self.rin.read,
  52. self.rout.write)
  53. def test_caps(self):
  54. self.assertEqual(set(['multi_ack', 'side-band-64k', 'ofs-delta',
  55. 'thin-pack', 'multi_ack_detailed']),
  56. set(self.client._fetch_capabilities))
  57. self.assertEqual(set(['ofs-delta', 'report-status', 'side-band-64k']),
  58. set(self.client._send_capabilities))
  59. def test_archive_ack(self):
  60. self.rin.write(
  61. '0009NACK\n'
  62. '0000')
  63. self.rin.seek(0)
  64. self.client.archive('bla', 'HEAD', None, None)
  65. self.assertEqual(self.rout.getvalue(), '0011argument HEAD0000')
  66. def test_fetch_pack_none(self):
  67. self.rin.write(
  68. '008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD.multi_ack '
  69. 'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  70. 'include-tag\n'
  71. '0000')
  72. self.rin.seek(0)
  73. self.client.fetch_pack('bla', lambda heads: [], None, None, None)
  74. self.assertEqual(self.rout.getvalue(), '0000')
  75. def test_get_transport_and_path_tcp(self):
  76. client, path = get_transport_and_path('git://foo.com/bar/baz')
  77. self.assertTrue(isinstance(client, TCPGitClient))
  78. self.assertEqual('foo.com', client._host)
  79. self.assertEqual(TCP_GIT_PORT, client._port)
  80. self.assertEqual('/bar/baz', path)
  81. client, path = get_transport_and_path('git://foo.com:1234/bar/baz')
  82. self.assertTrue(isinstance(client, TCPGitClient))
  83. self.assertEqual('foo.com', client._host)
  84. self.assertEqual(1234, client._port)
  85. self.assertEqual('/bar/baz', path)
  86. def test_get_transport_and_path_ssh_explicit(self):
  87. client, path = get_transport_and_path('git+ssh://foo.com/bar/baz')
  88. self.assertTrue(isinstance(client, SSHGitClient))
  89. self.assertEqual('foo.com', client.host)
  90. self.assertEqual(None, client.port)
  91. self.assertEqual(None, client.username)
  92. self.assertEqual('/bar/baz', path)
  93. client, path = get_transport_and_path(
  94. 'git+ssh://foo.com:1234/bar/baz')
  95. self.assertTrue(isinstance(client, SSHGitClient))
  96. self.assertEqual('foo.com', client.host)
  97. self.assertEqual(1234, client.port)
  98. self.assertEqual('/bar/baz', path)
  99. def test_get_transport_and_path_ssh_implicit(self):
  100. client, path = get_transport_and_path('foo:/bar/baz')
  101. self.assertTrue(isinstance(client, SSHGitClient))
  102. self.assertEqual('foo', client.host)
  103. self.assertEqual(None, client.port)
  104. self.assertEqual(None, client.username)
  105. self.assertEqual('/bar/baz', path)
  106. client, path = get_transport_and_path('foo.com:/bar/baz')
  107. self.assertTrue(isinstance(client, SSHGitClient))
  108. self.assertEqual('foo.com', client.host)
  109. self.assertEqual(None, client.port)
  110. self.assertEqual(None, client.username)
  111. self.assertEqual('/bar/baz', path)
  112. client, path = get_transport_and_path('user@foo.com:/bar/baz')
  113. self.assertTrue(isinstance(client, SSHGitClient))
  114. self.assertEqual('foo.com', client.host)
  115. self.assertEqual(None, client.port)
  116. self.assertEqual('user', client.username)
  117. self.assertEqual('/bar/baz', path)
  118. def test_get_transport_and_path_subprocess(self):
  119. client, path = get_transport_and_path('foo.bar/baz')
  120. self.assertTrue(isinstance(client, SubprocessGitClient))
  121. self.assertEqual('foo.bar/baz', path)
  122. def test_get_transport_and_path_error(self):
  123. # Need to use a known urlparse.uses_netloc URL scheme to get the
  124. # expected parsing of the URL on Python versions less than 2.6.5
  125. self.assertRaises(ValueError, get_transport_and_path,
  126. 'prospero://bar/baz')
  127. def test_get_transport_and_path_http(self):
  128. url = 'https://github.com/jelmer/dulwich'
  129. client, path = get_transport_and_path(url)
  130. self.assertTrue(isinstance(client, HttpGitClient))
  131. self.assertEqual('/jelmer/dulwich', path)
  132. def test_send_pack_no_sideband64k_with_update_ref_error(self):
  133. # No side-bank-64k reported by server shouldn't try to parse
  134. # side band data
  135. pkts = ['55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 capabilities^{}\x00 report-status ofs-delta\n',
  136. '',
  137. "unpack ok",
  138. "ng refs/foo/bar pre-receive hook declined",
  139. '']
  140. for pkt in pkts:
  141. if pkt == '':
  142. self.rin.write("0000")
  143. else:
  144. self.rin.write("%04x%s" % (len(pkt)+4, pkt))
  145. self.rin.seek(0)
  146. self.assertRaises(UpdateRefsError,
  147. self.client.send_pack, "blah", lambda x: {}, lambda h,w: [])
  148. class SSHGitClientTests(TestCase):
  149. def setUp(self):
  150. super(SSHGitClientTests, self).setUp()
  151. self.client = SSHGitClient('git.samba.org')
  152. def test_default_command(self):
  153. self.assertEqual('git-upload-pack',
  154. self.client._get_cmd_path('upload-pack'))
  155. def test_alternative_command_path(self):
  156. self.client.alternative_paths['upload-pack'] = (
  157. '/usr/lib/git/git-upload-pack')
  158. self.assertEqual('/usr/lib/git/git-upload-pack',
  159. self.client._get_cmd_path('upload-pack'))
  160. class ReportStatusParserTests(TestCase):
  161. def test_invalid_pack(self):
  162. parser = ReportStatusParser()
  163. parser.handle_packet("unpack error - foo bar")
  164. parser.handle_packet("ok refs/foo/bar")
  165. parser.handle_packet(None)
  166. self.assertRaises(SendPackError, parser.check)
  167. def test_update_refs_error(self):
  168. parser = ReportStatusParser()
  169. parser.handle_packet("unpack ok")
  170. parser.handle_packet("ng refs/foo/bar need to pull")
  171. parser.handle_packet(None)
  172. self.assertRaises(UpdateRefsError, parser.check)
  173. def test_ok(self):
  174. parser = ReportStatusParser()
  175. parser.handle_packet("unpack ok")
  176. parser.handle_packet("ok refs/foo/bar")
  177. parser.handle_packet(None)
  178. parser.check()