179
|
1 |
#============================================================================
|
|
2 |
#Name : test_filter_heliumlog.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
|
|
20 |
""" Test cases for filter meta data logs.
|
|
21 |
|
|
22 |
"""
|
|
23 |
|
|
24 |
# pylint: disable-msg=E1101
|
|
25 |
|
|
26 |
import logging
|
|
27 |
import os
|
|
28 |
import sys
|
|
29 |
import time
|
|
30 |
import mocker
|
|
31 |
import re
|
|
32 |
import tempfile
|
|
33 |
import shutil
|
|
34 |
|
|
35 |
_logger = logging.getLogger('test.test_filter_heliumlog')
|
|
36 |
|
|
37 |
TEMP_PATH = tempfile.mkdtemp()
|
|
38 |
#NOTE: THE COMMENTED OUT CODE IS REQUIRED WHEN RUNNING THIS TEST USING THE PT TARGET
|
|
39 |
#WITH THE COMMENTED OUT CODE IT DOES NOT WORK IN THE FULL UNITTEST TARGET DUE TO RAPTOR.PY
|
|
40 |
#FILE BEING CALLED WITHOUT ALL THE ENVIRONMENT VARIABLES SET WHEN RUN SINGLY AND THEY ARE SET WHEN
|
|
41 |
#RUN AS PART OF THE UNITTEST TARGET.
|
|
42 |
|
|
43 |
PYTHON_FILE_NAME = os.path.join(TEMP_PATH, 'raptor.py')
|
|
44 |
FILTER_FILE_NAME = os.path.join(TEMP_PATH, 'filter_interface.py')
|
|
45 |
|
|
46 |
sys.path = [TEMP_PATH] + sys.path
|
|
47 |
print "temp path is %s " % TEMP_PATH
|
|
48 |
|
|
49 |
def setup_module():
|
|
50 |
""" Setup for test_filter_metadata """
|
|
51 |
#in order to allow the filter_metadata.py file to compile we need to create a dummy
|
|
52 |
#raptor.py file as this is part of SBS and not present here, the classes and objects
|
|
53 |
#it uses from raptor.py file are created in this file, the same goes for the file filter_interface.py
|
|
54 |
|
|
55 |
#does not exist so create empty python.py file
|
|
56 |
f_handle = open(PYTHON_FILE_NAME, "w")
|
|
57 |
f_handle.write("class testRaptor():\n")
|
|
58 |
f_handle.write(" def testRaptorFunc(self):\n")
|
|
59 |
f_handle.write(" return True \n")
|
|
60 |
f_handle.close()
|
|
61 |
|
|
62 |
#does not exist so create empty python.py file
|
|
63 |
f_handle = open(FILTER_FILE_NAME, "w")
|
|
64 |
f_handle.write("class Filter():\n")
|
|
65 |
f_handle.write(" def testFilterFunc(self):\n")
|
|
66 |
f_handle.write(" return True \n")
|
|
67 |
f_handle.close()
|
|
68 |
|
|
69 |
|
|
70 |
def teardown_module():
|
|
71 |
""" Teardown test_filter_metadata. """
|
|
72 |
if os.path.exists(TEMP_PATH):
|
|
73 |
shutil.rmtree(TEMP_PATH)
|
|
74 |
|
|
75 |
# regex for "bare" drive letters
|
|
76 |
DRIVERE = re.compile('^[A-Za-z]:$')
|
|
77 |
|
|
78 |
# are we on windows, and if so what is the current drive letter
|
|
79 |
ISWIN = sys.platform.lower().startswith("win")
|
|
80 |
if ISWIN:
|
|
81 |
DRIVE = re.match('^([A-Za-z]:)', os.getcwd()).group(0)
|
|
82 |
|
|
83 |
# Base class
|
|
84 |
class Path(object):
|
|
85 |
"""This class represents a file path.
|
|
86 |
|
|
87 |
A generic path object supports operations without needing to know
|
|
88 |
about Windows and Linux differences. The standard str() function can
|
|
89 |
obtain a string version of the path in Local format for use by
|
|
90 |
platform-specific functions (file opening for example).
|
|
91 |
|
|
92 |
We use forward slashes as path separators (even on Windows).
|
|
93 |
|
|
94 |
For example,
|
|
95 |
|
|
96 |
path1 = generic_path.Path("/foo")
|
|
97 |
path2 = generic_path.Path("bar", "bing.bang")
|
|
98 |
|
|
99 |
print str(path1.Append(path2))
|
|
100 |
|
|
101 |
Prints /foo/bar/bing.bang on Linux
|
|
102 |
Prints c:/foo/bar/bing.bang on Windows (if c is the current drive)
|
|
103 |
"""
|
|
104 |
|
|
105 |
def __init__(self, *arguments):
|
|
106 |
"""construct a path from a list of path elements"""
|
|
107 |
|
|
108 |
if len(arguments) == 0:
|
|
109 |
self.path = ""
|
|
110 |
return
|
|
111 |
|
|
112 |
list = []
|
|
113 |
for i, arg in enumerate(arguments):
|
|
114 |
if ISWIN:
|
|
115 |
if i == 0:
|
|
116 |
# If the first element starts with \ or / then we will
|
|
117 |
# add the current drive letter to make a fully absolute path
|
|
118 |
if arg.startswith("\\\\"):
|
|
119 |
list.append(arg) # A UNC path - don't mess with it
|
|
120 |
elif arg.startswith("\\") or arg.startswith("/"):
|
|
121 |
list.append(DRIVE + arg)
|
|
122 |
# If the first element is a bare drive then dress it with a \
|
|
123 |
# temporarily otherwise "join" will not work properly.
|
|
124 |
elif DRIVERE.match(arg):
|
|
125 |
list.append(arg + "\\")
|
|
126 |
# nothing special about the first element
|
|
127 |
else:
|
|
128 |
list.append(arg)
|
|
129 |
else:
|
|
130 |
if arg.startswith("\\\\"):
|
|
131 |
raise ValueError("non-initial path components must not start with \\\\ : %s" % arg)
|
|
132 |
else:
|
|
133 |
list.append(arg)
|
|
134 |
if ";" in arg:
|
|
135 |
raise ValueError("An individual windows Path may not contain ';' : %s" % arg)
|
|
136 |
else:
|
|
137 |
list.append(arg)
|
|
138 |
|
|
139 |
self.path = os.path.join(*list)
|
|
140 |
|
|
141 |
# normalise to avoid nastiness with dots and multiple separators
|
|
142 |
# but do not normalise "" as it will become "."
|
|
143 |
if self.path != "":
|
|
144 |
self.path = os.path.normpath(self.path)
|
|
145 |
|
|
146 |
# always use forward slashes as separators
|
|
147 |
self.path = self.path.replace("\\", "/")
|
|
148 |
|
|
149 |
# remove trailing slashes unless we are just /
|
|
150 |
if self.path != "/":
|
|
151 |
self.path = self.path.rstrip("/")
|
|
152 |
|
|
153 |
def Dir(self):
|
|
154 |
"return an object for the directory part of this path"
|
|
155 |
if DRIVERE.match(self.path):
|
|
156 |
return Path(self.path)
|
|
157 |
else:
|
|
158 |
return Path(os.path.dirname(self.path))
|
|
159 |
def __str__(self):
|
|
160 |
return self.path
|
|
161 |
|
|
162 |
|
|
163 |
class Raptor(object):
|
|
164 |
"""An instance of a running Raptor program.
|
|
165 |
|
|
166 |
When operated from the command-line there is a single Raptor object
|
|
167 |
created by the Main function. When operated by an IDE several Raptor
|
|
168 |
objects may be created and operated at the same time."""
|
|
169 |
|
|
170 |
def defaultSetUp(self):
|
|
171 |
""" setup some variables for use by the unit under test """
|
|
172 |
self.logFile = None #self.mocker.proxy(generic_path.Join)
|
|
173 |
self.summary = True
|
|
174 |
# things to initialise
|
|
175 |
self.starttime = time.time()
|
|
176 |
self.timestring = time.strftime("%Y-%m-%d-%H-%M-%S")
|
|
177 |
self.logFileName = Path(os.path.join(TEMP_PATH, 'makefile_2009-11-12-12-32-34.log'))
|
|
178 |
|
|
179 |
class FilterHeliumLogTest(mocker.MockerTestCase):
|
|
180 |
""" Tests the filter_heliumLog wrapper to the SBS plugin.
|
|
181 |
The plugin uses the SBS API of open, write, summary and close. """
|
|
182 |
|
|
183 |
# def test_a_setupOK(self): #need to call it this to get it executed in the right order (alphabetical)
|
|
184 |
# """test_a_setupOK: tests that we have imported the correct files and that they are present
|
|
185 |
# before importing the file to be tested and having it fail due to missing files"""
|
|
186 |
# import filter_heliumlog
|
|
187 |
# #setup an instance of the class and test it exists OK
|
|
188 |
# obj = filter_heliumlog.raptor.testRaptor()
|
|
189 |
# result = obj.testRaptorFunc()
|
|
190 |
# assert result == True
|
|
191 |
# obj = filter_heliumlog.filter_interface.Filter()
|
|
192 |
# result = obj.testFilterFunc()
|
|
193 |
# assert result == True
|
|
194 |
|
|
195 |
def test_openWriteSummaryCloseInvalidData(self):
|
|
196 |
"""test_openWriteSummaryCloseInvalidData: test the opening writing, summary and
|
|
197 |
closing of the log file with invalid data."""
|
|
198 |
import filter_heliumlog
|
|
199 |
obj = self.mocker.patch(filter_heliumlog.SBSScanlog)
|
|
200 |
obj.initialize(mocker.ANY)
|
|
201 |
self.mocker.result(False)
|
|
202 |
self.mocker.replay()
|
|
203 |
|
|
204 |
raptor_instance = Raptor()
|
|
205 |
raptor_instance.defaultSetUp()
|
|
206 |
raptor_instance.logFileName = Path("..")
|
|
207 |
filter_HLog = filter_heliumlog.FilterHeliumLog()
|
|
208 |
result = filter_HLog.open(raptor_instance)
|
|
209 |
assert result == False
|
|
210 |
result = filter_HLog.close()
|
|
211 |
assert result == False
|
|
212 |
|
|
213 |
def test_openValidData_default(self):
|
|
214 |
"""test_openValidData_default: test the opening of the log file with valid data."""
|
|
215 |
import filter_heliumlog
|
|
216 |
obj = self.mocker.patch(filter_heliumlog.SBSScanlog)
|
|
217 |
obj.initialize(mocker.ANY)
|
|
218 |
self.mocker.result(True)
|
|
219 |
self.mocker.replay()
|
|
220 |
|
|
221 |
raptor_instance = Raptor()
|
|
222 |
raptor_instance.defaultSetUp()
|
|
223 |
filter_HLog = filter_heliumlog.FilterHeliumLog()
|
|
224 |
result = filter_HLog.open(raptor_instance)
|
|
225 |
assert result == True
|
|
226 |
|
|
227 |
def test_openValidData_empty(self):
|
|
228 |
"""test_openValidData_empty: test the opening of the log file with valid data."""
|
|
229 |
import filter_heliumlog
|
|
230 |
obj = self.mocker.patch(filter_heliumlog.SBSScanlog)
|
|
231 |
obj.initialize(mocker.ANY)
|
|
232 |
self.mocker.result(True)
|
|
233 |
self.mocker.replay()
|
|
234 |
|
|
235 |
raptor_instance = Raptor()
|
|
236 |
raptor_instance.defaultSetUp()
|
|
237 |
raptor_instance.logFileName = Path("")
|
|
238 |
filter_HLog = filter_heliumlog.FilterHeliumLog()
|
|
239 |
result = filter_HLog.open(raptor_instance)
|
|
240 |
assert result == True
|
|
241 |
|
|
242 |
def test_openValidData_stdout(self):
|
|
243 |
"""test_openValidData_stdout: test the opening of the log file with valid data."""
|
|
244 |
import filter_heliumlog
|
|
245 |
obj = self.mocker.patch(filter_heliumlog.SBSScanlog)
|
|
246 |
obj.initialize(mocker.ANY)
|
|
247 |
self.mocker.count(0, 0)
|
|
248 |
self.mocker.replay()
|
|
249 |
|
|
250 |
raptor_instance = Raptor()
|
|
251 |
raptor_instance.defaultSetUp()
|
|
252 |
raptor_instance.logFileName = None
|
|
253 |
filter_HLog = filter_heliumlog.FilterHeliumLog()
|
|
254 |
result = filter_HLog.open(raptor_instance)
|
|
255 |
assert result == True
|
|
256 |
|