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_multi
39 class TestLog(unittest.TestCase):
40 '''Test of log command: launch of browser
43 def test_write_xmllog(self):
44 '''Test the write of xml log when invoking a command
49 # launch the command that will write a log
51 sat.config('appli-test -v USER.browser')
54 logDir = sat.cfg.USER.log_dir
55 logPath = os.path.join(logDir, sat.cfg.VARS.datehour + "_" + sat.cfg.VARS.command + ".xml")
57 if os.path.exists(logPath):
60 # pyunit method to compare 2 str
61 self.assertEqual(OK, "OK")
63 def test_option_terminal(self):
64 '''Test the terminal option without application
69 # launch the command that will write a log
73 sys.stdin = io.StringIO(one)
79 sys.stdin = sys.__stdin__
81 sys.stdin = sys.__stdin__
83 # pyunit method to compare 2 str
84 self.assertEqual(OK, "OK")
86 def test_option_terminal2(self):
87 '''Test the terminal option with application
92 # launch the command that will write a log
95 sat.config('appli-test -v VARS.python')
98 sys.stdin = io.StringIO(one)
101 sat.log('appli-test -t --last')
103 sys.stdin = sys.__stdin__
107 # pyunit method to compare 2 str
108 self.assertEqual(OK, "OK")
110 def test_option_terminal3(self):
111 '''Test the terminal option with 0 as input
116 # launch the command that will write a log
119 sat.config('appli-test -v VARS.python')
122 sys.stdin = io.StringIO(zero)
125 sat.log('--terminal')
128 sys.stdin = sys.__stdin__
130 # pyunit method to compare 2 str
131 self.assertEqual(OK, "OK")
133 def test_option_terminal4(self):
134 '''Test the terminal option with input bigger than the number of logs
139 # launch the command that will write a log
142 sat.config('appli-test -v VARS.python')
144 nb_logs = len(os.listdir(sat.cfg.USER.log_dir))
146 nb_logs_u = unicode(str(nb_logs) + "\n1")
147 sys.stdin = io.StringIO(nb_logs_u)
150 sat.log('--terminal')
153 sys.stdin = sys.__stdin__
155 # pyunit method to compare 2 str
156 self.assertEqual(OK, "OK")
158 def test_option_terminal5(self):
159 '''Test the terminal option with input return
164 # launch the command that will write a log
167 sat.config('appli-test -v VARS.python')
170 sys.stdin = io.StringIO(ret)
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_terminal6(self):
182 '''Test the terminal option with input not int
187 # launch the command that will write a log
190 sat.config('appli-test -v VARS.python')
192 ret = unicode("blabla\n0")
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_terminal7(self):
205 '''Test the terminal option and option last
210 # launch the command that will write a log
214 sat.log('--terminal --last')
217 sys.stdin = sys.__stdin__
219 # pyunit method to compare 2 str
220 self.assertEqual(OK, "OK")
222 def test_option_last(self):
223 '''Test the option --last
228 # launch the command that will write a log
229 sat = Sat("-oUSER.browser='konqueror'")
231 sat.config('appli-test -v VARS.python')
234 time.sleep(sleep_time)
235 cmd_log = threading.Thread(target=sat.log, args=('appli-test --last',))
238 time.sleep(sleep_time)
240 browser = sat.cfg.USER.browser
241 pid = check_proc_existence_and_kill_multi(browser + ".*" + "xml", 10)
246 # pyunit method to compare 2 str
247 self.assertEqual(OK, "OK")
249 def test_option_clean(self):
250 '''Test the option --clean
255 # launch the command that will write a log
258 sat.config('-v VARS.user')
260 nb_logs_t0 = len(os.listdir(sat.cfg.USER.log_dir))
264 nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
266 if nb_logs_t1-nb_logs_t0 == 0:
269 # pyunit method to compare 2 str
270 self.assertEqual(OK, "OK")
272 def test_option_clean2(self):
273 '''Test the option --clean with big number of files to 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.USER.log_dir))
285 if os.path.exists(sat.cfg.USER.log_dir + "_save"):
286 shutil.rmtree(sat.cfg.USER.log_dir + "_save")
287 shutil.copytree(sat.cfg.USER.log_dir,sat.cfg.USER.log_dir + "_save")
289 sat.log('--clean ' + str(nb_logs_t0))
291 nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
293 shutil.rmtree(sat.cfg.USER.log_dir)
294 shutil.move(sat.cfg.USER.log_dir + "_save", sat.cfg.USER.log_dir)
296 if nb_logs_t0-nb_logs_t1 > 10:
299 # pyunit method to compare 2 str
300 self.assertEqual(OK, "OK")
303 def test_option_full(self):
304 '''Test the option --full
309 sat = Sat("-oUSER.browser='konqueror'")
310 time.sleep(sleep_time)
311 cmd_log = threading.Thread(target=sat.log, args=('--full',))
314 time.sleep(sleep_time)
316 browser = sat.cfg.USER.browser
317 check_proc_existence_and_kill_multi(browser + ".*" + "hat\.xml", 10)
319 # Read and check the hat.xml file contains at least one log file corresponding to log
320 hatFilePath = os.path.join(sat.cfg.USER.log_dir, "hat.xml")
321 xmlHatFile = src.xmlManager.ReadXmlFile(hatFilePath)
322 for field in xmlHatFile.xmlroot:
323 if field.attrib[b'cmd'] == b'log':
327 # pyunit method to compare 2 str
328 self.assertEqual(OK, "OK")
330 def test_description(self):
331 '''Test the sat -h log
338 if "Gives access to the logs produced" in log.description():
341 # pyunit method to compare 2 str
342 self.assertEqual(OK, "OK")
345 if __name__ == '__main__':
346 HTMLTestRunner.main()