Salome HOME
style: black format
[tools/sat.git] / test / test_sat5_0 / log / test_launch_browser.py
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3
4 #  Copyright (C) 2010-2018  CEA/DEN
5 #
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.
10 #
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.
15 #
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
19
20 import os
21 import sys
22 import threading
23 import time
24 import shutil
25 import io
26 import unittest
27
28 from src.salomeTools import Sat
29 from unittestpy.tools import check_proc_existence_and_kill_multi
30
31 sleep_time = 2
32
33
34 class TestCase(unittest.TestCase):
35     """Test of log command: launch of browser"""
36
37     def test_010(self):
38         # Test the write of xml log when invoking a command
39         OK = "KO"
40
41         # launch the command that will write a log
42         sat = Sat()
43         sat.config("appli-test -v USER.browser")
44
45         # get log file path
46         logDir = sat.cfg.USER.log_dir
47         logPath = os.path.join(
48             logDir, sat.cfg.VARS.datehour + "_" + sat.cfg.VARS.command + ".xml"
49         )
50
51         if os.path.exists(logPath):
52             OK = "OK"
53         self.assertEqual(OK, "OK")
54
55     def test_020(self):
56         # Test the terminal option without application
57         OK = "KO"
58
59         # launch the command that will write a log
60         sat = Sat()
61
62         one = u"1"
63         sys.stdin = io.StringIO(one)
64
65         try:
66             sat.log("-t")
67             OK = "OK"
68             sys.stdin = sys.__stdin__
69         except:
70             sys.stdin = sys.__stdin__
71         self.assertEqual(OK, "OK")
72
73     def test_030(self):
74         # Test the terminal option with application
75         OK = "KO"
76
77         # launch the command that will write a log
78         sat = Sat()
79
80         sat.config("appli-test -v VARS.python")
81
82         one = u"1"
83         sys.stdin = io.StringIO(one)
84
85         try:
86             sat.log("appli-test -t --last")
87             OK = "OK"
88             sys.stdin = sys.__stdin__
89         except:
90             pass
91         self.assertEqual(OK, "OK")
92
93     def test_040(self):
94         # Test the terminal option with 0 as input
95         OK = "KO"
96
97         # launch the command that will write a log
98         sat = Sat()
99
100         sat.config("appli-test -v VARS.python")
101
102         zero = u"0\n1"
103         sys.stdin = io.StringIO(zero)
104
105         try:
106             sat.log("--terminal")
107             OK = "OK"
108         finally:
109             sys.stdin = sys.__stdin__
110         self.assertEqual(OK, "OK")
111
112     def test_050(self):
113         # Test the terminal option with input bigger than the number of logs
114         OK = "KO"
115
116         # launch the command that will write a log
117         sat = Sat()
118
119         sat.config("appli-test -v VARS.python")
120
121         nb_logs = len(os.listdir(sat.cfg.USER.log_dir))
122
123         nb_logs_u = unicode(str(nb_logs) + "\n1")
124         sys.stdin = io.StringIO(nb_logs_u)
125
126         try:
127             sat.log("--terminal")
128             OK = "OK"
129         finally:
130             sys.stdin = sys.__stdin__
131         self.assertEqual(OK, "OK")
132
133     def test_060(self):
134         # Test the terminal option with input return
135         OK = "KO"
136
137         # launch the command that will write a log
138         sat = Sat()
139
140         sat.config("appli-test -v VARS.python")
141
142         ret = unicode("\n0")
143         sys.stdin = io.StringIO(ret)
144
145         try:
146             sat.log("--terminal")
147             OK = "OK"
148         finally:
149             sys.stdin = sys.__stdin__
150         self.assertEqual(OK, "OK")
151
152     def test_070(self):
153         # Test the terminal option with input not int
154         OK = "KO"
155
156         # launch the command that will write a log
157         sat = Sat()
158
159         sat.config("appli-test -v VARS.python")
160
161         ret = unicode("blabla\n0")
162         sys.stdin = io.StringIO(ret)
163
164         try:
165             sat.log("--terminal")
166             OK = "OK"
167         finally:
168             sys.stdin = sys.__stdin__
169         self.assertEqual(OK, "OK")
170
171     def test_080(self):
172         # Test the terminal option and option last
173         OK = "KO"
174
175         # launch the command that will write a log
176         sat = Sat()
177
178         try:
179             sat.log("--terminal --last")
180             OK = "OK"
181         finally:
182             sys.stdin = sys.__stdin__
183
184         # pyunit method to compare 2 str
185         self.assertEqual(OK, "OK")
186
187     def test_090(self):
188         # Test the option --last
189         OK = "KO"
190
191         # launch the command that will write a log
192         sat = Sat("-oUSER.browser='konqueror'")
193
194         sat.config("appli-test -v VARS.python")
195
196         time.sleep(sleep_time)
197         cmd_log = threading.Thread(target=sat.log, args=("appli-test --last",))
198         cmd_log.start()
199
200         time.sleep(sleep_time)
201
202         browser = sat.cfg.USER.browser
203         pid = check_proc_existence_and_kill_multi(browser + ".*" + "xml", 10)
204
205         if pid:
206             OK = "OK"
207         self.assertEqual(OK, "OK")
208
209     def test_100(self):
210         # Test the option --clean
211         OK = "KO"
212
213         # launch the command that will write a log
214         sat = Sat()
215
216         sat.config("-v VARS.user")
217
218         nb_logs_t0 = len(os.listdir(sat.cfg.USER.log_dir))
219
220         sat.log("--clean 1")
221
222         nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
223
224         if nb_logs_t1 - nb_logs_t0 == 0:
225             OK = "OK"
226         self.assertEqual(OK, "OK")
227
228     def test_120(self):
229         # Test the option --clean with big number of files to clean
230         OK = "KO"
231
232         # launch the command that will write a log
233         sat = Sat()
234
235         sat.config("-v VARS.user")
236
237         nb_logs_t0 = len(os.listdir(sat.cfg.USER.log_dir))
238
239         if os.path.exists(sat.cfg.USER.log_dir + "_save"):
240             shutil.rmtree(sat.cfg.USER.log_dir + "_save")
241         print(
242             "TODO: risky !!!copytree!!!",
243             sat.cfg.USER.log_dir,
244             sat.cfg.USER.log_dir + "_save",
245         )
246         """
247         shutil.copytree(sat.cfg.USER.log_dir,sat.cfg.USER.log_dir + "_save")
248         
249         sat.log('--clean ' + str(nb_logs_t0))
250         
251         nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
252         
253         shutil.rmtree(sat.cfg.USER.log_dir)
254         shutil.move(sat.cfg.USER.log_dir + "_save", sat.cfg.USER.log_dir)
255                 
256         if nb_logs_t0-nb_logs_t1 > 10:
257             OK = "OK"
258         """
259         self.assertEqual(OK, "OK")
260
261     """
262     def test_130(self):
263         # Test the option --full
264         OK = "KO"
265
266         sat = Sat("-oUSER.browser='konqueror'")
267         time.sleep(sleep_time)
268         cmd_log = threading.Thread(target=sat.log, args=('--full',))
269         cmd_log.start()
270
271         time.sleep(sleep_time)
272
273         browser = sat.cfg.USER.browser
274         check_proc_existence_and_kill_multi(browser + ".*" + "hat\.xml", 10)
275         
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':
281                 OK = "OK"
282                 break
283         self.assertEqual(OK, "OK")
284     """
285
286     def test_140(self):
287         # Test the sat -h log
288         OK = "KO"
289
290         import log
291
292         if "Gives access to the logs produced" in log.description():
293             OK = "OK"
294         self.assertEqual(OK, "OK")
295
296
297 # test launch
298 if __name__ == "__main__":
299     unittest.main()
300     pass