From 717ff3b75bca054a7f14de43a6ef6fc0535d3953 Mon Sep 17 00:00:00 2001 From: Nima Talebi Date: Sat, 23 May 2009 18:39:30 +1000 Subject: Expanding the test case to include the POC demo The POC demo does not actually do much testing yet, other than just working or not working - but it's in place now for future enhancements. --- unit-tests/unit | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100755 unit-tests/unit (limited to 'unit-tests/unit') diff --git a/unit-tests/unit b/unit-tests/unit new file mode 100755 index 0000000..160641a --- /dev/null +++ b/unit-tests/unit @@ -0,0 +1,193 @@ +#!/usr/bin/env python +#.awk '$0 ~ /case [0-9]+: .. 3/ { sys.stdout.write($2 }' src/dmidecode.c|tr ':\n' ', ' + +from pprint import pprint +import os, sys, random, tempfile, time +import commands +import libxml2 +from POCDemo import POCDemo + +DUMPS_D = "private" + +def ascii(s, i): return "\033[%d;1m%s\033[0m"%(30+i, str(s)) +def black(s): return "\033[30;1m%s\033[0m"%(str(s)) +def red(s): return "\033[31;1m%s\033[0m"%(str(s)) +def green(s): return "\033[32;1m%s\033[0m"%(str(s)) +def yellow(s): return "\033[33;1m%s\033[0m"%(str(s)) +def blue(s): return "\033[34;1m%s\033[0m"%(str(s)) +def magenta(s): return "\033[35;1m%s\033[0m"%(str(s)) +def cyan(s): return "\033[36;1m%s\033[0m"%(str(s)) +def white(s): return "\033[37;1m%s\033[0m"%(str(s)) + +DISPATCH = { + 1 : red, + 2 : green, + 3 : yellow, + 4 : blue, + 5 : magenta, + 6 : cyan, + 7 : white, +} + +LINE = "%s\n"%(magenta("="*80)) + +score = { + "total" : 0, + "skipped" : 0, + "passed" : 0, + "failed" : 0, +} + +def passed(msg=None, indent=1): + global score + score["total"] += 1 + score["passed"] += 1 + sys.stdout.write("%s\n"%green("PASS")) + if msg: sys.stdout.write("%s %s %s\n"%(" "*indent, green("P"), msg)) +def skipped(msg=None, indent=1): + global score + score["total"] += 1 + score["skipped"] += 1 + sys.stdout.write("%s\n"%yellow("SKIP")) + if msg: sys.stdout.write("%s %s %s\n"%(" "*indent, yellow("S"), msg)) +def failed(msg=None, indent=1): + global score + score["total"] += 1 + score["failed"] += 1 + sys.stdout.write("%s\n"%red("FAIL")) + if msg: sys.stdout.write("%s %s %s\n"%(" "*indent, red("F"), msg)) +def test(r, msg=None, indent=1): + if r: + passed(msg, indent) + return True + else: + failed(msg, indent) + return False + +sys.stdout.write(LINE) +sys.stdout.write(" * Testing for dmidecode (upstream)...") +d = True in [os.path.exists(os.path.join(_, "dmidecode")) for _ in os.getenv("PATH").split(':')] +test(d) + +sys.stdout.write(" * Creation of temporary files...") +try: + FH, DUMP = tempfile.mkstemp() + os.unlink(DUMP) + os.close(FH) + passed() +except: + failed() + +sys.stdout.write(LINE) +sys.stdout.write(" * Importing module...") +try: + import dmidecode + passed() + sys.stdout.write(" * Version: %s\n"%blue(dmidecode.version)) + sys.stdout.write(" * DMI Version String: %s\n"%blue(dmidecode.dmi)) + + sys.stdout.write(" * Testing that default device is /dev/mem...") + test(dmidecode.get_dev() == "/dev/mem") + + sys.stdout.write(" * Testing that write-lock will not break on dump()...") + test(not dmidecode.dump()) + + sys.stdout.write(" * Testing ability to change device to %s..."%DUMP) + test(dmidecode.set_dev(DUMP)) + + sys.stdout.write(" * Testing that device has changed to %s..."%DUMP) + test(dmidecode.get_dev() == DUMP) + + sys.stdout.write(" * Testing that write on new file is ok...") + test(dmidecode.dump()) + + sys.stdout.write(" * Testing that file was actually written...") + time.sleep(0.1) + if test(os.path.exists(DUMP)): + os.unlink(DUMP) + + types = range(0, 42)+range(126, 128) + bad_types = [-1, -1000, 256] + sections = ["bios", "system", "baseboard", "chassis", "processor", "memory", "cache", "connector", "slot"] + devices = [] + if os.path.exists(DUMPS_D): + devices.extend([os.path.join(DUMPS_D, _) for _ in os.listdir(DUMPS_D)]) + else: + sys.stdout.write(" * If you have memory dumps to test, create a directory called `%s' and drop them in there.\n"%(DUMPS_D)) + devices.append("/dev/mem") + random.shuffle(types) + random.shuffle(devices) + random.shuffle(sections) + + for dev in devices: + sys.stdout.write(LINE) + sys.stdout.write(" * Testing %s..."%yellow(dev)); sys.stdout.flush() + try: + fH = open(dev, 'r') + fH.close() + passed() + sys.stdout.write(" * Testing set_dev/get_dev on %s..."%(yellow(dev))); sys.stdout.flush() + if test(dmidecode.set_dev(dev) and dmidecode.get_dev() == dev): + i = 0 + for section in sections: + i += 1 + sys.stdout.write(" * Testing %s (%d/%d)..."%(cyan(section), i, len(sections))); sys.stdout.flush() + try: + output = getattr(dmidecode, section)() + test(output is not False) + if output: + sys.stdout.write(" * %s\n"%black(output.keys())) + except LookupError, e: + failed(e, 2) + + for i in bad_types: + sys.stdout.write(" * Testing bad type %s..."%red(i)); sys.stdout.flush() + try: + output = dmidecode.type(i) + test(output is False) + except SystemError: + failed() + + for i in types: + sys.stdout.write(" * Testing type %s..."%red(i)); sys.stdout.flush() + try: + output = dmidecode.type(i) + if dmidecode: + _output = commands.getoutput("dmidecode -t %d"%i).strip().split('\n') + test(len(_output) == 1 and len(output) == 0 or True) + else: + test(output is not False) + if output: + sys.stdout.write(" * %s\n"%output.keys()) + except IOError, e: + failed(e, 2) + except IOError: + skipped() + +except ImportError: + failed() + + + +test = POCDemo() +print "Please note the dmixml_demo/@entrypoint attribute in the root node" +print +print "-------- xmlDoc ---------------" +xmldoc = test.GetXMLdoc() +xmldoc.saveFormatFileEnc("-", "UTF-8", 1) + +print +print "-------- xmlNode ---------------" +xmldoc2 = libxml2.newDoc("1.0") +xmlnode = test.GetXMLnode() +xmldoc2.setRootElement(xmlnode) +xmldoc2.saveFormatFileEnc("-", "UTF-8", 1) + + + +sys.stdout.write(LINE) +sys.stdout.write("Devices : %s\n"%cyan(len(devices))) +sys.stdout.write("Total : %s\n"%blue(score["total"])) +sys.stdout.write("Skipped : %s\n"%yellow(score["skipped"])) +sys.stdout.write("Passed : %s\n"%green(score["passed"])) +sys.stdout.write("Failed : %s\n"%red(score["failed"])) -- cgit From af27ba4dd2ffdef8e4ee3abf187475c1b3303f71 Mon Sep 17 00:00:00 2001 From: Nima Talebi Date: Sun, 24 May 2009 00:26:23 +1000 Subject: Completed preliminary reimplementation of type() Updated test unit to match. Throw an exception instead of returning None/False in some functions. --- unit-tests/unit | 2 ++ 1 file changed, 2 insertions(+) (limited to 'unit-tests/unit') diff --git a/unit-tests/unit b/unit-tests/unit index 160641a..61a194c 100755 --- a/unit-tests/unit +++ b/unit-tests/unit @@ -161,6 +161,8 @@ try: sys.stdout.write(" * %s\n"%output.keys()) except IOError, e: failed(e, 2) + except LookupError, e: + failed(e, 2) except IOError: skipped() -- cgit From 6ff73a634b11dacfd246ea8bdb6df3e51cda5118 Mon Sep 17 00:00:00 2001 From: Nima Talebi Date: Sun, 24 May 2009 04:09:43 +1000 Subject: Cleanup --- unit-tests/unit | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'unit-tests/unit') diff --git a/unit-tests/unit b/unit-tests/unit index 61a194c..0f8a51b 100755 --- a/unit-tests/unit +++ b/unit-tests/unit @@ -4,8 +4,6 @@ from pprint import pprint import os, sys, random, tempfile, time import commands -import libxml2 -from POCDemo import POCDemo DUMPS_D = "private" @@ -170,6 +168,9 @@ except ImportError: failed() +""" +import libxml2 +from POCDemo import POCDemo test = POCDemo() print "Please note the dmixml_demo/@entrypoint attribute in the root node" @@ -184,6 +185,7 @@ xmldoc2 = libxml2.newDoc("1.0") xmlnode = test.GetXMLnode() xmldoc2.setRootElement(xmlnode) xmldoc2.saveFormatFileEnc("-", "UTF-8", 1) +""" -- cgit From e9ae400e4dc70966bfd940c27f9a8b91d5caffa4 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Wed, 10 Jun 2009 16:48:51 +0200 Subject: Updated unit-test to check the XML API --- unit-tests/unit | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) (limited to 'unit-tests/unit') diff --git a/unit-tests/unit b/unit-tests/unit index 0f8a51b..b6018f9 100755 --- a/unit-tests/unit +++ b/unit-tests/unit @@ -79,7 +79,10 @@ except: sys.stdout.write(LINE) sys.stdout.write(" * Importing module...") try: + import libxml2 import dmidecode + import dmidecodeXML + passed() sys.stdout.write(" * Version: %s\n"%blue(dmidecode.version)) sys.stdout.write(" * DMI Version String: %s\n"%blue(dmidecode.dmi)) @@ -161,11 +164,79 @@ try: failed(e, 2) except LookupError, e: failed(e, 2) + + dmixml = dmidecodeXML.dmidecodeXML() + try: + sys.stdout.write(" * XML: Swapping result type dmidecodeXML::SetResultType('-')..."); + sys.stdout.flush() + test(not dmixml.SetResultType('-')) + except TypeError: + sys.stdout.write("Not working => ") + passed() + except: + sys.stdout.write("Accepted => ") + failed() + + try: + sys.stdout.write(" * XML: Swapping result type - dmidecodeXML::SetResultType(dmidecodeXML.DMIXML_DOC)..."); + sys.stdout.flush() + test(dmixml.SetResultType(dmidecodeXML.DMIXML_DOC)) + sys.stdout.write(" * XML: Swapping result type - dmidecodeXML::SetResultType(dmidecodeXML.DMIXML_NODE)..."); + sys.stdout.flush() + test(dmixml.SetResultType(dmidecodeXML.DMIXML_NODE)) + except: + failed() + + for i in bad_types: + sys.stdout.write(" * XML: Testing bad type - dmidecodeXML::QueryTypeId(%s)..." + % red(i)) + sys.stdout.flush() + try: + output_node = dmixml.QueryTypeId(i) + test(not isinstance(output_node, libxml2.xmlNode)) + except SystemError: + sys.stdout.write("Accepted => ") + failed() + except TypeError: + sys.stdout.write("Not working => ") + passed() + except ValueError: + sys.stdout.write("Not working => ") + passed() + + for i in types: + sys.stdout.write(" * XML: Testing dmidecodeXML::QueryTypeId(%s)..." + % red(i)) + sys.stdout.flush() + try: + output_node = dmixml.QueryTypeId(i) + test(isinstance(output_node, libxml2.xmlNode)) + except Exception, e: + failed(e, 2) + except: + failed() + + dmixml.SetResultType(dmidecodeXML.DMIXML_DOC) + i = 0 + for section in sections: + i += 1 + sys.stdout.write(" * XML: Testing dmidecodeXML::QuerySection('%s') (%d/%d)..." + % (cyan(section), i, len(sections))) + sys.stdout.flush() + try: + output_doc = dmixml.QuerySection(section) + test(isinstance(output_doc, libxml2.xmlDoc)) + except Exception, e: + failed(e, 2) + except: + failed() + except IOError: skipped() -except ImportError: +except ImportError, err: failed() + print err """ -- cgit From 65c9384ec9b6e265aba11227ffa37ae7a6a787d1 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Wed, 10 Jun 2009 18:57:42 +0200 Subject: Fixed import issues with dmidecode As we now include libxml2 and the required libxml2mod (which is used to wrap xmlDoc and xmlNode data into Python objects), importing only dmidecode caused a failure. If adding import libxml2 first, everything would work out fine. To avoid this issue, due to backwards compatibility, a tiny dmidecode wrapper is implemted as well. The dmidecode.so module is now renamed to dmidecodemodule.so, and the wrapper is now called dmidecode.py. To simplify things, the dmidecodeXML module introduced in commit b25b2ca548508cd2beb26f465b7bc5a296592461 is not merged into the new dmidecode.py The constants mentioned are now called dmidecode.DMIXML_NODE and dmidecode.DMIXML_DOC and to instantiate the dmidecodeXML class, dmidecode.dmidecodeXML() is used. --- unit-tests/unit | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'unit-tests/unit') diff --git a/unit-tests/unit b/unit-tests/unit index b6018f9..8156945 100755 --- a/unit-tests/unit +++ b/unit-tests/unit @@ -64,8 +64,8 @@ def test(r, msg=None, indent=1): sys.stdout.write(LINE) sys.stdout.write(" * Testing for dmidecode (upstream)...") -d = True in [os.path.exists(os.path.join(_, "dmidecode")) for _ in os.getenv("PATH").split(':')] -test(d) +dmidecode_bin = True in [os.path.exists(os.path.join(_, "dmidecode")) for _ in os.getenv("PATH").split(':')] +test(dmidecode_bin) sys.stdout.write(" * Creation of temporary files...") try: @@ -77,13 +77,12 @@ except: failed() sys.stdout.write(LINE) -sys.stdout.write(" * Importing module...") try: + sys.stdout.write(" * Importing module...") import libxml2 import dmidecode - import dmidecodeXML - passed() + sys.stdout.write(" * Version: %s\n"%blue(dmidecode.version)) sys.stdout.write(" * DMI Version String: %s\n"%blue(dmidecode.dmi)) @@ -153,7 +152,7 @@ try: sys.stdout.write(" * Testing type %s..."%red(i)); sys.stdout.flush() try: output = dmidecode.type(i) - if dmidecode: + if dmidecode_bin: _output = commands.getoutput("dmidecode -t %d"%i).strip().split('\n') test(len(_output) == 1 and len(output) == 0 or True) else: @@ -165,7 +164,7 @@ try: except LookupError, e: failed(e, 2) - dmixml = dmidecodeXML.dmidecodeXML() + dmixml = dmidecode.dmidecodeXML() try: sys.stdout.write(" * XML: Swapping result type dmidecodeXML::SetResultType('-')..."); sys.stdout.flush() @@ -178,12 +177,12 @@ try: failed() try: - sys.stdout.write(" * XML: Swapping result type - dmidecodeXML::SetResultType(dmidecodeXML.DMIXML_DOC)..."); + sys.stdout.write(" * XML: Swapping result type - dmidecodeXML::SetResultType(dmidecode.DMIXML_DOC)..."); sys.stdout.flush() - test(dmixml.SetResultType(dmidecodeXML.DMIXML_DOC)) - sys.stdout.write(" * XML: Swapping result type - dmidecodeXML::SetResultType(dmidecodeXML.DMIXML_NODE)..."); + test(dmixml.SetResultType(dmidecode.DMIXML_DOC)) + sys.stdout.write(" * XML: Swapping result type - dmidecodeXML::SetResultType(dmidecode.DMIXML_NODE)..."); sys.stdout.flush() - test(dmixml.SetResultType(dmidecodeXML.DMIXML_NODE)) + test(dmixml.SetResultType(dmidecode.DMIXML_NODE)) except: failed() @@ -216,7 +215,7 @@ try: except: failed() - dmixml.SetResultType(dmidecodeXML.DMIXML_DOC) + dmixml.SetResultType(dmidecode.DMIXML_DOC) i = 0 for section in sections: i += 1 -- cgit From 541ee10989640f028d777c9341c63573aac4a55f Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Wed, 10 Jun 2009 19:30:28 +0200 Subject: Discovered another issue with Python and imports The shard library got renamed to dmidecodemodule.so, and this was not clever. When you do 'import dmidecode' in Python, it will look for files in this order: dmidecode dmidecode.so dmidecodemodule.so dmidecode.py dmidecode.pyc This is of course a problem when the wrapper introduced in commit 65c9384ec9b6e265aba11227ffa37ae7a6a787d1 is called dmidecode.py, and Python attempts to load dmidecodemodule.so before dmidecode.py. To solve this, dmidecodemodule.so is now renamed to dmidecodemod.so. --- unit-tests/unit | 1 + 1 file changed, 1 insertion(+) (limited to 'unit-tests/unit') diff --git a/unit-tests/unit b/unit-tests/unit index 8156945..ac3edab 100755 --- a/unit-tests/unit +++ b/unit-tests/unit @@ -80,6 +80,7 @@ sys.stdout.write(LINE) try: sys.stdout.write(" * Importing module...") import libxml2 +# from dmidecodemod import * import dmidecode passed() -- cgit