3 # Copyright (C) 2010-2012 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 testdir = os.path.dirname(os.path.realpath(__file__))
29 sys.path.append(os.path.join(testdir, '..', '..'))
30 sys.path.append(os.path.join(testdir, '..', '_testTools'))
31 sys.path.append(os.path.join(testdir, '..', '..','commands'))
33 from salomeTools import Sat
34 from tools import check_proc_existence_and_kill
35 from tools import outRedirection
41 class TestLog(unittest.TestCase):
42 '''Test of log command: launch of browser
45 def test_launch_browser(self):
46 '''Test the launch of browser when invoking the log command
51 sat = Sat("-oUSER.browser='konqueror'")
52 cmd_log = threading.Thread(target=sat.log, args=('',))
55 time.sleep(sleep_time)
57 browser = sat.cfg.USER.browser
58 pid = check_proc_existence_and_kill(browser + ".*" + "xml")
62 # pyunit method to compare 2 str
63 self.assertEqual(OK, "OK")
65 def test_write_xmllog(self):
66 '''Test the write of xml log when invoking a command
71 # launch the command that will write a log
73 sat.config('appli-test -v USER.browser')
76 logDir = sat.cfg.SITE.log.log_dir
77 logPath = os.path.join(logDir, sat.cfg.VARS.datehour + "_" + sat.cfg.VARS.command + ".xml")
79 if os.path.exists(logPath):
82 # pyunit method to compare 2 str
83 self.assertEqual(OK, "OK")
85 def test_option_terminal(self):
86 '''Test the terminal option without application
91 # launch the command that will write a log
95 sys.stdin = io.StringIO(one)
101 sys.stdin = sys.__stdin__
105 # pyunit method to compare 2 str
106 self.assertEqual(OK, "OK")
108 def test_option_terminal2(self):
109 '''Test the terminal option with application
114 # launch the command that will write a log
117 sat.config('appli-test -v VARS.python')
120 sys.stdin = io.StringIO(one)
123 sat.log('appli-test --last')
125 sys.stdin = sys.__stdin__
129 # pyunit method to compare 2 str
130 self.assertEqual(OK, "OK")
132 def test_option_terminal3(self):
133 '''Test the terminal option with 0 as input
138 # launch the command that will write a log
141 sat.config('appli-test -v VARS.python')
144 sys.stdin = io.StringIO(zero)
147 sat.log('--terminal')
150 sys.stdin = sys.__stdin__
152 # pyunit method to compare 2 str
153 self.assertEqual(OK, "OK")
155 def test_option_terminal4(self):
156 '''Test the terminal option with input bigger than the number of logs
161 # launch the command that will write a log
164 sat.config('appli-test -v VARS.python')
166 nb_logs = len(os.listdir(sat.cfg.SITE.log.log_dir))
168 nb_logs_u = unicode(str(nb_logs) + "\n1")
169 sys.stdin = io.StringIO(nb_logs_u)
172 sat.log('--terminal')
175 sys.stdin = sys.__stdin__
177 # pyunit method to compare 2 str
178 self.assertEqual(OK, "OK")
180 def test_option_terminal5(self):
181 '''Test the terminal option with input return
186 # launch the command that will write a log
189 sat.config('appli-test -v VARS.python')
192 sys.stdin = io.StringIO(ret)
195 sat.log('--terminal')
198 sys.stdin = sys.__stdin__
200 # pyunit method to compare 2 str
201 self.assertEqual(OK, "OK")
203 def test_option_terminal6(self):
204 '''Test the terminal option with input not int
209 # launch the command that will write a log
212 sat.config('appli-test -v VARS.python')
214 ret = unicode("blabla\n0")
215 sys.stdin = io.StringIO(ret)
218 sat.log('--terminal')
221 sys.stdin = sys.__stdin__
223 # pyunit method to compare 2 str
224 self.assertEqual(OK, "OK")
226 def test_option_terminal7(self):
227 '''Test the terminal option and option last
232 # launch the command that will write a log
236 sat.log('--terminal --last')
239 sys.stdin = sys.__stdin__
241 # pyunit method to compare 2 str
242 self.assertEqual(OK, "OK")
244 def test_option_last(self):
245 '''Test the option --last
250 # launch the command that will write a log
251 sat = Sat("-oUSER.browser='konqueror'")
253 sat.config('appli-test -v VARS.python')
255 cmd_log = threading.Thread(target=sat.log, args=('appli-test --last',))
258 time.sleep(sleep_time)
260 browser = sat.cfg.USER.browser
261 pid = check_proc_existence_and_kill(browser + ".*" + "xml")
266 # pyunit method to compare 2 str
267 self.assertEqual(OK, "OK")
269 def test_option_clean(self):
270 '''Test the option --clean
275 # launch the command that will write a log
278 sat.config('-v VARS.user')
280 nb_logs_t0 = len(os.listdir(sat.cfg.SITE.log.log_dir))
284 nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.log_dir))
286 if nb_logs_t1-nb_logs_t0 == 0:
289 # pyunit method to compare 2 str
290 self.assertEqual(OK, "OK")
292 def test_option_clean2(self):
293 '''Test the option --clean with big number of files to clean
298 # launch the command that will write a log
301 sat.config('-v VARS.user')
303 nb_logs_t0 = len(os.listdir(sat.cfg.SITE.log.log_dir))
305 if os.path.exists(sat.cfg.SITE.log.log_dir + "_save"):
306 shutil.rmtree(sat.cfg.SITE.log.log_dir + "_save")
307 shutil.copytree(sat.cfg.SITE.log.log_dir,sat.cfg.SITE.log.log_dir + "_save")
309 sat.log('--clean ' + str(nb_logs_t0))
311 nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.log_dir))
313 shutil.rmtree(sat.cfg.SITE.log.log_dir)
314 shutil.move(sat.cfg.SITE.log.log_dir + "_save", sat.cfg.SITE.log.log_dir)
316 if nb_logs_t0-nb_logs_t1 > 10:
319 # pyunit method to compare 2 str
320 self.assertEqual(OK, "OK")
322 def test_option_full(self):
323 '''Test the option --full
328 sat = Sat("-oUSER.browser='konqueror'")
329 cmd_log = threading.Thread(target=sat.log, args=('--full',))
332 time.sleep(sleep_time)
334 browser = sat.cfg.USER.browser
335 check_proc_existence_and_kill(browser + ".*" + "xml")
337 # Read and check the hat.xml file contains at least one log file corresponding to log
338 hatFilePath = os.path.join(sat.cfg.SITE.log.log_dir, "hat.xml")
339 xmlHatFile = src.xmlManager.ReadXmlFile(hatFilePath)
340 for field in xmlHatFile.xmlroot:
341 if field.attrib[b'cmd'] == b'log':
345 # pyunit method to compare 2 str
346 self.assertEqual(OK, "OK")
348 def test_description(self):
349 '''Test the sat -h log
356 if "Gives access to the logs produced" in log.description():
359 # pyunit method to compare 2 str
360 self.assertEqual(OK, "OK")
363 if __name__ == '__main__':
364 HTMLTestRunner.main()