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