Merge "Fix PEP8 errors in prox_binseach"
[yardstick.git] / yardstick / tests / unit / network_services / traffic_profile / test_prox_profile.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 import unittest
17 import mock
18
19 from yardstick.tests import STL_MOCKS
20
21 STLClient = mock.MagicMock()
22 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
23 stl_patch.start()
24
25 if stl_patch:
26     from yardstick.network_services.traffic_profile.prox_profile import ProxProfile
27     from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxResourceHelper
28
29
30 class TestProxProfile(unittest.TestCase):
31
32     def test_sort_vpci(self):
33         traffic_generator = mock.Mock()
34         interface_1 = {'virtual-interface': {'vpci': 'id1'}, 'name': 'name1'}
35         interface_2 = {'virtual-interface': {'vpci': 'id2'}, 'name': 'name2'}
36         interface_3 = {'virtual-interface': {'vpci': 'id3'}, 'name': 'name3'}
37         interfaces = [interface_2, interface_3, interface_1]
38         traffic_generator.vnfd_helper = {
39             'vdu': [{'external-interface': interfaces}]}
40         output = ProxProfile.sort_vpci(traffic_generator)
41         self.assertEqual([interface_1, interface_2, interface_3], output)
42
43     def test_fill_samples(self):
44         samples = {}
45
46         traffic_generator = mock.MagicMock()
47         interfaces = [
48             ['id1', 'name1'],
49             ['id2', 'name2']
50         ]
51         traffic_generator.resource_helper.sut.port_stats.side_effect = [
52             list(range(12)),
53             list(range(10, 22)),
54         ]
55
56         expected = {
57             'name1': {
58                 'in_packets': 6,
59                 'out_packets': 7,
60             },
61             'name2': {
62                 'in_packets': 16,
63                 'out_packets': 17,
64             },
65         }
66         with mock.patch.object(ProxProfile, 'sort_vpci', return_value=interfaces):
67             ProxProfile.fill_samples(samples, traffic_generator)
68
69         self.assertDictEqual(samples, expected)
70
71     def test_init(self):
72         tp_config = {
73             'traffic_profile': {},
74         }
75
76         profile = ProxProfile(tp_config)
77         queue = mock.Mock()
78         profile.init(queue)
79         self.assertIs(profile.queue, queue)
80
81     def test_execute_traffic(self):
82         packet_sizes = [
83             10,
84             100,
85             1000,
86         ]
87         tp_config = {
88             'traffic_profile': {
89                 'packet_sizes': packet_sizes,
90             },
91         }
92
93         traffic_generator = mock.MagicMock()
94
95         setup_helper = traffic_generator.setup_helper
96         setup_helper.find_in_section.return_value = None
97
98         prox_resource_helper = ProxResourceHelper(setup_helper)
99         traffic_generator.resource_helper = prox_resource_helper
100
101         profile = ProxProfile(tp_config)
102
103         self.assertFalse(profile.done)
104         for _ in packet_sizes:
105             with self.assertRaises(NotImplementedError):
106                 profile.execute_traffic(traffic_generator)
107
108         self.assertIsNone(profile.execute_traffic(traffic_generator))
109         self.assertTrue(profile.done)
110
111     def test_bounds_iterator(self):
112         tp_config = {
113             'traffic_profile': {},
114         }
115
116         profile = ProxProfile(tp_config)
117         value = 0.0
118         for value in profile.bounds_iterator():
119             pass
120
121         self.assertEqual(value, 100.0)
122
123         mock_logger = mock.MagicMock()
124         for _ in profile.bounds_iterator(mock_logger):
125             pass
126
127         self.assertEqual(mock_logger.debug.call_count, 1)
128         self.assertEqual(mock_logger.info.call_count, 10)