Merge "Install dependencies: bare-metal, standalone"
[yardstick.git] / yardstick / tests / unit / network_services / test_yang_model.py
1 # Copyright (c) 2016-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 import mock
17 import unittest
18
19 from yardstick.network_services.yang_model import YangModel
20
21
22 class YangModelTestCase(unittest.TestCase):
23     """Test all Yang Model methods."""
24
25     ENTRIES = {
26         'access-list1': {
27             'acl': {
28                 'access-list-entries': [{
29                     'ace': {
30                         'ace-oper-data': {
31                             'match-counter': 0},
32                         'actions': 'drop,count',
33                         'matches': {
34                             'destination-ipv4-network':
35                                 '152.16.40.20/24',
36                             'destination-port-range': {
37                                 'lower-port': 0,
38                                 'upper-port': 65535},
39                             'source-ipv4-network': '0.0.0.0/0',
40                             'source-port-range': {
41                                 'lower-port': 0,
42                                 'upper-port': 65535}},
43                         'rule-name': 'rule1588'}},
44                     {
45                         'ace': {
46                             'ace-oper-data': {
47                                 'match-counter': 0},
48                             'actions': 'drop,count',
49                             'matches': {
50                                 'destination-ipv4-network':
51                                     '0.0.0.0/0',
52                                 'destination-port-range': {
53                                     'lower-port': 0,
54                                     'upper-port': 65535},
55                                 'source-ipv4-network':
56                                     '152.16.100.20/24',
57                                 'source-port-range': {
58                                     'lower-port': 0,
59                                     'upper-port': 65535}},
60                             'rule-name': 'rule1589'}}],
61                 'acl-name': 'sample-ipv4-acl',
62                     'acl-type': 'ipv4-acl'}
63         }
64     }
65
66     def test__init__(self):
67         cfg = "yang.yaml"
68         y = YangModel(cfg)
69         self.assertEqual(y.config_file, cfg)
70
71     def test_config_file_setter(self):
72         cfg = "yang.yaml"
73         y = YangModel(cfg)
74         self.assertEqual(y.config_file, cfg)
75         cfg2 = "yang2.yaml"
76         y.config_file = cfg2
77         self.assertEqual(y.config_file, cfg2)
78
79     def test__get_entries(self):
80         cfg = "yang.yaml"
81         y = YangModel(cfg)
82         y._options = self.ENTRIES
83         y._get_entries()
84         self.assertIn("p acl add", y._rules)
85
86     def test__get_entries_no_options(self):
87         cfg = "yang.yaml"
88         y = YangModel(cfg)
89         y._get_entries()
90         self.assertEqual(y._rules, '')
91
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):
95         cfg = "yang.yaml"
96         y = YangModel(cfg)
97         mock_safe_load.return_value = expected = {'key1': 'value1', 'key2': 'value2'}
98         y._read_config()
99         self.assertDictEqual(y._options, expected)
100
101     @mock.patch('yardstick.network_services.yang_model.open')
102     def test__read_config_open_error(self, mock_open):
103         cfg = "yang.yaml"
104         y = YangModel(cfg)
105         mock_open.side_effect = IOError('my error')
106
107         self.assertEqual(y._options, {})
108         with self.assertRaises(IOError) as raised:
109             y._read_config()
110
111         self.assertIn('my error', str(raised.exception))
112         self.assertEqual(y._options, {})
113
114     def test_get_rules(self):
115         cfg = "yang.yaml"
116         y = YangModel(cfg)
117         y._read_config = read_mock = mock.Mock()
118         y._get_entries = get_mock = mock.Mock()
119
120         y._rules = None
121         self.assertIsNone(y.get_rules())
122         self.assertEqual(read_mock.call_count, 1)
123         self.assertEqual(get_mock.call_count, 1)
124
125         # True value should prevent calling read and get
126         y._rules = 999
127         self.assertEqual(y.get_rules(), 999)
128         self.assertEqual(read_mock.call_count, 1)
129         self.assertEqual(get_mock.call_count, 1)