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