summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/selftest/cases/gitarchivetests.py
blob: 71382089c124102116867e197afc0210ea59162a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

import os
import sys
basepath = os.path.abspath(os.path.dirname(__file__) + '/../../../../../')
lib_path = basepath + '/scripts/lib'
sys.path = sys.path + [lib_path]
import oeqa.utils.gitarchive as ga
from oeqa.utils.git import GitError
import tempfile
import shutil
import scriptutils
import logging
from oeqa.selftest.case import OESelftestTestCase

logger = scriptutils.logger_create('resulttool')

def create_fake_repository(commit, tag_list=[], add_remote=True):
    """ Create a testing git directory

    Initialize a simple git repository with one initial commit, and as many
    tags on this commit as listed in tag_list
    Returns both git directory path and gitarchive git object
    If commit is true, fake data will be commited, otherwise it will stay in staging area
    If commit is true and tag_lsit is non empty, all tags in tag_list will be
    created on the initial commit
    Fake remote will also be added to make git ls-remote work
    """
    fake_data_file = "fake_data.txt"
    tempdir = tempfile.mkdtemp(prefix='fake_results.')
    repo = ga.init_git_repo(tempdir, False, False, logger)
    if add_remote:
        repo.run_cmd(["remote", "add", "origin", "."])
    with open(os.path.join(tempdir, fake_data_file), "w") as fake_data:
        fake_data.write("Fake data")
    if commit:
        repo.run_cmd(["add", fake_data_file])
        repo.run_cmd(["commit", "-m", "\"Add fake data\""])
        for tag in tag_list:
            repo.run_cmd(["tag", tag])
    
    return tempdir, repo

def delete_fake_repository(path):
    shutil.rmtree(path)

def tag_exists(git_obj, target_tag):
    for tag in git_obj.run_cmd(["tag"]).splitlines():
        if target_tag == tag:
            return True
    return False

class GitArchiveTests(OESelftestTestCase):
    TEST_BRANCH="main"
    TEST_COMMIT="0f7d5df"
    TEST_COMMIT_COUNT="42"

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.log = logging.getLogger('gitarchivetests')
        cls.log.setLevel(logging.DEBUG)

    def test_create_first_test_tag(self):
        path, git_obj = create_fake_repository(False)
        keywords = {'commit': self.TEST_COMMIT, 'branch': self.TEST_BRANCH, "commit_count": self.TEST_COMMIT_COUNT}
        target_tag = f"{self.TEST_BRANCH}/{self.TEST_COMMIT_COUNT}-g{self.TEST_COMMIT}/0"

        ga.gitarchive(path, path, True, False,
                              "Results of {branch}:{commit}", "branch: {branch}\ncommit: {commit}", "{branch}",
                              False, "{branch}/{commit_count}-g{commit}/{tag_number}",
                              'Test run #{tag_number} of {branch}:{commit}', '',
                              [], [], False, keywords, logger)
        self.assertTrue(tag_exists(git_obj, target_tag), msg=f"Tag {target_tag} has not been created")
        delete_fake_repository(path)

    def test_create_second_test_tag(self):
        first_tag = f"{self.TEST_BRANCH}/{self.TEST_COMMIT_COUNT}-g{self.TEST_COMMIT}/0"
        second_tag = f"{self.TEST_BRANCH}/{self.TEST_COMMIT_COUNT}-g{self.TEST_COMMIT}/1"
        keywords = {'commit': self.TEST_COMMIT, 'branch': self.TEST_BRANCH, "commit_count": self.TEST_COMMIT_COUNT}

        path, git_obj = create_fake_repository(True, [first_tag])
        ga.gitarchive(path, path, True, False,
                              "Results of {branch}:{commit}", "branch: {branch}\ncommit: {commit}", "{branch}",
                              False, "{branch}/{commit_count}-g{commit}/{tag_number}",
                              'Test run #{tag_number} of {branch}:{commit}', '',
                              [], [], False, keywords, logger)
        self.assertTrue(tag_exists(git_obj, second_tag), msg=f"Second tag {second_tag} has not been created")
        delete_fake_repository(path)

    def test_get_revs_on_branch(self):
        fake_tags_list=["main/10-g0f7d5df/0", "main/10-g0f7d5df/1", "foo/20-g2468f5d/0"]
        tag_name = "{branch}/{commit_number}-g{commit}/{tag_number}"

        path, git_obj = create_fake_repository(True, fake_tags_list)
        revs = ga.get_test_revs(logger, git_obj, tag_name, branch="main")
        self.assertEqual(len(revs), 1)
        self.assertEqual(revs[0].commit, "0f7d5df")
        self.assertEqual(len(revs[0].tags), 2)
        self.assertEqual(revs[0].tags, ['main/10-g0f7d5df/0', 'main/10-g0f7d5df/1'])
        delete_fake_repository(path)

    def test_get_tags_without_valid_remote(self):
        url = 'git://git.yoctoproject.org/poky'
        path, git_obj = create_fake_repository(False, None, False)

        tags = ga.get_tags(git_obj, self.log, pattern="yocto-*", url=url)
        """Test for some well established tags (released tags)"""
        self.assertIn("yocto-4.0", tags)
        self.assertIn("yocto-4.1", tags)
        self.assertIn("yocto-4.2", tags)
        delete_fake_repository(path)

    def test_get_tags_with_only_local_tag(self):
        fake_tags_list=["main/10-g0f7d5df/0", "main/10-g0f7d5df/1", "foo/20-g2468f5d/0"]
        path, git_obj = create_fake_repository(True, fake_tags_list, False)

        """No remote is configured and no url is passed: get_tags must fall
        back to local tags
        """
        tags = ga.get_tags(git_obj, self.log)
        self.assertCountEqual(tags, fake_tags_list)
        delete_fake_repository(path)

    def test_get_tags_without_valid_remote_and_wrong_url(self):
        url = 'git://git.foo.org/bar'
        path, git_obj = create_fake_repository(False, None, False)

        """Test for some well established tags (released tags)"""
        with self.assertRaises(GitError):
            tags = ga.get_tags(git_obj, self.log, pattern="yocto-*", url=url)
        delete_fake_repository(path)