test_client.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 BaseHTTPServer
  21. import SimpleHTTPServer
  22. import copy
  23. import os
  24. import select
  25. import shutil
  26. import signal
  27. import subprocess
  28. import tempfile
  29. import threading
  30. import urllib
  31. import urlparse
  32. from dulwich import (
  33. client,
  34. errors,
  35. file,
  36. index,
  37. protocol,
  38. objects,
  39. repo,
  40. )
  41. from dulwich.tests import (
  42. SkipTest,
  43. )
  44. from dulwich.tests.compat.utils import (
  45. CompatTestCase,
  46. check_for_daemon,
  47. import_repo_to_dir,
  48. run_git_or_fail,
  49. )
  50. from dulwich.tests.compat.server_utils import (
  51. ShutdownServerMixIn,
  52. )
  53. class DulwichClientTestBase(object):
  54. """Tests for client/server compatibility."""
  55. def setUp(self):
  56. self.gitroot = os.path.dirname(import_repo_to_dir('server_new.export'))
  57. dest = os.path.join(self.gitroot, 'dest')
  58. file.ensure_dir_exists(dest)
  59. run_git_or_fail(['init', '--quiet', '--bare'], cwd=dest)
  60. def tearDown(self):
  61. shutil.rmtree(self.gitroot)
  62. def assertDestEqualsSrc(self):
  63. src = repo.Repo(os.path.join(self.gitroot, 'server_new.export'))
  64. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  65. self.assertReposEqual(src, dest)
  66. def _client(self):
  67. raise NotImplementedError()
  68. def _build_path(self):
  69. raise NotImplementedError()
  70. def _do_send_pack(self):
  71. c = self._client()
  72. srcpath = os.path.join(self.gitroot, 'server_new.export')
  73. src = repo.Repo(srcpath)
  74. sendrefs = dict(src.get_refs())
  75. del sendrefs['HEAD']
  76. c.send_pack(self._build_path('/dest'), lambda _: sendrefs,
  77. src.object_store.generate_pack_contents)
  78. def test_send_pack(self):
  79. self._do_send_pack()
  80. self.assertDestEqualsSrc()
  81. def test_send_pack_nothing_to_send(self):
  82. self._do_send_pack()
  83. self.assertDestEqualsSrc()
  84. # nothing to send, but shouldn't raise either.
  85. self._do_send_pack()
  86. def test_send_without_report_status(self):
  87. c = self._client()
  88. c._send_capabilities.remove('report-status')
  89. srcpath = os.path.join(self.gitroot, 'server_new.export')
  90. src = repo.Repo(srcpath)
  91. sendrefs = dict(src.get_refs())
  92. del sendrefs['HEAD']
  93. c.send_pack(self._build_path('/dest'), lambda _: sendrefs,
  94. src.object_store.generate_pack_contents)
  95. self.assertDestEqualsSrc()
  96. def disable_ff_and_make_dummy_commit(self):
  97. # disable non-fast-forward pushes to the server
  98. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  99. run_git_or_fail(['config', 'receive.denyNonFastForwards', 'true'],
  100. cwd=dest.path)
  101. b = objects.Blob.from_string('hi')
  102. dest.object_store.add_object(b)
  103. t = index.commit_tree(dest.object_store, [('hi', b.id, 0100644)])
  104. c = objects.Commit()
  105. c.author = c.committer = 'Foo Bar <foo@example.com>'
  106. c.author_time = c.commit_time = 0
  107. c.author_timezone = c.commit_timezone = 0
  108. c.message = 'hi'
  109. c.tree = t
  110. dest.object_store.add_object(c)
  111. return dest, c.id
  112. def compute_send(self):
  113. srcpath = os.path.join(self.gitroot, 'server_new.export')
  114. src = repo.Repo(srcpath)
  115. sendrefs = dict(src.get_refs())
  116. del sendrefs['HEAD']
  117. return sendrefs, src.object_store.generate_pack_contents
  118. def test_send_pack_one_error(self):
  119. dest, dummy_commit = self.disable_ff_and_make_dummy_commit()
  120. dest.refs['refs/heads/master'] = dummy_commit
  121. sendrefs, gen_pack = self.compute_send()
  122. c = self._client()
  123. try:
  124. c.send_pack(self._build_path('/dest'), lambda _: sendrefs, gen_pack)
  125. except errors.UpdateRefsError, e:
  126. self.assertEqual('refs/heads/master failed to update', str(e))
  127. self.assertEqual({'refs/heads/branch': 'ok',
  128. 'refs/heads/master': 'non-fast-forward'},
  129. e.ref_status)
  130. def test_send_pack_multiple_errors(self):
  131. dest, dummy = self.disable_ff_and_make_dummy_commit()
  132. # set up for two non-ff errors
  133. dest.refs['refs/heads/branch'] = dest.refs['refs/heads/master'] = dummy
  134. sendrefs, gen_pack = self.compute_send()
  135. c = self._client()
  136. try:
  137. c.send_pack(self._build_path('/dest'), lambda _: sendrefs, gen_pack)
  138. except errors.UpdateRefsError, e:
  139. self.assertEqual('refs/heads/branch, refs/heads/master failed to '
  140. 'update', str(e))
  141. self.assertEqual({'refs/heads/branch': 'non-fast-forward',
  142. 'refs/heads/master': 'non-fast-forward'},
  143. e.ref_status)
  144. def test_fetch_pack(self):
  145. c = self._client()
  146. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  147. refs = c.fetch(self._build_path('/server_new.export'), dest)
  148. map(lambda r: dest.refs.set_if_equals(r[0], None, r[1]), refs.items())
  149. self.assertDestEqualsSrc()
  150. def test_incremental_fetch_pack(self):
  151. self.test_fetch_pack()
  152. dest, dummy = self.disable_ff_and_make_dummy_commit()
  153. dest.refs['refs/heads/master'] = dummy
  154. c = self._client()
  155. dest = repo.Repo(os.path.join(self.gitroot, 'server_new.export'))
  156. refs = c.fetch(self._build_path('/dest'), dest)
  157. map(lambda r: dest.refs.set_if_equals(r[0], None, r[1]), refs.items())
  158. self.assertDestEqualsSrc()
  159. class DulwichTCPClientTest(CompatTestCase, DulwichClientTestBase):
  160. def setUp(self):
  161. CompatTestCase.setUp(self)
  162. DulwichClientTestBase.setUp(self)
  163. if check_for_daemon(limit=1):
  164. raise SkipTest('git-daemon was already running on port %s' %
  165. protocol.TCP_GIT_PORT)
  166. fd, self.pidfile = tempfile.mkstemp(prefix='dulwich-test-git-client',
  167. suffix=".pid")
  168. os.fdopen(fd).close()
  169. run_git_or_fail(
  170. ['daemon', '--verbose', '--export-all',
  171. '--pid-file=%s' % self.pidfile, '--base-path=%s' % self.gitroot,
  172. '--detach', '--reuseaddr', '--enable=receive-pack',
  173. '--listen=localhost', self.gitroot], cwd=self.gitroot)
  174. if not check_for_daemon():
  175. raise SkipTest('git-daemon failed to start')
  176. def tearDown(self):
  177. try:
  178. os.kill(int(open(self.pidfile).read().strip()), signal.SIGKILL)
  179. os.unlink(self.pidfile)
  180. except (OSError, IOError):
  181. pass
  182. DulwichClientTestBase.tearDown(self)
  183. CompatTestCase.tearDown(self)
  184. def _client(self):
  185. return client.TCPGitClient('localhost')
  186. def _build_path(self, path):
  187. return path
  188. class TestSSHVendor(object):
  189. @staticmethod
  190. def connect_ssh(host, command, username=None, port=None):
  191. cmd, path = command[0].replace("'", '').split(' ')
  192. cmd = cmd.split('-', 1)
  193. p = subprocess.Popen(cmd + [path], stdin=subprocess.PIPE,
  194. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  195. return client.SubprocessWrapper(p)
  196. class DulwichMockSSHClientTest(CompatTestCase, DulwichClientTestBase):
  197. def setUp(self):
  198. CompatTestCase.setUp(self)
  199. DulwichClientTestBase.setUp(self)
  200. self.real_vendor = client.get_ssh_vendor
  201. client.get_ssh_vendor = TestSSHVendor
  202. def tearDown(self):
  203. DulwichClientTestBase.tearDown(self)
  204. CompatTestCase.tearDown(self)
  205. client.get_ssh_vendor = self.real_vendor
  206. def _client(self):
  207. return client.SSHGitClient('localhost')
  208. def _build_path(self, path):
  209. return self.gitroot + path
  210. class DulwichSubprocessClientTest(CompatTestCase, DulwichClientTestBase):
  211. def setUp(self):
  212. CompatTestCase.setUp(self)
  213. DulwichClientTestBase.setUp(self)
  214. def tearDown(self):
  215. DulwichClientTestBase.tearDown(self)
  216. CompatTestCase.tearDown(self)
  217. def _client(self):
  218. return client.SubprocessGitClient()
  219. def _build_path(self, path):
  220. return self.gitroot + path
  221. class GitHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
  222. """HTTP Request handler that calls out to 'git http-backend'."""
  223. # Make rfile unbuffered -- we need to read one line and then pass
  224. # the rest to a subprocess, so we can't use buffered input.
  225. rbufsize = 0
  226. def do_POST(self):
  227. self.run_backend()
  228. def do_GET(self):
  229. self.run_backend()
  230. def send_head(self):
  231. return self.run_backend()
  232. def log_request(self, code='-', size='-'):
  233. # Let's be quiet, the test suite is noisy enough already
  234. pass
  235. def run_backend(self):
  236. """Call out to git http-backend."""
  237. # Based on CGIHTTPServer.CGIHTTPRequestHandler.run_cgi:
  238. # Copyright (c) 2001-2010 Python Software Foundation; All Rights Reserved
  239. # Licensed under the Python Software Foundation License.
  240. rest = self.path
  241. # find an explicit query string, if present.
  242. i = rest.rfind('?')
  243. if i >= 0:
  244. rest, query = rest[:i], rest[i+1:]
  245. else:
  246. query = ''
  247. env = copy.deepcopy(os.environ)
  248. env['SERVER_SOFTWARE'] = self.version_string()
  249. env['SERVER_NAME'] = self.server.server_name
  250. env['GATEWAY_INTERFACE'] = 'CGI/1.1'
  251. env['SERVER_PROTOCOL'] = self.protocol_version
  252. env['SERVER_PORT'] = str(self.server.server_port)
  253. env['GIT_PROJECT_ROOT'] = self.server.root_path
  254. env["GIT_HTTP_EXPORT_ALL"] = "1"
  255. env['REQUEST_METHOD'] = self.command
  256. uqrest = urllib.unquote(rest)
  257. env['PATH_INFO'] = uqrest
  258. env['SCRIPT_NAME'] = "/"
  259. if query:
  260. env['QUERY_STRING'] = query
  261. host = self.address_string()
  262. if host != self.client_address[0]:
  263. env['REMOTE_HOST'] = host
  264. env['REMOTE_ADDR'] = self.client_address[0]
  265. authorization = self.headers.getheader("authorization")
  266. if authorization:
  267. authorization = authorization.split()
  268. if len(authorization) == 2:
  269. import base64, binascii
  270. env['AUTH_TYPE'] = authorization[0]
  271. if authorization[0].lower() == "basic":
  272. try:
  273. authorization = base64.decodestring(authorization[1])
  274. except binascii.Error:
  275. pass
  276. else:
  277. authorization = authorization.split(':')
  278. if len(authorization) == 2:
  279. env['REMOTE_USER'] = authorization[0]
  280. # XXX REMOTE_IDENT
  281. if self.headers.typeheader is None:
  282. env['CONTENT_TYPE'] = self.headers.type
  283. else:
  284. env['CONTENT_TYPE'] = self.headers.typeheader
  285. length = self.headers.getheader('content-length')
  286. if length:
  287. env['CONTENT_LENGTH'] = length
  288. referer = self.headers.getheader('referer')
  289. if referer:
  290. env['HTTP_REFERER'] = referer
  291. accept = []
  292. for line in self.headers.getallmatchingheaders('accept'):
  293. if line[:1] in "\t\n\r ":
  294. accept.append(line.strip())
  295. else:
  296. accept = accept + line[7:].split(',')
  297. env['HTTP_ACCEPT'] = ','.join(accept)
  298. ua = self.headers.getheader('user-agent')
  299. if ua:
  300. env['HTTP_USER_AGENT'] = ua
  301. co = filter(None, self.headers.getheaders('cookie'))
  302. if co:
  303. env['HTTP_COOKIE'] = ', '.join(co)
  304. # XXX Other HTTP_* headers
  305. # Since we're setting the env in the parent, provide empty
  306. # values to override previously set values
  307. for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
  308. 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
  309. env.setdefault(k, "")
  310. self.send_response(200, "Script output follows")
  311. decoded_query = query.replace('+', ' ')
  312. try:
  313. nbytes = int(length)
  314. except (TypeError, ValueError):
  315. nbytes = 0
  316. if self.command.lower() == "post" and nbytes > 0:
  317. data = self.rfile.read(nbytes)
  318. else:
  319. data = None
  320. # throw away additional data [see bug #427345]
  321. while select.select([self.rfile._sock], [], [], 0)[0]:
  322. if not self.rfile._sock.recv(1):
  323. break
  324. args = ['-c', 'http.uploadpack=true', '-c', 'http.receivepack=true', 'http-backend']
  325. if '=' not in decoded_query:
  326. args.append(decoded_query)
  327. stdout = run_git_or_fail(args, input=data, env=env, stderr=subprocess.PIPE)
  328. self.wfile.write(stdout)
  329. class HTTPGitServer(BaseHTTPServer.HTTPServer):
  330. allow_reuse_address = True
  331. def __init__(self, server_address, root_path):
  332. BaseHTTPServer.HTTPServer.__init__(self, server_address, GitHTTPRequestHandler)
  333. self.root_path = root_path
  334. def get_url(self):
  335. return 'http://%s:%s/' % (self.server_name, self.server_port)
  336. if not getattr(HTTPGitServer, 'shutdown', None):
  337. _HTTPGitServer = HTTPGitServer
  338. class TCPGitServer(ShutdownServerMixIn, HTTPGitServer):
  339. """Subclass of HTTPGitServer that can be shut down."""
  340. def __init__(self, *args, **kwargs):
  341. # BaseServer is old-style so we have to call both __init__s
  342. ShutdownServerMixIn.__init__(self)
  343. _HTTPGitServer.__init__(self, *args, **kwargs)
  344. class DulwichHttpClientTest(CompatTestCase, DulwichClientTestBase):
  345. def setUp(self):
  346. CompatTestCase.setUp(self)
  347. DulwichClientTestBase.setUp(self)
  348. self._httpd = HTTPGitServer(("localhost", 0), self.gitroot)
  349. self.addCleanup(self._httpd.shutdown)
  350. threading.Thread(target=self._httpd.serve_forever).start()
  351. def tearDown(self):
  352. DulwichClientTestBase.tearDown(self)
  353. CompatTestCase.tearDown(self)
  354. def _client(self):
  355. ret, self._path = client.HttpGitClient.from_url(self._httpd.get_url())
  356. return ret
  357. def _build_path(self, path):
  358. return urlparse.urljoin(self._path.strip("/"), path.strip("/"))