Salome HOME
Add test for shell, job and jobs commands
authorSerge Rehbinder <serge.rehbinder@cea.fr>
Thu, 23 Jun 2016 09:37:21 +0000 (11:37 +0200)
committerSerge Rehbinder <serge.rehbinder@cea.fr>
Thu, 23 Jun 2016 09:37:21 +0000 (11:37 +0200)
commands/job.py
commands/jobs.py
salomeTools.py
src/logger.py
test/_testTools/tools.py
test/config/option_edit.py
test/job/test_job.py [new file with mode: 0644]
test/jobs/test_jobs.py [new file with mode: 0644]
test/log/launch_browser.py
test/run_all.sh
test/shell/test_shell.py [new file with mode: 0644]

index fec177f65d1a82d490c11c94bbe80c19198eb8a1..d8a10a09e53dd2c2a4e065180689ec4b7439f8be 100644 (file)
@@ -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
index 80a6730717e8a7dc896a7eb960ea0a39bf18fa1c..f5729ce6affff0461d3e78ad7c1f1a535493cc53 100644 (file)
@@ -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
index 4a7aa52d2f9997dfd0403f8a9d83eaeae9b06ccc..38badf80b11990208f425f519a69825ce3d7c18d 100755 (executable)
@@ -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)
index 40c01367e7c7575e15d21fd601062f5607c66c14..a87a7719019f714b92458dfcc98e3f846029233d 100644 (file)
@@ -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)})
index a518644559ded66862f9eb2fa1abeb15f4530fa0..2930b99601f8fb1bfe8a044c80bde19328808ac1 100644 (file)
@@ -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
index c156419a9fdad05dc025f6c47aef276af42be0a8..f732dcf28e3b7407bfc838143f657da0377ad0b7 100644 (file)
@@ -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 (file)
index 0000000..f7fb66a
--- /dev/null
@@ -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 (file)
index 0000000..f9cd80b
--- /dev/null
@@ -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()
index 9d62d861008aa4b6031a23bb67b136e74e24ded3..4d2988b7576c1f586db19ca990d00bc0c4ce5fbe 100644 (file)
@@ -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")
index 1afb4eae8e6d0d8f562a93bf59aa2c9d70b60cc9..35fba266f268ba2d79235f77cf9d3815673aa0b5 100755 (executable)
 #  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 (file)
index 0000000..56b0ce6
--- /dev/null
@@ -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()