Salome HOME
suppresion d'un log
[tools/sat.git] / test / log / launch_browser.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2012  CEA/DEN
4 #
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.
9 #
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.
14 #
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
18
19 import unittest
20 import os
21 import sys
22 import threading
23 import time
24 import shutil
25 import io
26
27 # get execution path
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'))
32
33 from salomeTools import Sat
34 from tools import check_proc_existence_and_kill_multi
35 import HTMLTestRunner
36
37 sleep_time = 2
38
39 class TestLog(unittest.TestCase):
40     '''Test of log command: launch of browser
41     '''
42            
43     def test_write_xmllog(self):
44         '''Test the write of xml log when invoking a command
45         '''
46
47         OK = "KO"
48         
49         # launch the command that will write a log
50         sat = Sat()
51         sat.config('appli-test -v USER.browser')
52         
53         # get log file path
54         logDir = sat.cfg.USER.log_dir
55         logPath = os.path.join(logDir, sat.cfg.VARS.datehour + "_" + sat.cfg.VARS.command + ".xml")
56         
57         if os.path.exists(logPath):
58             OK = "OK"
59         
60         # pyunit method to compare 2 str
61         self.assertEqual(OK, "OK")
62
63     def test_option_terminal(self):
64         '''Test the terminal option without application
65         '''
66
67         OK = "KO"
68         
69         # launch the command that will write a log
70         sat = Sat()
71         
72         one = u"1"
73         sys.stdin = io.StringIO(one)
74         
75         
76         try:
77             sat.log('-t')
78             OK = "OK"
79             sys.stdin = sys.__stdin__
80         except:
81             sys.stdin = sys.__stdin__
82         
83         # pyunit method to compare 2 str
84         self.assertEqual(OK, "OK")
85
86     def test_option_terminal2(self):
87         '''Test the terminal option with application
88         '''
89
90         OK = "KO"
91         
92         # launch the command that will write a log
93         sat = Sat()
94               
95         sat.config('appli-test -v VARS.python')
96         
97         one = u"1"
98         sys.stdin = io.StringIO(one)
99         
100         try:
101             sat.log('appli-test -t --last')
102             OK = "OK"
103             sys.stdin = sys.__stdin__
104         except:
105             pass
106         
107         # pyunit method to compare 2 str
108         self.assertEqual(OK, "OK")
109
110     def test_option_terminal3(self):
111         '''Test the terminal option with 0 as input
112         '''
113
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         zero = u"0\n1"
122         sys.stdin = io.StringIO(zero)
123         
124         try:
125             sat.log('--terminal')
126             OK = "OK"
127         finally:
128             sys.stdin = sys.__stdin__
129         
130         # pyunit method to compare 2 str
131         self.assertEqual(OK, "OK")
132
133     def test_option_terminal4(self):
134         '''Test the terminal option with input bigger than the number of logs
135         '''
136
137         OK = "KO"
138         
139         # launch the command that will write a log
140         sat = Sat()
141               
142         sat.config('appli-test -v VARS.python')
143         
144         nb_logs = len(os.listdir(sat.cfg.USER.log_dir))
145         
146         nb_logs_u = unicode(str(nb_logs) + "\n1")
147         sys.stdin = io.StringIO(nb_logs_u)
148         
149         try:
150             sat.log('--terminal')
151             OK = "OK"
152         finally:
153             sys.stdin = sys.__stdin__
154         
155         # pyunit method to compare 2 str
156         self.assertEqual(OK, "OK")
157
158     def test_option_terminal5(self):
159         '''Test the terminal option with input return
160         '''
161
162         OK = "KO"
163         
164         # launch the command that will write a log
165         sat = Sat()
166               
167         sat.config('appli-test -v VARS.python')
168         
169         ret = unicode("\n0")
170         sys.stdin = io.StringIO(ret)
171         
172         try:
173             sat.log('--terminal')
174             OK = "OK"
175         finally:
176             sys.stdin = sys.__stdin__
177         
178         # pyunit method to compare 2 str
179         self.assertEqual(OK, "OK")
180
181     def test_option_terminal6(self):
182         '''Test the terminal option with input not int
183         '''
184
185         OK = "KO"
186         
187         # launch the command that will write a log
188         sat = Sat()
189               
190         sat.config('appli-test -v VARS.python')
191         
192         ret = unicode("blabla\n0")
193         sys.stdin = io.StringIO(ret)
194         
195         try:
196             sat.log('--terminal')
197             OK = "OK"
198         finally:
199             sys.stdin = sys.__stdin__
200         
201         # pyunit method to compare 2 str
202         self.assertEqual(OK, "OK")
203
204     def test_option_terminal7(self):
205         '''Test the terminal option and option last
206         '''
207
208         OK = "KO"
209         
210         # launch the command that will write a log
211         sat = Sat()
212         
213         try:
214             sat.log('--terminal --last')
215             OK = "OK"
216         finally:
217             sys.stdin = sys.__stdin__
218         
219         # pyunit method to compare 2 str
220         self.assertEqual(OK, "OK")
221     
222     def test_option_last(self):
223         '''Test the option --last
224         '''
225
226         OK = "KO"
227         
228         # launch the command that will write a log
229         sat = Sat("-oUSER.browser='konqueror'")
230               
231         sat.config('appli-test -v VARS.python')
232         
233         
234         time.sleep(sleep_time)
235         cmd_log = threading.Thread(target=sat.log, args=('appli-test --last',))
236         cmd_log.start()
237         
238         time.sleep(sleep_time)
239
240         browser = sat.cfg.USER.browser
241         pid = check_proc_existence_and_kill_multi(browser + ".*" + "xml", 10)
242         
243         if pid:
244             OK = "OK"
245         
246         # pyunit method to compare 2 str
247         self.assertEqual(OK, "OK")
248     
249     def test_option_clean(self):
250         '''Test the option --clean
251         '''
252
253         OK = "KO"
254         
255         # launch the command that will write a log
256         sat = Sat()
257                
258         sat.config('-v VARS.user')
259         
260         nb_logs_t0 = len(os.listdir(sat.cfg.USER.log_dir))
261
262         sat.log('--clean 1')
263         
264         nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
265         
266         if nb_logs_t1-nb_logs_t0 == 0:
267             OK = "OK"
268         
269         # pyunit method to compare 2 str
270         self.assertEqual(OK, "OK")
271
272     def test_option_clean2(self):
273         '''Test the option --clean with big number of files to clean
274         '''
275
276         OK = "KO"
277         
278         # launch the command that will write a log
279         sat = Sat()
280                
281         sat.config('-v VARS.user')
282         
283         nb_logs_t0 = len(os.listdir(sat.cfg.USER.log_dir))
284         
285         if os.path.exists(sat.cfg.USER.log_dir + "_save"):
286             shutil.rmtree(sat.cfg.USER.log_dir + "_save")
287         shutil.copytree(sat.cfg.USER.log_dir,sat.cfg.USER.log_dir + "_save")
288         
289         sat.log('--clean ' + str(nb_logs_t0))
290         
291         nb_logs_t1 = len(os.listdir(sat.cfg.USER.log_dir))
292         
293         shutil.rmtree(sat.cfg.USER.log_dir)
294         shutil.move(sat.cfg.USER.log_dir + "_save", sat.cfg.USER.log_dir)
295                 
296         if nb_logs_t0-nb_logs_t1 > 10:
297             OK = "OK"
298         
299         # pyunit method to compare 2 str
300         self.assertEqual(OK, "OK")
301     
302     """
303     def test_option_full(self):
304         '''Test the option --full
305         '''
306
307         OK = "KO"
308
309         sat = Sat("-oUSER.browser='konqueror'")
310         time.sleep(sleep_time)
311         cmd_log = threading.Thread(target=sat.log, args=('--full',))
312         cmd_log.start()
313
314         time.sleep(sleep_time)
315
316         browser = sat.cfg.USER.browser
317         check_proc_existence_and_kill_multi(browser + ".*" + "hat\.xml", 10)
318         
319         # Read and check the hat.xml file contains at least one log file corresponding to log
320         hatFilePath = os.path.join(sat.cfg.USER.log_dir, "hat.xml")
321         xmlHatFile = src.xmlManager.ReadXmlFile(hatFilePath)
322         for field in xmlHatFile.xmlroot:
323             if field.attrib[b'cmd'] == b'log':
324                 OK = "OK"
325                 break
326
327         # pyunit method to compare 2 str
328         self.assertEqual(OK, "OK")
329     """
330     def test_description(self):
331         '''Test the sat -h log
332         '''        
333
334         OK = "KO"
335
336         import log
337         
338         if "Gives access to the logs produced" in log.description():
339             OK = "OK"
340
341         # pyunit method to compare 2 str
342         self.assertEqual(OK, "OK")
343
344 # test launch
345 if __name__ == '__main__':
346     HTMLTestRunner.main()