4 # Copyright (C) 2010-2018 CEA/DEN
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 from src.salomeTools import Sat
29 from unittestpy.tools import check_proc_existence_and_kill_multi
33 class TestCase(unittest.TestCase):
34 """Test of log command: launch of browser"""
37 # Test the write of xml log when invoking a command
40 # launch the command that will write a log
42 sat.config('appli-test -v USER.browser')
45 logDir = sat.cfg.USER.log_dir
46 logPath = os.path.join(logDir, sat.cfg.VARS.datehour + "_" + sat.cfg.VARS.command + ".xml")
48 if os.path.exists(logPath):
50 self.assertEqual(OK, "OK")
53 # Test the terminal option without application
56 # launch the command that will write a log
60 sys.stdin = io.StringIO(one)
66 sys.stdin = sys.__stdin__
68 sys.stdin = sys.__stdin__
69 self.assertEqual(OK, "OK")
72 # Test the terminal option with application
75 # launch the command that will write a log
78 sat.config('appli-test -v VARS.python')
81 sys.stdin = io.StringIO(one)
84 sat.log('appli-test -t --last')
86 sys.stdin = sys.__stdin__
89 self.assertEqual(OK, "OK")
92 # Test the terminal option with 0 as input
95 # launch the command that will write a log
98 sat.config('appli-test -v VARS.python')
101 sys.stdin = io.StringIO(zero)
104 sat.log('--terminal')
107 sys.stdin = sys.__stdin__
108 self.assertEqual(OK, "OK")
111 # Test the terminal option with input bigger than the number of logs
114 # launch the command that will write a log
117 sat.config('appli-test -v VARS.python')
119 nb_logs = len(os.listdir(sat.cfg.USER.log_dir))
121 nb_logs_u = unicode(str(nb_logs) + "\n1")
122 sys.stdin = io.StringIO(nb_logs_u)
125 sat.log('--terminal')
128 sys.stdin = sys.__stdin__
129 self.assertEqual(OK, "OK")
132 # Test the terminal option with input return
135 # launch the command that will write a log
138 sat.config('appli-test -v VARS.python')
141 sys.stdin = io.StringIO(ret)
144 sat.log('--terminal')
147 sys.stdin = sys.__stdin__
148 self.assertEqual(OK, "OK")
151 # Test the terminal option with input not int
154 # launch the command that will write a log
157 sat.config('appli-test -v VARS.python')
159 ret = unicode("blabla\n0")
160 sys.stdin = io.StringIO(ret)
163 sat.log('--terminal')
166 sys.stdin = sys.__stdin__
167 self.assertEqual(OK, "OK")
170 # Test the terminal option and option last
173 # launch the command that will write a log
177 sat.log('--terminal --last')
180 sys.stdin = sys.__stdin__
182 # pyunit method to compare 2 str
183 self.assertEqual(OK, "OK")
186 # Test the option --last
189 # launch the command that will write a log
190 sat = Sat("-oUSER.browser='konqueror'")
192 sat.config('appli-test -v VARS.python')
195 time.sleep(sleep_time)
196 cmd_log = threading.Thread(target=sat.log, args=('appli-test --last',))
199 time.sleep(sleep_time)
201 browser = sat.cfg.USER.browser
202 pid = check_proc_existence_and_kill_multi(browser + ".*" + "xml", 10)
206 self.assertEqual(OK, "OK")
209 # Test the option --clean
212 # launch the command that will write a log
215 sat.config('-v VARS.user')
217 nb_logs_t0 = len(os.listdir(sat.cfg.USER.log_dir))
221 nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
223 if nb_logs_t1-nb_logs_t0 == 0:
225 self.assertEqual(OK, "OK")
228 # Test the option --clean with big number of files to clean
231 # launch the command that will write a log
234 sat.config('-v VARS.user')
236 nb_logs_t0 = len(os.listdir(sat.cfg.USER.log_dir))
238 if os.path.exists(sat.cfg.USER.log_dir + "_save"):
239 shutil.rmtree(sat.cfg.USER.log_dir + "_save")
240 print("TODO: risky !!!copytree!!!", sat.cfg.USER.log_dir, sat.cfg.USER.log_dir + "_save")
242 shutil.copytree(sat.cfg.USER.log_dir,sat.cfg.USER.log_dir + "_save")
244 sat.log('--clean ' + str(nb_logs_t0))
246 nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
248 shutil.rmtree(sat.cfg.USER.log_dir)
249 shutil.move(sat.cfg.USER.log_dir + "_save", sat.cfg.USER.log_dir)
251 if nb_logs_t0-nb_logs_t1 > 10:
254 self.assertEqual(OK, "OK")
258 # Test the option --full
261 sat = Sat("-oUSER.browser='konqueror'")
262 time.sleep(sleep_time)
263 cmd_log = threading.Thread(target=sat.log, args=('--full',))
266 time.sleep(sleep_time)
268 browser = sat.cfg.USER.browser
269 check_proc_existence_and_kill_multi(browser + ".*" + "hat\.xml", 10)
271 # Read and check the hat.xml file contains at least one log file corresponding to log
272 hatFilePath = os.path.join(sat.cfg.USER.log_dir, "hat.xml")
273 xmlHatFile = src.xmlManager.ReadXmlFile(hatFilePath)
274 for field in xmlHatFile.xmlroot:
275 if field.attrib[b'cmd'] == b'log':
278 self.assertEqual(OK, "OK")
282 # Test the sat -h log
287 if "Gives access to the logs produced" in log.description():
289 self.assertEqual(OK, "OK")
292 if __name__ == '__main__':