PROX VNF and TG
[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 import unittest
18
19 import mock
20
21 STL_MOCKS = {
22     'stl': mock.MagicMock(),
23     'stl.trex_stl_lib': mock.MagicMock(),
24     'stl.trex_stl_lib.base64': mock.MagicMock(),
25     'stl.trex_stl_lib.binascii': mock.MagicMock(),
26     'stl.trex_stl_lib.collections': mock.MagicMock(),
27     'stl.trex_stl_lib.copy': mock.MagicMock(),
28     'stl.trex_stl_lib.datetime': mock.MagicMock(),
29     'stl.trex_stl_lib.functools': mock.MagicMock(),
30     'stl.trex_stl_lib.imp': mock.MagicMock(),
31     'stl.trex_stl_lib.inspect': mock.MagicMock(),
32     'stl.trex_stl_lib.json': mock.MagicMock(),
33     'stl.trex_stl_lib.linecache': mock.MagicMock(),
34     'stl.trex_stl_lib.math': mock.MagicMock(),
35     'stl.trex_stl_lib.os': mock.MagicMock(),
36     'stl.trex_stl_lib.platform': mock.MagicMock(),
37     'stl.trex_stl_lib.pprint': mock.MagicMock(),
38     'stl.trex_stl_lib.random': mock.MagicMock(),
39     'stl.trex_stl_lib.re': mock.MagicMock(),
40     'stl.trex_stl_lib.scapy': mock.MagicMock(),
41     'stl.trex_stl_lib.socket': mock.MagicMock(),
42     'stl.trex_stl_lib.string': mock.MagicMock(),
43     'stl.trex_stl_lib.struct': mock.MagicMock(),
44     'stl.trex_stl_lib.sys': mock.MagicMock(),
45     'stl.trex_stl_lib.threading': mock.MagicMock(),
46     'stl.trex_stl_lib.time': mock.MagicMock(),
47     'stl.trex_stl_lib.traceback': mock.MagicMock(),
48     'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
49     'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
50     'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
51     'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
52     'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
53     'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
54     'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
55     'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
56     'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
57     'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
58     'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
59     'stl.trex_stl_lib.types': mock.MagicMock(),
60     'stl.trex_stl_lib.utils': mock.MagicMock(),
61     'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
62     'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
63     'stl.trex_stl_lib.utils.common': mock.MagicMock(),
64     'stl.trex_stl_lib.utils.json': mock.MagicMock(),
65     'stl.trex_stl_lib.utils.os': mock.MagicMock(),
66     'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
67     'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
68     'stl.trex_stl_lib.utils.random': mock.MagicMock(),
69     'stl.trex_stl_lib.utils.re': mock.MagicMock(),
70     'stl.trex_stl_lib.utils.string': mock.MagicMock(),
71     'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
72     'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
73     'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
74     'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
75     'stl.trex_stl_lib.warnings': mock.MagicMock(),
76     'stl.trex_stl_lib.yaml': mock.MagicMock(),
77     'stl.trex_stl_lib.zlib': mock.MagicMock(),
78     'stl.trex_stl_lib.zmq': mock.MagicMock(),
79 }
80
81 STLClient = mock.MagicMock()
82 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
83 stl_patch.start()
84
85 if stl_patch:
86     from yardstick.network_services.traffic_profile.prox_profile import ProxProfile
87
88
89 class TestProxProfile(unittest.TestCase):
90
91     def test_fill_samples(self):
92         samples = {}
93         traffic_generator = mock.MagicMock()
94         traffic_generator.vpci_if_name_ascending = [
95             ['id1', 'name1'],
96             ['id2', 'name2'],
97         ]
98
99         traffic_generator.resource_helper.sut.port_stats.side_effect = [
100             list(range(12)),
101             list(range(10, 22)),
102         ]
103
104         expected = {
105             'name1': {
106                 'in_packets': 6,
107                 'out_packets': 7,
108             },
109             'name2': {
110                 'in_packets': 16,
111                 'out_packets': 17,
112             },
113         }
114         ProxProfile.fill_samples(samples, traffic_generator)
115         self.assertDictEqual(samples, expected)
116
117     def test_init(self):
118         tp_config = {
119             'traffic_profile': {},
120         }
121
122         profile = ProxProfile(tp_config)
123         profile.init(234)
124         self.assertEqual(profile.queue, 234)
125
126     def test_execute(self):
127         packet_sizes = [
128             10,
129             100,
130             1000,
131         ]
132         tp_config = {
133             'traffic_profile': {
134                 'packet_sizes': packet_sizes,
135             },
136         }
137
138         traffic_generator = mock.MagicMock()
139         profile = ProxProfile(tp_config)
140
141         self.assertFalse(profile.done)
142         for _ in packet_sizes:
143             with self.assertRaises(NotImplementedError):
144                 profile.execute(traffic_generator)
145
146         self.assertIsNone(profile.execute(traffic_generator))
147
148     def test_bounds_iterator(self):
149         tp_config = {
150             'traffic_profile': {},
151         }
152
153         profile = ProxProfile(tp_config)
154         value = 0.0
155         for value in profile.bounds_iterator():
156             pass
157
158         self.assertEqual(value, 100.0)
159
160         mock_logger = mock.MagicMock()
161         for _ in profile.bounds_iterator(mock_logger):
162             pass
163
164         self.assertEqual(mock_logger.debug.call_count, 1)
165         self.assertEqual(mock_logger.info.call_count, 10)