test_client.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. ReportStatusParser,
  25. SendPackError,
  26. UpdateRefsError,
  27. get_transport_and_path,
  28. )
  29. from dulwich.tests import (
  30. TestCase,
  31. )
  32. from dulwich.protocol import (
  33. TCP_GIT_PORT,
  34. Protocol,
  35. )
  36. class DummyClient(TraditionalGitClient):
  37. def __init__(self, can_read, read, write):
  38. self.can_read = can_read
  39. self.read = read
  40. self.write = write
  41. TraditionalGitClient.__init__(self)
  42. def _connect(self, service, path):
  43. return Protocol(self.read, self.write), self.can_read
  44. # TODO(durin42): add unit-level tests of GitClient
  45. class GitClientTests(TestCase):
  46. def setUp(self):
  47. super(GitClientTests, self).setUp()
  48. self.rout = StringIO()
  49. self.rin = StringIO()
  50. self.client = DummyClient(lambda x: True, self.rin.read,
  51. self.rout.write)
  52. def test_caps(self):
  53. self.assertEquals(set(['multi_ack', 'side-band-64k', 'ofs-delta',
  54. 'thin-pack', 'multi_ack_detailed']),
  55. set(self.client._fetch_capabilities))
  56. self.assertEquals(set(['ofs-delta', 'report-status', 'side-band-64k']),
  57. set(self.client._send_capabilities))
  58. def test_fetch_pack_none(self):
  59. self.rin.write(
  60. '008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD.multi_ack '
  61. 'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  62. 'include-tag\n'
  63. '0000')
  64. self.rin.seek(0)
  65. self.client.fetch_pack('bla', lambda heads: [], None, None, None)
  66. self.assertEquals(self.rout.getvalue(), '0000')
  67. def test_get_transport_and_path_tcp(self):
  68. client, path = get_transport_and_path('git://foo.com/bar/baz')
  69. self.assertTrue(isinstance(client, TCPGitClient))
  70. self.assertEquals('foo.com', client._host)
  71. self.assertEquals(TCP_GIT_PORT, client._port)
  72. self.assertEqual('/bar/baz', path)
  73. client, path = get_transport_and_path('git://foo.com:1234/bar/baz')
  74. self.assertTrue(isinstance(client, TCPGitClient))
  75. self.assertEquals('foo.com', client._host)
  76. self.assertEquals(1234, client._port)
  77. self.assertEqual('/bar/baz', path)
  78. def test_get_transport_and_path_ssh_explicit(self):
  79. client, path = get_transport_and_path('git+ssh://foo.com/bar/baz')
  80. self.assertTrue(isinstance(client, SSHGitClient))
  81. self.assertEquals('foo.com', client.host)
  82. self.assertEquals(None, client.port)
  83. self.assertEquals(None, client.username)
  84. self.assertEqual('/bar/baz', path)
  85. client, path = get_transport_and_path(
  86. 'git+ssh://foo.com:1234/bar/baz')
  87. self.assertTrue(isinstance(client, SSHGitClient))
  88. self.assertEquals('foo.com', client.host)
  89. self.assertEquals(1234, client.port)
  90. self.assertEqual('/bar/baz', path)
  91. def test_get_transport_and_path_ssh_implicit(self):
  92. client, path = get_transport_and_path('foo:/bar/baz')
  93. self.assertTrue(isinstance(client, SSHGitClient))
  94. self.assertEquals('foo', client.host)
  95. self.assertEquals(None, client.port)
  96. self.assertEquals(None, client.username)
  97. self.assertEqual('/bar/baz', path)
  98. client, path = get_transport_and_path('foo.com:/bar/baz')
  99. self.assertTrue(isinstance(client, SSHGitClient))
  100. self.assertEquals('foo.com', client.host)
  101. self.assertEquals(None, client.port)
  102. self.assertEquals(None, client.username)
  103. self.assertEqual('/bar/baz', path)
  104. client, path = get_transport_and_path('user@foo.com:/bar/baz')
  105. self.assertTrue(isinstance(client, SSHGitClient))
  106. self.assertEquals('foo.com', client.host)
  107. self.assertEquals(None, client.port)
  108. self.assertEquals('user', client.username)
  109. self.assertEqual('/bar/baz', path)
  110. def test_get_transport_and_path_subprocess(self):
  111. client, path = get_transport_and_path('foo.bar/baz')
  112. self.assertTrue(isinstance(client, SubprocessGitClient))
  113. self.assertEquals('foo.bar/baz', path)
  114. def test_get_transport_and_path_error(self):
  115. # Need to use a known urlparse.uses_netloc URL scheme to get the
  116. # expected parsing of the URL on Python versions less than 2.6.5
  117. self.assertRaises(ValueError, get_transport_and_path,
  118. 'prospero://bar/baz')
  119. class SSHGitClientTests(TestCase):
  120. def setUp(self):
  121. super(SSHGitClientTests, self).setUp()
  122. self.client = SSHGitClient('git.samba.org')
  123. def test_default_command(self):
  124. self.assertEquals('git-upload-pack',
  125. self.client._get_cmd_path('upload-pack'))
  126. def test_alternative_command_path(self):
  127. self.client.alternative_paths['upload-pack'] = (
  128. '/usr/lib/git/git-upload-pack')
  129. self.assertEquals('/usr/lib/git/git-upload-pack',
  130. self.client._get_cmd_path('upload-pack'))
  131. class ReportStatusParserTests(TestCase):
  132. def test_invalid_pack(self):
  133. parser = ReportStatusParser()
  134. parser.handle_packet("unpack error - foo bar")
  135. parser.handle_packet("ok refs/foo/bar")
  136. parser.handle_packet(None)
  137. self.assertRaises(SendPackError, parser.check)
  138. def test_update_refs_error(self):
  139. parser = ReportStatusParser()
  140. parser.handle_packet("unpack ok")
  141. parser.handle_packet("ng refs/foo/bar need to pull")
  142. parser.handle_packet(None)
  143. self.assertRaises(UpdateRefsError, parser.check)
  144. def test_ok(self):
  145. parser = ReportStatusParser()
  146. parser.handle_packet("unpack ok")
  147. parser.handle_packet("ok refs/foo/bar")
  148. parser.handle_packet(None)
  149. parser.check()