test_swift_smoke.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # test_smoke.py -- Functional tests for the Swift backend.
  2. # Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
  3. #
  4. # Author: Fabien Boucher <fabien.boucher@enovance.com>
  5. #
  6. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  7. # General Public License as public by the Free Software Foundation; version 2.0
  8. # or (at your option) any later version. You can redistribute it and/or
  9. # modify it under the terms of either of these two licenses.
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # You should have received a copy of the licenses; if not, see
  18. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  19. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  20. # License, Version 2.0.
  21. #
  22. """Start functional tests.
  23. A Swift installation must be available before
  24. starting those tests. The account and authentication method used
  25. during this functional tests must be changed in the configuration file
  26. passed as environment variable.
  27. The container used to create a fake repository is defined
  28. in cls.fakerepo and will be deleted after the tests.
  29. DULWICH_SWIFT_CFG=/tmp/conf.cfg PYTHONPATH=. python -m unittest \
  30. dulwich.tests_swift.test_smoke
  31. """
  32. import os
  33. import shutil
  34. import tempfile
  35. import unittest
  36. import gevent
  37. from gevent import monkey
  38. monkey.patch_all()
  39. from dulwich import client, index, objects, repo, server # noqa: E402
  40. from dulwich.contrib import swift # noqa: E402
  41. class DulwichServer:
  42. """Start the TCPGitServer with Swift backend."""
  43. def __init__(self, backend, port) -> None:
  44. self.port = port
  45. self.backend = backend
  46. def run(self):
  47. self.server = server.TCPGitServer(self.backend, "localhost", port=self.port)
  48. self.job = gevent.spawn(self.server.serve_forever)
  49. def stop(self):
  50. self.server.shutdown()
  51. gevent.joinall((self.job,))
  52. class SwiftSystemBackend(server.Backend):
  53. def open_repository(self, path):
  54. return swift.SwiftRepo(path, conf=swift.load_conf())
  55. class SwiftRepoSmokeTest(unittest.TestCase):
  56. @classmethod
  57. def setUpClass(cls):
  58. cls.backend = SwiftSystemBackend()
  59. cls.port = 9148
  60. cls.server_address = "localhost"
  61. cls.fakerepo = "fakerepo"
  62. cls.th_server = DulwichServer(cls.backend, cls.port)
  63. cls.th_server.run()
  64. cls.conf = swift.load_conf()
  65. @classmethod
  66. def tearDownClass(cls):
  67. cls.th_server.stop()
  68. def setUp(self):
  69. self.scon = swift.SwiftConnector(self.fakerepo, self.conf)
  70. if self.scon.test_root_exists():
  71. try:
  72. self.scon.del_root()
  73. except swift.SwiftException:
  74. pass
  75. self.temp_d = tempfile.mkdtemp()
  76. if os.path.isdir(self.temp_d):
  77. shutil.rmtree(self.temp_d)
  78. def tearDown(self):
  79. if self.scon.test_root_exists():
  80. try:
  81. self.scon.del_root()
  82. except swift.SwiftException:
  83. pass
  84. if os.path.isdir(self.temp_d):
  85. shutil.rmtree(self.temp_d)
  86. def test_init_bare(self):
  87. swift.SwiftRepo.init_bare(self.scon, self.conf)
  88. self.assertTrue(self.scon.test_root_exists())
  89. obj = self.scon.get_container_objects()
  90. filtered = [
  91. o for o in obj if o["name"] == "info/refs" or o["name"] == "objects/pack"
  92. ]
  93. self.assertEqual(len(filtered), 2)
  94. def test_clone_bare(self):
  95. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  96. swift.SwiftRepo.init_bare(self.scon, self.conf)
  97. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  98. remote_refs = tcp_client.fetch(self.fakerepo, local_repo)
  99. # The remote repo is empty (no refs retrieved)
  100. self.assertEqual(remote_refs, None)
  101. def test_push_commit(self):
  102. def determine_wants(*args, **kwargs):
  103. return {"refs/heads/master": local_repo.refs["HEAD"]}
  104. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  105. # Nothing in the staging area
  106. local_repo.do_commit("Test commit", "fbo@localhost")
  107. sha = local_repo.refs.read_loose_ref("refs/heads/master")
  108. swift.SwiftRepo.init_bare(self.scon, self.conf)
  109. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  110. tcp_client.send_pack(
  111. self.fakerepo, determine_wants, local_repo.generate_pack_data
  112. )
  113. swift_repo = swift.SwiftRepo("fakerepo", self.conf)
  114. remote_sha = swift_repo.refs.read_loose_ref("refs/heads/master")
  115. self.assertEqual(sha, remote_sha)
  116. def test_push_branch(self):
  117. def determine_wants(*args, **kwargs):
  118. return {"refs/heads/mybranch": local_repo.refs["refs/heads/mybranch"]}
  119. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  120. # Nothing in the staging area
  121. local_repo.do_commit("Test commit", "fbo@localhost", ref="refs/heads/mybranch")
  122. sha = local_repo.refs.read_loose_ref("refs/heads/mybranch")
  123. swift.SwiftRepo.init_bare(self.scon, self.conf)
  124. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  125. tcp_client.send_pack(
  126. "/fakerepo", determine_wants, local_repo.generate_pack_data
  127. )
  128. swift_repo = swift.SwiftRepo(self.fakerepo, self.conf)
  129. remote_sha = swift_repo.refs.read_loose_ref("refs/heads/mybranch")
  130. self.assertEqual(sha, remote_sha)
  131. def test_push_multiple_branch(self):
  132. def determine_wants(*args, **kwargs):
  133. return {
  134. "refs/heads/mybranch": local_repo.refs["refs/heads/mybranch"],
  135. "refs/heads/master": local_repo.refs["refs/heads/master"],
  136. "refs/heads/pullr-108": local_repo.refs["refs/heads/pullr-108"],
  137. }
  138. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  139. # Nothing in the staging area
  140. local_shas = {}
  141. remote_shas = {}
  142. for branch in ("master", "mybranch", "pullr-108"):
  143. local_shas[branch] = local_repo.do_commit(
  144. f"Test commit {branch}",
  145. "fbo@localhost",
  146. ref=f"refs/heads/{branch}",
  147. )
  148. swift.SwiftRepo.init_bare(self.scon, self.conf)
  149. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  150. tcp_client.send_pack(
  151. self.fakerepo, determine_wants, local_repo.generate_pack_data
  152. )
  153. swift_repo = swift.SwiftRepo("fakerepo", self.conf)
  154. for branch in ("master", "mybranch", "pullr-108"):
  155. remote_shas[branch] = swift_repo.refs.read_loose_ref(f"refs/heads/{branch}")
  156. self.assertDictEqual(local_shas, remote_shas)
  157. def test_push_data_branch(self):
  158. def determine_wants(*args, **kwargs):
  159. return {"refs/heads/master": local_repo.refs["HEAD"]}
  160. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  161. os.mkdir(os.path.join(self.temp_d, "dir"))
  162. files = ("testfile", "testfile2", "dir/testfile3")
  163. i = 0
  164. for f in files:
  165. open(os.path.join(self.temp_d, f), "w").write(f"DATA {i}")
  166. i += 1
  167. local_repo.stage(files)
  168. local_repo.do_commit("Test commit", "fbo@localhost", ref="refs/heads/master")
  169. swift.SwiftRepo.init_bare(self.scon, self.conf)
  170. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  171. tcp_client.send_pack(
  172. self.fakerepo, determine_wants, local_repo.generate_pack_data
  173. )
  174. swift_repo = swift.SwiftRepo("fakerepo", self.conf)
  175. commit_sha = swift_repo.refs.read_loose_ref("refs/heads/master")
  176. otype, data = swift_repo.object_store.get_raw(commit_sha)
  177. commit = objects.ShaFile.from_raw_string(otype, data)
  178. otype, data = swift_repo.object_store.get_raw(commit._tree)
  179. tree = objects.ShaFile.from_raw_string(otype, data)
  180. objs = tree.items()
  181. objs_ = []
  182. for tree_entry in objs:
  183. objs_.append(swift_repo.object_store.get_raw(tree_entry.sha))
  184. # Blob
  185. self.assertEqual(objs_[1][1], "DATA 0")
  186. self.assertEqual(objs_[2][1], "DATA 1")
  187. # Tree
  188. self.assertEqual(objs_[0][0], 2)
  189. def test_clone_then_push_data(self):
  190. self.test_push_data_branch()
  191. shutil.rmtree(self.temp_d)
  192. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  193. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  194. remote_refs = tcp_client.fetch(self.fakerepo, local_repo)
  195. files = (
  196. os.path.join(self.temp_d, "testfile"),
  197. os.path.join(self.temp_d, "testfile2"),
  198. )
  199. local_repo["HEAD"] = remote_refs["refs/heads/master"]
  200. indexfile = local_repo.index_path()
  201. tree = local_repo["HEAD"].tree
  202. index.build_index_from_tree(
  203. local_repo.path, indexfile, local_repo.object_store, tree
  204. )
  205. for f in files:
  206. self.assertEqual(os.path.isfile(f), True)
  207. def determine_wants(*args, **kwargs):
  208. return {"refs/heads/master": local_repo.refs["HEAD"]}
  209. os.mkdir(os.path.join(self.temp_d, "test"))
  210. files = ("testfile11", "testfile22", "test/testfile33")
  211. i = 0
  212. for f in files:
  213. open(os.path.join(self.temp_d, f), "w").write(f"DATA {i}")
  214. i += 1
  215. local_repo.stage(files)
  216. local_repo.do_commit("Test commit", "fbo@localhost", ref="refs/heads/master")
  217. tcp_client.send_pack(
  218. "/fakerepo", determine_wants, local_repo.generate_pack_data
  219. )
  220. def test_push_remove_branch(self):
  221. def determine_wants(*args, **kwargs):
  222. return {
  223. "refs/heads/pullr-108": objects.ZERO_SHA,
  224. "refs/heads/master": local_repo.refs["refs/heads/master"],
  225. "refs/heads/mybranch": local_repo.refs["refs/heads/mybranch"],
  226. }
  227. self.test_push_multiple_branch()
  228. local_repo = repo.Repo(self.temp_d)
  229. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  230. tcp_client.send_pack(
  231. self.fakerepo, determine_wants, local_repo.generate_pack_data
  232. )
  233. swift_repo = swift.SwiftRepo("fakerepo", self.conf)
  234. self.assertNotIn("refs/heads/pullr-108", swift_repo.refs.allkeys())
  235. def test_push_annotated_tag(self):
  236. def determine_wants(*args, **kwargs):
  237. return {
  238. "refs/heads/master": local_repo.refs["HEAD"],
  239. "refs/tags/v1.0": local_repo.refs["refs/tags/v1.0"],
  240. }
  241. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  242. # Nothing in the staging area
  243. sha = local_repo.do_commit("Test commit", "fbo@localhost")
  244. otype, data = local_repo.object_store.get_raw(sha)
  245. commit = objects.ShaFile.from_raw_string(otype, data)
  246. tag = objects.Tag()
  247. tag.tagger = "fbo@localhost"
  248. tag.message = "Annotated tag"
  249. tag.tag_timezone = objects.parse_timezone("-0200")[0]
  250. tag.tag_time = commit.author_time
  251. tag.object = (objects.Commit, commit.id)
  252. tag.name = "v0.1"
  253. local_repo.object_store.add_object(tag)
  254. local_repo.refs["refs/tags/v1.0"] = tag.id
  255. swift.SwiftRepo.init_bare(self.scon, self.conf)
  256. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  257. tcp_client.send_pack(
  258. self.fakerepo, determine_wants, local_repo.generate_pack_data
  259. )
  260. swift_repo = swift.SwiftRepo(self.fakerepo, self.conf)
  261. tag_sha = swift_repo.refs.read_loose_ref("refs/tags/v1.0")
  262. otype, data = swift_repo.object_store.get_raw(tag_sha)
  263. rtag = objects.ShaFile.from_raw_string(otype, data)
  264. self.assertEqual(rtag.object[1], commit.id)
  265. self.assertEqual(rtag.id, tag.id)
  266. if __name__ == "__main__":
  267. unittest.main()