Merge "Yardstick TC082: move sample test case perf.yaml"
[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 key with no value
49 ; another comment line
50 key5=
51 """
52
53 PARSE_TEXT_2 = """\
54 [section1]
55 list1 = item1
56         item2
57         ended by eof"""
58
59 PARSE_TEXT_BAD_1 = """\
60 key1=value1
61 """
62
63 PARSE_TEXT_BAD_2 = """\
64 [section1
65 """
66
67 PARSE_TEXT_BAD_3 = """\
68 []
69 """
70
71 PARSE_TEXT_BAD_4 = """\
72 [section1]
73     bad continuation
74 """
75
76 PARSE_TEXT_BAD_5 = """\
77 [section1]
78 =value with no key
79 """
80
81
82 class TestParseError(unittest.TestCase):
83
84     def test___str__(self):
85         error = ParseError('a', 2, 'c')
86         self.assertEqual(str(error), "at line 2, a: 'c'")
87
88
89 class TestBaseParser(unittest.TestCase):
90
91     @staticmethod
92     def make_open(text_blob):
93         @contextmanager
94         def internal_open(*args, **kwargs):
95             yield text_blob.split('\n')
96
97         return internal_open
98
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('')
102
103         parser = BaseParser()
104
105         parser.parse([])
106
107     def test_not_implemented_methods(self):
108         parser = BaseParser()
109
110         with self.assertRaises(NotImplementedError):
111             parser.assignment('key', 'value')
112
113         with self.assertRaises(NotImplementedError):
114             parser.new_section('section')
115
116
117 class TestConfigParser(unittest.TestCase):
118
119     @staticmethod
120     def make_open(text_blob):
121         @contextmanager
122         def internal_open(*args, **kwargs):
123             yield text_blob.split('\n')
124
125         return internal_open
126
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)
130
131         config_parser = ConfigParser('my_file', [])
132         config_parser.parse()
133
134         expected = [
135             [
136                 'section1',
137                 [
138                     ['key1', 'value1'],
139                     ['list1', 'value2\nvalue3\nvalue4'],
140                     ['key2', 'double quote value'],
141                     ['key3', 'single quote value'],
142                     ['key4', ''],
143                 ],
144             ],
145             [
146                 'section2',
147                 [
148                     ['list2', 'value5'],
149                     ['key with no value', '@'],
150                     ['key5', ''],
151                 ],
152             ],
153         ]
154
155         self.assertEqual(config_parser.sections, expected)
156
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)
160
161         config_parser = ConfigParser('my_file', [])
162         config_parser.parse()
163
164         expected = [
165             [
166                 'section1',
167                 [
168                     ['list1', 'item1\nitem2\nended by eof'],
169                 ],
170             ],
171         ]
172
173         self.assertEqual(config_parser.sections, expected)
174
175     @mock.patch('yardstick.network_services.vnf_generic.vnf.iniparser.open')
176     def test_parse_negative(self, mock_open):
177         bad_text_dict = {
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,
183         }
184
185         for bad_reason, bad_text in bad_text_dict.items():
186             mock_open.side_effect = self.make_open(bad_text)
187
188             config_parser = ConfigParser('my_file', [])
189
190             try:
191                 # TODO: replace with assertRaises, when the UT framework supports
192                 # advanced messages when exceptions fail to occur
193                 config_parser.parse()
194             except ParseError:
195                 pass
196             else:
197                 self.fail('\n'.join([bad_reason, bad_text, str(config_parser.sections)]))