summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHumberto Ibarra <humberto.ibarra.lopez@intel.com>2017-02-27 10:49:09 -0600
committerRichard Purdie <richard.purdie@linuxfoundation.org>2017-03-01 23:27:10 +0000
commitdf55b06cebd5d990bedc06a4c4ff59e0d7d84c54 (patch)
tree06edcff3a535b689bd2e173dde5c14288289fae0
parent661c73b716aced4728659ac02ab63c83fa232c8c (diff)
downloadpoky-df55b06cebd5d990bedc06a4c4ff59e0d7d84c54.tar.gz
oelib/buildhistory.py: Add unittest for buildhistory_analysis
The buildhistory_analysis module (in which buildhistory-diff is based) was lacking unittest for its functions. Created selftest module for this and a few testcases to cover basic cases. [YOCTO #10727] (From OE-Core rev: d868816ecb470d59216eecc97ef5d42261625cc1) Signed-off-by: Humberto Ibarra <humberto.ibarra.lopez@intel.com> Signed-off-by: Ross Burton <ross.burton@intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--meta/lib/oeqa/selftest/oelib/buildhistory.py88
1 files changed, 88 insertions, 0 deletions
diff --git a/meta/lib/oeqa/selftest/oelib/buildhistory.py b/meta/lib/oeqa/selftest/oelib/buildhistory.py
new file mode 100644
index 0000000000..5ed4b026fe
--- /dev/null
+++ b/meta/lib/oeqa/selftest/oelib/buildhistory.py
@@ -0,0 +1,88 @@
1import os
2import unittest
3import tempfile
4from git import Repo
5from oeqa.utils.commands import get_bb_var
6from oe.buildhistory_analysis import blob_to_dict, compare_dict_blobs
7
8class TestBlobParsing(unittest.TestCase):
9
10 def setUp(self):
11 import time
12 self.repo_path = tempfile.mkdtemp(prefix='selftest-buildhistory',
13 dir=get_bb_var('TOPDIR'))
14
15 self.repo = Repo.init(self.repo_path)
16 self.test_file = "test"
17 self.var_map = {}
18
19 def tearDown(self):
20 import shutil
21 shutil.rmtree(self.repo_path)
22
23 def commit_vars(self, to_add={}, to_remove = [], msg="A commit message"):
24 if len(to_add) == 0 and len(to_remove) == 0:
25 return
26
27 for k in to_remove:
28 self.var_map.pop(x,None)
29 for k in to_add:
30 self.var_map[k] = to_add[k]
31
32 with open(os.path.join(self.repo_path, self.test_file), 'w') as repo_file:
33 for k in self.var_map:
34 repo_file.write("%s = %s\n" % (k, self.var_map[k]))
35
36 self.repo.git.add("--all")
37 self.repo.git.commit(message=msg)
38
39 def test_blob_to_dict(self):
40 """
41 Test convertion of git blobs to dictionary
42 """
43 valuesmap = { "foo" : "1", "bar" : "2" }
44 self.commit_vars(to_add = valuesmap)
45
46 blob = self.repo.head.commit.tree.blobs[0]
47 self.assertEqual(valuesmap, blob_to_dict(blob),
48 "commit was not translated correctly to dictionary")
49
50 def test_compare_dict_blobs(self):
51 """
52 Test comparisson of dictionaries extracted from git blobs
53 """
54 changesmap = { "foo-2" : ("2", "8"), "bar" : ("","4"), "bar-2" : ("","5")}
55
56 self.commit_vars(to_add = { "foo" : "1", "foo-2" : "2", "foo-3" : "3" })
57 blob1 = self.repo.heads.master.commit.tree.blobs[0]
58
59 self.commit_vars(to_add = { "foo-2" : "8", "bar" : "4", "bar-2" : "5" })
60 blob2 = self.repo.heads.master.commit.tree.blobs[0]
61
62 change_records = compare_dict_blobs(os.path.join(self.repo_path, self.test_file),
63 blob1, blob2, False, False)
64
65 var_changes = { x.fieldname : (x.oldvalue, x.newvalue) for x in change_records}
66 self.assertEqual(changesmap, var_changes, "Changes not reported correctly")
67
68 def test_compare_dict_blobs_default(self):
69 """
70 Test default values for comparisson of git blob dictionaries
71 """
72 defaultmap = { x : ("default", "1") for x in ["PKG", "PKGE", "PKGV", "PKGR"]}
73
74 self.commit_vars(to_add = { "foo" : "1" })
75 blob1 = self.repo.heads.master.commit.tree.blobs[0]
76
77 self.commit_vars(to_add = { "PKG" : "1", "PKGE" : "1", "PKGV" : "1", "PKGR" : "1" })
78 blob2 = self.repo.heads.master.commit.tree.blobs[0]
79
80 change_records = compare_dict_blobs(os.path.join(self.repo_path, self.test_file),
81 blob1, blob2, False, False)
82
83 var_changes = {}
84 for x in change_records:
85 oldvalue = "default" if ("default" in x.oldvalue) else x.oldvalue
86 var_changes[x.fieldname] = (oldvalue, x.newvalue)
87
88 self.assertEqual(defaultmap, var_changes, "Defaults not set properly")