gitarchivetests.py 5.7 KB

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