Salome HOME
Improve tests
[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.logDir
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.logDir))
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_last(self):
227         '''Test the option --last
228         '''
229
230         OK = "KO"
231         
232         # launch the command that will write a log
233         sat = Sat("-oUSER.browser='konqueror'")
234               
235         sat.config('appli-test -v VARS.python')
236         
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.logDir))
263
264         sat.log('--clean 1')
265         
266         nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.logDir))
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.logDir))
286         
287         if os.path.exists(sat.cfg.SITE.log.logDir + "_save"):
288             shutil.rmtree(sat.cfg.SITE.log.logDir + "_save")
289         shutil.copytree(sat.cfg.SITE.log.logDir,sat.cfg.SITE.log.logDir + "_save")
290         
291         sat.log('--clean ' + str(nb_logs_t0))
292         
293         nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.logDir))
294         
295         shutil.rmtree(sat.cfg.SITE.log.logDir)
296         shutil.move(sat.cfg.SITE.log.logDir + "_save", sat.cfg.SITE.log.logDir)
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     def test_option_full(self):
305         '''Test the option --full
306         '''
307
308         OK = "KO"
309
310         sat = Sat("-oUSER.browser='konqueror'")
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(browser + ".*" + "xml")
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.SITE.log.logDir, "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()