Merge "Add Grafana Dashboard for vCMTS testcase"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_tg_pktgen.py
1 # Copyright (c) 2018-2019 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 mock
16
17 from yardstick.common import constants
18 from yardstick.common import exceptions
19 from yardstick.network_services.vnf_generic.vnf import base as vnf_base
20 from yardstick.network_services.vnf_generic.vnf import tg_pktgen
21 from yardstick.tests.unit import base as ut_base
22
23
24 class PktgenTrafficGenTestCase(ut_base.BaseUnitTestCase):
25
26     SERVICE_PORTS = [{'port': constants.LUA_PORT,
27                       'node_port': '34501'}]
28     VNFD = {'mgmt-interface': {'ip': '1.2.3.4',
29                                'service_ports': SERVICE_PORTS},
30             'vdu': [{'external-interface': 'interface'}],
31             'benchmark': {'kpi': 'fake_kpi'}
32             }
33
34     def test__init(self):
35         tg = tg_pktgen.PktgenTrafficGen('name1', self.VNFD)
36         self.assertTrue(isinstance(tg, vnf_base.GenericTrafficGen))
37
38     def test_run_traffic(self):
39         tg = tg_pktgen.PktgenTrafficGen('name1', self.VNFD)
40         mock_tp = mock.Mock()
41         with mock.patch.object(tg, '_is_running', return_value=True):
42             tg.run_traffic(mock_tp)
43
44         mock_tp.init.assert_called_once_with(tg._node_ip, tg._lua_node_port)
45
46     def test__get_lua_node_port(self):
47         tg = tg_pktgen.PktgenTrafficGen('name1', self.VNFD)
48         service_ports = [{'port': constants.LUA_PORT,
49                           'node_port': '12345'}]
50         self.assertEqual(12345, tg._get_lua_node_port(service_ports))
51
52     def test__get_lua_node_port_no_lua_port(self):
53         tg = tg_pktgen.PktgenTrafficGen('name1', self.VNFD)
54         service_ports = [{'port': '333'}]
55         self.assertIsNone(tg._get_lua_node_port(service_ports))
56
57     def test__is_running(self):
58         tg = tg_pktgen.PktgenTrafficGen('name1', self.VNFD)
59         with mock.patch.object(tg, '_traffic_profile'):
60             self.assertTrue(tg._is_running())
61
62     def test__is_running_exception(self):
63         tg = tg_pktgen.PktgenTrafficGen('name1', self.VNFD)
64         with mock.patch.object(tg, '_traffic_profile') as mock_tp:
65             mock_tp.help.side_effect = exceptions.PktgenActionError()
66             self.assertFalse(tg._is_running())