Create Dockerfile to create a yardstick-image of docker
[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 import unittest
20
21 from yardstick import ssh
22 from yardstick.benchmark.contexts import base as ctx_base
23 from yardstick.common import utils
24 from yardstick.network_services.vnf_generic.vnf import tg_ixload
25 from yardstick.network_services.traffic_profile.base import TrafficProfile
26
27
28 NAME = "tg__1"
29
30
31 class TestIxLoadTrafficGen(unittest.TestCase):
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.addCleanup(self._stop_mock)
115
116     def _stop_mock(self):
117         self._mock_call.stop()
118         self._mock_open.stop()
119
120     @mock.patch.object(ssh, 'SSH')
121     def test___init__(self, *args):
122         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
123         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
124         self.assertIsNone(ixload_traffic_gen.resource_helper.data)
125
126     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server',
127                        return_value='mock_node')
128     @mock.patch.object(ssh, 'SSH')
129     def test_collect_kpi(self, *args):
130         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
131         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
132         ixload_traffic_gen.scenario_helper.scenario_cfg = {
133             'nodes': {ixload_traffic_gen.name: "mock"}
134         }
135         ixload_traffic_gen.data = {}
136         result = ixload_traffic_gen.collect_kpi()
137
138         expected = {
139             'physical_node': 'mock_node',
140             'collect_stats': {}}
141         self.assertEqual(expected, result)
142
143     @mock.patch.object(ssh, 'SSH')
144     def test_listen_traffic(self, *args):
145         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
146         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
147         self.assertIsNone(ixload_traffic_gen.listen_traffic({}))
148
149     @mock.patch.object(utils, 'find_relative_file')
150     @mock.patch.object(utils, 'makedirs')
151     @mock.patch.object(ctx_base.Context, 'get_context_from_server')
152     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
153     @mock.patch.object(ssh, 'SSH')
154     def test_instantiate(self, mock_shutil, *args):
155         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
156         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
157         scenario_cfg = {'tc': "nsb_test_case",
158                         'ixia_profile': "ixload.cfg",
159                         'task_path': "/path/to/task"}
160         ixload_traffic_gen.RESULTS_MOUNT = "/tmp/result"
161         mock_shutil.copy = mock.Mock()
162         scenario_cfg.update({'options': {'packetsize': 64, 'traffic_type': 4,
163                                          'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
164                                          'vnf__1': {'rules': 'acl_1rule.yaml',
165                                                     'vnf_config': {'lb_config': 'SW',
166                                                                    'lb_count': 1,
167                                                                    'worker_config':
168                                                                    '1C/1T',
169                                                                    'worker_threads': 1}}
170                                          }})
171         scenario_cfg.update({
172             'nodes': {ixload_traffic_gen.name: "mock"}
173         })
174         with mock.patch.object(six.moves.builtins, 'open',
175                                create=True) as mock_open:
176             mock_open.return_value = mock.MagicMock()
177             ixload_traffic_gen.instantiate(scenario_cfg, {})
178
179     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.open")
180     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.min")
181     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.max")
182     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.len")
183     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
184     @mock.patch.object(ssh, 'SSH')
185     def test_run_traffic(self, *args):
186         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
187         mock_traffic_profile.get_traffic_definition.return_value = "64"
188         mock_traffic_profile.params = self.TRAFFIC_PROFILE
189         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
190         vnfd["mgmt-interface"].update({"tg-config": {}})
191         vnfd["mgmt-interface"]["tg-config"].update({"ixchassis":
192                                                     "1.1.1.1"})
193         vnfd["mgmt-interface"]["tg-config"].update({"py_bin_path":
194                                                     "/root"})
195         sut = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
196         sut.connection = mock.Mock()
197         sut.connection.run = mock.Mock()
198         sut._traffic_runner = mock.Mock(return_value=0)
199         result = sut.run_traffic(mock_traffic_profile)
200         self.assertIsNone(result)
201
202     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.open")
203     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.min")
204     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.max")
205     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.len")
206     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
207     @mock.patch.object(ssh, 'SSH')
208     def test_run_traffic_csv(self, *args):
209         mock_traffic_profile = mock.Mock(autospec=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":
215                                                     "1.1.1.1"})
216         vnfd["mgmt-interface"]["tg-config"].update({"py_bin_path":
217                                                     "/root"})
218         sut = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
219         sut.connection = mock.Mock()
220         sut.connection.run = mock.Mock()
221         sut._traffic_runner = mock.Mock(return_value=0)
222         sut.rel_bin_path = mock.Mock(return_value="/tmp/*.csv")
223         result = sut.run_traffic(mock_traffic_profile)
224         self.assertIsNone(result)
225
226     @mock.patch.object(ssh, 'SSH')
227     def test_terminate(self, *args):
228         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
229         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
230         self.assertIsNone(ixload_traffic_gen.terminate())
231
232     @mock.patch.object(ssh, 'SSH')
233     def test_parse_csv_read(self, mock_ssh):
234         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
235         kpi_data = {
236             'HTTP Total Throughput (Kbps)': 1,
237             'HTTP Simulated Users': 2,
238             'HTTP Concurrent Connections': '3',
239             'HTTP Connection Rate': 4.3,
240             'HTTP Transaction Rate': True,
241         }
242         http_reader = [kpi_data]
243
244         mock_ssh_type = mock.Mock(autospec=mock_ssh.SSH)
245         mock_ssh_type.execute.return_value = 0, "", ""
246         mock_ssh.from_node.return_value = mock_ssh_type
247
248         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
249         result = ixload_traffic_gen.resource_helper.result
250
251         ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
252         for key_left, key_right in (
253                 tg_ixload.IxLoadResourceHelper.KPI_LIST.items()):
254             self.assertEqual(result[key_left][-1], int(kpi_data[key_right]))
255
256     @mock.patch.object(ssh, 'SSH')
257     def test_parse_csv_read_value_error(self, mock_ssh, *args):
258         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
259         http_reader = [{
260             'HTTP Total Throughput (Kbps)': 1,
261             'HTTP Simulated Users': 2,
262             'HTTP Concurrent Connections': "not a number",
263             'HTTP Connection Rate': 4,
264             'HTTP Transaction Rate': 5,
265         }]
266
267         mock_ssh_type = mock.Mock(autospec=mock_ssh.SSH)
268         mock_ssh_type.execute.return_value = 0, "", ""
269         mock_ssh.from_node.return_value = mock_ssh_type
270
271         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
272         init_value = ixload_traffic_gen.resource_helper.result
273
274         ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
275         self.assertDictEqual(ixload_traffic_gen.resource_helper.result, init_value)
276
277     @mock.patch.object(ssh, 'SSH')
278     def test_parse_csv_read_error(self, mock_ssh, *args):
279         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
280         http_reader = [{
281             'HTTP Total Throughput (Kbps)': 1,
282             'HTTP Simulated Users': 2,
283             'HTTP Concurrent Connections': 3,
284             'HTTP Transaction Rate': 5,
285         }]
286
287         mock_ssh_type = mock.Mock(autospec=mock_ssh.SSH)
288         mock_ssh_type.execute.return_value = 0, "", ""
289         mock_ssh.from_node.return_value = mock_ssh_type
290
291         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd)
292
293         with self.assertRaises(KeyError):
294             ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)