test_client.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 import (
  20. client,
  21. )
  22. from dulwich.client import (
  23. TraditionalGitClient,
  24. TCPGitClient,
  25. SubprocessGitClient,
  26. SSHGitClient,
  27. HttpGitClient,
  28. ReportStatusParser,
  29. SendPackError,
  30. UpdateRefsError,
  31. get_transport_and_path,
  32. )
  33. from dulwich.tests import (
  34. TestCase,
  35. )
  36. from dulwich.protocol import (
  37. TCP_GIT_PORT,
  38. Protocol,
  39. )
  40. class DummyClient(TraditionalGitClient):
  41. def __init__(self, can_read, read, write):
  42. self.can_read = can_read
  43. self.read = read
  44. self.write = write
  45. TraditionalGitClient.__init__(self)
  46. def _connect(self, service, path):
  47. return Protocol(self.read, self.write), self.can_read
  48. # TODO(durin42): add unit-level tests of GitClient
  49. class GitClientTests(TestCase):
  50. def setUp(self):
  51. super(GitClientTests, self).setUp()
  52. self.rout = StringIO()
  53. self.rin = StringIO()
  54. self.client = DummyClient(lambda x: True, self.rin.read,
  55. self.rout.write)
  56. def test_caps(self):
  57. self.assertEqual(set(['multi_ack', 'side-band-64k', 'ofs-delta',
  58. 'thin-pack', 'multi_ack_detailed']),
  59. set(self.client._fetch_capabilities))
  60. self.assertEqual(set(['ofs-delta', 'report-status', 'side-band-64k']),
  61. set(self.client._send_capabilities))
  62. def test_archive_ack(self):
  63. self.rin.write(
  64. '0009NACK\n'
  65. '0000')
  66. self.rin.seek(0)
  67. self.client.archive('bla', 'HEAD', None, None)
  68. self.assertEqual(self.rout.getvalue(), '0011argument HEAD0000')
  69. def test_fetch_empty(self):
  70. self.rin.write('0000')
  71. self.rin.seek(0)
  72. self.client.fetch_pack('/', lambda heads: [], None, None)
  73. def test_fetch_pack_none(self):
  74. self.rin.write(
  75. '008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD.multi_ack '
  76. 'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  77. 'include-tag\n'
  78. '0000')
  79. self.rin.seek(0)
  80. self.client.fetch_pack('bla', lambda heads: [], None, None, None)
  81. self.assertEqual(self.rout.getvalue(), '0000')
  82. def test_get_transport_and_path_tcp(self):
  83. client, path = get_transport_and_path('git://foo.com/bar/baz')
  84. self.assertTrue(isinstance(client, TCPGitClient))
  85. self.assertEqual('foo.com', client._host)
  86. self.assertEqual(TCP_GIT_PORT, client._port)
  87. self.assertEqual('/bar/baz', path)
  88. def test_get_transport_and_path_tcp_port(self):
  89. client, path = get_transport_and_path('git://foo.com:1234/bar/baz')
  90. self.assertTrue(isinstance(client, TCPGitClient))
  91. self.assertEqual('foo.com', client._host)
  92. self.assertEqual(1234, client._port)
  93. self.assertEqual('/bar/baz', path)
  94. def test_get_transport_and_path_ssh_explicit(self):
  95. client, path = get_transport_and_path('git+ssh://foo.com/bar/baz')
  96. self.assertTrue(isinstance(client, SSHGitClient))
  97. self.assertEqual('foo.com', client.host)
  98. self.assertEqual(None, client.port)
  99. self.assertEqual(None, client.username)
  100. self.assertEqual('bar/baz', path)
  101. def test_get_transport_and_path_ssh_port_explicit(self):
  102. client, path = get_transport_and_path(
  103. 'git+ssh://foo.com:1234/bar/baz')
  104. self.assertTrue(isinstance(client, SSHGitClient))
  105. self.assertEqual('foo.com', client.host)
  106. self.assertEqual(1234, client.port)
  107. self.assertEqual('bar/baz', path)
  108. def test_get_transport_and_path_ssh_abspath_explicit(self):
  109. client, path = get_transport_and_path('git+ssh://foo.com//bar/baz')
  110. self.assertTrue(isinstance(client, SSHGitClient))
  111. self.assertEqual('foo.com', client.host)
  112. self.assertEqual(None, client.port)
  113. self.assertEqual(None, client.username)
  114. self.assertEqual('/bar/baz', path)
  115. def test_get_transport_and_path_ssh_port_abspath_explicit(self):
  116. client, path = get_transport_and_path(
  117. 'git+ssh://foo.com:1234//bar/baz')
  118. self.assertTrue(isinstance(client, SSHGitClient))
  119. self.assertEqual('foo.com', client.host)
  120. self.assertEqual(1234, client.port)
  121. self.assertEqual('/bar/baz', path)
  122. def test_get_transport_and_path_ssh_implicit(self):
  123. client, path = get_transport_and_path('foo:/bar/baz')
  124. self.assertTrue(isinstance(client, SSHGitClient))
  125. self.assertEqual('foo', client.host)
  126. self.assertEqual(None, client.port)
  127. self.assertEqual(None, client.username)
  128. self.assertEqual('/bar/baz', path)
  129. def test_get_transport_and_path_ssh_host(self):
  130. client, path = get_transport_and_path('foo.com:/bar/baz')
  131. self.assertTrue(isinstance(client, SSHGitClient))
  132. self.assertEqual('foo.com', client.host)
  133. self.assertEqual(None, client.port)
  134. self.assertEqual(None, client.username)
  135. self.assertEqual('/bar/baz', path)
  136. def test_get_transport_and_path_ssh_user_host(self):
  137. client, path = get_transport_and_path('user@foo.com:/bar/baz')
  138. self.assertTrue(isinstance(client, SSHGitClient))
  139. self.assertEqual('foo.com', client.host)
  140. self.assertEqual(None, client.port)
  141. self.assertEqual('user', client.username)
  142. self.assertEqual('/bar/baz', path)
  143. def test_get_transport_and_path_ssh_relpath(self):
  144. client, path = get_transport_and_path('foo:bar/baz')
  145. self.assertTrue(isinstance(client, SSHGitClient))
  146. self.assertEqual('foo', client.host)
  147. self.assertEqual(None, client.port)
  148. self.assertEqual(None, client.username)
  149. self.assertEqual('bar/baz', path)
  150. def test_get_transport_and_path_ssh_host_relpath(self):
  151. client, path = get_transport_and_path('foo.com:bar/baz')
  152. self.assertTrue(isinstance(client, SSHGitClient))
  153. self.assertEqual('foo.com', client.host)
  154. self.assertEqual(None, client.port)
  155. self.assertEqual(None, client.username)
  156. self.assertEqual('bar/baz', path)
  157. def test_get_transport_and_path_ssh_user_host_relpath(self):
  158. client, path = get_transport_and_path('user@foo.com:bar/baz')
  159. self.assertTrue(isinstance(client, SSHGitClient))
  160. self.assertEqual('foo.com', client.host)
  161. self.assertEqual(None, client.port)
  162. self.assertEqual('user', client.username)
  163. self.assertEqual('bar/baz', path)
  164. def test_get_transport_and_path_subprocess(self):
  165. client, path = get_transport_and_path('foo.bar/baz')
  166. self.assertTrue(isinstance(client, SubprocessGitClient))
  167. self.assertEqual('foo.bar/baz', path)
  168. def test_get_transport_and_path_error(self):
  169. # Need to use a known urlparse.uses_netloc URL scheme to get the
  170. # expected parsing of the URL on Python versions less than 2.6.5
  171. self.assertRaises(ValueError, get_transport_and_path,
  172. 'prospero://bar/baz')
  173. def test_get_transport_and_path_http(self):
  174. url = 'https://github.com/jelmer/dulwich'
  175. client, path = get_transport_and_path(url)
  176. self.assertTrue(isinstance(client, HttpGitClient))
  177. self.assertEqual('/jelmer/dulwich', path)
  178. def test_send_pack_no_sideband64k_with_update_ref_error(self):
  179. # No side-bank-64k reported by server shouldn't try to parse
  180. # side band data
  181. pkts = ['55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 capabilities^{}\x00 report-status ofs-delta\n',
  182. '',
  183. "unpack ok",
  184. "ng refs/foo/bar pre-receive hook declined",
  185. '']
  186. for pkt in pkts:
  187. if pkt == '':
  188. self.rin.write("0000")
  189. else:
  190. self.rin.write("%04x%s" % (len(pkt)+4, pkt))
  191. self.rin.seek(0)
  192. self.assertRaises(UpdateRefsError,
  193. self.client.send_pack, "blah", lambda x: {}, lambda h,w: [])
  194. class TestSSHVendor(object):
  195. def __init__(self):
  196. self.host = None
  197. self.command = ""
  198. self.username = None
  199. self.port = None
  200. def connect_ssh(self, host, command, username=None, port=None):
  201. self.host = host
  202. self.command = command
  203. self.username = username
  204. self.port = port
  205. class Subprocess: pass
  206. setattr(Subprocess, 'read', lambda: None)
  207. setattr(Subprocess, 'write', lambda: None)
  208. setattr(Subprocess, 'can_read', lambda: None)
  209. return Subprocess()
  210. class SSHGitClientTests(TestCase):
  211. def setUp(self):
  212. super(SSHGitClientTests, self).setUp()
  213. self.server = TestSSHVendor()
  214. self.real_vendor = client.get_ssh_vendor
  215. client.get_ssh_vendor = lambda: self.server
  216. self.client = SSHGitClient('git.samba.org')
  217. def tearDown(self):
  218. super(SSHGitClientTests, self).tearDown()
  219. client.get_ssh_vendor = self.real_vendor
  220. def test_default_command(self):
  221. self.assertEqual('git-upload-pack',
  222. self.client._get_cmd_path('upload-pack'))
  223. def test_alternative_command_path(self):
  224. self.client.alternative_paths['upload-pack'] = (
  225. '/usr/lib/git/git-upload-pack')
  226. self.assertEqual('/usr/lib/git/git-upload-pack',
  227. self.client._get_cmd_path('upload-pack'))
  228. def test_connect(self):
  229. server = self.server
  230. client = self.client
  231. client.username = "username"
  232. client.port = 1337
  233. client._connect("command", "/path/to/repo")
  234. self.assertEquals("username", server.username)
  235. self.assertEquals(1337, server.port)
  236. self.assertEquals(["git-command '/path/to/repo'"], server.command)
  237. client._connect("relative-command", "/~/path/to/repo")
  238. self.assertEquals(["git-relative-command '~/path/to/repo'"],
  239. server.command)
  240. class ReportStatusParserTests(TestCase):
  241. def test_invalid_pack(self):
  242. parser = ReportStatusParser()
  243. parser.handle_packet("unpack error - foo bar")
  244. parser.handle_packet("ok refs/foo/bar")
  245. parser.handle_packet(None)
  246. self.assertRaises(SendPackError, parser.check)
  247. def test_update_refs_error(self):
  248. parser = ReportStatusParser()
  249. parser.handle_packet("unpack ok")
  250. parser.handle_packet("ng refs/foo/bar need to pull")
  251. parser.handle_packet(None)
  252. self.assertRaises(UpdateRefsError, parser.check)
  253. def test_ok(self):
  254. parser = ReportStatusParser()
  255. parser.handle_packet("unpack ok")
  256. parser.handle_packet("ok refs/foo/bar")
  257. parser.handle_packet(None)
  258. parser.check()