summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexis Lothoré <alexis.lothore@bootlin.com>2023-08-18 16:17:10 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2023-08-21 11:34:12 +0100
commitf30867795c6eddad62c4a97cc57ca4e766698890 (patch)
treef4d71b355b5f3efc49992c8a55be4f26dddddbb0
parent169b71c11fd45faa594b1c851f1e1a37daf332a2 (diff)
downloadpoky-f30867795c6eddad62c4a97cc57ca4e766698890.tar.gz
oeqa/selftest: introduce gitarchive tests
Add a test suite for gitarchive.py. For now, only introduce tests on methods which needs to read existing tags The tests rely on tmpdirs to create local, "fake" results repository in order to allow basic git commands (From OE-Core rev: bc9b8ef4bc3e51ba7fca372ab7e532a80975d819) Signed-off-by: Alexis Lothoré <alexis.lothore@bootlin.com> Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--meta/lib/oeqa/selftest/cases/gitarchivetests.py96
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
7import os
8import sys
9basepath = os.path.abspath(os.path.dirname(__file__) + '/../../../../../')
10lib_path = basepath + '/scripts/lib'
11sys.path = sys.path + [lib_path]
12import oeqa.utils.gitarchive as ga
13import tempfile
14import shutil
15import scriptutils
16from oeqa.selftest.case import OESelftestTestCase
17
18logger = scriptutils.logger_create('resulttool')
19
20def 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
45def delete_fake_repository(path):
46 shutil.rmtree(path)
47
48def 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
54class 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)