1 # Copyright (c) 2016-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 # Unittest for yardstick.network_services.utils
18 from __future__ import absolute_import
25 from yardstick.network_services.yang_model import YangModel
28 class YangModelTestCase(unittest.TestCase):
29 """Test all Yang Model methods."""
34 'access-list-entries': [{
38 'actions': 'drop,count',
40 'destination-ipv4-network':
42 'destination-port-range': {
45 'source-ipv4-network': '0.0.0.0/0',
46 'source-port-range': {
48 'upper-port': 65535}},
49 'rule-name': 'rule1588'}},
54 'actions': 'drop,count',
56 'destination-ipv4-network':
58 'destination-port-range': {
61 'source-ipv4-network':
63 'source-port-range': {
65 'upper-port': 65535}},
66 'rule-name': 'rule1589'}}],
67 'acl-name': 'sample-ipv4-acl',
68 'acl-type': 'ipv4-acl'}
72 def test__init__(self):
75 self.assertEqual(y.config_file, cfg)
77 def test_config_file_setter(self):
80 self.assertEqual(y.config_file, cfg)
83 self.assertEqual(y.config_file, cfg2)
85 def test__get_entries(self):
88 y._options = self.ENTRIES
90 self.assertIn("p acl add", y._rules)
92 def test__get_entries_no_options(self):
96 self.assertEqual(y._rules, '')
98 @mock.patch('yardstick.network_services.yang_model.yaml_load')
99 @mock.patch('yardstick.network_services.yang_model.open')
100 def test__read_config(self, mock_open, mock_safe_load):
103 mock_safe_load.return_value = expected = {'key1': 'value1', 'key2': 'value2'}
105 self.assertDictEqual(y._options, expected)
107 @mock.patch('yardstick.network_services.yang_model.open')
108 def test__read_config_open_error(self, mock_open):
111 mock_open.side_effect = IOError('my error')
113 self.assertEqual(y._options, {})
114 with self.assertRaises(IOError) as raised:
117 self.assertIn('my error', str(raised.exception))
118 self.assertEqual(y._options, {})
120 def test_get_rules(self):
123 y._read_config = read_mock = mock.Mock()
124 y._get_entries = get_mock = mock.Mock()
127 self.assertIsNone(y.get_rules())
128 self.assertEqual(read_mock.call_count, 1)
129 self.assertEqual(get_mock.call_count, 1)
131 # True value should prevent calling read and get
133 self.assertEqual(y.get_rules(), 999)
134 self.assertEqual(read_mock.call_count, 1)
135 self.assertEqual(get_mock.call_count, 1)