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.
19 from yardstick.network_services.yang_model import YangModel
22 class YangModelTestCase(unittest.TestCase):
23 """Test all Yang Model methods."""
28 'access-list-entries': [{
32 'actions': 'drop,count',
34 'destination-ipv4-network':
36 'destination-port-range': {
39 'source-ipv4-network': '0.0.0.0/0',
40 'source-port-range': {
42 'upper-port': 65535}},
43 'rule-name': 'rule1588'}},
48 'actions': 'drop,count',
50 'destination-ipv4-network':
52 'destination-port-range': {
55 'source-ipv4-network':
57 'source-port-range': {
59 'upper-port': 65535}},
60 'rule-name': 'rule1589'}}],
61 'acl-name': 'sample-ipv4-acl',
62 'acl-type': 'ipv4-acl'}
66 def test__init__(self):
69 self.assertEqual(y.config_file, cfg)
71 def test_config_file_setter(self):
74 self.assertEqual(y.config_file, cfg)
77 self.assertEqual(y.config_file, cfg2)
79 def test__get_entries(self):
82 y._options = self.ENTRIES
84 self.assertIn("p acl add", y._rules)
86 def test__get_entries_no_options(self):
90 self.assertEqual(y._rules, '')
92 @mock.patch('yardstick.network_services.yang_model.open')
93 @mock.patch('yardstick.network_services.yang_model.yaml_load')
94 def test__read_config(self, mock_safe_load, *args):
97 mock_safe_load.return_value = expected = {'key1': 'value1', 'key2': 'value2'}
99 self.assertDictEqual(y._options, expected)
101 @mock.patch('yardstick.network_services.yang_model.open')
102 def test__read_config_open_error(self, mock_open):
105 mock_open.side_effect = IOError('my error')
107 self.assertEqual(y._options, {})
108 with self.assertRaises(IOError) as raised:
111 self.assertIn('my error', str(raised.exception))
112 self.assertEqual(y._options, {})
114 def test_get_rules(self):
117 y._read_config = read_mock = mock.Mock()
118 y._get_entries = get_mock = mock.Mock()
121 self.assertIsNone(y.get_rules())
122 self.assertEqual(read_mock.call_count, 1)
123 self.assertEqual(get_mock.call_count, 1)
125 # True value should prevent calling read and get
127 self.assertEqual(y.get_rules(), 999)
128 self.assertEqual(read_mock.call_count, 1)
129 self.assertEqual(get_mock.call_count, 1)