From bf364ea1f3f019264ee7a238134f425fae6d0503 Mon Sep 17 00:00:00 2001 From: Christian Van Wambeke Date: Tue, 19 Jun 2018 16:50:11 +0200 Subject: [PATCH] unittest OK for AllTestLauncherSat.py -p "test_???_*.py" --- commands/check.py | 2 +- commands/template.py | 2 +- src/linksXml.py | 11 +++++-- src/loggingSat.py | 43 ++++++++++++++++++--------- test/test_019_linksXml.py | 2 +- test/test_024_logging.py | 4 +-- test/test_035_pyconf.py | 8 ++--- test/test_100_satHelp.py | 58 +++++++++++++++++++++++++------------ test/test_500_APPLI_TEST.py | 36 +++++++++++++---------- 9 files changed, 106 insertions(+), 60 deletions(-) diff --git a/commands/check.py b/commands/check.py index 8da2ca2..759386f 100644 --- a/commands/check.py +++ b/commands/check.py @@ -115,7 +115,7 @@ Optional: products to configure. """ res = [] for p_name_info in products_infos: - res.append(self.check_product(p_name_info) + res.append(self.check_product(p_name_info)) return res def check_product(self, p_name_info): diff --git a/commands/template.py b/commands/template.py index 1bbdb2d..92a1e1b 100644 --- a/commands/template.py +++ b/commands/template.py @@ -388,7 +388,7 @@ def prepare_from_template(config, # ask for missing parameters msg = UTS.label(_("Make substitution in files")) - msg += "\n" + _("Delimiter") + " = '%s'" % tsettings.delimiter_char) + msg += "\n" + _("Delimiter") + " = '%s'" % tsettings.delimiter_char msg += "\n" + _("Ignore Filters") + "= %s" % ', '.join(tsettings.ignore_filters) logger.debug(msg) dico = tsettings.get_parameters(conf_values) diff --git a/src/linksXml.py b/src/linksXml.py index ab4ce4a..afc7132 100755 --- a/src/linksXml.py +++ b/src/linksXml.py @@ -87,7 +87,7 @@ class LinkXml(object): import src.debug as DBG if self.findLink(idName) is not None: msg = "appendLink: idname '%s' existing yet" % idName - DBG.write(msg, self, True) + DBG.write(msg, self) raise Exception(msg) app = LinkXml(idName) self._linksXml.append(app) @@ -156,10 +156,15 @@ def appendLinkForCommand(cmdParent, cmdNew): def setAttribLinkForCommand(cmd, nameAttrib, value): """init an attribute value in link of a command in singleton tree""" + DBG.write("setAttribLinkForCommand", (cmd.name, nameAttrib, value)) k0 = getLinksXml() # get singleton kCmd = k0.findLink(cmd.getId()) - kCmd.setAuthAttr(nameAttrib, value) - # DBG.write("setAttribLinkForCommand", (nameAttrib, value), True) + if kCmd is not None: + kCmd.setAuthAttr(nameAttrib, value) + else: # SatUnittestLogger implies no xml + msg = "no links for log command only if logger 'SatUnittestLogger' " + DBG.write("warning", msg) + def getLinksForXml(idCommand): """return list of links of one command from its id""" diff --git a/src/loggingSat.py b/src/loggingSat.py index e925422..2970c31 100755 --- a/src/loggingSat.py +++ b/src/loggingSat.py @@ -54,6 +54,7 @@ import logging as LOGI from logging.handlers import BufferingHandler import pprint as PP +import src.debug as DBG # Easy print stderr (for DEBUG only) _verbose = False _name = "loggingSat" @@ -429,6 +430,7 @@ class LoggerSat(LOGI.Logger): add file handler to logger to set log files for a salometools command. when command is known from pyconf/config instance + functionality only for | Example: | log files names for command prepare @@ -439,6 +441,10 @@ class LoggerSat(LOGI.Logger): | ~/LOGS/OUT/micro_20180510_140607_clean_lenovo.txt | etc. """ + if self.name == "SatUnittestLogger": + DBG.write("warning", "setFileHandlerForCommand not valid for SatUnittestLogger") + return self.idCommandHandlers + import src.utilsSat as UTS # needs sys.path.insert(0, satdir) logger = self @@ -454,19 +460,30 @@ class LoggerSat(LOGI.Logger): log_dir_jobs = os.path.join(log_dir, "JOBS") # files txt UTS.ensure_path_exists(log_dir) UTS.ensure_path_exists(log_dir_out) - if self.idCommandHandlers == 0: - datehour = config.VARS.datehour - self.dateHour = datehour # save dateHour for micro commands - else: - datehour = self.dateHour # micro commands have same datehour for naming files - - cmd = config.VARS.command - fullNameCmd = cmdInstance.getFullNameStr() - hostname = config.VARS.hostname - nameFileXml = "%s_%03i_%s_%s.xml" % (datehour, self.idCommandHandlers, cmd, hostname) - nameFileTxt = "%s_%03i_%s_%s.txt" % (datehour, self.idCommandHandlers, cmd, hostname) - fileXml = os.path.join(log_dir, nameFileXml) - fileTxt = os.path.join(log_dir_out, nameFileTxt) + + ok = False + for ii in range(0, 30): # max simultaneous sat processes launched in same second (unittest....) + if self.idCommandHandlers == 0: + datehour = config.VARS.datehour + self.dateHour = datehour # save dateHour for micro commands + else: + datehour = self.dateHour # micro commands have same datehour for naming files + + cmd = config.VARS.command + fullNameCmd = cmdInstance.getFullNameStr() + hostname = config.VARS.hostname + nameFileXml = "%s_%02i_%03i_%s_%s.xml" % (datehour, ii, self.idCommandHandlers, cmd, hostname) + nameFileTxt = "%s_%02i_%03i_%s_%s.txt" % (datehour, ii, self.idCommandHandlers, cmd, hostname) + fileXml = os.path.join(log_dir, nameFileXml) + fileTxt = os.path.join(log_dir_out, nameFileTxt) + if os.path.exists(fileXml): + continue + else: + ok = True + break + + if not ok: + raise Exception("max %i simultaneous sat processes launched in same second reached" % ii) # precaution lastCmd = cmdInstance.getFullNameList()[-1] diff --git a/test/test_019_linksXml.py b/test/test_019_linksXml.py index b88017b..30a8aa4 100755 --- a/test/test_019_linksXml.py +++ b/test/test_019_linksXml.py @@ -31,7 +31,7 @@ class TestCase(unittest.TestCase): def test_000(self): # one shot setUp() for this TestCase - DBG.push_debug(True) + DBG.push_debug(False) # SAT.setNotLocale() # test english return diff --git a/test/test_024_logging.py b/test/test_024_logging.py index 4f37c60..37e132a 100755 --- a/test/test_024_logging.py +++ b/test/test_024_logging.py @@ -120,9 +120,9 @@ class TestCase(unittest.TestCase): self.assertEqual(lgr.name, name) self.assertIn("trace", dir(lgr)) self.assertIn("TRACE", LOGI._levelNames.keys()) - self.assertIn(lgr._TRACE, LOGI._levelNames.keys()) + self.assertIn(_TRACE, LOGI._levelNames.keys()) self.assertEqual(LOGI.getLevelName(LOGI.INFO), "INFO") - self.assertEqual(LOGI.getLevelName(lgr._TRACE), "TRACE") + self.assertEqual(LOGI.getLevelName(_TRACE), "TRACE") # creation d'un handler pour chaque log sur la console formatter = LOGI.Formatter('%(levelname)-8s :: %(message)s') diff --git a/test/test_035_pyconf.py b/test/test_035_pyconf.py index 69c8f22..6fae3dc 100755 --- a/test/test_035_pyconf.py +++ b/test/test_035_pyconf.py @@ -237,20 +237,20 @@ Bienvenue, Yves cfg.__save__(outStream) res = outStream.value - DBG.write("test_120 cfg save", res, True) + DBG.write("test_120 cfg save", res) self.assertNotIn("ERROR", res) res = DBG.getStrConfigDbg(cfg) - DBG.write("test_120 cfg debug", res, True) + DBG.write("test_120 cfg debug", res) # no error circular !!! # self.assertIn("ERROR", res) # no error circular !!! # self.assertIn("unable to evaluate $cc", res) # self.assertIn("unable to evaluate $bb", res) res = cfg.bb - DBG.write("test_120 cfg.bb debug", res, True) + DBG.write("test_120 cfg.bb debug", res) res = cfg.cc - DBG.write("test_120 cfg.cc debug", res, True) + DBG.write("test_120 cfg.cc debug", res) def test_999(self): # one shot tearDown() for this TestCase diff --git a/test/test_100_satHelp.py b/test/test_100_satHelp.py index cb1b289..5ae3500 100755 --- a/test/test_100_satHelp.py +++ b/test/test_100_satHelp.py @@ -30,18 +30,19 @@ import src.loggingSat as LOG class TestCase(unittest.TestCase): "Test the sat --help commands""" - logger = LOG.getUnittestLogger() debug = False def tearDown(self): # print "tearDown", __file__ # assure self.logger clear for next test - logs = self.logger.getLogsAndClear() + logger = LOG.getUnittestLogger() + logs = logger.getLogsAndClear() # using assertNotIn() is too much verbose self.assertFalse("ERROR" in logs) self.assertFalse("CRITICAL" in logs) def test_000(self): + logger = LOG.getUnittestLogger() # one shot setUp() for this TestCase if self.debug: DBG.push_debug(True) SAT.setNotLocale() # test english @@ -61,11 +62,12 @@ class TestCase(unittest.TestCase): self.assertTrue(" - compile" in out) def test_011(self): + logger = LOG.getUnittestLogger() cmd = "--help" - s = SAT.Sat(self.logger) + s = SAT.Sat(logger) returnCode = s.execute_cli(cmd) self.assertTrue(returnCode.isOk()) - logs = self.logger.getLogs() + logs = logger.getLogs() DBG.write("test_011 logger", logs) self.assertTrue(" - config" in logs) self.assertTrue(" - prepare" in logs) @@ -73,20 +75,32 @@ class TestCase(unittest.TestCase): def test_030(self): cmd = "sat config --help" - stdout, stderr = SAT.launchSat(cmd) - DBG.write("test_030 stdout", stdout) - self.assertEqual(stderr, "") - self.assertTrue("--value" in stdout) + returnCode = SAT.launchSat(cmd) + self.assertTrue(returnCode.isOk()) + out = returnCode.getValue() + DBG.write("test_030 stdout", out) + self.assertTrue("--value" in out) def test_031(self): + logger = LOG.getUnittestLogger() cmd = "config --help" - s = SAT.Sat(self.logger) + s = SAT.Sat(logger) returnCode = s.execute_cli(cmd) self.assertTrue(returnCode.isOk()) - logs = self.logger.getLogs() + logs = logger.getLogs() DBG.write("test_031 logger", logs) - self.assertTrue("--value" in logs) - + self.assertTrue("--help" in logs) + + def test_032(self): + logger = LOG.getUnittestLogger() + cmd = "prepare --help" + s = SAT.Sat(logger) + returnCode = s.execute_cli(cmd) + self.assertTrue(returnCode.isOk()) + logs = logger.getLogs() + DBG.write("test_031 logger", logs) + self.assertTrue("--help" in logs) + def xxtest_040(self): cmd = "config --list" s = SAT.Sat(self.logger) @@ -100,20 +114,26 @@ class TestCase(unittest.TestCase): DBG.write("test_050 getCommandsList", cmds) for c in cmds: cmd = "sat %s --help" % c - stdout, stderr = SAT.launchSat(cmd) - self.assertEqual(stderr, "") - DBG.write("test_050 %s stdout" % c, stdout) - self.assertTrue("The %s command" % c in stdout) - self.assertTrue("Available options" in stdout) + DBG.write("test_050", cmd) + returnCode = SAT.launchSat(cmd) + if not returnCode.isOk(): + DBG.write("test_050 %s" % cmd, returnCode.getValue(), True) + self.assertTrue(returnCode.isOk()) + out = returnCode.getValue() + DBG.write("test_050 %s stdout" % c, out) + self.assertTrue("The %s command" % c in out) + self.assertTrue("Available options" in out) def test_051(self): + logger = LOG.getUnittestLogger() cmds = SAT.getCommandsList() for c in cmds: cmd = "%s --help" % c - s = SAT.Sat(self.logger) + DBG.write("test_051", cmd) + s = SAT.Sat(logger) returnCode = s.execute_cli(cmd) self.assertTrue(returnCode.isOk()) - logs = self.logger.getLogsAndClear() + logs = logger.getLogsAndClear() DBG.write(cmd, logs) self.assertTrue("The %s command" % c in logs) self.assertTrue("Available options" in logs) diff --git a/test/test_500_APPLI_TEST.py b/test/test_500_APPLI_TEST.py index e0fe70c..b68a158 100755 --- a/test/test_500_APPLI_TEST.py +++ b/test/test_500_APPLI_TEST.py @@ -29,8 +29,7 @@ import src.loggingSat as LOG class TestCase(unittest.TestCase): "Test the sat commands on APPLI_TEST configuration pyconf etc. files""" - - logger = LOG.getUnittestLogger() + debug = False #see test_100, # commands are expected OK @@ -57,7 +56,8 @@ class TestCase(unittest.TestCase): def tearDown(self): # print "tearDown", __file__ # assure self.logger clear for next test - logs = self.logger.getLogsAndClear() + logger = LOG.getUnittestLogger() + logs = logger.getLogsAndClear() # using assertNotIn() is too much verbose self.assertFalse("ERROR ::" in logs) self.assertFalse("CRITICAL ::" in logs) @@ -74,24 +74,26 @@ class TestCase(unittest.TestCase): if self.debug: DBG.pop_debug() def test_010(self): + logger = LOG.getUnittestLogger() cmd = "config -l" - s = SAT.Sat(self.logger) + s = SAT.Sat(logger) DBG.write("s.getConfig()", s.getConfig()) #none DBG.write("s.__dict__", s.__dict__) # have returnCode = s.execute_cli(cmd) - DBG.write("test_010 returnCode", returnCode) - logs = self.logger.getLogs() + DBG.write("test_010", returnCode) + logs = logger.getLogs() DBG.write("test_010 logger", logs) self.assertTrue(returnCode.isOk()) def test_100(self): # test all satCommands expected OK - dbg = self.debug # True # + logger = LOG.getUnittestLogger() + dbg = self.debug for cmd in self.satCommandsToTestOk: - s = SAT.Sat(self.logger) + s = SAT.Sat(logger) returnCode = s.execute_cli(cmd) - DBG.write("test_800 'sat %s' returnCode" % cmd, str(returnCode), True) - logs = self.logger.getLogsAndClear() + DBG.write("test_100 'sat %s'" % cmd, str(returnCode), dbg) + logs = logger.getLogsAndClear() DBG.write("logs", logs, dbg) # using assertNotIn() is too much verbose self.assertFalse("ERROR ::" in logs) @@ -99,23 +101,25 @@ class TestCase(unittest.TestCase): def test_110(self): # test all satCommands expected KO + logger = LOG.getUnittestLogger() dbg = self.debug for cmd in self.satCommandsToTestKo: - s = SAT.Sat(self.logger) + s = SAT.Sat(logger) returnCode = s.execute_cli(cmd) - DBG.write("test_810 'sat %s' returnCode" % cmd, returnCode, dbg) - logs = self.logger.getLogsAndClear() + DBG.write("test_110 'sat %s'" % cmd, returnCode, dbg) + logs = logger.getLogsAndClear() DBG.write("logs", logs, dbg) def test_120(self): # test all satCommands expected raise + logger = LOG.getUnittestLogger() dbg = self.debug for cmd in self.satCommandsToTestRaise: - s = SAT.Sat(self.logger) - DBG.write("test_820 'sat %s'" % cmd, "expected raise", dbg) + s = SAT.Sat(logger) + DBG.write("test_120 'sat %s'" % cmd, "expected raise", dbg) with self.assertRaises(Exception): returnCode = s.execute_cli(cmd) - logs = self.logger.getLogsAndClear() + logs = logger.getLogsAndClear() DBG.write("logs", logs, dbg) -- 2.39.2