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
49 ; another comment line
59 PARSE_TEXT_BAD_1 = """\
63 PARSE_TEXT_BAD_2 = """\
67 PARSE_TEXT_BAD_3 = """\
71 PARSE_TEXT_BAD_4 = """\
76 PARSE_TEXT_BAD_5 = """\
82 class TestParseError(unittest.TestCase):
84 def test___str__(self):
85 error = ParseError('a', 2, 'c')
86 self.assertEqual(str(error), "at line 2, a: 'c'")
89 class TestBaseParser(unittest.TestCase):
92 def make_open(text_blob):
94 def internal_open(*args, **kwargs):
95 yield text_blob.split('\n')
99 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
100 def test_parse_none(self, mock_open):
101 mock_open.side_effect = self.make_open('')
103 parser = BaseParser()
107 def test_not_implemented_methods(self):
108 parser = BaseParser()
110 with self.assertRaises(NotImplementedError):
111 parser.assignment('key', 'value')
113 with self.assertRaises(NotImplementedError):
114 parser.new_section('section')
117 class TestConfigParser(unittest.TestCase):
120 def make_open(text_blob):
122 def internal_open(*args, **kwargs):
123 yield text_blob.split('\n')
127 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
128 def test_parse(self, mock_open):
129 mock_open.side_effect = self.make_open(PARSE_TEXT_1)
131 config_parser = ConfigParser('my_file', [])
132 config_parser.parse()
139 ['list1', 'value2\nvalue3\nvalue4'],
140 ['key2', 'double quote value'],
141 ['key3', 'single quote value'],
149 ['key with no value', '@'],
155 self.assertEqual(config_parser.sections, expected)
157 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
158 def test_parse_2(self, mock_open):
159 mock_open.side_effect = self.make_open(PARSE_TEXT_2)
161 config_parser = ConfigParser('my_file', [])
162 config_parser.parse()
168 ['list1', 'item1\nitem2\nended by eof'],
173 self.assertEqual(config_parser.sections, expected)
175 @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
176 def test_parse_negative(self, mock_open):
178 'no section': PARSE_TEXT_BAD_1,
179 'incomplete section': PARSE_TEXT_BAD_2,
180 'empty section name': PARSE_TEXT_BAD_3,
181 'bad_continuation': PARSE_TEXT_BAD_4,
182 'value with no key': PARSE_TEXT_BAD_5,
185 for bad_reason, bad_text in bad_text_dict.items():
186 mock_open.side_effect = self.make_open(bad_text)
188 config_parser = ConfigParser('my_file', [])
191 # TODO: replace with assertRaises, when the UT framework supports
192 # advanced messages when exceptions fail to occur
193 config_parser.parse()
197 self.fail('\n'.join([bad_reason, bad_text, str(config_parser.sections)]))