Standardized TRex client library path
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_tg_trex.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 from __future__ import absolute_import
19
20 import unittest
21 import mock
22
23 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
24 from tests.unit import STL_MOCKS
25
26
27 NAME = 'vnf_1'
28
29 STLClient = mock.MagicMock()
30 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
31 stl_patch.start()
32
33 if stl_patch:
34     from yardstick.network_services.vnf_generic.vnf.tg_trex import \
35     TrexTrafficGen, TrexResourceHelper
36     from yardstick.network_services.traffic_profile.base import TrafficProfile
37
38
39 class TestTrexTrafficGen(unittest.TestCase):
40     VNFD = {'vnfd:vnfd-catalog':
41             {'vnfd':
42              [{'short-name': 'VpeVnf',
43                'vdu':
44                [{'routing_table':
45                  [{'network': '152.16.100.20',
46                    'netmask': '255.255.255.0',
47                    'gateway': '152.16.100.20',
48                    'if': 'xe0'},
49                   {'network': '152.16.40.20',
50                    'netmask': '255.255.255.0',
51                    'gateway': '152.16.40.20',
52                    'if': 'xe1'}],
53                  'description': 'VPE approximation using DPDK',
54                  'name': 'vpevnf-baremetal',
55                  'nd_route_tbl':
56                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
57                    'netmask': '112',
58                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
59                    'if': 'xe0'},
60                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
61                    'netmask': '112',
62                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
63                    'if': 'xe1'}],
64                  'id': 'vpevnf-baremetal',
65                  'external-interface':
66                  [{'virtual-interface':
67                    {'dst_mac': '00:00:00:00:00:04',
68                     'vpci': '0000:05:00.0',
69                     'local_ip': '152.16.100.19',
70                     'type': 'PCI-PASSTHROUGH',
71                     'netmask': '255.255.255.0',
72                     'dpdk_port_num': '0',
73                     'bandwidth': '10 Gbps',
74                     'driver': "i40e",
75                     'dst_ip': '152.16.100.20',
76                     'local_iface_name': 'xe0',
77                     'local_mac': '00:00:00:00:00:02'},
78                    'vnfd-connection-point-ref': 'xe0',
79                    'name': 'xe0'},
80                   {'virtual-interface':
81                    {'dst_mac': '00:00:00:00:00:03',
82                     'vpci': '0000:05:00.1',
83                     'local_ip': '152.16.40.19',
84                     'type': 'PCI-PASSTHROUGH',
85                     'driver': "i40e",
86                     'netmask': '255.255.255.0',
87                     'dpdk_port_num': '1',
88                     'bandwidth': '10 Gbps',
89                     'dst_ip': '152.16.40.20',
90                     'local_iface_name': 'xe1',
91                     'local_mac': '00:00:00:00:00:01'},
92                    'vnfd-connection-point-ref': 'xe1',
93                    'name': 'xe1'}]}],
94                'description': 'Vpe approximation using DPDK',
95                'mgmt-interface':
96                    {'vdu-id': 'vpevnf-baremetal',
97                     'host': '1.1.1.1',
98                     'password': 'r00t',
99                     'user': 'root',
100                     'ip': '1.1.1.1'},
101                'benchmark':
102                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
103                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
104                                     {'type': 'VPORT', 'name': 'xe1'}],
105                'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
106
107     TRAFFIC_PROFILE = {
108         "schema": "isb:traffic_profile:0.1",
109         "name": "fixed",
110         "description": "Fixed traffic profile to run UDP traffic",
111         "traffic_profile": {
112             "traffic_type": "FixedTraffic",
113             "frame_rate": 100,  # pps
114             "flow_number": 10,
115             "frame_size": 64}}
116
117     @mock.patch("yardstick.ssh.SSH")
118     def test___init__(self, ssh):
119         mock_ssh(ssh)
120         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
121         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
122         self.assertIsInstance(trex_traffic_gen.resource_helper, TrexResourceHelper)
123
124     @mock.patch("yardstick.ssh.SSH")
125     def test_collect_kpi(self, ssh):
126         mock_ssh(ssh)
127         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
128         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
129         trex_traffic_gen.resource_helper._queue.put({})
130         result = trex_traffic_gen.collect_kpi()
131         self.assertEqual({}, result)
132
133     @mock.patch("yardstick.ssh.SSH")
134     def test_listen_traffic(self, ssh):
135         mock_ssh(ssh)
136         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
137         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
138         self.assertIsNone(trex_traffic_gen.listen_traffic({}))
139
140     @mock.patch("yardstick.ssh.SSH")
141     def test_instantiate(self, ssh):
142         mock_ssh(ssh)
143
144         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
145         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
146         trex_traffic_gen._start_server = mock.Mock(return_value=0)
147         trex_traffic_gen._tg_process = mock.MagicMock()
148         trex_traffic_gen._tg_process.start = mock.Mock()
149         trex_traffic_gen._tg_process.exitcode = 0
150         trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=1)
151         trex_traffic_gen.ssh_helper = mock.MagicMock()
152         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
153         self.assertIsNone(trex_traffic_gen.instantiate({}, {}))
154
155     @mock.patch("yardstick.ssh.SSH")
156     def test_instantiate_error(self, ssh):
157         mock_ssh(ssh, exec_result=(1, "", ""))
158
159         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
160         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
161         trex_traffic_gen._start_server = mock.Mock(return_value=0)
162         trex_traffic_gen._tg_process = mock.MagicMock()
163         trex_traffic_gen._tg_process.start = mock.Mock()
164         trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=0)
165         trex_traffic_gen.ssh_helper = mock.MagicMock()
166         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
167         self.assertIsNone(trex_traffic_gen.instantiate({}, {}))
168
169     @mock.patch("yardstick.ssh.SSH")
170     def test__start_server(self, ssh):
171         mock_ssh(ssh)
172         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
173         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
174         trex_traffic_gen.ssh_helper = mock.MagicMock()
175         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
176         self.assertIsNone(trex_traffic_gen._start_server())
177
178     @mock.patch("yardstick.ssh.SSH")
179     def test__traffic_runner(self, ssh):
180         mock_ssh(ssh)
181
182         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
183         mock_traffic_profile.get_traffic_definition.return_value = "64"
184         mock_traffic_profile.execute.return_value = "64"
185         mock_traffic_profile.params = self.TRAFFIC_PROFILE
186
187         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
188         self.sut = TrexTrafficGen(NAME, vnfd)
189         self.sut.ssh_helper = mock.Mock()
190         self.sut.ssh_helper.run = mock.Mock()
191         self.sut._vpci_ascending = ["0000:05:00.0", "0000:05:00.1"]
192         self.sut._connect_client = mock.Mock(autospec=STLClient)
193         self.sut._connect_client.get_stats = mock.Mock(return_value="0")
194         self.sut.resource_helper.RUN_DURATION = 0
195         self.sut.resource_helper.QUEUE_WAIT_TIME = 0
196         self.sut._traffic_runner(mock_traffic_profile)
197
198     @mock.patch("yardstick.ssh.SSH")
199     def test__generate_trex_cfg(self, ssh):
200         mock_ssh(ssh)
201         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
202         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
203         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
204         self.assertIsNone(trex_traffic_gen.resource_helper.generate_cfg())
205
206     @mock.patch("yardstick.ssh.SSH")
207     def test_run_traffic(self, ssh):
208         mock_ssh(ssh)
209
210         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
211         mock_traffic_profile.get_traffic_definition.return_value = "64"
212         mock_traffic_profile.params = self.TRAFFIC_PROFILE
213
214         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
215         self.sut = TrexTrafficGen(NAME, vnfd)
216         self.sut.ssh_helper = mock.Mock()
217         self.sut.ssh_helper.run = mock.Mock()
218         self.sut._traffic_runner = mock.Mock(return_value=0)
219         self.sut.resource_helper.client_started.value = 1
220         result = self.sut.run_traffic(mock_traffic_profile)
221         self.sut._traffic_process.terminate()
222         self.assertIsNotNone(result)
223
224     @mock.patch("yardstick.ssh.SSH")
225     def test_scale(self, ssh):
226         mock_ssh(ssh, exec_result=(1, "", ""))
227         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
228         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
229         trex_traffic_gen.scale('')
230
231     @mock.patch("yardstick.ssh.SSH")
232     def test_setup_vnf_environment(self, ssh):
233         mock_ssh(ssh, exec_result=(1, "", ""))
234         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
235         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
236         self.assertIsNone(trex_traffic_gen.setup_helper.setup_vnf_environment())
237
238     @mock.patch("yardstick.ssh.SSH")
239     def test_terminate(self, ssh):
240         mock_ssh(ssh)
241         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
242         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
243         trex_traffic_gen.ssh_helper = mock.MagicMock()
244         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
245         self.assertIsNone(trex_traffic_gen.terminate())
246
247     @mock.patch("yardstick.ssh.SSH")
248     def test__connect_client(self, ssh):
249         mock_ssh(ssh)
250         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
251         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
252         client = mock.Mock(autospec=STLClient)
253         client.connect = mock.Mock(return_value=0)
254         self.assertIsNotNone(trex_traffic_gen.resource_helper._connect(client))
255
256 if __name__ == '__main__':
257     unittest.main()