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