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 time.sleep(sleep_time)
53 cmd_log = threading.Thread(target=sat.log, args=('',))
56 time.sleep(sleep_time)
58 browser = sat.cfg.USER.browser
59 pid = check_proc_existence_and_kill(browser + ".*" + "hat\.xml")
63 # pyunit method to compare 2 str
64 self.assertEqual(OK, "OK")
66 def test_write_xmllog(self):
67 '''Test the write of xml log when invoking a command
72 # launch the command that will write a log
74 sat.config('appli-test -v USER.browser')
77 logDir = sat.cfg.SITE.log.log_dir
78 logPath = os.path.join(logDir, sat.cfg.VARS.datehour + "_" + sat.cfg.VARS.command + ".xml")
80 if os.path.exists(logPath):
83 # pyunit method to compare 2 str
84 self.assertEqual(OK, "OK")
86 def test_option_terminal(self):
87 '''Test the terminal option without application
92 # launch the command that will write a log
96 sys.stdin = io.StringIO(one)
102 sys.stdin = sys.__stdin__
104 sys.stdin = sys.__stdin__
106 # pyunit method to compare 2 str
107 self.assertEqual(OK, "OK")
109 def test_option_terminal2(self):
110 '''Test the terminal option with application
115 # launch the command that will write a log
118 sat.config('appli-test -v VARS.python')
121 sys.stdin = io.StringIO(one)
124 sat.log('appli-test -t --last')
126 sys.stdin = sys.__stdin__
130 # pyunit method to compare 2 str
131 self.assertEqual(OK, "OK")
133 def test_option_terminal3(self):
134 '''Test the terminal option with 0 as input
139 # launch the command that will write a log
142 sat.config('appli-test -v VARS.python')
145 sys.stdin = io.StringIO(zero)
148 sat.log('--terminal')
151 sys.stdin = sys.__stdin__
153 # pyunit method to compare 2 str
154 self.assertEqual(OK, "OK")
156 def test_option_terminal4(self):
157 '''Test the terminal option with input bigger than the number of logs
162 # launch the command that will write a log
165 sat.config('appli-test -v VARS.python')
167 nb_logs = len(os.listdir(sat.cfg.SITE.log.log_dir))
169 nb_logs_u = unicode(str(nb_logs) + "\n1")
170 sys.stdin = io.StringIO(nb_logs_u)
173 sat.log('--terminal')
176 sys.stdin = sys.__stdin__
178 # pyunit method to compare 2 str
179 self.assertEqual(OK, "OK")
181 def test_option_terminal5(self):
182 '''Test the terminal option with input return
187 # launch the command that will write a log
190 sat.config('appli-test -v VARS.python')
193 sys.stdin = io.StringIO(ret)
196 sat.log('--terminal')
199 sys.stdin = sys.__stdin__
201 # pyunit method to compare 2 str
202 self.assertEqual(OK, "OK")
204 def test_option_terminal6(self):
205 '''Test the terminal option with input not int
210 # launch the command that will write a log
213 sat.config('appli-test -v VARS.python')
215 ret = unicode("blabla\n0")
216 sys.stdin = io.StringIO(ret)
219 sat.log('--terminal')
222 sys.stdin = sys.__stdin__
224 # pyunit method to compare 2 str
225 self.assertEqual(OK, "OK")
227 def test_option_terminal7(self):
228 '''Test the terminal option and option last
233 # launch the command that will write a log
237 sat.log('--terminal --last')
240 sys.stdin = sys.__stdin__
242 # pyunit method to compare 2 str
243 self.assertEqual(OK, "OK")
245 def test_option_last(self):
246 '''Test the option --last
251 # launch the command that will write a log
252 sat = Sat("-oUSER.browser='konqueror'")
254 sat.config('appli-test -v VARS.python')
257 time.sleep(sleep_time)
258 cmd_log = threading.Thread(target=sat.log, args=('appli-test --last',))
261 time.sleep(sleep_time)
263 browser = sat.cfg.USER.browser
264 pid = check_proc_existence_and_kill(browser + ".*" + "xml")
269 # pyunit method to compare 2 str
270 self.assertEqual(OK, "OK")
272 def test_option_clean(self):
273 '''Test the option --clean
278 # launch the command that will write a log
281 sat.config('-v VARS.user')
283 nb_logs_t0 = len(os.listdir(sat.cfg.SITE.log.log_dir))
287 nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.log_dir))
289 if nb_logs_t1-nb_logs_t0 == 0:
292 # pyunit method to compare 2 str
293 self.assertEqual(OK, "OK")
295 def test_option_clean2(self):
296 '''Test the option --clean with big number of files to clean
301 # launch the command that will write a log
304 sat.config('-v VARS.user')
306 nb_logs_t0 = len(os.listdir(sat.cfg.SITE.log.log_dir))
308 if os.path.exists(sat.cfg.SITE.log.log_dir + "_save"):
309 shutil.rmtree(sat.cfg.SITE.log.log_dir + "_save")
310 shutil.copytree(sat.cfg.SITE.log.log_dir,sat.cfg.SITE.log.log_dir + "_save")
312 sat.log('--clean ' + str(nb_logs_t0))
314 nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.log_dir))
316 shutil.rmtree(sat.cfg.SITE.log.log_dir)
317 shutil.move(sat.cfg.SITE.log.log_dir + "_save", sat.cfg.SITE.log.log_dir)
319 if nb_logs_t0-nb_logs_t1 > 10:
322 # pyunit method to compare 2 str
323 self.assertEqual(OK, "OK")
325 def test_option_full(self):
326 '''Test the option --full
331 sat = Sat("-oUSER.browser='konqueror'")
332 time.sleep(sleep_time)
333 cmd_log = threading.Thread(target=sat.log, args=('--full',))
336 time.sleep(sleep_time)
338 browser = sat.cfg.USER.browser
339 check_proc_existence_and_kill(browser + ".*" + "hat\.xml")
341 # Read and check the hat.xml file contains at least one log file corresponding to log
342 hatFilePath = os.path.join(sat.cfg.SITE.log.log_dir, "hat.xml")
343 xmlHatFile = src.xmlManager.ReadXmlFile(hatFilePath)
344 for field in xmlHatFile.xmlroot:
345 if field.attrib[b'cmd'] == b'log':
349 # pyunit method to compare 2 str
350 self.assertEqual(OK, "OK")
352 def test_description(self):
353 '''Test the sat -h log
360 if "Gives access to the logs produced" in log.description():
363 # pyunit method to compare 2 str
364 self.assertEqual(OK, "OK")
367 if __name__ == '__main__':
368 HTMLTestRunner.main()