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.
17 from contextlib import contextmanager
20 from yardstick.tests import STL_MOCKS
23 STLClient = mock.MagicMock()
24 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
28 from yardstick.network_services.helpers.iniparser import ParseError
29 from yardstick.network_services.helpers.iniparser import LineParser
30 from yardstick.network_services.helpers.iniparser import BaseParser
31 from yardstick.network_services.helpers.iniparser import ConfigParser
40 key3='single quote value' ; comment here
43 [section2] ; comment with #2 other symbol
44 # here is a comment line
46 key with no value # mixed comment ; symbols
47 ; another comment line
50 [section1] ; reopen a section!
51 key2="double quote value"
60 PARSE_TEXT_BAD_1 = """\
64 PARSE_TEXT_BAD_2 = """\
68 PARSE_TEXT_BAD_3 = """\
72 PARSE_TEXT_BAD_4 = """\
77 PARSE_TEXT_BAD_5 = """\
83 class TestParseError(unittest.TestCase):
85 def test___str__(self):
86 error = ParseError('a', 2, 'c')
87 self.assertEqual(str(error), "at line 2, a: 'c'")
90 class TestLineParser(unittest.TestCase):
92 def test___repr__(self):
93 line_parser = LineParser('', 101)
94 self.assertIsNotNone(repr(line_parser))
96 def test_error_invalid_assignment(self):
97 line_parser = LineParser('', 101)
98 self.assertIsNotNone(line_parser.error_invalid_assignment())
101 class TestBaseParser(unittest.TestCase):
104 def make_open(text_blob):
106 def internal_open(*args):
107 yield text_blob.split('\n')
111 def test_parse(self):
112 parser = BaseParser()
115 def test_parse_empty_string(self):
116 parser = BaseParser()
117 self.assertIsNone(parser.parse(''))
119 def test_not_implemented_methods(self):
120 parser = BaseParser()
122 with self.assertRaises(NotImplementedError):
123 parser.assignment('key', 'value', LineParser('', 100))
125 with self.assertRaises(NotImplementedError):
126 parser.new_section('section')
128 with self.assertRaises(NotImplementedError):
129 parser.comment('comment')
132 class TestConfigParser(unittest.TestCase):
135 def make_open(text_blob):
137 def internal_open(*args):
138 yield text_blob.split('\n')
142 @mock.patch('yardstick.network_services.helpers.iniparser.open')
143 def test_parse(self, mock_open):
144 mock_open.side_effect = self.make_open(PARSE_TEXT_1)
146 existing_data = [['section0', [['key0', 'value0']]]]
147 config_parser = ConfigParser('my_file', existing_data)
148 config_parser.parse()
161 ['list1', 'value2\nvalue3\nvalue4'],
162 ['key3', 'single quote value'],
164 ['key2', 'double quote value'],
171 ['key with no value', '@'],
177 self.assertEqual(config_parser.sections, expected)
178 self.assertIsNotNone(config_parser.find_section('section1'))
179 self.assertIsNone(config_parser.find_section('section3'))
180 self.assertEqual(config_parser.find_section_index('section1'), 1)
181 self.assertEqual(config_parser.find_section_index('section3'), -1)
183 @mock.patch('yardstick.network_services.helpers.iniparser.open')
184 def test_parse_2(self, mock_open):
185 mock_open.side_effect = self.make_open(PARSE_TEXT_2)
187 config_parser = ConfigParser('my_file')
188 config_parser.parse()
194 ['list1', 'item1\nitem2\nended by eof'],
199 self.assertEqual(config_parser.sections, expected)
201 @mock.patch('yardstick.network_services.helpers.iniparser.open')
202 def test_parse_negative(self, mock_open):
204 'no section': PARSE_TEXT_BAD_1,
205 'incomplete section': PARSE_TEXT_BAD_2,
206 'empty section name': PARSE_TEXT_BAD_3,
207 'bad_continuation': PARSE_TEXT_BAD_4,
208 'value with no key': PARSE_TEXT_BAD_5,
211 for bad_reason, bad_text in bad_text_dict.items():
212 mock_open.side_effect = self.make_open(bad_text)
214 config_parser = ConfigParser('my_file', [])
217 # TODO: replace with assertRaises, when the UT framework supports
218 # advanced messages when exceptions fail to occur
219 config_parser.parse()
223 self.fail('\n'.join([bad_reason, bad_text, str(config_parser.sections)]))