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