Merge "improve tc002 to make packet size parameterize"
[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
18 import unittest
19 from contextlib import contextmanager
20 import mock
21
22 from tests.unit import STL_MOCKS
23
24
25 STLClient = mock.MagicMock()
26 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
27 stl_patch.start()
28
29 if stl_patch:
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
33
34 PARSE_TEXT_1 = """\
35
36 [section1]
37 key1=value1
38 list1: value2
39        value3
40        value4
41 key2="double quote value"
42 key3='single quote value'  ; comment here
43 key4=
44
45 [section2]
46 # here is a comment line
47 list2: value5
48 ; another comment line
49 key5=
50 """
51
52 PARSE_TEXT_2 = """\
53 [section1]
54 list1 = item1
55         item2
56         ended by eof"""
57
58 PARSE_TEXT_BAD_1 = """\
59 key1=value1
60 """
61
62 PARSE_TEXT_BAD_2 = """\
63 [section1
64 """
65
66 PARSE_TEXT_BAD_3 = """\
67 []
68 """
69
70 PARSE_TEXT_BAD_4 = """\
71 [section1]
72 no list or key
73 """
74
75 PARSE_TEXT_BAD_5 = """\
76 [section1]
77     bad continuation
78 """
79
80 PARSE_TEXT_BAD_6 = """\
81 [section1]
82 =value with no key
83 """
84
85
86 class TestParseError(unittest.TestCase):
87
88     def test___str__(self):
89         error = ParseError('a', 2, 'c')
90         self.assertEqual(str(error), "at line 2, a: 'c'")
91
92
93 class TestBaseParser(unittest.TestCase):
94
95     @staticmethod
96     def make_open(text_blob):
97         @contextmanager
98         def internal_open(*args, **kwargs):
99             yield text_blob.split('\n')
100
101         return internal_open
102
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('')
106
107         parser = BaseParser()
108
109         self.assertIsNone(parser.parse())
110
111     def test_not_implemented_methods(self):
112         parser = BaseParser()
113
114         with self.assertRaises(NotImplementedError):
115             parser.assignment('key', 'value')
116
117         with self.assertRaises(NotImplementedError):
118             parser.new_section('section')
119
120
121 class TestConfigParser(unittest.TestCase):
122
123     @staticmethod
124     def make_open(text_blob):
125         @contextmanager
126         def internal_open(*args, **kwargs):
127             yield text_blob.split('\n')
128
129         return internal_open
130
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)
134
135         config_parser = ConfigParser('my_file', {})
136         config_parser.parse()
137
138         expected = {
139             'section1': [
140                 ['key1', 'value1'],
141                 ['list1', 'value2\nvalue3\nvalue4'],
142                 ['key2', 'double quote value'],
143                 ['key3', 'single quote value'],
144                 ['key4', ''],
145             ],
146             'section2': [
147                 ['list2', 'value5'],
148                 ['key5', ''],
149             ],
150         }
151
152         self.assertDictEqual(config_parser.sections, expected)
153
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)
157
158         config_parser = ConfigParser('my_file', {})
159         config_parser.parse()
160
161         expected = {
162             'section1': [
163                 ['list1', 'item1\nitem2\nended by eof'],
164             ],
165         }
166
167         self.assertDictEqual(config_parser.sections, expected)
168
169     @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
170     def test_parse_negative(self, mock_open):
171         bad_text_dict = {
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,
178         }
179
180         for bad_reason, bad_text in bad_text_dict.items():
181             mock_open.side_effect = self.make_open(bad_text)
182
183             config_parser = ConfigParser('my_file', {})
184
185             try:
186                 # TODO: replace with assertRaises, when the UT framework supports
187                 # advanced messages when exceptions fail to occur
188                 config_parser.parse()
189             except ParseError:
190                 pass
191             else:
192                 self.fail('\n'.join([bad_reason, bad_text, str(config_parser.sections)]))