2
0

server_utils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # server_utils.py -- Git server compatibility utilities
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Utilities for testing git server compatibility."""
  22. import errno
  23. import os
  24. import shutil
  25. import socket
  26. import tempfile
  27. from dulwich.objects import hex_to_sha
  28. from dulwich.protocol import CAPABILITY_SIDE_BAND_64K
  29. from dulwich.repo import Repo
  30. from dulwich.server import ReceivePackHandler
  31. from dulwich.tests.utils import tear_down_repo
  32. from .utils import require_git_version, run_git_or_fail
  33. class _StubRepo:
  34. """A stub repo that just contains a path to tear down."""
  35. def __init__(self, name) -> None:
  36. temp_dir = tempfile.mkdtemp()
  37. self.path = os.path.join(temp_dir, name)
  38. os.mkdir(self.path)
  39. def close(self) -> None:
  40. pass
  41. def _get_shallow(repo):
  42. shallow_file = repo.get_named_file("shallow")
  43. if not shallow_file:
  44. return []
  45. shallows = []
  46. with shallow_file:
  47. for line in shallow_file:
  48. sha = line.strip()
  49. if not sha:
  50. continue
  51. hex_to_sha(sha)
  52. shallows.append(sha)
  53. return shallows
  54. class ServerTests:
  55. """Base tests for testing servers.
  56. Does not inherit from TestCase so tests are not automatically run.
  57. """
  58. min_single_branch_version = (
  59. 1,
  60. 7,
  61. 10,
  62. )
  63. def import_repos(self) -> None:
  64. self._old_repo = self.import_repo("server_old.export")
  65. self._new_repo = self.import_repo("server_new.export")
  66. def url(self, port) -> str:
  67. return f"{self.protocol}://localhost:{port}/"
  68. def branch_args(self, branches=None):
  69. if branches is None:
  70. branches = ["master", "branch"]
  71. return [f"{b}:{b}" for b in branches]
  72. def test_push_to_dulwich(self) -> None:
  73. self.import_repos()
  74. self.assertReposNotEqual(self._old_repo, self._new_repo)
  75. port = self._start_server(self._old_repo)
  76. run_git_or_fail(
  77. ["push", self.url(port), *self.branch_args()],
  78. cwd=self._new_repo.path,
  79. )
  80. self.assertReposEqual(self._old_repo, self._new_repo)
  81. def test_push_to_dulwich_no_op(self) -> None:
  82. self._old_repo = self.import_repo("server_old.export")
  83. self._new_repo = self.import_repo("server_old.export")
  84. self.assertReposEqual(self._old_repo, self._new_repo)
  85. port = self._start_server(self._old_repo)
  86. run_git_or_fail(
  87. ["push", self.url(port), *self.branch_args()],
  88. cwd=self._new_repo.path,
  89. )
  90. self.assertReposEqual(self._old_repo, self._new_repo)
  91. def test_push_to_dulwich_remove_branch(self) -> None:
  92. self._old_repo = self.import_repo("server_old.export")
  93. self._new_repo = self.import_repo("server_old.export")
  94. self.assertReposEqual(self._old_repo, self._new_repo)
  95. port = self._start_server(self._old_repo)
  96. run_git_or_fail(["push", self.url(port), ":master"], cwd=self._new_repo.path)
  97. self.assertEqual(list(self._old_repo.get_refs().keys()), [b"refs/heads/branch"])
  98. def test_fetch_from_dulwich(self) -> None:
  99. self.import_repos()
  100. self.assertReposNotEqual(self._old_repo, self._new_repo)
  101. port = self._start_server(self._new_repo)
  102. run_git_or_fail(
  103. ["fetch", self.url(port), *self.branch_args()],
  104. cwd=self._old_repo.path,
  105. )
  106. # flush the pack cache so any new packs are picked up
  107. self._old_repo.object_store._pack_cache_time = 0
  108. self.assertReposEqual(self._old_repo, self._new_repo)
  109. def test_fetch_from_dulwich_no_op(self) -> None:
  110. self._old_repo = self.import_repo("server_old.export")
  111. self._new_repo = self.import_repo("server_old.export")
  112. self.assertReposEqual(self._old_repo, self._new_repo)
  113. port = self._start_server(self._new_repo)
  114. run_git_or_fail(
  115. ["fetch", self.url(port), *self.branch_args()],
  116. cwd=self._old_repo.path,
  117. )
  118. # flush the pack cache so any new packs are picked up
  119. self._old_repo.object_store._pack_cache_time = 0
  120. self.assertReposEqual(self._old_repo, self._new_repo)
  121. def test_clone_from_dulwich_empty(self) -> None:
  122. old_repo_dir = tempfile.mkdtemp()
  123. self.addCleanup(shutil.rmtree, old_repo_dir)
  124. self._old_repo = Repo.init_bare(old_repo_dir)
  125. port = self._start_server(self._old_repo)
  126. new_repo_base_dir = tempfile.mkdtemp()
  127. self.addCleanup(shutil.rmtree, new_repo_base_dir)
  128. new_repo_dir = os.path.join(new_repo_base_dir, "empty_new")
  129. run_git_or_fail(["clone", self.url(port), new_repo_dir], cwd=new_repo_base_dir)
  130. new_repo = Repo(new_repo_dir)
  131. self.assertReposEqual(self._old_repo, new_repo)
  132. def test_lsremote_from_dulwich(self) -> None:
  133. self._repo = self.import_repo("server_old.export")
  134. port = self._start_server(self._repo)
  135. o = run_git_or_fail(["ls-remote", self.url(port)])
  136. self.assertEqual(len(o.split(b"\n")), 4)
  137. def test_new_shallow_clone_from_dulwich(self) -> None:
  138. require_git_version(self.min_single_branch_version)
  139. self._source_repo = self.import_repo("server_new.export")
  140. self._stub_repo = _StubRepo("shallow")
  141. self.addCleanup(tear_down_repo, self._stub_repo)
  142. port = self._start_server(self._source_repo)
  143. # Fetch at depth 1
  144. run_git_or_fail(
  145. [
  146. "clone",
  147. "--mirror",
  148. "--depth=1",
  149. "--no-single-branch",
  150. self.url(port),
  151. self._stub_repo.path,
  152. ]
  153. )
  154. clone = self._stub_repo = Repo(self._stub_repo.path)
  155. expected_shallow = [
  156. b"35e0b59e187dd72a0af294aedffc213eaa4d03ff",
  157. b"514dc6d3fbfe77361bcaef320c4d21b72bc10be9",
  158. ]
  159. self.assertEqual(expected_shallow, _get_shallow(clone))
  160. self.assertReposNotEqual(clone, self._source_repo)
  161. def test_shallow_clone_from_git_is_identical(self) -> None:
  162. require_git_version(self.min_single_branch_version)
  163. self._source_repo = self.import_repo("server_new.export")
  164. self._stub_repo_git = _StubRepo("shallow-git")
  165. self.addCleanup(tear_down_repo, self._stub_repo_git)
  166. self._stub_repo_dw = _StubRepo("shallow-dw")
  167. self.addCleanup(tear_down_repo, self._stub_repo_dw)
  168. # shallow clone using stock git, then using dulwich
  169. run_git_or_fail(
  170. [
  171. "clone",
  172. "--mirror",
  173. "--depth=1",
  174. "--no-single-branch",
  175. "file://" + self._source_repo.path,
  176. self._stub_repo_git.path,
  177. ]
  178. )
  179. port = self._start_server(self._source_repo)
  180. run_git_or_fail(
  181. [
  182. "clone",
  183. "--mirror",
  184. "--depth=1",
  185. "--no-single-branch",
  186. self.url(port),
  187. self._stub_repo_dw.path,
  188. ]
  189. )
  190. # compare the two clones; they should be equal
  191. self.assertReposEqual(
  192. Repo(self._stub_repo_git.path), Repo(self._stub_repo_dw.path)
  193. )
  194. def test_fetch_same_depth_into_shallow_clone_from_dulwich(self) -> None:
  195. require_git_version(self.min_single_branch_version)
  196. self._source_repo = self.import_repo("server_new.export")
  197. self._stub_repo = _StubRepo("shallow")
  198. self.addCleanup(tear_down_repo, self._stub_repo)
  199. port = self._start_server(self._source_repo)
  200. # Fetch at depth 2
  201. run_git_or_fail(
  202. [
  203. "clone",
  204. "--mirror",
  205. "--depth=2",
  206. "--no-single-branch",
  207. self.url(port),
  208. self._stub_repo.path,
  209. ]
  210. )
  211. clone = self._stub_repo = Repo(self._stub_repo.path)
  212. # Fetching at the same depth is a no-op.
  213. run_git_or_fail(
  214. ["fetch", "--depth=2", self.url(port), *self.branch_args()],
  215. cwd=self._stub_repo.path,
  216. )
  217. expected_shallow = [
  218. b"94de09a530df27ac3bb613aaecdd539e0a0655e1",
  219. b"da5cd81e1883c62a25bb37c4d1f8ad965b29bf8d",
  220. ]
  221. self.assertEqual(expected_shallow, _get_shallow(clone))
  222. self.assertReposNotEqual(clone, self._source_repo)
  223. def test_fetch_full_depth_into_shallow_clone_from_dulwich(self) -> None:
  224. require_git_version(self.min_single_branch_version)
  225. self._source_repo = self.import_repo("server_new.export")
  226. self._stub_repo = _StubRepo("shallow")
  227. self.addCleanup(tear_down_repo, self._stub_repo)
  228. port = self._start_server(self._source_repo)
  229. # Fetch at depth 2
  230. run_git_or_fail(
  231. [
  232. "clone",
  233. "--mirror",
  234. "--depth=2",
  235. "--no-single-branch",
  236. self.url(port),
  237. self._stub_repo.path,
  238. ]
  239. )
  240. clone = self._stub_repo = Repo(self._stub_repo.path)
  241. # Fetching at the same depth is a no-op.
  242. run_git_or_fail(
  243. ["fetch", "--depth=2", self.url(port), *self.branch_args()],
  244. cwd=self._stub_repo.path,
  245. )
  246. # The whole repo only has depth 4, so it should equal server_new.
  247. run_git_or_fail(
  248. ["fetch", "--depth=4", self.url(port), *self.branch_args()],
  249. cwd=self._stub_repo.path,
  250. )
  251. self.assertEqual([], _get_shallow(clone))
  252. self.assertReposEqual(clone, self._source_repo)
  253. def test_fetch_from_dulwich_issue_88_standard(self) -> None:
  254. # Basically an integration test to see that the ACK/NAK
  255. # generation works on repos with common head.
  256. self._source_repo = self.import_repo("issue88_expect_ack_nak_server.export")
  257. self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  258. port = self._start_server(self._source_repo)
  259. run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
  260. self.assertObjectStoreEqual(
  261. self._source_repo.object_store, self._client_repo.object_store
  262. )
  263. def test_fetch_from_dulwich_issue_88_alternative(self) -> None:
  264. # likewise, but the case where the two repos have no common parent
  265. self._source_repo = self.import_repo("issue88_expect_ack_nak_other.export")
  266. self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  267. port = self._start_server(self._source_repo)
  268. self.assertRaises(
  269. KeyError,
  270. self._client_repo.get_object,
  271. b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323",
  272. )
  273. run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
  274. self.assertEqual(
  275. b"commit",
  276. self._client_repo.get_object(
  277. b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323"
  278. ).type_name,
  279. )
  280. def test_push_to_dulwich_issue_88_standard(self) -> None:
  281. # Same thing, but we reverse the role of the server/client
  282. # and do a push instead.
  283. self._source_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  284. self._client_repo = self.import_repo("issue88_expect_ack_nak_server.export")
  285. port = self._start_server(self._source_repo)
  286. run_git_or_fail(["push", self.url(port), "master"], cwd=self._client_repo.path)
  287. self.assertReposEqual(self._source_repo, self._client_repo)
  288. # TODO(dborowitz): Come up with a better way of testing various permutations of
  289. # capabilities. The only reason it is the way it is now is that side-band-64k
  290. # was only recently introduced into git-receive-pack.
  291. class NoSideBand64kReceivePackHandler(ReceivePackHandler):
  292. """ReceivePackHandler that does not support side-band-64k."""
  293. @classmethod
  294. def capabilities(cls):
  295. return [
  296. c
  297. for c in ReceivePackHandler.capabilities()
  298. if c != CAPABILITY_SIDE_BAND_64K
  299. ]
  300. def ignore_error(error):
  301. """Check whether this error is safe to ignore."""
  302. (e_type, e_value, e_tb) = error
  303. return issubclass(e_type, socket.error) and e_value[0] in (
  304. errno.ECONNRESET,
  305. errno.EPIPE,
  306. )