Salome HOME
c0cf72c4d685b39da30a7da4e627b3d696f76bdf
[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
35 from tools import outRedirection
36 import HTMLTestRunner
37 import src.xmlManager
38
39 sleep_time = 2
40
41 class TestLog(unittest.TestCase):
42     '''Test of log command: launch of browser
43     '''
44     
45     def test_launch_browser(self):
46         '''Test the launch of browser when invoking the log command
47         '''
48
49         OK = "KO"
50
51         sat = Sat("-oUSER.browser='konqueror'")
52         time.sleep(sleep_time)
53         cmd_log = threading.Thread(target=sat.log, args=('',))
54         cmd_log.start()
55
56         time.sleep(sleep_time)
57
58         browser = sat.cfg.USER.browser
59         pid = check_proc_existence_and_kill(browser + ".*" + "xml")
60
61         if pid:
62             OK = "OK"
63         # pyunit method to compare 2 str
64         self.assertEqual(OK, "OK")
65         
66     def test_write_xmllog(self):
67         '''Test the write of xml log when invoking a command
68         '''
69
70         OK = "KO"
71         
72         # launch the command that will write a log
73         sat = Sat()
74         sat.config('appli-test -v USER.browser')
75         
76         # get log file path
77         logDir = sat.cfg.SITE.log.log_dir
78         logPath = os.path.join(logDir, sat.cfg.VARS.datehour + "_" + sat.cfg.VARS.command + ".xml")
79         
80         if os.path.exists(logPath):
81             OK = "OK"
82         
83         # pyunit method to compare 2 str
84         self.assertEqual(OK, "OK")
85
86     def test_option_terminal(self):
87         '''Test the terminal option without application
88         '''
89
90         OK = "KO"
91         
92         # launch the command that will write a log
93         sat = Sat()
94         
95         one = u"1"
96         sys.stdin = io.StringIO(one)
97         
98         
99         try:
100             sat.log('-t')
101             OK = "OK"
102             sys.stdin = sys.__stdin__
103         except:
104             sys.stdin = sys.__stdin__
105         
106         # pyunit method to compare 2 str
107         self.assertEqual(OK, "OK")
108
109     def test_option_terminal2(self):
110         '''Test the terminal option with application
111         '''
112
113         OK = "KO"
114         
115         # launch the command that will write a log
116         sat = Sat()
117               
118         sat.config('appli-test -v VARS.python')
119         
120         one = u"1"
121         sys.stdin = io.StringIO(one)
122         
123         try:
124             sat.log('appli-test -t --last')
125             OK = "OK"
126             sys.stdin = sys.__stdin__
127         except:
128             pass
129         
130         # pyunit method to compare 2 str
131         self.assertEqual(OK, "OK")
132
133     def test_option_terminal3(self):
134         '''Test the terminal option with 0 as input
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         zero = u"0\n1"
145         sys.stdin = io.StringIO(zero)
146         
147         try:
148             sat.log('--terminal')
149             OK = "OK"
150         finally:
151             sys.stdin = sys.__stdin__
152         
153         # pyunit method to compare 2 str
154         self.assertEqual(OK, "OK")
155
156     def test_option_terminal4(self):
157         '''Test the terminal option with input bigger than the number of logs
158         '''
159
160         OK = "KO"
161         
162         # launch the command that will write a log
163         sat = Sat()
164               
165         sat.config('appli-test -v VARS.python')
166         
167         nb_logs = len(os.listdir(sat.cfg.SITE.log.log_dir))
168         
169         nb_logs_u = unicode(str(nb_logs) + "\n1")
170         sys.stdin = io.StringIO(nb_logs_u)
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_terminal5(self):
182         '''Test the terminal option with input return
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("\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_terminal6(self):
205         '''Test the terminal option with input not int
206         '''
207
208         OK = "KO"
209         
210         # launch the command that will write a log
211         sat = Sat()
212               
213         sat.config('appli-test -v VARS.python')
214         
215         ret = unicode("blabla\n0")
216         sys.stdin = io.StringIO(ret)
217         
218         try:
219             sat.log('--terminal')
220             OK = "OK"
221         finally:
222             sys.stdin = sys.__stdin__
223         
224         # pyunit method to compare 2 str
225         self.assertEqual(OK, "OK")
226
227     def test_option_terminal7(self):
228         '''Test the terminal option and option last
229         '''
230
231         OK = "KO"
232         
233         # launch the command that will write a log
234         sat = Sat()
235         
236         try:
237             sat.log('--terminal --last')
238             OK = "OK"
239         finally:
240             sys.stdin = sys.__stdin__
241         
242         # pyunit method to compare 2 str
243         self.assertEqual(OK, "OK")
244
245     def test_option_last(self):
246         '''Test the option --last
247         '''
248
249         OK = "KO"
250         
251         # launch the command that will write a log
252         sat = Sat("-oUSER.browser='konqueror'")
253               
254         sat.config('appli-test -v VARS.python')
255         
256         
257         time.sleep(sleep_time)
258         cmd_log = threading.Thread(target=sat.log, args=('appli-test --last',))
259         cmd_log.start()
260         
261         time.sleep(sleep_time)
262
263         browser = sat.cfg.USER.browser
264         pid = check_proc_existence_and_kill(browser + ".*" + "xml")
265         
266         if pid:
267             OK = "OK"
268         
269         # pyunit method to compare 2 str
270         self.assertEqual(OK, "OK")
271
272     def test_option_clean(self):
273         '''Test the option --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.SITE.log.log_dir))
284
285         sat.log('--clean 1')
286         
287         nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.log_dir))
288         
289         if nb_logs_t1-nb_logs_t0 == 0:
290             OK = "OK"
291         
292         # pyunit method to compare 2 str
293         self.assertEqual(OK, "OK")
294
295     def test_option_clean2(self):
296         '''Test the option --clean with big number of files to clean
297         '''
298
299         OK = "KO"
300         
301         # launch the command that will write a log
302         sat = Sat()
303                
304         sat.config('-v VARS.user')
305         
306         nb_logs_t0 = len(os.listdir(sat.cfg.SITE.log.log_dir))
307         
308         if os.path.exists(sat.cfg.SITE.log.log_dir + "_save"):
309             shutil.rmtree(sat.cfg.SITE.log.log_dir + "_save")
310         shutil.copytree(sat.cfg.SITE.log.log_dir,sat.cfg.SITE.log.log_dir + "_save")
311         
312         sat.log('--clean ' + str(nb_logs_t0))
313         
314         nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.log_dir))
315         
316         shutil.rmtree(sat.cfg.SITE.log.log_dir)
317         shutil.move(sat.cfg.SITE.log.log_dir + "_save", sat.cfg.SITE.log.log_dir)
318                 
319         if nb_logs_t0-nb_logs_t1 > 10:
320             OK = "OK"
321         
322         # pyunit method to compare 2 str
323         self.assertEqual(OK, "OK")
324
325     def test_option_full(self):
326         '''Test the option --full
327         '''
328
329         OK = "KO"
330
331         sat = Sat("-oUSER.browser='konqueror'")
332         time.sleep(sleep_time)
333         time.sleep(5)
334         cmd_log = threading.Thread(target=sat.log, args=('--full',))
335         cmd_log.start()
336
337         time.sleep(sleep_time)
338
339         browser = sat.cfg.USER.browser
340         check_proc_existence_and_kill(browser + ".*" + "xml")
341         
342         # Read and check the hat.xml file contains at least one log file corresponding to log
343         hatFilePath = os.path.join(sat.cfg.SITE.log.log_dir, "hat.xml")
344         xmlHatFile = src.xmlManager.ReadXmlFile(hatFilePath)
345         for field in xmlHatFile.xmlroot:
346             if field.attrib[b'cmd'] == b'log':
347                 OK = "OK"
348                 break
349
350         # pyunit method to compare 2 str
351         self.assertEqual(OK, "OK")
352
353     def test_description(self):
354         '''Test the sat -h log
355         '''        
356
357         OK = "KO"
358
359         import log
360         
361         if "Gives access to the logs produced" in log.description():
362             OK = "OK"
363
364         # pyunit method to compare 2 str
365         self.assertEqual(OK, "OK")
366
367 # test launch
368 if __name__ == '__main__':
369     HTMLTestRunner.main()