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
19 from contextlib import contextmanager
22 from tests.unit import STL_MOCKS
25 STLClient = mock.MagicMock()
26 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
30 from yardstick.network_services.vnf_generic.vnf.iniparser import ParseError
31 from yardstick.network_services.vnf_generic.vnf.iniparser import BaseParser
32 from yardstick.network_services.vnf_generic.vnf.iniparser import ConfigParser
41 key2="double quote value"
42 key3='single quote value' ; comment here
46 # here is a comment line
48 ; another comment line
58 PARSE_TEXT_BAD_1 = """\
62 PARSE_TEXT_BAD_2 = """\
66 PARSE_TEXT_BAD_3 = """\
70 PARSE_TEXT_BAD_4 = """\
75 PARSE_TEXT_BAD_5 = """\
80 PARSE_TEXT_BAD_6 = """\
86 class TestParseError(unittest.TestCase):
88 def test___str__(self):
89 error = ParseError('a', 2, 'c')
90 self.assertEqual(str(error), "at line 2, a: 'c'")
93 class TestBaseParser(unittest.TestCase):
96 def make_open(text_blob):
98 def internal_open(*args, **kwargs):
99 yield text_blob.split('\n')
103 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
104 def test_parse_none(self, mock_open):
105 mock_open.side_effect = self.make_open('')
107 parser = BaseParser()
109 self.assertIsNone(parser.parse())
111 def test_not_implemented_methods(self):
112 parser = BaseParser()
114 with self.assertRaises(NotImplementedError):
115 parser.assignment('key', 'value')
117 with self.assertRaises(NotImplementedError):
118 parser.new_section('section')
121 class TestConfigParser(unittest.TestCase):
124 def make_open(text_blob):
126 def internal_open(*args, **kwargs):
127 yield text_blob.split('\n')
131 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
132 def test_parse(self, mock_open):
133 mock_open.side_effect = self.make_open(PARSE_TEXT_1)
135 config_parser = ConfigParser('my_file', {})
136 config_parser.parse()
141 ['list1', 'value2\nvalue3\nvalue4'],
142 ['key2', 'double quote value'],
143 ['key3', 'single quote value'],
152 self.assertDictEqual(config_parser.sections, expected)
154 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
155 def test_parse_2(self, mock_open):
156 mock_open.side_effect = self.make_open(PARSE_TEXT_2)
158 config_parser = ConfigParser('my_file', {})
159 config_parser.parse()
163 ['list1', 'item1\nitem2\nended by eof'],
167 self.assertDictEqual(config_parser.sections, expected)
169 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
170 def test_parse_negative(self, mock_open):
172 'no section': PARSE_TEXT_BAD_1,
173 'incomplete section': PARSE_TEXT_BAD_2,
174 'empty section name': PARSE_TEXT_BAD_3,
175 'no list or key': PARSE_TEXT_BAD_4,
176 'bad_continuation': PARSE_TEXT_BAD_5,
177 'value with no key': PARSE_TEXT_BAD_6,
180 for bad_reason, bad_text in bad_text_dict.items():
181 mock_open.side_effect = self.make_open(bad_text)
183 config_parser = ConfigParser('my_file', {})
186 # TODO: replace with assertRaises, when the UT framework supports
187 # advanced messages when exceptions fail to occur
188 config_parser.parse()
192 self.fail('\n'.join([bad_reason, bad_text, str(config_parser.sections)]))