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