gitarchivetests.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. import os
  7. import sys
  8. basepath = os.path.abspath(os.path.dirname(__file__) + '/../../../../../')
  9. lib_path = basepath + '/scripts/lib'
  10. sys.path = sys.path + [lib_path]
  11. import oeqa.utils.gitarchive as ga
  12. from oeqa.utils.git import GitError
  13. import tempfile
  14. import shutil
  15. import scriptutils
  16. from oeqa.selftest.case import OESelftestTestCase
  17. logger = scriptutils.logger_create('resulttool')
  18. def create_fake_repository(commit, tag_list=[], add_remote=True):
  19. """ Create a testing git directory
  20. Initialize a simple git repository with one initial commit, and as many
  21. tags on this commit as listed in tag_list
  22. Returns both git directory path and gitarchive git object
  23. If commit is true, fake data will be commited, otherwise it will stay in staging area
  24. If commit is true and tag_lsit is non empty, all tags in tag_list will be
  25. created on the initial commit
  26. Fake remote will also be added to make git ls-remote work
  27. """
  28. fake_data_file = "fake_data.txt"
  29. tempdir = tempfile.mkdtemp(prefix='fake_results.')
  30. repo = ga.init_git_repo(tempdir, False, False, logger)
  31. if add_remote:
  32. repo.run_cmd(["remote", "add", "origin", "."])
  33. with open(os.path.join(tempdir, fake_data_file), "w") as fake_data:
  34. fake_data.write("Fake data")
  35. if commit:
  36. repo.run_cmd(["add", fake_data_file])
  37. repo.run_cmd(["commit", "-m", "\"Add fake data\""])
  38. for tag in tag_list:
  39. repo.run_cmd(["tag", tag])
  40. return tempdir, repo
  41. def delete_fake_repository(path):
  42. shutil.rmtree(path)
  43. def tag_exists(git_obj, target_tag):
  44. for tag in git_obj.run_cmd(["tag"]).splitlines():
  45. if target_tag == tag:
  46. return True
  47. return False
  48. class GitArchiveTests(OESelftestTestCase):
  49. TEST_BRANCH="main"
  50. TEST_COMMIT="0f7d5df"
  51. TEST_COMMIT_COUNT="42"
  52. def test_create_first_test_tag(self):
  53. path, git_obj = create_fake_repository(False)
  54. keywords = {'commit': self.TEST_COMMIT, 'branch': self.TEST_BRANCH, "commit_count": self.TEST_COMMIT_COUNT}
  55. target_tag = f"{self.TEST_BRANCH}/{self.TEST_COMMIT_COUNT}-g{self.TEST_COMMIT}/0"
  56. ga.gitarchive(path, path, True, False,
  57. "Results of {branch}:{commit}", "branch: {branch}\ncommit: {commit}", "{branch}",
  58. False, "{branch}/{commit_count}-g{commit}/{tag_number}",
  59. 'Test run #{tag_number} of {branch}:{commit}', '',
  60. [], [], False, keywords, logger)
  61. self.assertTrue(tag_exists(git_obj, target_tag), msg=f"Tag {target_tag} has not been created")
  62. delete_fake_repository(path)
  63. def test_create_second_test_tag(self):
  64. first_tag = f"{self.TEST_BRANCH}/{self.TEST_COMMIT_COUNT}-g{self.TEST_COMMIT}/0"
  65. second_tag = f"{self.TEST_BRANCH}/{self.TEST_COMMIT_COUNT}-g{self.TEST_COMMIT}/1"
  66. keywords = {'commit': self.TEST_COMMIT, 'branch': self.TEST_BRANCH, "commit_count": self.TEST_COMMIT_COUNT}
  67. path, git_obj = create_fake_repository(True, [first_tag])
  68. ga.gitarchive(path, path, True, False,
  69. "Results of {branch}:{commit}", "branch: {branch}\ncommit: {commit}", "{branch}",
  70. False, "{branch}/{commit_count}-g{commit}/{tag_number}",
  71. 'Test run #{tag_number} of {branch}:{commit}', '',
  72. [], [], False, keywords, logger)
  73. self.assertTrue(tag_exists(git_obj, second_tag), msg=f"Second tag {second_tag} has not been created")
  74. delete_fake_repository(path)
  75. def test_get_revs_on_branch(self):
  76. fake_tags_list=["main/10-g0f7d5df/0", "main/10-g0f7d5df/1", "foo/20-g2468f5d/0"]
  77. tag_name = "{branch}/{commit_number}-g{commit}/{tag_number}"
  78. path, git_obj = create_fake_repository(True, fake_tags_list)
  79. revs = ga.get_test_revs(logger, git_obj, tag_name, branch="main")
  80. self.assertEqual(len(revs), 1)
  81. self.assertEqual(revs[0].commit, "0f7d5df")
  82. self.assertEqual(len(revs[0].tags), 2)
  83. self.assertEqual(revs[0].tags, ['main/10-g0f7d5df/0', 'main/10-g0f7d5df/1'])
  84. delete_fake_repository(path)
  85. def test_get_tags_without_valid_remote(self):
  86. url = 'git://git.yoctoproject.org/poky'
  87. path, git_obj = create_fake_repository(False, None, False)
  88. tags = ga.get_tags(git_obj, pattern="yocto-*", url=url)
  89. """Test for some well established tags (released tags)"""
  90. self.assertIn("yocto-4.0", tags)
  91. self.assertIn("yocto-4.1", tags)
  92. self.assertIn("yocto-4.2", tags)
  93. delete_fake_repository(path)
  94. def test_get_tags_without_valid_remote_neither_url(self):
  95. url = 'git://git.yoctoproject.org/poky'
  96. path, git_obj = create_fake_repository(False, None, False)
  97. """Test for some well established tags (released tags)"""
  98. with self.assertRaises(GitError):
  99. tags = ga.get_tags(git_obj, pattern="yocto-*")
  100. delete_fake_repository(path)
  101. def test_get_tags_without_valid_remote_and_wrong_url(self):
  102. url = 'git://git.foo.org/bar'
  103. path, git_obj = create_fake_repository(False, None, False)
  104. """Test for some well established tags (released tags)"""
  105. with self.assertRaises(GitError):
  106. tags = ga.get_tags(git_obj, pattern="yocto-*", url=url)
  107. delete_fake_repository(path)