587
|
1 |
#============================================================================
|
|
2 |
#Name : test_configuration.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 |
#===============================================================================
|
588
|
19 |
""" test configuration """
|
|
20 |
|
628
|
21 |
# pylint: disable=R0201
|
587
|
22 |
|
|
23 |
import logging
|
|
24 |
import StringIO
|
|
25 |
import unittest
|
|
26 |
import os
|
|
27 |
import tempfile
|
|
28 |
import sys
|
|
29 |
import configuration
|
|
30 |
|
|
31 |
_logger = logging.getLogger('test.configuration')
|
|
32 |
logging.basicConfig(level=logging.INFO)
|
|
33 |
|
|
34 |
class NestedConfigurationBuilderTest(unittest.TestCase):
|
|
35 |
""" Acceptance tests for NestedConfigurationBuilder of configuration.py """
|
|
36 |
def setUp(self):
|
|
37 |
""" Setup. """
|
|
38 |
config_file = open(os.path.join(os.environ['TEST_DATA'], 'data/config_test.cfg.xml'), 'r')
|
|
39 |
self._builder = configuration.NestedConfigurationBuilder(config_file)
|
|
40 |
|
|
41 |
def test_config_parsing(self):
|
|
42 |
"""A basic configuration can be parsed."""
|
|
43 |
config_set = self._builder.getConfiguration()
|
|
44 |
configs = config_set.getConfigurations()
|
|
45 |
|
|
46 |
assert len(configs) == 10
|
|
47 |
for config in configs:
|
|
48 |
print
|
|
49 |
for k in config.keys():
|
|
50 |
print k + ': ' + str(config[k])
|
|
51 |
|
|
52 |
assert configs[0]['A'] == 'foo'
|
|
53 |
assert configs[0]['B'] == 'child -> foo'
|
|
54 |
assert configs[0]['C'] == 'missing value test ${does_not_exist}'
|
|
55 |
assert configs[0]['node.content'].strip() == 'This is the value!'
|
|
56 |
assert configs[1]['A'] == 'foo'
|
|
57 |
assert configs[1]['B'] == 'parent: foo'
|
|
58 |
assert configs[1]['C'] == ['one', 'two']
|
|
59 |
assert 'C' in configs[1]
|
|
60 |
assert 'Z' not in configs[1]
|
|
61 |
|
|
62 |
configs = config_set.getConfigurations('spec.with.type')
|
|
63 |
assert len(configs) == 1
|
|
64 |
assert configs[0].type == 'test.type', "config.type must match 'test.type'."
|
|
65 |
|
628
|
66 |
configs = config_set.getConfigurations(type_='test.type')
|
587
|
67 |
assert len(configs) == 2
|
|
68 |
assert configs[0].type == 'test.type', "config.type must match 'test.type'."
|
|
69 |
|
628
|
70 |
configs = config_set.getConfigurations(name='test_spec', type_='test.type')
|
587
|
71 |
assert len(configs) == 2
|
|
72 |
assert configs[0].type == 'test.type', "config.type must match 'test.type'."
|
|
73 |
|
|
74 |
def test_append(self):
|
|
75 |
"""A child value can be appended to a parent value."""
|
|
76 |
configs = self._builder.getConfigurations()
|
|
77 |
config = configs[4]
|
|
78 |
assert config['A'] == ['foo', 'bar']
|
|
79 |
|
|
80 |
def test_parent_interpolated_by_child(self):
|
|
81 |
""" A child value can be interpolated into a parent template. """
|
|
82 |
configs = self._builder.getConfigurations()
|
|
83 |
parent_config = configs[5]
|
|
84 |
child_config = configs[6]
|
|
85 |
assert parent_config['template'] == 'value -> from parent'
|
|
86 |
assert child_config['template'] == 'value -> from child'
|
|
87 |
|
|
88 |
def test_property_escaping(self):
|
|
89 |
""" Property values can be escaped in the values of other properties. """
|
|
90 |
config_text = """
|
|
91 |
<build>
|
|
92 |
<config name="test_spec">
|
|
93 |
<set name="A" value="foo"/>
|
|
94 |
<set name="B" value="A = ${A}"/>
|
|
95 |
</config>
|
|
96 |
</build>"""
|
|
97 |
|
|
98 |
builder = configuration.NestedConfigurationBuilder(StringIO.StringIO(config_text))
|
|
99 |
config = builder.getConfiguration().getConfigurations()[0]
|
|
100 |
print config['B']
|
|
101 |
#assert configs[1]['C'] == ['one', 'two']
|
|
102 |
|
|
103 |
def test_any_root_element(self):
|
|
104 |
""" Any root element name can be used. """
|
|
105 |
config_text = """
|
|
106 |
<someConfigData>
|
|
107 |
<config name="test_spec">
|
|
108 |
<set name="A" value="foo"/>
|
|
109 |
</config>
|
|
110 |
</someConfigData>"""
|
|
111 |
|
|
112 |
builder = configuration.NestedConfigurationBuilder(StringIO.StringIO(config_text))
|
|
113 |
config = builder.getConfiguration().getConfigurations()[0]
|
|
114 |
assert config['A'] == 'foo'
|
|
115 |
|
|
116 |
def test_list_templating(self):
|
|
117 |
""" Testing list templating. """
|
|
118 |
configs = self._builder.getConfigurations('test_list_config1')
|
|
119 |
# should return only one config.
|
|
120 |
assert len(configs) == 1
|
|
121 |
|
|
122 |
_logger.debug(configs[0].get_list('include', []))
|
|
123 |
result = configs[0].get_list('include', [])
|
|
124 |
result.sort()
|
|
125 |
print result
|
|
126 |
assert len(result) == 3
|
|
127 |
assert result == [u'bar1_config1', u'bar2_config1', u'foo_config1']
|
|
128 |
|
|
129 |
def test_list_templating2(self):
|
|
130 |
""" Testing list templating 2. """
|
|
131 |
configs = self._builder.getConfigurations('test_list_config2')
|
|
132 |
# should return only one config.
|
|
133 |
assert len(configs) == 1
|
|
134 |
|
|
135 |
_logger.debug(configs[0].get_list('include', []))
|
|
136 |
result = configs[0].get_list('include', [])
|
|
137 |
result.sort()
|
|
138 |
print result
|
|
139 |
assert len(result) == 3
|
|
140 |
assert result == [u'bar1_config2', u'bar2_config2', u'foo_config2']
|
|
141 |
|
|
142 |
|
|
143 |
def test_append_list(self):
|
|
144 |
""" Testing if append handles the list correctly..."""
|
|
145 |
config_text = """
|
|
146 |
<build>
|
|
147 |
<config name="prebuild_zip" abstract="true">
|
|
148 |
<set name="exclude" value="**/_ccmwaid.inf" />
|
|
149 |
<set name="exclude" value="build/**" />
|
|
150 |
<set name="exclude" value="config/**" />
|
|
151 |
<set name="exclude" value="ncp_sw/**" />
|
|
152 |
<set name="exclude" value="ppd_sw/**" />
|
|
153 |
<set name="exclude" value="psw/**" />
|
|
154 |
<set name="exclude" value="tools/**" />
|
|
155 |
<set name="include" value="foo/**" />
|
|
156 |
<config>
|
|
157 |
<set name="root.dir" value="X:/rootdir" />
|
|
158 |
<set name="name" value="PF5250_200832_internal_code" />
|
|
159 |
<set name="include" value="**/internal/**" />
|
|
160 |
<set name="grace.filters" value="tsrc" />
|
|
161 |
<set name="grace.default" value="false" />
|
|
162 |
</config>
|
|
163 |
<config>
|
|
164 |
<set name="root.dir" value="X:/rootdir" />
|
|
165 |
<set name="name" value="PF5250_200832_doc" />
|
|
166 |
<append name="include" value="**/doc/**" />
|
|
167 |
<set name="include" value="**/docs/**" />
|
|
168 |
<append name="exclude" value="**/internal/**" /> <!-- set changed to append -->
|
|
169 |
<set name="grace.filters" value="tsrc" />
|
|
170 |
<set name="grace.default" value="false" />
|
|
171 |
</config>
|
|
172 |
</config>
|
|
173 |
</build>
|
|
174 |
"""
|
|
175 |
builder = configuration.NestedConfigurationBuilder(StringIO.StringIO(config_text))
|
|
176 |
configs = builder.getConfigurations()
|
|
177 |
config = configs[1]
|
|
178 |
print config['exclude']
|
|
179 |
print config['include']
|
|
180 |
exclude_match = [u'**/_ccmwaid.inf', u'build/**', u'config/**',
|
|
181 |
u'ncp_sw/**', u'ppd_sw/**', u'psw/**', u'tools/**',
|
|
182 |
u'**/internal/**']
|
|
183 |
exclude_result = config['exclude']
|
|
184 |
exclude_match.sort()
|
|
185 |
exclude_result.sort()
|
|
186 |
assert len(config['include']) == 3
|
|
187 |
assert exclude_result == exclude_match
|
|
188 |
|
|
189 |
config = configs[0]
|
|
190 |
assert config['include'] == '**/internal/**'
|
|
191 |
assert len(config['exclude']) == 7
|
|
192 |
|
|
193 |
def test_writeToXML(self):
|
|
194 |
""" To write the configurations into XML file. """
|
|
195 |
config_text = """
|
|
196 |
<build>
|
|
197 |
<config name="test_spec">
|
|
198 |
<set name="A" value="foo"/>
|
|
199 |
<set name="B" value="A = ${A}"/>
|
|
200 |
<config name="test_spec_1">
|
|
201 |
<set name="A" value="foo"/>
|
|
202 |
<set name="B" value="A = ${A}"/>
|
|
203 |
</config>
|
|
204 |
<config name="test_spec_2">
|
|
205 |
<set name="A" value="foo"/>
|
|
206 |
<set name="B" value="A = ${A}"/>
|
|
207 |
<config name="test_spec_3">
|
|
208 |
<set name="A" value="foo"/>
|
|
209 |
<set name="B" value="A = ${A}"/>
|
|
210 |
</config>
|
|
211 |
</config>
|
|
212 |
</config>
|
|
213 |
</build>"""
|
|
214 |
|
|
215 |
builder = configuration.NestedConfigurationBuilder(StringIO.StringIO(config_text))
|
|
216 |
configSet = builder.getConfiguration()
|
|
217 |
configs = configSet.getConfigurations('test_spec_1')
|
588
|
218 |
(_, outputFile) = tempfile.mkstemp('.tmp', 'zip_test')
|
587
|
219 |
builder.writeToXML(outputFile, configs, 'test_spec_1')
|
|
220 |
|
|
221 |
builder = configuration.NestedConfigurationBuilder(open(outputFile), 'r')
|
|
222 |
configSet = builder.getConfiguration()
|
|
223 |
configs = configSet.getConfigurations('test_spec_1')
|
|
224 |
config = configs[0]
|
|
225 |
assert config['A'] == 'foo'
|
|
226 |
|
|
227 |
builder = configuration.NestedConfigurationBuilder(StringIO.StringIO(config_text))
|
|
228 |
configSet = builder.getConfiguration()
|
|
229 |
configs = configSet.getConfigurations('test_spec')
|
588
|
230 |
(_, outputFile) = tempfile.mkstemp('.tmp', 'zip_test')
|
587
|
231 |
builder.writeToXML(outputFile, configs )
|
|
232 |
|
|
233 |
builder = configuration.NestedConfigurationBuilder(open(outputFile), 'r')
|
|
234 |
configSet = builder.getConfiguration()
|
|
235 |
configs = configSet.getConfigurations('test_spec')
|
|
236 |
config = configs[0]
|
|
237 |
assert config['B'] == 'A = foo'
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
class PropertiesConfigurationTest(unittest.TestCase):
|
|
242 |
""" Test plain text configuration files. """
|
|
243 |
def test_text_config(self):
|
|
244 |
""" Basic text properties can be read. """
|
|
245 |
config = configuration.PropertiesConfiguration(open(os.path.join(os.environ['TEST_DATA'], 'data/ant_config_test.txt'), 'r'))
|
|
246 |
|
|
247 |
assert config['text.a'] == 'text.value.A'
|
|
248 |
assert config['text.b'] == 'text.value.B'
|
|
249 |
|
|
250 |
def test_text_config_store(self):
|
|
251 |
""" Basic text properties can be read. """
|
|
252 |
config = configuration.PropertiesConfiguration(open(os.path.join(os.environ['TEST_DATA'], 'data/ant_config_test.txt'), 'r'))
|
|
253 |
|
|
254 |
config['foo'] = "bar"
|
588
|
255 |
(f_d, filename) = tempfile.mkstemp()
|
|
256 |
f_file = os.fdopen(f_d, 'w')
|
|
257 |
config.store(f_file)
|
587
|
258 |
config = configuration.PropertiesConfiguration(open(filename))
|
|
259 |
|
|
260 |
assert config['text.a'] == 'text.value.A'
|
|
261 |
assert config['text.b'] == 'text.value.B'
|
|
262 |
assert config['foo'] == 'bar'
|
|
263 |
|
|
264 |
|