From 84b97f158be4fa81ca8fd09180aad189cdc7d223 Mon Sep 17 00:00:00 2001 From: Serge Rehbinder Date: Thu, 23 Jun 2016 11:37:21 +0200 Subject: [PATCH] Add test for shell, job and jobs commands --- commands/job.py | 3 +- commands/jobs.py | 11 ++- salomeTools.py | 16 ++-- src/logger.py | 2 +- test/_testTools/tools.py | 4 +- test/config/option_edit.py | 8 +- test/job/test_job.py | 141 ++++++++++++++++++++++++++++ test/jobs/test_jobs.py | 183 +++++++++++++++++++++++++++++++++++++ test/log/launch_browser.py | 6 +- test/run_all.sh | 19 +++- test/shell/test_shell.py | 105 +++++++++++++++++++++ 11 files changed, 474 insertions(+), 24 deletions(-) create mode 100644 test/job/test_job.py create mode 100644 test/jobs/test_jobs.py create mode 100644 test/shell/test_shell.py diff --git a/commands/job.py b/commands/job.py index fec177f..d8a10a0 100644 --- a/commands/job.py +++ b/commands/job.py @@ -54,7 +54,8 @@ def run(args, runner, logger): # Make sure the jobs_config option has been called if not options.jobs_cfg: message = _("The option --jobs_config is required\n") - raise src.SatException( message ) + logger.write(src.printcolors.printcError(message)) + return 1 # Find the file in the directories found = False diff --git a/commands/jobs.py b/commands/jobs.py index 80a6730..f5729ce 100644 --- a/commands/jobs.py +++ b/commands/jobs.py @@ -356,8 +356,8 @@ class Job(object): if not os.path.exists(local_path): self.machine.sftp.get(job_path_remote, local_path) self.remote_log_files.append(local_path) - except: - self.err += _("Unable to get %s log file from remote.") % job_path_remote + except Exception as e: + self.err += _("Unable to get %s log file from remote: %s") % (job_path_remote, str(e)) def has_failed(self): '''Returns True if the job has failed. @@ -423,8 +423,8 @@ class Job(object): self.err = "TIMEOUT : %s seconds elapsed\n" % str(self.timeout) try: self.get_log_files() - except: - self.err += _("Unable to get remote log files") + except Exception as e: + self.err += _("Unable to get remote log files: %s" % e) def total_duration(self): return self._Tf - self._T0 @@ -1205,7 +1205,8 @@ def run(args, runner, logger): # Make sure the jobs_config option has been called if not options.jobs_cfg: message = _("The option --jobs_config is required\n") - raise src.SatException( message ) + src.printcolors.printcError(message) + return 1 # Find the file in the directories found = False diff --git a/salomeTools.py b/salomeTools.py index 4a7aa52..38badf8 100755 --- a/salomeTools.py +++ b/salomeTools.py @@ -213,20 +213,20 @@ class Sat(object): # Check that the path given by the logs_paths_in_file option # is a file path that can be written - if options.logs_paths_in_file: + if self.options.logs_paths_in_file: try: - dir_file = os.path.dirname(options.logs_paths_in_file) + dir_file = os.path.dirname(self.options.logs_paths_in_file) if not os.path.exists(dir_file): os.makedirs(dir_file) - if os.path.exists(options.logs_paths_in_file): - os.remove(options.logs_paths_in_file) - file_test = open(options.logs_paths_in_file, "w") + if os.path.exists(self.options.logs_paths_in_file): + os.remove(self.options.logs_paths_in_file) + file_test = open(self.options.logs_paths_in_file, "w") file_test.close() except Exception as e: msg = _("WARNING: the logs_paths_in_file option will " "not be taken into account.\nHere is the error:") logger_command.write("%s\n%s\n\n" % (src.printcolors.printcWarning(msg), str(e))) - options.logs_paths_in_file = None + self.options.logs_paths_in_file = None try: res = None @@ -288,8 +288,8 @@ class Sat(object): res = 1 # If the logs_paths_in_file was called, write the result # and log files in the given file path - if options.logs_paths_in_file: - file_res = open(options.logs_paths_in_file, "w") + if self.options.logs_paths_in_file: + file_res = open(self.options.logs_paths_in_file, "w") file_res.write(str(res) + "\n") for i, filepath in enumerate(logger_command.l_logFiles): file_res.write(filepath) diff --git a/src/logger.py b/src/logger.py index 40c0136..a87a771 100644 --- a/src/logger.py +++ b/src/logger.py @@ -185,7 +185,7 @@ class Logger(object): seconds = total_time - hours*3600 - minutes*60 # Add the fields corresponding to the end time # and the total time of command - endtime = dt.strftime('%d/%Y/%m %Hh%Mm%Ss') + endtime = dt.strftime('%Y/%m/%d %Hh%Mm%Ss') self.xmlFile.append_node_attrib("Site", attrib={"endTime" : endtime}) self.xmlFile.append_node_attrib("Site", attrib={"TotalTime" : "%ih%im%is" % (hours, minutes, seconds)}) diff --git a/test/_testTools/tools.py b/test/_testTools/tools.py index a518644..2930b99 100644 --- a/test/_testTools/tools.py +++ b/test/_testTools/tools.py @@ -65,13 +65,13 @@ def check_proc_existence_and_kill(regex): return pid return 0 -def check_proc_existence_and_kill_multi(regex, nb_kills, time_between_to_checks = 1): +def check_proc_existence_and_kill_multi(regex, nb_kills, time_between_two_checks = 1): found = False i = 0 while not found and i < nb_kills : found = check_proc_existence_and_kill(regex) if found: return found - time.sleep(time_between_to_checks) + time.sleep(time_between_two_checks) i+=1 return 0 \ No newline at end of file diff --git a/test/config/option_edit.py b/test/config/option_edit.py index c156419..f732dcf 100644 --- a/test/config/option_edit.py +++ b/test/config/option_edit.py @@ -28,7 +28,7 @@ sys.path.append(os.path.join(testdir, '..', '..')) sys.path.append(os.path.join(testdir, '..', '_testTools')) from salomeTools import Sat -from tools import check_proc_existence_and_kill +from tools import check_proc_existence_and_kill_multi import HTMLTestRunner sleep_time = 3 @@ -44,13 +44,14 @@ class TestConfig(unittest.TestCase): OK = "KO" sat = Sat("-oUSER.editor='cooledit'") + sat.config() cmd_config = threading.Thread(target=sat.config, args=('-e',)) cmd_config.start() time.sleep(sleep_time) editor = sat.cfg.USER.editor - pid = check_proc_existence_and_kill(editor + ".*" + "salomeTools\.pyconf") + pid = check_proc_existence_and_kill_multi(editor + ".*" + "salomeTools\.pyconf", 10) if pid: OK = "OK" @@ -64,13 +65,14 @@ class TestConfig(unittest.TestCase): OK = "KO" sat = Sat("-oUSER.editor='cooledit'") + sat.config() cmd_config = threading.Thread(target=sat.config, args=('appli-test -e',)) cmd_config.start() time.sleep(sleep_time) editor = sat.cfg.USER.editor - pid = check_proc_existence_and_kill(editor + ".*" + "appli-test\.pyconf") + pid = check_proc_existence_and_kill_multi(editor + ".*" + "appli-test\.pyconf", 10) if pid: OK = "OK" diff --git a/test/job/test_job.py b/test/job/test_job.py new file mode 100644 index 0000000..f7fb66a --- /dev/null +++ b/test/job/test_job.py @@ -0,0 +1,141 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +# Copyright (C) 2010-2012 CEA/DEN +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import unittest +import os +import sys + +# get execution path +testdir = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(testdir, '..', '..')) +sys.path.append(os.path.join(testdir, '..', '_testTools')) +sys.path.append(os.path.join(testdir, '..', '..','commands')) + +from salomeTools import Sat +import HTMLTestRunner + +class TestJob(unittest.TestCase): + '''Test of the job command + ''' + + def test_job(self): + '''Test the job command + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + # Execute the job command + sat.job("--jobs_config .test --job Job 1" ) + + ff = open(tmp_file, "r") + log_files = ff.readlines() + ff.close() + os.remove(tmp_file) + log_config = [line.replace("\n", "") for line in log_files if 'config.xml' in line] + + text = open(log_config[0], "r").read() + + if "nb_proc" in text: + OK = 'OK' + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + + def test_failing_job(self): + '''Test the job command with a failing command + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + # Execute the job command + res = sat.job("--jobs_config .test --job Job 4" ) + + if res == 1: + OK = 'OK' + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_file_conf_not_found(self): + '''Test the job command with a wrong file configuration + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + # Execute the job command + res = sat.job("--jobs_config NOTEXIST --job Job 4" ) + + if res == 1: + OK = 'OK' + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_no_option_jobs_config(self): + '''Test the job command without --jobs_config option + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + # Execute the job command + res = sat.job("--job Job 4" ) + + if res == 1: + OK = 'OK' + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_job_not_found(self): + '''Test the job command without --jobs_config option + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + # Execute the job command + res = sat.job("--jobs_config .test --job NOTEXIST" ) + + if res == 1: + OK = 'OK' + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_description(self): + '''Test the sat -h job + ''' + + OK = "KO" + + import job + + if "Executes the commands of the job defined in the jobs configuration file" in job.description(): + OK = "OK" + + # pyunit method to compare 2 str + self.assertEqual(OK, "OK") + +# test launch +if __name__ == '__main__': + HTMLTestRunner.main() diff --git a/test/jobs/test_jobs.py b/test/jobs/test_jobs.py new file mode 100644 index 0000000..f9cd80b --- /dev/null +++ b/test/jobs/test_jobs.py @@ -0,0 +1,183 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +# Copyright (C) 2010-2012 CEA/DEN +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import unittest +import os +import sys + +# get execution path +testdir = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(testdir, '..', '..')) +sys.path.append(os.path.join(testdir, '..', '_testTools')) +sys.path.append(os.path.join(testdir, '..', '..','commands')) + +from salomeTools import Sat +from tools import outRedirection +import HTMLTestRunner + +class TestJobs(unittest.TestCase): + '''Test of the jobs command + ''' + + def test_jobs(self): + '''Test the jobs command + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + # Execute the jobs command + sat.jobs("--jobs_config .test --publish" ) + + ff = open(tmp_file, "r") + log_files = ff.readlines() + ff.close() + os.remove(tmp_file) + log_jobs = [line.replace("\n", "") for line in log_files if 'jobs.xml' in line] + + text = open(log_jobs[0], "r").read() + + expected_res = [ + "Establishing connection with all the machines", + "Executing the jobs", + "Results for job" + ] + + res = 0 + for exp_res in expected_res: + if exp_res not in text: + res += 1 + + if res == 0: + OK = 'OK' + + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_jobs_only_jobs(self): + '''Test the jobs command with option --only_jobs + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + # Execute the jobs command + sat.jobs("--jobs_config .test --publish --only_jobs Job 1" ) + + ff = open(tmp_file, "r") + log_files = ff.readlines() + ff.close() + os.remove(tmp_file) + log_jobs = [line.replace("\n", "") for line in log_files if 'jobs.xml' in line] + + text = open(log_jobs[0], "r").read() + + expected_res = [ + "Establishing connection with all the machines", + "Executing the jobs", + "Results for job" + ] + + res = 0 + for exp_res in expected_res: + if exp_res not in text: + res += 1 + + if res == 0: + OK = 'OK' + + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_no_option_jobs_config(self): + '''Test the jobs command without --jobs_config option + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + # Execute the job command + res = sat.jobs() + + if res == 1: + OK = 'OK' + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_file_conf_not_found(self): + '''Test the jobs command with a wrong file configuration + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + # Execute the job command + res = sat.jobs("--jobs_config NOTEXIST" ) + + if res == 1: + OK = 'OK' + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_option_list(self): + '''Test the display of the right value of "sat jobs --list" + ''' + + OK = "KO" + + # output redirection + my_out = outRedirection() + + # The command to test + sat = Sat() + sat.jobs('--list') + + # stop output redirection + my_out.end_redirection() + + # get results + res = my_out.read_results() + + # get results + if "ERROR" not in res: + OK = "OK" + + # pyunit method to compare 2 str + self.assertEqual(OK, "OK") + + def test_description(self): + '''Test the sat -h jobs + ''' + + OK = "KO" + + import jobs + + if "The jobs command launches maintenances that are described in the dedicated jobs configuration file." in jobs.description(): + OK = "OK" + + # pyunit method to compare 2 str + self.assertEqual(OK, "OK") + +# test launch +if __name__ == '__main__': + HTMLTestRunner.main() diff --git a/test/log/launch_browser.py b/test/log/launch_browser.py index 9d62d86..4d2988b 100644 --- a/test/log/launch_browser.py +++ b/test/log/launch_browser.py @@ -31,7 +31,7 @@ sys.path.append(os.path.join(testdir, '..', '_testTools')) sys.path.append(os.path.join(testdir, '..', '..','commands')) from salomeTools import Sat -from tools import check_proc_existence_and_kill +from tools import check_proc_existence_and_kill_multi import HTMLTestRunner sleep_time = 2 @@ -238,7 +238,7 @@ class TestLog(unittest.TestCase): time.sleep(sleep_time) browser = sat.cfg.USER.browser - pid = check_proc_existence_and_kill(browser + ".*" + "xml") + pid = check_proc_existence_and_kill_multi(browser + ".*" + "xml", 10) if pid: OK = "OK" @@ -314,7 +314,7 @@ class TestLog(unittest.TestCase): time.sleep(sleep_time) browser = sat.cfg.USER.browser - check_proc_existence_and_kill(browser + ".*" + "hat\.xml") + check_proc_existence_and_kill_multi(browser + ".*" + "hat\.xml", 10) # Read and check the hat.xml file contains at least one log file corresponding to log hatFilePath = os.path.join(sat.cfg.SITE.log.log_dir, "hat.xml") diff --git a/test/run_all.sh b/test/run_all.sh index 1afb4ea..35fba26 100755 --- a/test/run_all.sh +++ b/test/run_all.sh @@ -16,8 +16,14 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +echo "Begin date:" +date +echo +echo "Remove old results ... " rm -rf .coverage htmlcov - +echo "Done" +echo +echo "****************************" coverage run --source=../commands/config.py config/option_value.py > test_res.html coverage run --source=../commands/config.py -a config/option_value_2.py >> test_res.html coverage run --source=../commands/config.py -a config/create_user_pyconf.py >> test_res.html @@ -34,6 +40,17 @@ coverage run --source=../commands/config.py,../commands/configure.py,../commands coverage run --source=../commands/config.py,../commands/make.py,../commands/environ.py -a compilation/test_make.py >> test_res.html coverage run --source=../commands/config.py,../commands/makeinstall.py,../commands/environ.py -a compilation/test_makeinstall.py >> test_res.html coverage run --source=../commands/config.py,../commands/compile.py,../commands/configure.py,../commands/make.py,../commands/makeinstall.py,../commands/environ.py -a compilation/test_compilation.py >> test_res.html +coverage run --source=../commands/shell.py -a shell/test_shell.py >> test_res.html +coverage run --source=../commands/job.py -a job/test_job.py >> test_res.html +coverage run --source=../commands/jobs.py -a jobs/test_jobs.py >> test_res.html +echo "****************************" +echo +echo "building html coverage" coverage html +echo "Done" +echo +echo "End date:" +date +echo #firefox test_res.html htmlcov/index.html diff --git a/test/shell/test_shell.py b/test/shell/test_shell.py new file mode 100644 index 0000000..56b0ce6 --- /dev/null +++ b/test/shell/test_shell.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +# Copyright (C) 2010-2012 CEA/DEN +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import unittest +import os +import sys + +# get execution path +testdir = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(testdir, '..', '..')) +sys.path.append(os.path.join(testdir, '..', '_testTools')) +sys.path.append(os.path.join(testdir, '..', '..','commands')) + +from salomeTools import Sat +import HTMLTestRunner + +class TestShell(unittest.TestCase): + '''Test of the shell command + ''' + + def test_shell(self): + '''Test the shell command with the --command option + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + sat.config() + sat_way = sat.cfg.VARS.salometoolsway + + # Execute the shell command + sat.shell("--command ls " + sat_way) + + ff = open(tmp_file, "r") + log_files = ff.readlines() + ff.close() + os.remove(tmp_file) + log_files = [line.replace("\n", "") for line in log_files] + + text = open(log_files[2], "r").read() + + if "salomeTools.py" in text: + OK = 'OK' + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_failing_shell(self): + '''Test the shell command with the --command option with a failing command + ''' + OK = 'KO' + tmp_file = "/tmp/test.txt" + + sat = Sat("-l " + tmp_file) + + sat.config() + + # Execute the shell command + res = sat.shell("--command i_fail") + + ff = open(tmp_file, "r") + log_files = ff.readlines() + ff.close() + os.remove(tmp_file) + log_files = [line.replace("\n", "") for line in log_files] + + text = open(log_files[2], "r").read() + + if "i_fail" in text and res == 1: + OK = 'OK' + # pyunit method to compare 2 str + self.assertEqual(OK, 'OK') + + def test_description(self): + '''Test the sat -h shell + ''' + + OK = "KO" + + import shell + + if "Executes the shell command passed as argument" in shell.description(): + OK = "OK" + + # pyunit method to compare 2 str + self.assertEqual(OK, "OK") + +# test launch +if __name__ == '__main__': + HTMLTestRunner.main() -- 2.39.2