Merge "Adding ping based sample VNF appliance"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_tg_ping.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 import unittest
20 import mock
21 from multiprocessing import Queue
22
23 from yardstick.network_services.vnf_generic.vnf.tg_ping import \
24     PingParser, PingTrafficGen
25 from yardstick.network_services.traffic_profile.base import TrafficProfile
26
27
28 class TestPingParser(unittest.TestCase):
29     def test___init__(self):
30         q_out = Queue()
31         ping_parser = PingParser(q_out)
32         self.assertIsNotNone(ping_parser.queue)
33
34     def test_clear(self):
35         sample_out = """
36 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
37          """
38         q_out = Queue()
39         ping_parser = PingParser(q_out)
40         ping_parser.write(sample_out)
41         ping_parser.clear()
42         self.assertEqual(True, q_out.empty())
43
44     def test_close(self):
45         q_out = Queue()
46         ping_parser = PingParser(q_out)
47         self.assertIsNone(ping_parser.close())
48
49     def test_write(self):
50         sample_out = """
51 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
52          """
53         q_out = Queue()
54         ping_parser = PingParser(q_out)
55         ping_parser.write(sample_out)
56
57         self.assertEqual({"packets_received": 3.0, "rtt": 0.296}, q_out.get())
58
59
60 class TestPingTrafficGen(unittest.TestCase):
61     VNFD = {'vnfd:vnfd-catalog':
62             {'vnfd':
63              [{'short-name': 'VpeVnf',
64                'vdu':
65                [{'routing_table':
66                  [{'network': '152.16.100.20',
67                    'netmask': '255.255.255.0',
68                    'gateway': '152.16.100.20',
69                    'if': 'xe0'},
70                   {'network': '152.16.40.20',
71                    'netmask': '255.255.255.0',
72                    'gateway': '152.16.40.20',
73                    'if': 'xe1'}],
74                  'description': 'VPE approximation using DPDK',
75                  'name': 'vpevnf-baremetal',
76                  'nd_route_tbl':
77                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
78                    'netmask': '112',
79                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
80                    'if': 'xe0'},
81                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
82                    'netmask': '112',
83                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
84                    'if': 'xe1'}],
85                  'id': 'vpevnf-baremetal',
86                  'external-interface':
87                  [{'virtual-interface':
88                    {'dst_mac': '00:00:00:00:00:04',
89                     'vpci': '0000:05:00.0',
90                     'local_ip': '152.16.100.19',
91                     'type': 'PCI-PASSTHROUGH',
92                     'netmask': '255.255.255.0',
93                     'dpdk_port_num': '0',
94                     'bandwidth': '10 Gbps',
95                     'driver': "i40e",
96                     'dst_ip': '152.16.100.20',
97                     'local_iface_name': 'xe0',
98                     'local_mac': '00:00:00:00:00:02'},
99                    'vnfd-connection-point-ref': 'xe0',
100                    'name': 'xe0'},
101                   {'virtual-interface':
102                    {'dst_mac': '00:00:00:00:00:03',
103                     'vpci': '0000:05:00.1',
104                     'local_ip': '152.16.40.19',
105                     'type': 'PCI-PASSTHROUGH',
106                     'driver': "i40e",
107                     'netmask': '255.255.255.0',
108                     'dpdk_port_num': '1',
109                     'bandwidth': '10 Gbps',
110                     'dst_ip': '152.16.40.20',
111                     'local_iface_name': 'xe1',
112                     'local_mac': '00:00:00:00:00:01'},
113                    'vnfd-connection-point-ref': 'xe1',
114                    'name': 'xe1'}]}],
115                'description': 'Vpe approximation using DPDK',
116                'mgmt-interface':
117                    {'vdu-id': 'vpevnf-baremetal',
118                     'host': '1.1.1.1',
119                     'password': 'r00t',
120                     'user': 'root',
121                     'ip': '1.1.1.1'},
122                'benchmark':
123                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
124                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
125                                     {'type': 'VPORT', 'name': 'xe1'}],
126                'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
127
128     TRAFFIC_PROFILE = {
129         "schema": "isb:traffic_profile:0.1",
130         "name": "fixed",
131         "description": "Fixed traffic profile to run UDP traffic",
132         "traffic_profile": {
133             "traffic_type": "FixedTraffic",
134             "frame_rate": 100,  # pps
135             "flow_number": 10,
136             "frame_size": 64}}
137
138     def test___init__(self):
139         with mock.patch("yardstick.ssh.SSH") as ssh:
140             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
141             ssh_mock = mock.Mock(autospec=ssh.SSH)
142             ssh_mock.execute = \
143                 mock.Mock(return_value=(0, "", ""))
144             ssh.return_value = ssh_mock
145             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
146             ping_traffic_gen = PingTrafficGen(vnfd)
147             self.assertEqual(ping_traffic_gen._queue, None)
148
149     def test_collect_kpi(self):
150         with mock.patch("yardstick.ssh.SSH") as ssh:
151             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
152             ssh_mock = mock.Mock(autospec=ssh.SSH)
153             ssh_mock.execute = \
154                 mock.Mock(return_value=(0, "", ""))
155             ssh.return_value = ssh_mock
156             ping_traffic_gen = PingTrafficGen(vnfd)
157             ping_traffic_gen._queue = Queue()
158             ping_traffic_gen._queue.put({})
159             ping_traffic_gen.collect_kpi()
160             self.assertEqual({}, ping_traffic_gen._result)
161
162     def test_instantiate(self):
163         with mock.patch("yardstick.ssh.SSH") as ssh:
164             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
165             ssh_mock = mock.Mock(autospec=ssh.SSH)
166             ssh_mock.execute = \
167                 mock.Mock(return_value=(0, "", ""))
168             ssh.return_value = ssh_mock
169             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
170             ping_traffic_gen = PingTrafficGen(vnfd)
171             self.assertEqual(None, ping_traffic_gen.instantiate({}, {}))
172
173     def test_listen_traffic(self):
174         with mock.patch("yardstick.ssh.SSH") as ssh:
175             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
176             ssh_mock = mock.Mock(autospec=ssh.SSH)
177             ssh_mock.execute = \
178                 mock.Mock(return_value=(0, "", ""))
179             ssh.return_value = ssh_mock
180             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
181             ping_traffic_gen = PingTrafficGen(vnfd)
182             self.assertEqual(None, ping_traffic_gen.listen_traffic({}))
183
184     def test_run_traffic(self):
185         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
186         mock_traffic_profile.get_traffic_definition.return_value = "64"
187         mock_traffic_profile.params = self.TRAFFIC_PROFILE
188         with mock.patch("yardstick.ssh.SSH") as ssh:
189             ssh_mock = mock.Mock(autospec=ssh.SSH)
190             ssh_mock.execute = \
191                 mock.Mock(return_value=(0, "", ""))
192             ssh_mock.run = \
193                 mock.Mock(return_value=(0, "", ""))
194             ssh.return_value = ssh_mock
195             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
196             self.sut = PingTrafficGen(vnfd)
197             self.sut.connection = mock.Mock()
198             self.sut.connection.run = mock.Mock()
199             self.sut._traffic_runner = mock.Mock(return_value=0)
200             self.assertEqual(
201                 False, self.sut.run_traffic(mock_traffic_profile))
202
203     def test_run_traffic_process(self):
204         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
205         mock_traffic_profile.get_traffic_definition.return_value = "64"
206         mock_traffic_profile.params = self.TRAFFIC_PROFILE
207         with mock.patch("yardstick.ssh.SSH") as ssh:
208             ssh_mock = mock.Mock(autospec=ssh.SSH)
209             ssh_mock.execute = \
210                 mock.Mock(return_value=(0, "", ""))
211             ssh_mock.run = \
212                 mock.Mock(return_value=(0, "", ""))
213             ssh.return_value = ssh_mock
214             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
215             self.sut = PingTrafficGen(vnfd)
216             self.sut.connection = mock.Mock()
217             self.sut.connection.run = mock.Mock()
218             q = Queue()
219             self.sut._traffic_runner(mock_traffic_profile, q)
220             self.sut.connection.run.assert_called_with(
221                 "ping -s 64 152.16.100.20",
222                 stdout=q, keep_stdin_open=True, pty=True)
223
224     def test_scale(self):
225         with mock.patch("yardstick.ssh.SSH") as ssh:
226             ssh_mock = mock.Mock(autospec=ssh.SSH)
227             ssh_mock.execute = \
228                 mock.Mock(return_value=(0, "", ""))
229             ssh_mock.run = \
230                 mock.Mock(return_value=(0, "", ""))
231             ssh.return_value = ssh_mock
232             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
233             flavor = ""
234             ping_traffic_gen = PingTrafficGen(vnfd)
235             self.assertRaises(NotImplementedError, ping_traffic_gen.scale, flavor)
236
237     def test_terminate(self):
238         with mock.patch("yardstick.ssh.SSH") as ssh:
239             ssh_mock = mock.Mock(autospec=ssh.SSH)
240             ssh_mock.execute = \
241                 mock.Mock(return_value=(0, "", ""))
242             ssh_mock.run = \
243                 mock.Mock(return_value=(0, "", ""))
244             ssh.return_value = ssh_mock
245             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
246             ping_traffic_gen = PingTrafficGen(vnfd)
247             self.assertEqual(None, ping_traffic_gen.terminate())