1 # Copyright (c) 2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from __future__ import absolute_import
18 from contextlib import contextmanager
23 'stl': mock.MagicMock(),
24 'stl.trex_stl_lib': mock.MagicMock(),
25 'stl.trex_stl_lib.base64': mock.MagicMock(),
26 'stl.trex_stl_lib.binascii': mock.MagicMock(),
27 'stl.trex_stl_lib.collections': mock.MagicMock(),
28 'stl.trex_stl_lib.copy': mock.MagicMock(),
29 'stl.trex_stl_lib.datetime': mock.MagicMock(),
30 'stl.trex_stl_lib.functools': mock.MagicMock(),
31 'stl.trex_stl_lib.imp': mock.MagicMock(),
32 'stl.trex_stl_lib.inspect': mock.MagicMock(),
33 'stl.trex_stl_lib.json': mock.MagicMock(),
34 'stl.trex_stl_lib.linecache': mock.MagicMock(),
35 'stl.trex_stl_lib.math': mock.MagicMock(),
36 'stl.trex_stl_lib.os': mock.MagicMock(),
37 'stl.trex_stl_lib.platform': mock.MagicMock(),
38 'stl.trex_stl_lib.pprint': mock.MagicMock(),
39 'stl.trex_stl_lib.random': mock.MagicMock(),
40 'stl.trex_stl_lib.re': mock.MagicMock(),
41 'stl.trex_stl_lib.scapy': mock.MagicMock(),
42 'stl.trex_stl_lib.socket': mock.MagicMock(),
43 'stl.trex_stl_lib.string': mock.MagicMock(),
44 'stl.trex_stl_lib.struct': mock.MagicMock(),
45 'stl.trex_stl_lib.sys': mock.MagicMock(),
46 'stl.trex_stl_lib.threading': mock.MagicMock(),
47 'stl.trex_stl_lib.time': mock.MagicMock(),
48 'stl.trex_stl_lib.traceback': mock.MagicMock(),
49 'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
50 'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
51 'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
52 'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
53 'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
54 'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
55 'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
56 'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
57 'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
58 'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
59 'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
60 'stl.trex_stl_lib.types': mock.MagicMock(),
61 'stl.trex_stl_lib.utils': mock.MagicMock(),
62 'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
63 'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
64 'stl.trex_stl_lib.utils.common': mock.MagicMock(),
65 'stl.trex_stl_lib.utils.json': mock.MagicMock(),
66 'stl.trex_stl_lib.utils.os': mock.MagicMock(),
67 'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
68 'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
69 'stl.trex_stl_lib.utils.random': mock.MagicMock(),
70 'stl.trex_stl_lib.utils.re': mock.MagicMock(),
71 'stl.trex_stl_lib.utils.string': mock.MagicMock(),
72 'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
73 'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
74 'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
75 'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
76 'stl.trex_stl_lib.warnings': mock.MagicMock(),
77 'stl.trex_stl_lib.yaml': mock.MagicMock(),
78 'stl.trex_stl_lib.zlib': mock.MagicMock(),
79 'stl.trex_stl_lib.zmq': mock.MagicMock(),
82 STLClient = mock.MagicMock()
83 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
87 from yardstick.network_services.vnf_generic.vnf.iniparser import ParseError
88 from yardstick.network_services.vnf_generic.vnf.iniparser import BaseParser
89 from yardstick.network_services.vnf_generic.vnf.iniparser import ConfigParser
98 key2="double quote value"
99 key3='single quote value' ; comment here
103 # here is a comment line
105 ; another comment line
115 PARSE_TEXT_BAD_1 = """\
119 PARSE_TEXT_BAD_2 = """\
123 PARSE_TEXT_BAD_3 = """\
127 PARSE_TEXT_BAD_4 = """\
132 PARSE_TEXT_BAD_5 = """\
137 PARSE_TEXT_BAD_6 = """\
143 class TestParseError(unittest.TestCase):
145 def test___str__(self):
146 error = ParseError('a', 2, 'c')
147 self.assertEqual(str(error), "at line 2, a: 'c'")
150 class TestBaseParser(unittest.TestCase):
153 def make_open(text_blob):
155 def internal_open(*args, **kwargs):
156 yield text_blob.split('\n')
160 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
161 def test_parse_none(self, mock_open):
162 mock_open.side_effect = self.make_open('')
164 parser = BaseParser()
166 self.assertIsNone(parser.parse())
168 def test_not_implemented_methods(self):
169 parser = BaseParser()
171 with self.assertRaises(NotImplementedError):
172 parser.assignment('key', 'value')
174 with self.assertRaises(NotImplementedError):
175 parser.new_section('section')
178 class TestConfigParser(unittest.TestCase):
181 def make_open(text_blob):
183 def internal_open(*args, **kwargs):
184 yield text_blob.split('\n')
188 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
189 def test_parse(self, mock_open):
190 mock_open.side_effect = self.make_open(PARSE_TEXT_1)
192 config_parser = ConfigParser('my_file', {})
193 config_parser.parse()
198 ['list1', 'value2\nvalue3\nvalue4'],
199 ['key2', 'double quote value'],
200 ['key3', 'single quote value'],
209 self.assertDictEqual(config_parser.sections, expected)
211 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
212 def test_parse_2(self, mock_open):
213 mock_open.side_effect = self.make_open(PARSE_TEXT_2)
215 config_parser = ConfigParser('my_file', {})
216 config_parser.parse()
220 ['list1', 'item1\nitem2\nended by eof'],
224 self.assertDictEqual(config_parser.sections, expected)
226 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
227 def test_parse_negative(self, mock_open):
229 'no section': PARSE_TEXT_BAD_1,
230 'incomplete section': PARSE_TEXT_BAD_2,
231 'empty section name': PARSE_TEXT_BAD_3,
232 'no list or key': PARSE_TEXT_BAD_4,
233 'bad_continuation': PARSE_TEXT_BAD_5,
234 'value with no key': PARSE_TEXT_BAD_6,
237 for bad_reason, bad_text in bad_text_dict.items():
238 mock_open.side_effect = self.make_open(bad_text)
240 config_parser = ConfigParser('my_file', {})
243 # TODO: replace with assertRaises, when the UT framework supports
244 # advanced messages when exceptions fail to occur
245 config_parser.parse()
249 self.fail('\n'.join([bad_reason, bad_text, str(config_parser.sections)]))