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
34 class TestCase(unittest.TestCase):
35 """Test of log command: launch of browser"""
38 # Test the write of xml log when invoking a command
41 # launch the command that will write a log
43 sat.config("appli-test -v USER.browser")
46 logDir = sat.cfg.USER.log_dir
47 logPath = os.path.join(
48 logDir, sat.cfg.VARS.datehour + "_" + sat.cfg.VARS.command + ".xml"
51 if os.path.exists(logPath):
53 self.assertEqual(OK, "OK")
56 # Test the terminal option without application
59 # launch the command that will write a log
63 sys.stdin = io.StringIO(one)
68 sys.stdin = sys.__stdin__
70 sys.stdin = sys.__stdin__
71 self.assertEqual(OK, "OK")
74 # Test the terminal option with application
77 # launch the command that will write a log
80 sat.config("appli-test -v VARS.python")
83 sys.stdin = io.StringIO(one)
86 sat.log("appli-test -t --last")
88 sys.stdin = sys.__stdin__
91 self.assertEqual(OK, "OK")
94 # Test the terminal option with 0 as input
97 # launch the command that will write a log
100 sat.config("appli-test -v VARS.python")
103 sys.stdin = io.StringIO(zero)
106 sat.log("--terminal")
109 sys.stdin = sys.__stdin__
110 self.assertEqual(OK, "OK")
113 # Test the terminal option with input bigger than the number of logs
116 # launch the command that will write a log
119 sat.config("appli-test -v VARS.python")
121 nb_logs = len(os.listdir(sat.cfg.USER.log_dir))
123 nb_logs_u = unicode(str(nb_logs) + "\n1")
124 sys.stdin = io.StringIO(nb_logs_u)
127 sat.log("--terminal")
130 sys.stdin = sys.__stdin__
131 self.assertEqual(OK, "OK")
134 # Test the terminal option with input return
137 # launch the command that will write a log
140 sat.config("appli-test -v VARS.python")
143 sys.stdin = io.StringIO(ret)
146 sat.log("--terminal")
149 sys.stdin = sys.__stdin__
150 self.assertEqual(OK, "OK")
153 # Test the terminal option with input not int
156 # launch the command that will write a log
159 sat.config("appli-test -v VARS.python")
161 ret = unicode("blabla\n0")
162 sys.stdin = io.StringIO(ret)
165 sat.log("--terminal")
168 sys.stdin = sys.__stdin__
169 self.assertEqual(OK, "OK")
172 # Test the terminal option and option last
175 # launch the command that will write a log
179 sat.log("--terminal --last")
182 sys.stdin = sys.__stdin__
184 # pyunit method to compare 2 str
185 self.assertEqual(OK, "OK")
188 # Test the option --last
191 # launch the command that will write a log
192 sat = Sat("-oUSER.browser='konqueror'")
194 sat.config("appli-test -v VARS.python")
196 time.sleep(sleep_time)
197 cmd_log = threading.Thread(target=sat.log, args=("appli-test --last",))
200 time.sleep(sleep_time)
202 browser = sat.cfg.USER.browser
203 pid = check_proc_existence_and_kill_multi(browser + ".*" + "xml", 10)
207 self.assertEqual(OK, "OK")
210 # Test the option --clean
213 # launch the command that will write a log
216 sat.config("-v VARS.user")
218 nb_logs_t0 = len(os.listdir(sat.cfg.USER.log_dir))
222 nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
224 if nb_logs_t1 - nb_logs_t0 == 0:
226 self.assertEqual(OK, "OK")
229 # Test the option --clean with big number of files to clean
232 # launch the command that will write a log
235 sat.config("-v VARS.user")
237 nb_logs_t0 = len(os.listdir(sat.cfg.USER.log_dir))
239 if os.path.exists(sat.cfg.USER.log_dir + "_save"):
240 shutil.rmtree(sat.cfg.USER.log_dir + "_save")
242 "TODO: risky !!!copytree!!!",
243 sat.cfg.USER.log_dir,
244 sat.cfg.USER.log_dir + "_save",
247 shutil.copytree(sat.cfg.USER.log_dir,sat.cfg.USER.log_dir + "_save")
249 sat.log('--clean ' + str(nb_logs_t0))
251 nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
253 shutil.rmtree(sat.cfg.USER.log_dir)
254 shutil.move(sat.cfg.USER.log_dir + "_save", sat.cfg.USER.log_dir)
256 if nb_logs_t0-nb_logs_t1 > 10:
259 self.assertEqual(OK, "OK")
263 # Test the option --full
266 sat = Sat("-oUSER.browser='konqueror'")
267 time.sleep(sleep_time)
268 cmd_log = threading.Thread(target=sat.log, args=('--full',))
271 time.sleep(sleep_time)
273 browser = sat.cfg.USER.browser
274 check_proc_existence_and_kill_multi(browser + ".*" + "hat\.xml", 10)
276 # Read and check the hat.xml file contains at least one log file corresponding to log
277 hatFilePath = os.path.join(sat.cfg.USER.log_dir, "hat.xml")
278 xmlHatFile = src.xmlManager.ReadXmlFile(hatFilePath)
279 for field in xmlHatFile.xmlroot:
280 if field.attrib[b'cmd'] == b'log':
283 self.assertEqual(OK, "OK")
287 # Test the sat -h log
292 if "Gives access to the logs produced" in log.description():
294 self.assertEqual(OK, "OK")
298 if __name__ == "__main__":