blob: 08675fd8208803786ab0661c01097d4bc4c94494 [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001import os
2from oeqa.selftest.case import OESelftestTestCase
3import tempfile
4from oeqa.utils.commands import get_bb_var
5from oeqa.core.decorator.oeid import OETestID
6
7class TestBlobParsing(OESelftestTestCase):
8
9 def setUp(self):
10 import time
11 self.repo_path = tempfile.mkdtemp(prefix='selftest-buildhistory',
12 dir=get_bb_var('TOPDIR'))
13
14 try:
15 from git import Repo
16 self.repo = Repo.init(self.repo_path)
17 except ImportError:
18 self.skipTest('Python module GitPython is not present')
19
20 self.test_file = "test"
21 self.var_map = {}
22
23 def tearDown(self):
24 import shutil
25 shutil.rmtree(self.repo_path)
26
27 def commit_vars(self, to_add={}, to_remove = [], msg="A commit message"):
28 if len(to_add) == 0 and len(to_remove) == 0:
29 return
30
31 for k in to_remove:
32 self.var_map.pop(x,None)
33 for k in to_add:
34 self.var_map[k] = to_add[k]
35
36 with open(os.path.join(self.repo_path, self.test_file), 'w') as repo_file:
37 for k in self.var_map:
38 repo_file.write("%s = %s\n" % (k, self.var_map[k]))
39
40 self.repo.git.add("--all")
41 self.repo.git.commit(message=msg)
42
43 @OETestID(1859)
44 def test_blob_to_dict(self):
45 """
46 Test convertion of git blobs to dictionary
47 """
48 from oe.buildhistory_analysis import blob_to_dict
49 valuesmap = { "foo" : "1", "bar" : "2" }
50 self.commit_vars(to_add = valuesmap)
51
52 blob = self.repo.head.commit.tree.blobs[0]
53 self.assertEqual(valuesmap, blob_to_dict(blob),
54 "commit was not translated correctly to dictionary")
55
56 @OETestID(1860)
57 def test_compare_dict_blobs(self):
58 """
59 Test comparisson of dictionaries extracted from git blobs
60 """
61 from oe.buildhistory_analysis import compare_dict_blobs
62
63 changesmap = { "foo-2" : ("2", "8"), "bar" : ("","4"), "bar-2" : ("","5")}
64
65 self.commit_vars(to_add = { "foo" : "1", "foo-2" : "2", "foo-3" : "3" })
66 blob1 = self.repo.heads.master.commit.tree.blobs[0]
67
68 self.commit_vars(to_add = { "foo-2" : "8", "bar" : "4", "bar-2" : "5" })
69 blob2 = self.repo.heads.master.commit.tree.blobs[0]
70
71 change_records = compare_dict_blobs(os.path.join(self.repo_path, self.test_file),
72 blob1, blob2, False, False)
73
74 var_changes = { x.fieldname : (x.oldvalue, x.newvalue) for x in change_records}
75 self.assertEqual(changesmap, var_changes, "Changes not reported correctly")
76
77 @OETestID(1861)
78 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")