53481ddd0caa489812358ae39e354c2ba6c02858
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_iniparser.py
1 # Copyright (c) 2017 Intel Corporation
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 #
15
16 from __future__ import absolute_import
17 import unittest
18 from contextlib import contextmanager
19
20 import mock
21
22 STL_MOCKS = {
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(),
80 }
81
82 STLClient = mock.MagicMock()
83 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
84 stl_patch.start()
85
86 if stl_patch:
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
90
91 PARSE_TEXT_1 = """\
92
93 [section1]
94 key1=value1
95 list1: value2
96        value3
97        value4
98 key2="double quote value"
99 key3='single quote value'  ; comment here
100 key4=
101
102 [section2]
103 # here is a comment line
104 list2: value5
105 ; another comment line
106 key5=
107 """
108
109 PARSE_TEXT_2 = """\
110 [section1]
111 list1 = item1
112         item2
113         ended by eof"""
114
115 PARSE_TEXT_BAD_1 = """\
116 key1=value1
117 """
118
119 PARSE_TEXT_BAD_2 = """\
120 [section1
121 """
122
123 PARSE_TEXT_BAD_3 = """\
124 []
125 """
126
127 PARSE_TEXT_BAD_4 = """\
128 [section1]
129 no list or key
130 """
131
132 PARSE_TEXT_BAD_5 = """\
133 [section1]
134     bad continuation
135 """
136
137 PARSE_TEXT_BAD_6 = """\
138 [section1]
139 =value with no key
140 """
141
142
143 class TestParseError(unittest.TestCase):
144
145     def test___str__(self):
146         error = ParseError('a', 2, 'c')
147         self.assertEqual(str(error), "at line 2, a: 'c'")
148
149
150 class TestBaseParser(unittest.TestCase):
151
152     @staticmethod
153     def make_open(text_blob):
154         @contextmanager
155         def internal_open(*args, **kwargs):
156             yield text_blob.split('\n')
157
158         return internal_open
159
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('')
163
164         parser = BaseParser()
165
166         self.assertIsNone(parser.parse())
167
168     def test_not_implemented_methods(self):
169         parser = BaseParser()
170
171         with self.assertRaises(NotImplementedError):
172             parser.assignment('key', 'value')
173
174         with self.assertRaises(NotImplementedError):
175             parser.new_section('section')
176
177
178 class TestConfigParser(unittest.TestCase):
179
180     @staticmethod
181     def make_open(text_blob):
182         @contextmanager
183         def internal_open(*args, **kwargs):
184             yield text_blob.split('\n')
185
186         return internal_open
187
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)
191
192         config_parser = ConfigParser('my_file', {})
193         config_parser.parse()
194
195         expected = {
196             'section1': [
197                 ['key1', 'value1'],
198                 ['list1', 'value2\nvalue3\nvalue4'],
199                 ['key2', 'double quote value'],
200                 ['key3', 'single quote value'],
201                 ['key4', ''],
202             ],
203             'section2': [
204                 ['list2', 'value5'],
205                 ['key5', ''],
206             ],
207         }
208
209         self.assertDictEqual(config_parser.sections, expected)
210
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)
214
215         config_parser = ConfigParser('my_file', {})
216         config_parser.parse()
217
218         expected = {
219             'section1': [
220                 ['list1', 'item1\nitem2\nended by eof'],
221             ],
222         }
223
224         self.assertDictEqual(config_parser.sections, expected)
225
226     @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
227     def test_parse_negative(self, mock_open):
228         bad_text_dict = {
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,
235         }
236
237         for bad_reason, bad_text in bad_text_dict.items():
238             mock_open.side_effect = self.make_open(bad_text)
239
240             config_parser = ConfigParser('my_file', {})
241
242             try:
243                 # TODO: replace with assertRaises, when the UT framework supports
244                 # advanced messages when exceptions fail to occur
245                 config_parser.parse()
246             except ParseError:
247                 pass
248             else:
249                 self.fail('\n'.join([bad_reason, bad_text, str(config_parser.sections)]))