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