test_swift_smoke.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 unittest
  34. import tempfile
  35. import shutil
  36. import gevent
  37. from gevent import monkey
  38. monkey.patch_all()
  39. from dulwich import ( # noqa:E402
  40. server,
  41. repo,
  42. index,
  43. client,
  44. objects,
  45. )
  46. from dulwich.contrib import swift # noqa:E402
  47. class DulwichServer:
  48. """Start the TCPGitServer with Swift backend"""
  49. def __init__(self, backend, port):
  50. self.port = port
  51. self.backend = backend
  52. def run(self):
  53. self.server = server.TCPGitServer(self.backend, "localhost", port=self.port)
  54. self.job = gevent.spawn(self.server.serve_forever)
  55. def stop(self):
  56. self.server.shutdown()
  57. gevent.joinall((self.job,))
  58. class SwiftSystemBackend(server.Backend):
  59. def open_repository(self, path):
  60. return swift.SwiftRepo(path, conf=swift.load_conf())
  61. class SwiftRepoSmokeTest(unittest.TestCase):
  62. @classmethod
  63. def setUpClass(cls):
  64. cls.backend = SwiftSystemBackend()
  65. cls.port = 9148
  66. cls.server_address = "localhost"
  67. cls.fakerepo = "fakerepo"
  68. cls.th_server = DulwichServer(cls.backend, cls.port)
  69. cls.th_server.run()
  70. cls.conf = swift.load_conf()
  71. @classmethod
  72. def tearDownClass(cls):
  73. cls.th_server.stop()
  74. def setUp(self):
  75. self.scon = swift.SwiftConnector(self.fakerepo, self.conf)
  76. if self.scon.test_root_exists():
  77. try:
  78. self.scon.del_root()
  79. except swift.SwiftException:
  80. pass
  81. self.temp_d = tempfile.mkdtemp()
  82. if os.path.isdir(self.temp_d):
  83. shutil.rmtree(self.temp_d)
  84. def tearDown(self):
  85. if self.scon.test_root_exists():
  86. try:
  87. self.scon.del_root()
  88. except swift.SwiftException:
  89. pass
  90. if os.path.isdir(self.temp_d):
  91. shutil.rmtree(self.temp_d)
  92. def test_init_bare(self):
  93. swift.SwiftRepo.init_bare(self.scon, self.conf)
  94. self.assertTrue(self.scon.test_root_exists())
  95. obj = self.scon.get_container_objects()
  96. filtered = [
  97. o for o in obj if o["name"] == "info/refs" or o["name"] == "objects/pack"
  98. ]
  99. self.assertEqual(len(filtered), 2)
  100. def test_clone_bare(self):
  101. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  102. swift.SwiftRepo.init_bare(self.scon, self.conf)
  103. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  104. remote_refs = tcp_client.fetch(self.fakerepo, local_repo)
  105. # The remote repo is empty (no refs retrieved)
  106. self.assertEqual(remote_refs, None)
  107. def test_push_commit(self):
  108. def determine_wants(*args, **kwargs):
  109. return {"refs/heads/master": local_repo.refs["HEAD"]}
  110. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  111. # Nothing in the staging area
  112. local_repo.do_commit("Test commit", "fbo@localhost")
  113. sha = local_repo.refs.read_loose_ref("refs/heads/master")
  114. swift.SwiftRepo.init_bare(self.scon, self.conf)
  115. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  116. tcp_client.send_pack(
  117. self.fakerepo, determine_wants, local_repo.generate_pack_data
  118. )
  119. swift_repo = swift.SwiftRepo("fakerepo", self.conf)
  120. remote_sha = swift_repo.refs.read_loose_ref("refs/heads/master")
  121. self.assertEqual(sha, remote_sha)
  122. def test_push_branch(self):
  123. def determine_wants(*args, **kwargs):
  124. return {"refs/heads/mybranch": local_repo.refs["refs/heads/mybranch"]}
  125. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  126. # Nothing in the staging area
  127. local_repo.do_commit("Test commit", "fbo@localhost", ref="refs/heads/mybranch")
  128. sha = local_repo.refs.read_loose_ref("refs/heads/mybranch")
  129. swift.SwiftRepo.init_bare(self.scon, self.conf)
  130. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  131. tcp_client.send_pack(
  132. "/fakerepo", determine_wants, local_repo.generate_pack_data
  133. )
  134. swift_repo = swift.SwiftRepo(self.fakerepo, self.conf)
  135. remote_sha = swift_repo.refs.read_loose_ref("refs/heads/mybranch")
  136. self.assertEqual(sha, remote_sha)
  137. def test_push_multiple_branch(self):
  138. def determine_wants(*args, **kwargs):
  139. return {
  140. "refs/heads/mybranch": local_repo.refs["refs/heads/mybranch"],
  141. "refs/heads/master": local_repo.refs["refs/heads/master"],
  142. "refs/heads/pullr-108": local_repo.refs["refs/heads/pullr-108"],
  143. }
  144. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  145. # Nothing in the staging area
  146. local_shas = {}
  147. remote_shas = {}
  148. for branch in ("master", "mybranch", "pullr-108"):
  149. local_shas[branch] = local_repo.do_commit(
  150. "Test commit %s" % branch,
  151. "fbo@localhost",
  152. ref="refs/heads/%s" % branch,
  153. )
  154. swift.SwiftRepo.init_bare(self.scon, self.conf)
  155. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  156. tcp_client.send_pack(
  157. self.fakerepo, determine_wants, local_repo.generate_pack_data
  158. )
  159. swift_repo = swift.SwiftRepo("fakerepo", self.conf)
  160. for branch in ("master", "mybranch", "pullr-108"):
  161. remote_shas[branch] = swift_repo.refs.read_loose_ref(
  162. "refs/heads/%s" % branch
  163. )
  164. self.assertDictEqual(local_shas, remote_shas)
  165. def test_push_data_branch(self):
  166. def determine_wants(*args, **kwargs):
  167. return {"refs/heads/master": local_repo.refs["HEAD"]}
  168. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  169. os.mkdir(os.path.join(self.temp_d, "dir"))
  170. files = ("testfile", "testfile2", "dir/testfile3")
  171. i = 0
  172. for f in files:
  173. open(os.path.join(self.temp_d, f), "w").write("DATA %s" % i)
  174. i += 1
  175. local_repo.stage(files)
  176. local_repo.do_commit("Test commit", "fbo@localhost", ref="refs/heads/master")
  177. swift.SwiftRepo.init_bare(self.scon, self.conf)
  178. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  179. tcp_client.send_pack(
  180. self.fakerepo, determine_wants, local_repo.generate_pack_data
  181. )
  182. swift_repo = swift.SwiftRepo("fakerepo", self.conf)
  183. commit_sha = swift_repo.refs.read_loose_ref("refs/heads/master")
  184. otype, data = swift_repo.object_store.get_raw(commit_sha)
  185. commit = objects.ShaFile.from_raw_string(otype, data)
  186. otype, data = swift_repo.object_store.get_raw(commit._tree)
  187. tree = objects.ShaFile.from_raw_string(otype, data)
  188. objs = tree.items()
  189. objs_ = []
  190. for tree_entry in objs:
  191. objs_.append(swift_repo.object_store.get_raw(tree_entry.sha))
  192. # Blob
  193. self.assertEqual(objs_[1][1], "DATA 0")
  194. self.assertEqual(objs_[2][1], "DATA 1")
  195. # Tree
  196. self.assertEqual(objs_[0][0], 2)
  197. def test_clone_then_push_data(self):
  198. self.test_push_data_branch()
  199. shutil.rmtree(self.temp_d)
  200. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  201. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  202. remote_refs = tcp_client.fetch(self.fakerepo, local_repo)
  203. files = (
  204. os.path.join(self.temp_d, "testfile"),
  205. os.path.join(self.temp_d, "testfile2"),
  206. )
  207. local_repo["HEAD"] = remote_refs["refs/heads/master"]
  208. indexfile = local_repo.index_path()
  209. tree = local_repo["HEAD"].tree
  210. index.build_index_from_tree(
  211. local_repo.path, indexfile, local_repo.object_store, tree
  212. )
  213. for f in files:
  214. self.assertEqual(os.path.isfile(f), True)
  215. def determine_wants(*args, **kwargs):
  216. return {"refs/heads/master": local_repo.refs["HEAD"]}
  217. os.mkdir(os.path.join(self.temp_d, "test"))
  218. files = ("testfile11", "testfile22", "test/testfile33")
  219. i = 0
  220. for f in files:
  221. open(os.path.join(self.temp_d, f), "w").write("DATA %s" % i)
  222. i += 1
  223. local_repo.stage(files)
  224. local_repo.do_commit("Test commit", "fbo@localhost", ref="refs/heads/master")
  225. tcp_client.send_pack(
  226. "/fakerepo", determine_wants, local_repo.generate_pack_data
  227. )
  228. def test_push_remove_branch(self):
  229. def determine_wants(*args, **kwargs):
  230. return {
  231. "refs/heads/pullr-108": objects.ZERO_SHA,
  232. "refs/heads/master": local_repo.refs["refs/heads/master"],
  233. "refs/heads/mybranch": local_repo.refs["refs/heads/mybranch"],
  234. }
  235. self.test_push_multiple_branch()
  236. local_repo = repo.Repo(self.temp_d)
  237. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  238. tcp_client.send_pack(
  239. self.fakerepo, determine_wants, local_repo.generate_pack_data
  240. )
  241. swift_repo = swift.SwiftRepo("fakerepo", self.conf)
  242. self.assertNotIn("refs/heads/pullr-108", swift_repo.refs.allkeys())
  243. def test_push_annotated_tag(self):
  244. def determine_wants(*args, **kwargs):
  245. return {
  246. "refs/heads/master": local_repo.refs["HEAD"],
  247. "refs/tags/v1.0": local_repo.refs["refs/tags/v1.0"],
  248. }
  249. local_repo = repo.Repo.init(self.temp_d, mkdir=True)
  250. # Nothing in the staging area
  251. sha = local_repo.do_commit("Test commit", "fbo@localhost")
  252. otype, data = local_repo.object_store.get_raw(sha)
  253. commit = objects.ShaFile.from_raw_string(otype, data)
  254. tag = objects.Tag()
  255. tag.tagger = "fbo@localhost"
  256. tag.message = "Annotated tag"
  257. tag.tag_timezone = objects.parse_timezone("-0200")[0]
  258. tag.tag_time = commit.author_time
  259. tag.object = (objects.Commit, commit.id)
  260. tag.name = "v0.1"
  261. local_repo.object_store.add_object(tag)
  262. local_repo.refs["refs/tags/v1.0"] = tag.id
  263. swift.SwiftRepo.init_bare(self.scon, self.conf)
  264. tcp_client = client.TCPGitClient(self.server_address, port=self.port)
  265. tcp_client.send_pack(
  266. self.fakerepo, determine_wants, local_repo.generate_pack_data
  267. )
  268. swift_repo = swift.SwiftRepo(self.fakerepo, self.conf)
  269. tag_sha = swift_repo.refs.read_loose_ref("refs/tags/v1.0")
  270. otype, data = swift_repo.object_store.get_raw(tag_sha)
  271. rtag = objects.ShaFile.from_raw_string(otype, data)
  272. self.assertEqual(rtag.object[1], commit.id)
  273. self.assertEqual(rtag.id, tag.id)
  274. if __name__ == "__main__":
  275. unittest.main()