blob: 6d8082765234b174cde1d9f914fc41b817089e58 [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
6from oeqa.selftest.case import OESelftestTestCase
7import tempfile
8from oeqa.utils.commands import get_bb_var
Brad Bishopd7bf8c12018-02-25 22:55:05 -05009
10class TestBlobParsing(OESelftestTestCase):
11
12 def setUp(self):
13 import time
14 self.repo_path = tempfile.mkdtemp(prefix='selftest-buildhistory',
15 dir=get_bb_var('TOPDIR'))
16
17 try:
18 from git import Repo
19 self.repo = Repo.init(self.repo_path)
20 except ImportError:
21 self.skipTest('Python module GitPython is not present')
22
23 self.test_file = "test"
24 self.var_map = {}
25
26 def tearDown(self):
27 import shutil
28 shutil.rmtree(self.repo_path)
29
30 def commit_vars(self, to_add={}, to_remove = [], msg="A commit message"):
31 if len(to_add) == 0 and len(to_remove) == 0:
32 return
33
34 for k in to_remove:
35 self.var_map.pop(x,None)
36 for k in to_add:
37 self.var_map[k] = to_add[k]
38
39 with open(os.path.join(self.repo_path, self.test_file), 'w') as repo_file:
40 for k in self.var_map:
41 repo_file.write("%s = %s\n" % (k, self.var_map[k]))
42
43 self.repo.git.add("--all")
44 self.repo.git.commit(message=msg)
45
Brad Bishopd7bf8c12018-02-25 22:55:05 -050046 def test_blob_to_dict(self):
47 """
48 Test convertion of git blobs to dictionary
49 """
50 from oe.buildhistory_analysis import blob_to_dict
51 valuesmap = { "foo" : "1", "bar" : "2" }
52 self.commit_vars(to_add = valuesmap)
53
54 blob = self.repo.head.commit.tree.blobs[0]
55 self.assertEqual(valuesmap, blob_to_dict(blob),
56 "commit was not translated correctly to dictionary")
57
Brad Bishopd7bf8c12018-02-25 22:55:05 -050058 def test_compare_dict_blobs(self):
59 """
60 Test comparisson of dictionaries extracted from git blobs
61 """
62 from oe.buildhistory_analysis import compare_dict_blobs
63
64 changesmap = { "foo-2" : ("2", "8"), "bar" : ("","4"), "bar-2" : ("","5")}
65
66 self.commit_vars(to_add = { "foo" : "1", "foo-2" : "2", "foo-3" : "3" })
67 blob1 = self.repo.heads.master.commit.tree.blobs[0]
68
69 self.commit_vars(to_add = { "foo-2" : "8", "bar" : "4", "bar-2" : "5" })
70 blob2 = self.repo.heads.master.commit.tree.blobs[0]
71
72 change_records = compare_dict_blobs(os.path.join(self.repo_path, self.test_file),
73 blob1, blob2, False, False)
74
75 var_changes = { x.fieldname : (x.oldvalue, x.newvalue) for x in change_records}
76 self.assertEqual(changesmap, var_changes, "Changes not reported correctly")
77
Brad Bishopd7bf8c12018-02-25 22:55:05 -050078 def test_compare_dict_blobs_default(self):
79 """
80 Test default values for comparisson of git blob dictionaries
81 """
82 from oe.buildhistory_analysis import compare_dict_blobs
83 defaultmap = { x : ("default", "1") for x in ["PKG", "PKGE", "PKGV", "PKGR"]}
84
85 self.commit_vars(to_add = { "foo" : "1" })
86 blob1 = self.repo.heads.master.commit.tree.blobs[0]
87
88 self.commit_vars(to_add = { "PKG" : "1", "PKGE" : "1", "PKGV" : "1", "PKGR" : "1" })
89 blob2 = self.repo.heads.master.commit.tree.blobs[0]
90
91 change_records = compare_dict_blobs(os.path.join(self.repo_path, self.test_file),
92 blob1, blob2, False, False)
93
94 var_changes = {}
95 for x in change_records:
96 oldvalue = "default" if ("default" in x.oldvalue) else x.oldvalue
97 var_changes[x.fieldname] = (oldvalue, x.newvalue)
98
99 self.assertEqual(defaultmap, var_changes, "Defaults not set properly")