[PATCH] python-linux-procfs: bitmasklist_test.py Add the first unit test
by John Kacur
This adds the first unit test to python-linux-procfs
It adds the bitmasklist_test for testing the function bitmasklist, which
is called by parse_affinity. Unlike parse_affinity, the number of cpus
is not detected, so the unit test can simulate different numbers of
cpus.
This is an expanded form of a reproducer from Jozef Bacik from
Bugzilla 1365902
If we add more unit tests in the future (we should), then we can
consider some kind of python package scheme, but for now this can live
in the base directory.
Signed-off-by: John Kacur <jkacur(a)redhat.com>
---
bitmasklist_test.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 67 insertions(+)
create mode 100755 bitmasklist_test.py
diff --git a/bitmasklist_test.py b/bitmasklist_test.py
new file mode 100755
index 000000000000..4fa9cc41205b
--- /dev/null
+++ b/bitmasklist_test.py
@@ -0,0 +1,67 @@
+#!/usr/bin/python
+from procfs import bitmasklist
+
+class bitmasklist_test:
+ # Assume true (passed) until proven false
+ # Many tests can be run, but just one failure is recorded overall here
+ unit_test_result = 0; # Assume true (passed) until proven false
+
+ def __init__(self, line, nr_entries, expected_result):
+ self.result = 0; # Assume pass
+ self.line = line
+ self.nr_entries = nr_entries # Corresponds to the number of cpus
+ self.expected_result = expected_result
+
+ # A failure in any single test is recorded as an overall failure
+ def set_unit_test_result(self):
+ if bitmasklist_test.unit_test_result == 1:
+ return
+ if self.result == 1:
+ bitmasklist_test.unit_test_result = 1
+ return
+
+ # This is the function that actually runs the test
+ def bitmasklist_test(self):
+ print "\n##################\n"
+ cpu = bitmasklist(self.line, self.nr_entries)
+ print "Converted : ", self.line, "\nto ", cpu
+ if cpu == self.expected_result:
+ self.result = 0
+ print "PASS"
+ else:
+ self.result = 1
+ print "expected : ", self.expected_result
+ print "FAIL"
+ self.set_unit_test_result()
+
+# CPU 2
+t = \
+ bitmasklist_test("00000000,00000000,00000000,00000000,00000000,00000004", 44, [2])
+t.bitmasklist_test()
+
+# CPU 34
+t = \
+ bitmasklist_test("00000000,00000000,00000000,00000000,00000004,00000000", 44, [34])
+t.bitmasklist_test()
+
+# CPU 30
+t = \
+ bitmasklist_test("00000000,00000000,00000000,00000000,00000000,40000000", 44, [30])
+t.bitmasklist_test()
+
+# CPU 0, 32
+t = \
+ bitmasklist_test("00000000,00000000,00000000,00000000,00000001,00000001", 44, [0,32])
+t.bitmasklist_test()
+
+# cpu 0-15
+t = \
+ bitmasklist_test("ffff", 44, [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
+t.bitmasklist_test()
+
+#cpu 0-71
+t = \
+ bitmasklist_test("ff,ffffffff,ffffffff", 96, [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71])
+t.bitmasklist_test()
+
+exit(bitmasklist_test.unit_test_result)
--
2.4.11
7 years
[PATCH] python-linux-procfs: fix parse_affinity for CPU numbers greater than 31
by John Kacur
From: Jozef Bacik <jobacik(a)redhat.com>
The function parse_affinity reports wrong results for CPU numbers
greater than 31.
The problem is caused by the function bitmastlist which parse_affinity
calls. The fix treats the inpput line as a long hexbitmask instead of an
array in order to produce correct results
Signed-off-by: Jozef Bacik <jobacik(a)redhat.com>
Signed-off-by: John Kacur <jkacur(a)redhat.com>
---
procfs/utilist.py | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/procfs/utilist.py b/procfs/utilist.py
index 18645c0ba45e..0e7c24f45cda 100755
--- a/procfs/utilist.py
+++ b/procfs/utilist.py
@@ -37,18 +37,14 @@ def hexbitmask(l, nr_entries):
return hexbitmask
def bitmasklist(line, nr_entries):
- fields = line.strip().split(",")
+ hexmask = line.strip().replace(",", "")
bitmasklist = []
entry = 0
- for i in range(len(fields) - 1, -1, -1):
- mask = int(fields[i], 16)
- while mask != 0:
- if mask & 1:
- bitmasklist.append(entry)
- mask >>= 1
- entry += 1
- if entry == nr_entries:
- break
+ bitmask = bin(int(hexmask, 16))[2::]
+ for i in reversed(bitmask):
+ if int(i) & 1:
+ bitmasklist.append(entry)
+ entry +=1
if entry == nr_entries:
break
return bitmasklist
--
2.4.11
7 years