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