Create a SampleVNF MQ consumer class
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_tg_ixload.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 import subprocess
16
17 import mock
18 import six
19
20 from yardstick import ssh
21 from yardstick.benchmark.contexts import base as ctx_base
22 from yardstick.common import utils
23 from yardstick.network_services.vnf_generic.vnf import tg_ixload
24 from yardstick.network_services.traffic_profile import base as tp_base
25 from yardstick.tests.unit import base as ut_base
26
27
28 NAME = "tg__1"
29
30
31 class TestIxLoadTrafficGen(ut_base.BaseUnitTestCase):
32     VNFD = {'vnfd:vnfd-catalog':
33             {'vnfd':
34              [{'short-name': 'VpeVnf',
35                'vdu':
36                [{'routing_table':
37                  [{'network': '152.16.100.20',
38                    'netmask': '255.255.255.0',
39                    'gateway': '152.16.100.20',
40                    'if': 'xe0'},
41                   {'network': '152.16.40.20',
42                    'netmask': '255.255.255.0',
43                    'gateway': '152.16.40.20',
44                    'if': 'xe1'}],
45                  'description': 'VPE approximation using DPDK',
46                  'name': 'vpevnf-baremetal',
47                  'nd_route_tbl':
48                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
49                    'netmask': '112',
50                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
51                    'if': 'xe0'},
52                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
53                    'netmask': '112',
54                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
55                    'if': 'xe1'}],
56                  'id': 'vpevnf-baremetal',
57                  'external-interface':
58                  [{'virtual-interface':
59                    {'dst_mac': '00:00:00:00:00:04',
60                     'vpci': '0000:05:00.0',
61                     'local_ip': '152.16.100.19',
62                     'type': 'PCI-PASSTHROUGH',
63                     'netmask': '255.255.255.0',
64                     'dpdk_port_num': 0,
65                     'bandwidth': '10 Gbps',
66                     'driver': "i40e",
67                     'dst_ip': '152.16.100.20',
68                     'local_iface_name': 'xe0',
69                     'local_mac': '00:00:00:00:00:02'},
70                    'vnfd-connection-point-ref': 'xe0',
71                    'name': 'xe0'},
72                   {'virtual-interface':
73                    {'dst_mac': '00:00:00:00:00:03',
74                     'vpci': '0000:05:00.1',
75                     'local_ip': '152.16.40.19',
76                     'type': 'PCI-PASSTHROUGH',
77                     'driver': "i40e",
78                     'netmask': '255.255.255.0',
79                     'dpdk_port_num': 1,
80                     'bandwidth': '10 Gbps',
81                     'dst_ip': '152.16.40.20',
82                     'local_iface_name': 'xe1',
83                     'local_mac': '00:00:00:00:00:01'},
84                    'vnfd-connection-point-ref': 'xe1',
85                    'name': 'xe1'}]}],
86                'description': 'Vpe approximation using DPDK',
87                'mgmt-interface':
88                    {'vdu-id': 'vpevnf-baremetal',
89                     'host': '1.1.1.1',
90                     'password': 'r00t',
91                     'user': 'root',
92                     'ip': '1.1.1.1'},
93                'benchmark':
94                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
95                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
96                                     {'type': 'VPORT', 'name': 'xe1'}],
97                'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
98
99     TRAFFIC_PROFILE = {
100         "schema": "isb:traffic_profile:0.1",
101         "name": "fixed",
102         "description": "Fixed traffic profile to run UDP traffic",
103         "traffic_profile": {
104             "traffic_type": "FixedTraffic",
105             "frame_rate": 100,  # pps
106             "flow_number": 10,
107             "frame_size": 64}}
108
109     def setUp(self):
110         self._mock_call = mock.patch.object(subprocess, 'call')
111         self.mock_call = self._mock_call.start()
112         self._mock_open = mock.patch.object(tg_ixload, 'open')
113         self.mock_open = self._mock_open.start()
114         self._mock_ssh = mock.patch.object(ssh, 'SSH')
115         self.mock_ssh = self._mock_ssh.start()
116         ssh_obj_mock = mock.Mock(autospec=ssh.SSH)
117         ssh_obj_mock.execute = mock.Mock(return_value=(0, '', ''))
118         ssh_obj_mock.run = mock.Mock(return_value=(0, '', ''))
119         self.mock_ssh.from_node.return_value = ssh_obj_mock
120         self.addCleanup(self._stop_mock)
121
122     def _stop_mock(self):
123         self._mock_call.stop()
124         self._mock_open.stop()
125         self._mock_ssh.stop()
126
127     def test___init__(self):
128         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
129         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
130         self.assertIsNone(ixload_traffic_gen.resource_helper.data)
131
132     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server',
133                        return_value='mock_node')
134     def test_collect_kpi(self, *args):
135         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
136         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
137         ixload_traffic_gen.scenario_helper.scenario_cfg = {
138             'nodes': {ixload_traffic_gen.name: "mock"}
139         }
140         ixload_traffic_gen.data = {}
141         result = ixload_traffic_gen.collect_kpi()
142
143         expected = {
144             'physical_node': 'mock_node',
145             'collect_stats': {}}
146         self.assertEqual(expected, result)
147
148     def test_listen_traffic(self):
149         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
150         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
151         self.assertIsNone(ixload_traffic_gen.listen_traffic({}))
152
153     @mock.patch.object(utils, 'find_relative_file')
154     @mock.patch.object(utils, 'makedirs')
155     @mock.patch.object(ctx_base.Context, 'get_context_from_server')
156     @mock.patch.object(tg_ixload, 'shutil')
157     def test_instantiate(self, mock_shutil, *args):
158         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
159         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
160         scenario_cfg = {'tc': "nsb_test_case",
161                         'ixia_profile': "ixload.cfg",
162                         'task_path': "/path/to/task"}
163         ixload_traffic_gen.RESULTS_MOUNT = "/tmp/result"
164         mock_shutil.copy = mock.Mock()
165         scenario_cfg.update(
166             {'options':
167                  {'packetsize': 64, 'traffic_type': 4,
168                   'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
169                   'vnf__1': {'rules': 'acl_1rule.yaml',
170                              'vnf_config': {'lb_config': 'SW',
171                                             'lb_count': 1,
172                                             'worker_config':
173                                                 '1C/1T',
174                                             'worker_threads': 1}}
175                   }
176              }
177         )
178         scenario_cfg.update({'nodes': {ixload_traffic_gen.name: "mock"}})
179         with mock.patch.object(six.moves.builtins, 'open',
180                                create=True) as mock_open:
181             mock_open.return_value = mock.MagicMock()
182             ixload_traffic_gen.instantiate(scenario_cfg, {})
183
184     @mock.patch.object(tg_ixload, 'open')
185     @mock.patch.object(tg_ixload, 'min')
186     @mock.patch.object(tg_ixload, 'max')
187     @mock.patch.object(tg_ixload, 'len')
188     @mock.patch.object(tg_ixload, 'shutil')
189     def test_run_traffic(self, *args):
190         mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
191         mock_traffic_profile.get_traffic_definition.return_value = '64'
192         mock_traffic_profile.params = self.TRAFFIC_PROFILE
193         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
194         vnfd['mgmt-interface'].update({'tg-config': {}})
195         vnfd['mgmt-interface']['tg-config'].update({'ixchassis': '1.1.1.1'})
196         vnfd['mgmt-interface']['tg-config'].update({'py_bin_path': '/root'})
197         sut = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
198         sut.connection = mock.Mock()
199         sut._traffic_runner = mock.Mock(return_value=0)
200         result = sut.run_traffic(mock_traffic_profile)
201         self.assertIsNone(result)
202
203     @mock.patch.object(tg_ixload, 'open')
204     @mock.patch.object(tg_ixload, 'min')
205     @mock.patch.object(tg_ixload, 'max')
206     @mock.patch.object(tg_ixload, 'len')
207     @mock.patch.object(tg_ixload, 'shutil')
208     def test_run_traffic_csv(self, *args):
209         mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
210         mock_traffic_profile.get_traffic_definition.return_value = '64'
211         mock_traffic_profile.params = self.TRAFFIC_PROFILE
212         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
213         vnfd['mgmt-interface'].update({'tg-config': {}})
214         vnfd['mgmt-interface']['tg-config'].update({'ixchassis': '1.1.1.1'})
215         vnfd['mgmt-interface']['tg-config'].update({'py_bin_path': '/root'})
216         sut = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
217         sut.connection = mock.Mock()
218         sut._traffic_runner = mock.Mock(return_value=0)
219         subprocess.call(['touch', '/tmp/1.csv'])
220         sut.rel_bin_path = mock.Mock(return_value='/tmp/*.csv')
221         result = sut.run_traffic(mock_traffic_profile)
222         self.assertIsNone(result)
223
224     def test_terminate(self):
225         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
226         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
227         self.assertIsNone(ixload_traffic_gen.terminate())
228
229     def test_parse_csv_read(self):
230         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
231         kpi_data = {
232             'HTTP Total Throughput (Kbps)': 1,
233             'HTTP Simulated Users': 2,
234             'HTTP Concurrent Connections': '3',
235             'HTTP Connection Rate': 4.3,
236             'HTTP Transaction Rate': True,
237         }
238         http_reader = [kpi_data]
239         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
240         result = ixload_traffic_gen.resource_helper.result
241         ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
242         for k_left, k_right in tg_ixload.IxLoadResourceHelper.KPI_LIST.items():
243             self.assertEqual(result[k_left][-1], int(kpi_data[k_right]))
244
245     def test_parse_csv_read_value_error(self):
246         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
247         http_reader = [{
248             'HTTP Total Throughput (Kbps)': 1,
249             'HTTP Simulated Users': 2,
250             'HTTP Concurrent Connections': "not a number",
251             'HTTP Connection Rate': 4,
252             'HTTP Transaction Rate': 5,
253         }]
254         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
255         init_value = ixload_traffic_gen.resource_helper.result
256         ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
257         self.assertDictEqual(ixload_traffic_gen.resource_helper.result,
258                              init_value)
259
260     def test_parse_csv_read_error(self,):
261         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
262         http_reader = [{
263             'HTTP Total Throughput (Kbps)': 1,
264             'HTTP Simulated Users': 2,
265             'HTTP Concurrent Connections': 3,
266             'HTTP Transaction Rate': 5,
267         }]
268         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
269
270         with self.assertRaises(KeyError):
271             ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)