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_write_xmllog(self):
46 '''Test the write of xml log when invoking a command
51 # launch the command that will write a log
53 sat.config('appli-test -v USER.browser')
56 logDir = sat.cfg.SITE.log.log_dir
57 logPath = os.path.join(logDir, sat.cfg.VARS.datehour + "_" + sat.cfg.VARS.command + ".xml")
59 if os.path.exists(logPath):
62 # pyunit method to compare 2 str
63 self.assertEqual(OK, "OK")
65 def test_option_terminal(self):
66 '''Test the terminal option without application
71 # launch the command that will write a log
75 sys.stdin = io.StringIO(one)
81 sys.stdin = sys.__stdin__
83 sys.stdin = sys.__stdin__
85 # pyunit method to compare 2 str
86 self.assertEqual(OK, "OK")
88 def test_option_terminal2(self):
89 '''Test the terminal option with application
94 # launch the command that will write a log
97 sat.config('appli-test -v VARS.python')
100 sys.stdin = io.StringIO(one)
103 sat.log('appli-test -t --last')
105 sys.stdin = sys.__stdin__
109 # pyunit method to compare 2 str
110 self.assertEqual(OK, "OK")
112 def test_option_terminal3(self):
113 '''Test the terminal option with 0 as input
118 # launch the command that will write a log
121 sat.config('appli-test -v VARS.python')
124 sys.stdin = io.StringIO(zero)
127 sat.log('--terminal')
130 sys.stdin = sys.__stdin__
132 # pyunit method to compare 2 str
133 self.assertEqual(OK, "OK")
135 def test_option_terminal4(self):
136 '''Test the terminal option with input bigger than the number of logs
141 # launch the command that will write a log
144 sat.config('appli-test -v VARS.python')
146 nb_logs = len(os.listdir(sat.cfg.SITE.log.log_dir))
148 nb_logs_u = unicode(str(nb_logs) + "\n1")
149 sys.stdin = io.StringIO(nb_logs_u)
152 sat.log('--terminal')
155 sys.stdin = sys.__stdin__
157 # pyunit method to compare 2 str
158 self.assertEqual(OK, "OK")
160 def test_option_terminal5(self):
161 '''Test the terminal option with input return
166 # launch the command that will write a log
169 sat.config('appli-test -v VARS.python')
172 sys.stdin = io.StringIO(ret)
175 sat.log('--terminal')
178 sys.stdin = sys.__stdin__
180 # pyunit method to compare 2 str
181 self.assertEqual(OK, "OK")
183 def test_option_terminal6(self):
184 '''Test the terminal option with input not int
189 # launch the command that will write a log
192 sat.config('appli-test -v VARS.python')
194 ret = unicode("blabla\n0")
195 sys.stdin = io.StringIO(ret)
198 sat.log('--terminal')
201 sys.stdin = sys.__stdin__
203 # pyunit method to compare 2 str
204 self.assertEqual(OK, "OK")
206 def test_option_terminal7(self):
207 '''Test the terminal option and option last
212 # launch the command that will write a log
216 sat.log('--terminal --last')
219 sys.stdin = sys.__stdin__
221 # pyunit method to compare 2 str
222 self.assertEqual(OK, "OK")
224 def test_option_last(self):
225 '''Test the option --last
230 # launch the command that will write a log
231 sat = Sat("-oUSER.browser='konqueror'")
233 sat.config('appli-test -v VARS.python')
236 time.sleep(sleep_time)
237 cmd_log = threading.Thread(target=sat.log, args=('appli-test --last',))
240 time.sleep(sleep_time)
242 browser = sat.cfg.USER.browser
243 pid = check_proc_existence_and_kill(browser + ".*" + "xml")
248 # pyunit method to compare 2 str
249 self.assertEqual(OK, "OK")
251 def test_option_clean(self):
252 '''Test the option --clean
257 # launch the command that will write a log
260 sat.config('-v VARS.user')
262 nb_logs_t0 = len(os.listdir(sat.cfg.SITE.log.log_dir))
266 nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.log_dir))
268 if nb_logs_t1-nb_logs_t0 == 0:
271 # pyunit method to compare 2 str
272 self.assertEqual(OK, "OK")
274 def test_option_clean2(self):
275 '''Test the option --clean with big number of files to clean
280 # launch the command that will write a log
283 sat.config('-v VARS.user')
285 nb_logs_t0 = len(os.listdir(sat.cfg.SITE.log.log_dir))
287 if os.path.exists(sat.cfg.SITE.log.log_dir + "_save"):
288 shutil.rmtree(sat.cfg.SITE.log.log_dir + "_save")
289 shutil.copytree(sat.cfg.SITE.log.log_dir,sat.cfg.SITE.log.log_dir + "_save")
291 sat.log('--clean ' + str(nb_logs_t0))
293 nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.log_dir))
295 shutil.rmtree(sat.cfg.SITE.log.log_dir)
296 shutil.move(sat.cfg.SITE.log.log_dir + "_save", sat.cfg.SITE.log.log_dir)
298 if nb_logs_t0-nb_logs_t1 > 10:
301 # pyunit method to compare 2 str
302 self.assertEqual(OK, "OK")
305 def test_option_full(self):
306 '''Test the option --full
311 sat = Sat("-oUSER.browser='konqueror'")
312 time.sleep(sleep_time)
313 cmd_log = threading.Thread(target=sat.log, args=('--full',))
316 time.sleep(sleep_time)
318 browser = sat.cfg.USER.browser
319 check_proc_existence_and_kill(browser + ".*" + "hat\.xml")
321 # Read and check the hat.xml file contains at least one log file corresponding to log
322 hatFilePath = os.path.join(sat.cfg.SITE.log.log_dir, "hat.xml")
323 xmlHatFile = src.xmlManager.ReadXmlFile(hatFilePath)
324 for field in xmlHatFile.xmlroot:
325 if field.attrib[b'cmd'] == b'log':
329 # pyunit method to compare 2 str
330 self.assertEqual(OK, "OK")
332 def test_description(self):
333 '''Test the sat -h log
340 if "Gives access to the logs produced" in log.description():
343 # pyunit method to compare 2 str
344 self.assertEqual(OK, "OK")
347 if __name__ == '__main__':
348 HTMLTestRunner.main()