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