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