From 70e1cdcd01322ddc202503bf19fdfd9708239a63 Mon Sep 17 00:00:00 2001 From: Douglas Clowes Date: Wed, 21 May 2014 12:27:22 +1000 Subject: [PATCH] Push test_suite, sicsconfig and hipadaba down to base class setup --- .../TEST_SICS/unit_tests/sics_test.py | 146 ++++++++++-------- 1 file changed, 79 insertions(+), 67 deletions(-) diff --git a/site_ansto/instrument/TEST_SICS/unit_tests/sics_test.py b/site_ansto/instrument/TEST_SICS/unit_tests/sics_test.py index 6d441da8..1f5acaa4 100644 --- a/site_ansto/instrument/TEST_SICS/unit_tests/sics_test.py +++ b/site_ansto/instrument/TEST_SICS/unit_tests/sics_test.py @@ -15,7 +15,6 @@ from twisted.internet import reactor, protocol, defer import ConfigParser, StringIO from datetime import datetime as dt - class Able(unittest.TestCase): def setUp(self): #print "Connecting ...", @@ -24,7 +23,17 @@ class Able(unittest.TestCase): self.deferred = defer.Deferred() def cb_login(client, *args, **kw): #print "cb_login:", client, args, kw - self.deferred.callback(True) + if 'test_suite' not in globals(): + d = self.load_test_suite() + d.addCallback(cb_login) + elif 'sicsconfig' not in globals(): + d = self.load_sicsconfig() + d.addCallback(cb_login) + elif 'hipadaba' not in globals(): + d = self.load_hipadaba() + d.addCallback(cb_login) + else: + self.deferred.callback(True) def cb_connect(client, *args, **kw): #print "cb_connect:", client, client.login, args, kw self.sics = client @@ -41,8 +50,44 @@ class Able(unittest.TestCase): #print "Command:", command, d return d + def load_test_suite(self): + debug = False + def cb1(result, *args, **kw): + global test_suite + if debug: + print "fileeval:", result, args, kw + if result[0] != "OK": + raise Exception("fileeval returned " + repr(result)) + test_suite = True + d = self.send_command("fileeval TEST_SICS/unit_tests/test_suite.tcl") + d.addCallback(cb1) + return d + + def load_sicsconfig(self): + debug = False + def cb2(result, *args, **kw): + global sicsconfig + with open("sicsconfig.ini", "w") as outfile: + for line in result: + outfile.write(line + '\n') + if debug: + print "config:", result, args, kw + ini = StringIO.StringIO("\n".join(result)) + if debug: + print "ini:", ini.readlines() + ini.seek(0) + print "ini:", ini.read() + ini.seek(0) + print "ini:", dir(ini) + sicsconfig = ConfigParser.SafeConfigParser() + sicsconfig.readfp(ini) + d = self.send_command("tcl:test_suite::show_config ::config_dict") + d.addCallback(cb2) + return d + def load_hipadaba(self): def cb0(result, *args, **kw): + global hipadaba with open("hipadaba.xml", "w") as outfile: for line in result: outfile.write(line + '\n') @@ -51,11 +96,10 @@ class Able(unittest.TestCase): if result[-1] != '': raise Exception("Tree does not end with :" + result[-1]) self.hipadaba = HipadabaTree('\n'.join(result)) - self.tree_deferred.callback(self.hipadaba) - self.tree_deferred = defer.Deferred() + hipadaba = self.hipadaba d = self.send_command("tcl:test_suite::getsicsxml /") d.addCallback(cb0) - return self.tree_deferred + return d def tearDown(self): #print "tearDownModule" @@ -70,60 +114,34 @@ class Baker(Able): timeout = 5 def test_000_000_login(self): debug = False - self.deferred = defer.Deferred() def cb0(result, *args, **kw): if debug: print "Login:", result, args, kw if self.sics.login != 2: raise Exception("Bad login:" + repr(self.sics.login)) - self.deferred.callback(None) d = self.sics.login_deferred d.addCallback(cb0) - return self.deferred + return d def test_000_001_fileeval(self): - debug = False - self.deferred = defer.Deferred() - def cb1(result, *args, **kw): - if debug: - print "fileeval:", result, args, kw - if result[0] != "OK": - raise Exception("fileeval returned " + repr(result)) - self.deferred.callback(None) - d = self.send_command("fileeval TEST_SICS/unit_tests/test_suite.tcl") - d.addCallback(cb1) - return self.deferred + self.assertTrue('test_suite' in globals()) + self.assertTrue(test_suite) - def test_001_000_config(self): + def test_000_002_config(self): debug = False - self.deferred = defer.Deferred() - def cb2(result, *args, **kw): - global config - if debug: - print "config:", result, args, kw - ini = StringIO.StringIO("\n".join(result)) - if debug: - print "ini:", ini.readlines() - ini.seek(0) - print "ini:", ini.read() - ini.seek(0) - print "ini:", dir(ini) - config = ConfigParser.SafeConfigParser() - if debug: - print "config:", repr(config) - config.readfp(ini) - if debug: - print "config:", repr(config) - print "config:", dir(config) - for section in sorted(config.sections()): - print ("[%s]\n" % section), - for option in sorted(config.options(section)): - print ("%s = %s\n" % (option, config.get(section, option))), - print - self.deferred.callback(None) - d = self.send_command("tcl:test_suite::show_config ::config_dict") - d.addCallback(cb2) - return self.deferred + self.assertTrue('sicsconfig' in globals()) + if debug: + print "config:", repr(sicsconfig) + print "config:", dir(sicsconfig) + for section in sorted(sicsconfig.sections()): + print ("[%s]\n" % section), + for option in sorted(sicsconfig.options(section)): + print ("%s = %s\n" % (option, sicsconfig.get(section, option))), + print + + def test_000_003_hipadaba(self): + debug = False + self.assertTrue('hipadaba' in globals()) def test_001_001_clock(self): debug = False @@ -148,14 +166,8 @@ class Baker(Able): return self.deferred def test_001_002_hipadaba(self): - self.deferred = defer.Deferred() - def cb3(result, *args, **kw): - global hipadaba - hipadaba = result - self.deferred.callback(None) - d = self.load_hipadaba() - d.addCallback(cb3) - return self.deferred + debug = False + self.assertTrue('hipadaba' in globals()) def test_001_003_status(self): d = self.send_command("status") @@ -252,8 +264,8 @@ class Baker(Able): self.assertTrue('control' in id_list) def test_004_002_gumtreexml(self): - global config, hipadaba - if not ('pfeiffer_hg' in config.sections() and config.get("pfeiffer_hg", "enabled") == "True"): + global sicsconfig + if not ('pfeiffer_hg' in sicsconfig.sections() and sicsconfig.get("pfeiffer_hg", "enabled") == "True"): raise unittest.SkipTest("Pfeiffer PS9 is not configured") child = gumtreexml for node in "/sample/ps9/".strip('/').split('/'): @@ -369,11 +381,11 @@ class Baker(Able): if not result[0].startswith("m2 = "): raise Exception("Unexpected m2 response: " + repr(result)) self.m2 = float(result[0].split("=")[1]) - if self.m1 > 20: + if self.m1 > 20.1: self.m1 = round(self.m1 - 1, 0) else: self.m1 = round(self.m1 + 1, 0) - if self.m2 > 20: + if self.m2 > 20.1: self.m2 = round(self.m2 - 1, 0) else: self.m2 = round(self.m2 + 1, 0) @@ -389,20 +401,20 @@ class Baker(Able): d = self.send_command("m1") d.addCallback(cb1) return self.deferred - test_006_001_drive_one.timeout = 20 + test_006_001_drive_one.timeout = 30 def Test_007_000_mercury(self): raise unittest.SkipTest("Not doing this test now (borken)") - global config, hipadaba + global sicsconfig debug = False def cb0(result, *args, **kw): if debug: print "\nMercury:", result.short_tree() self.deferred.callback(result) - if not ("mercury_scpi" in config.sections() and config.get("mercury_scpi", "enabled") == "True"): + if not ("mercury_scpi" in sicsconfig.sections() and sicsconfig.get("mercury_scpi", "enabled") == "True"): raise unittest.SkipTest("mercury_scpi is not configured") self.deferred = defer.Deferred() - d = self.load_hipadaba("/sample/%s" % config.get("mercury_scpi", "name")) + d = self.load_hipadaba("/sample/%s" % sicsconfig.get("mercury_scpi", "name")) d.addCallback(cb0) return self.deferred Test_007_000_mercury.timeout = 2 @@ -412,9 +424,9 @@ class Baker(Able): """ global hipadaba debug = False - if not 'hipadaba' in globals() or hipadaba is None: - raise unittest.SkipTest("hipadaba is missing") - if not ('mercury_scpi' in config.sections() and config.get("mercury_scpi", "enabled") == "True"): + self.assertTrue('hipadaba' in globals()) + self.assertFalse(hipadaba is None, "hipadaba is missing") + if not ('mercury_scpi' in sicsconfig.sections() and sicsconfig.get("mercury_scpi", "enabled") == "True"): raise unittest.SkipTest("Mercury TC9 is not configured") if False: