aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/tests/unit/network_services/vnf_generic/vnf/test_tg_ping.py
blob: a3e4384cf26de53c50e07d75a3b65eb0711b32ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Copyright (c) 2016-2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from multiprocessing import Queue
import multiprocessing

import mock
import unittest

from yardstick.tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
from yardstick.benchmark.contexts import base as ctx_base
from yardstick.network_services.vnf_generic.vnf.tg_ping import PingParser
from yardstick.network_services.vnf_generic.vnf.tg_ping import PingTrafficGen
from yardstick.network_services.vnf_generic.vnf.tg_ping import PingResourceHelper
from yardstick.network_services.vnf_generic.vnf.tg_ping import PingSetupEnvHelper
from yardstick.network_services.vnf_generic.vnf.vnf_ssh_helper import VnfSshHelper


SSH_HELPER = "yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper"


class TestPingResourceHelper(unittest.TestCase):
    def test___init__(self):
        setup_helper = mock.Mock()
        helper = PingResourceHelper(setup_helper)

        self.assertIsInstance(helper._queue, multiprocessing.queues.Queue)
        self.assertIsInstance(helper._parser, PingParser)

    def test_run_traffic(self):
        setup_helper = mock.Mock()
        traffic_profile = mock.Mock()
        traffic_profile.params = {
            'traffic_profile': {
                'frame_size': 64,
            },
        }

        helper = PingResourceHelper(setup_helper)
        helper.cmd_kwargs = {'target_ip': '10.0.0.2',
                             'local_ip': '10.0.0.1',
                             'local_if_name': 'eth0',
                             }
        helper.ssh_helper = mock.Mock()
        helper.run_traffic(traffic_profile)
        helper.ssh_helper.run.called_with('ping-s 64 10.0.0.2')


class TestPingParser(unittest.TestCase):
    def test___init__(self):
        q_out = Queue()
        ping_parser = PingParser(q_out)
        self.assertIsNotNone(ping_parser.queue)

    def test_clear(self):
        sample_out = """
64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
         """
        q_out = Queue()
        ping_parser = PingParser(q_out)
        ping_parser.write(sample_out)
        ping_parser.clear()
        self.assertTrue(q_out.empty())

    def test_close(self):
        q_out = Queue()
        ping_parser = PingParser(q_out)
        self.assertIsNone(ping_parser.close())

    def test_write(self):
        sample_out = """
64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
         """
        q_out = Queue()
        ping_parser = PingParser(q_out)
        ping_parser.write(sample_out)

        self.assertEqual({"packets_received": 3.0, "rtt": 0.296}, q_out.get())


class TestPingTrafficGen(unittest.TestCase):
    VNFD_0_EXT_IF_0 = {
        'virtual-interface': {
            'dst_mac': '00:00:00:00:00:04',
            'vpci': '0000:05:00.0',
            'local_ip': u'152.16.100.19',
            'type': 'PCI-PASSTHROUGH',
            'netmask': '255.255.255.0',
            'bandwidth': '10 Gbps',
            'driver': "i40e",
            'dst_ip': u'152.16.100.20',
            'local_iface_name': 'xe0',
            'local_mac': '00:00:00:00:00:02',
        },
        'vnfd-connection-point-ref': 'xe0',
        'name': 'xe0',
    }

    VNFD_0_EXT_IF_1 = {
        'virtual-interface': {
            'dst_mac': '00:00:00:00:00:03',
            'vpci': '0000:05:00.1',
            'local_ip': u'152.16.40.19',
            'type': 'PCI-PASSTHROUGH',
            'driver': "i40e",
            'netmask': '255.255.255.0',
            'bandwidth': '10 Gbps',
            'dst_ip': u'152.16.40.20',
            'local_iface_name': 'xe1',
            'local_mac': '00:00:00:00:00:01',
        },
        'vnfd-connection-point-ref': 'xe1',
        'name': 'xe1',
    }

    VNFD_0_EXT_IF_LIST = [
        VNFD_0_EXT_IF_0,
        VNFD_0_EXT_IF_1,
    ]

    VNFD_0 = {
        'short-name': 'VpeVnf',
        'vdu': [
            {
                'routing_table': [
                    {
                        'network': u'152.16.100.20',
                        'netmask': u'255.255.255.0',
                        'gateway': u'152.16.100.20',
                        'if': 'xe0',
                    },
                    {
                        'network': u'152.16.40.20',
                        'netmask': u'255.255.255.0',
                        'gateway': u'152.16.40.20',
                        'if': 'xe1',
                    },
                ],
                'description': 'VPE approximation using DPDK',
                'name': 'vpevnf-baremetal',
                'nd_route_tbl': [
                    {
                        'network': '0064:ff9b:0:0:0:0:9810:6414',
                        'netmask': '112',
                        'gateway': '0064:ff9b:0:0:0:0:9810:6414',
                        'if': 'xe0',
                    },
                    {
                        'network': '0064:ff9b:0:0:0:0:9810:2814',
                        'netmask': '112',
                        'gateway': '0064:ff9b:0:0:0:0:9810:2814',
                        'if': 'xe1',
                    },
                ],
                'id': 'vpevnf-baremetal',
                'external-interface': VNFD_0_EXT_IF_LIST,
            },
        ],
        'description': 'Vpe approximation using DPDK',
        'mgmt-interface': {
            'vdu-id': 'vpevnf-baremetal',
            'host': '1.1.1.1',
            'password': 'r00t',
            'user': 'root',
            'ip': '1.1.1.1',
        },
        'benchmark': {
            'kpi': [
                'packets_in',
                'packets_fwd',
                'packets_dropped',
            ],
        },
        'connection-point': [
            {
                'type': 'VPORT',
                'name': 'xe0',
            },
            {
                'type': 'VPORT',
                'name': 'xe1',
            },
        ],
        'id': 'VpeApproxVnf',
        'name': 'VPEVnfSsh',
    }

    VNFD = {
        'vnfd:vnfd-catalog': {
            'vnfd': [
                VNFD_0,
            ],
        },
    }

    TRAFFIC_PROFILE = {
        "schema": "isb:traffic_profile:0.1",
        "name": "fixed",
        "description": "Fixed traffic profile to run UDP traffic",
        "traffic_profile": {
            "traffic_type": "FixedTraffic",
            "frame_rate": 100,  # pps
            "flow_number": 10,
            "frame_size": 64,
        },
    }

    CMD_KWARGS = {
        'target_ip': u'152.16.100.20',
        'local_ip': u'152.16.100.19',
        'local_if_name': u'xe0_fake',
    }

    @mock.patch("yardstick.ssh.SSH")
    def test___init__(self, ssh):
        ssh.from_node.return_value.execute.return_value = 0, "success", ""
        ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)

        self.assertIsInstance(ping_traffic_gen.setup_helper, PingSetupEnvHelper)
        self.assertIsInstance(ping_traffic_gen.resource_helper, PingResourceHelper)
        self.assertEqual(ping_traffic_gen._result, {})

    @mock.patch("yardstick.ssh.SSH")
    def test__bind_device_kernel_with_failure(self, ssh):
        mock_ssh(ssh)

        execute_result_data = [
            (1, 'bad stdout messages', 'error messages'),
            (0, '', ''),
            (0, 'if_name_1', ''),
            (0, 'if_name_2', ''),
        ]
        ssh.from_node.return_value.execute.side_effect = iter(execute_result_data)
        ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
        ext_ifs = ping_traffic_gen.vnfd_helper.interfaces
        self.assertNotEqual(ext_ifs[0]['virtual-interface']['local_iface_name'], 'if_name_1')
        self.assertNotEqual(ext_ifs[1]['virtual-interface']['local_iface_name'], 'if_name_2')

    @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
    @mock.patch("yardstick.ssh.SSH")
    def test_collect_kpi(self, ssh, *args):
        mock_ssh(ssh, exec_result=(0, "success", ""))

        ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
        ping_traffic_gen.scenario_helper.scenario_cfg = {
            'nodes': {ping_traffic_gen.name: "mock"}
        }
        ping_traffic_gen._queue = Queue()
        ping_traffic_gen._queue.put({})
        expected = {
            'physical_node': 'mock_node',
            'collect_stats': {}
        }
        # NOTE: Why we check _result but not collect_kpi() return value
        # self.assertEqual(ping_traffic_gen._result, {})
        self.assertEqual(ping_traffic_gen.collect_kpi(), expected)


    @mock.patch(SSH_HELPER)
    def test_instantiate(self, ssh):
        mock_ssh(ssh, spec=VnfSshHelper, exec_result=(0, "success", ""))
        ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
        ping_traffic_gen.setup_helper.ssh_helper = mock.MagicMock(
            **{"execute.return_value": (0, "xe0_fake", "")})
        self.assertIsInstance(ping_traffic_gen.ssh_helper, mock.Mock)
        self.assertEqual(ping_traffic_gen._result, {})

        self.assertIsNone(ping_traffic_gen.instantiate({}, {}))

        self.assertEqual(
            ping_traffic_gen.vnfd_helper.interfaces[0]['virtual-interface']['local_iface_name'],
            'xe0_fake')
        self.assertEqual(self.CMD_KWARGS, ping_traffic_gen.resource_helper.cmd_kwargs)
        self.assertIsNotNone(ping_traffic_gen._result)

    def test_listen_traffic(self):
        ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
        self.assertIsNone(ping_traffic_gen.listen_traffic({}))

    @mock.patch("yardstick.ssh.SSH")
    def test_terminate(self, ssh):
        ssh.from_node.return_value.execute.return_value = 0, "success", ""
        ssh.from_node.return_value.run.return_value = 0, "success", ""

        ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
        self.assertIsNone(ping_traffic_gen.terminate())