1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7import os
8import sys
9from oeqa.selftest.case import OESelftestTestCase
10import tempfile
11import operator
12from oeqa.utils.commands import get_bb_var
13
14class TestBlobParsing(OESelftestTestCase):
15
16    def setUp(self):
17        self.repo_path = tempfile.mkdtemp(prefix='selftest-buildhistory',
18            dir=get_bb_var('TOPDIR'))
19
20        try:
21            from git import Repo
22            self.repo = Repo.init(self.repo_path)
23        except ImportError as e:
24            self.skipTest('Python module GitPython is not present (%s)  (%s)' % (e, sys.path))
25
26        self.test_file = "test"
27        self.var_map = {}
28
29    def tearDown(self):
30        import shutil
31        shutil.rmtree(self.repo_path)
32
33    @property
34    def heads_default(self):
35        """
36        Support repos defaulting to master or to main branch
37        """
38        try:
39            return self.repo.heads.main
40        except AttributeError:
41            return self.repo.heads.master
42
43    def commit_vars(self, to_add={}, to_remove = [], msg="A commit message"):
44        if len(to_add) == 0 and len(to_remove) == 0:
45            return
46
47        for k in to_remove:
48            self.var_map.pop(x,None)
49        for k in to_add:
50            self.var_map[k] = to_add[k]
51
52        with open(os.path.join(self.repo_path, self.test_file), 'w') as repo_file:
53            for k in self.var_map:
54                repo_file.write("%s = %s\n" % (k, self.var_map[k]))
55
56        self.repo.git.add("--all")
57        self.repo.git.commit(message=msg)
58
59    def test_blob_to_dict(self):
60        """
61        Test conversion of git blobs to dictionary
62        """
63        from oe.buildhistory_analysis import blob_to_dict
64        valuesmap = { "foo" : "1", "bar" : "2" }
65        self.commit_vars(to_add = valuesmap)
66
67        blob = self.repo.head.commit.tree.blobs[0]
68        self.assertEqual(valuesmap, blob_to_dict(blob),
69            "commit was not translated correctly to dictionary")
70
71    def test_compare_dict_blobs(self):
72        """
73        Test comparisson of dictionaries extracted from git blobs
74        """
75        from oe.buildhistory_analysis import compare_dict_blobs
76
77        changesmap = { "foo-2" : ("2", "8"), "bar" : ("","4"), "bar-2" : ("","5")}
78
79        self.commit_vars(to_add = { "foo" : "1", "foo-2" : "2", "foo-3" : "3" })
80        blob1 = self.heads_default.commit.tree.blobs[0]
81
82        self.commit_vars(to_add = { "foo-2" : "8", "bar" : "4", "bar-2" : "5" })
83        blob2 = self.heads_default.commit.tree.blobs[0]
84
85        change_records = compare_dict_blobs(os.path.join(self.repo_path, self.test_file),
86            blob1, blob2, False, False)
87
88        var_changes = { x.fieldname : (x.oldvalue, x.newvalue) for x in change_records}
89        self.assertEqual(changesmap, var_changes, "Changes not reported correctly")
90
91    def test_compare_dict_blobs_default(self):
92        """
93        Test default values for comparisson of git blob dictionaries
94        """
95        from oe.buildhistory_analysis import compare_dict_blobs
96        defaultmap = { x : ("default", "1")  for x in ["PKG", "PKGE", "PKGV", "PKGR"]}
97
98        self.commit_vars(to_add = { "foo" : "1" })
99        blob1 = self.heads_default.commit.tree.blobs[0]
100
101        self.commit_vars(to_add = { "PKG" : "1", "PKGE" : "1", "PKGV" : "1", "PKGR" : "1" })
102        blob2 = self.heads_default.commit.tree.blobs[0]
103
104        change_records = compare_dict_blobs(os.path.join(self.repo_path, self.test_file),
105            blob1, blob2, False, False)
106
107        var_changes = {}
108        for x in change_records:
109            oldvalue = "default" if ("default" in x.oldvalue) else x.oldvalue
110            var_changes[x.fieldname] = (oldvalue, x.newvalue)
111
112        self.assertEqual(defaultmap, var_changes, "Defaults not set properly")
113
114class TestFileListCompare(OESelftestTestCase):
115
116    def test_compare_file_lists(self):
117        # Test that a directory tree that moves location such as /lib/modules/5.4.40-yocto-standard -> /lib/modules/5.4.43-yocto-standard
118        # is correctly identified as a move
119        from oe.buildhistory_analysis import compare_file_lists, FileChange
120
121        with open(self.tc.files_dir + "/buildhistory_filelist1.txt", "r") as f:
122            filelist1 = f.readlines()
123        with open(self.tc.files_dir + "/buildhistory_filelist2.txt", "r") as f:
124            filelist2 = f.readlines()
125
126        expectedResult = [
127            '/lib/libcap.so.2 changed symlink target from libcap.so.2.33 to libcap.so.2.34',
128            '/lib/libcap.so.2.33 moved to /lib/libcap.so.2.34',
129            '/lib/modules/5.4.40-yocto-standard moved to /lib/modules/5.4.43-yocto-standard',
130            '/lib/modules/5.4.43-yocto-standard/modules.builtin.alias.bin was added',
131            '/usr/bin/gawk-5.0.1 moved to /usr/bin/gawk-5.1.0',
132            '/usr/lib/libbtrfsutil.so changed symlink target from libbtrfsutil.so.1.1.1 to libbtrfsutil.so.1.2.0',
133            '/usr/lib/libbtrfsutil.so.1 changed symlink target from libbtrfsutil.so.1.1.1 to libbtrfsutil.so.1.2.0',
134            '/usr/lib/libbtrfsutil.so.1.1.1 moved to /usr/lib/libbtrfsutil.so.1.2.0',
135            '/usr/lib/libkmod.so changed symlink target from libkmod.so.2.3.4 to libkmod.so.2.3.5',
136            '/usr/lib/libkmod.so.2 changed symlink target from libkmod.so.2.3.4 to libkmod.so.2.3.5',
137            '/usr/lib/libkmod.so.2.3.4 moved to /usr/lib/libkmod.so.2.3.5',
138            '/usr/lib/libpixman-1.so.0 changed symlink target from libpixman-1.so.0.38.4 to libpixman-1.so.0.40.0',
139            '/usr/lib/libpixman-1.so.0.38.4 moved to /usr/lib/libpixman-1.so.0.40.0',
140            '/usr/lib/opkg/alternatives/rtcwake was added',
141            '/usr/lib/python3.8/site-packages/PyGObject-3.34.0.egg-info moved to /usr/lib/python3.8/site-packages/PyGObject-3.36.1.egg-info',
142            '/usr/lib/python3.8/site-packages/btrfsutil-1.1.1-py3.8.egg-info moved to /usr/lib/python3.8/site-packages/btrfsutil-1.2.0-py3.8.egg-info',
143            '/usr/lib/python3.8/site-packages/pycairo-1.19.0.egg-info moved to /usr/lib/python3.8/site-packages/pycairo-1.19.1.egg-info',
144            '/usr/sbin/rtcwake changed type from file to symlink',
145            '/usr/sbin/rtcwake changed permissions from rwxr-xr-x to rwxrwxrwx',
146            '/usr/sbin/rtcwake changed symlink target from None to /usr/sbin/rtcwake.util-linux',
147            '/usr/sbin/rtcwake.util-linux was added'
148        ]
149
150        result = compare_file_lists(filelist1, filelist2)
151        rendered = []
152        for entry in sorted(result, key=operator.attrgetter("path")):
153            rendered.append(str(entry))
154
155        self.maxDiff = None
156        self.assertCountEqual(rendered, expectedResult)
157
158