Merge "Fixed document for Grafana Port usage"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_vpe_vnf.py
index ffd0d53..c074dfb 100644 (file)
 # limitations under the License.
 #
 
-from __future__ import absolute_import
-import six.moves.configparser as configparser
-
+from multiprocessing import Process, Queue
 import os
-import unittest
+import time
+
 import mock
-from multiprocessing import Process, Queue
+import six.moves.configparser as configparser
+import unittest
 
 from tests.unit import STL_MOCKS
+from tests.unit.network_services.vnf_generic.vnf.test_base import FileAbsPath
+from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
 from yardstick.network_services.vnf_generic.vnf.base import QueueFileWrapper
+from yardstick.network_services.vnf_generic.vnf.base import VnfdHelper
 
 
 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
@@ -36,11 +39,8 @@ stl_patch.start()
 if stl_patch:
     from yardstick.network_services.vnf_generic.vnf.vpe_vnf import ConfigCreate
     from yardstick.network_services.nfvi.resource import ResourceProfile
-    from yardstick.network_services.vnf_generic.vnf import vpe_vnf
-    from yardstick.network_services.vnf_generic.vnf.vpe_vnf import VpeApproxVnf
-
-from tests.unit.network_services.vnf_generic.vnf.test_base import FileAbsPath
-from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
+    from yardstick.network_services.vnf_generic.vnf.vpe_vnf import \
+        VpeApproxVnf, VpeApproxSetupEnvHelper
 
 
 TEST_FILE_YAML = 'nsb_test_case.yaml'
@@ -55,14 +55,122 @@ get_file_abspath = MODULE_PATH.get_path
 
 class TestConfigCreate(unittest.TestCase):
 
+    VNFD_0 = {
+        'short-name': 'VpeVnf',
+        'vdu': [
+            {
+                'routing_table': [
+                    {
+                        'network': '152.16.100.20',
+                        'netmask': '255.255.255.0',
+                        'gateway': '152.16.100.20',
+                        'if': 'xe0'
+                    },
+                    {
+                        'network': '152.16.40.20',
+                        'netmask': '255.255.255.0',
+                        'gateway': '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': [
+                    {
+                        'virtual-interface': {
+                            'dst_mac': '00:00:00:00:00:03',
+                            'vpci': '0000:05:00.0',
+                            'local_ip': '152.16.100.19',
+                            'type': 'PCI-PASSTHROUGH',
+                            'netmask': '255.255.255.0',
+                            'dpdk_port_num': 0,
+                            'bandwidth': '10 Gbps',
+                            'dst_ip': '152.16.100.20',
+                            'local_mac': '00:00:00:00:00:01',
+                            'vld_id': 'uplink_0',
+                            'ifname': 'xe0',
+                        },
+                        'vnfd-connection-point-ref': 'xe0',
+                        'name': 'xe0'
+                    },
+                    {
+                        'virtual-interface': {
+                            'dst_mac': '00:00:00:00:00:04',
+                            'vpci': '0000:05:00.1',
+                            'local_ip': '152.16.40.19',
+                            'type': 'PCI-PASSTHROUGH',
+                            'netmask': '255.255.255.0',
+                            'dpdk_port_num': 1,
+                            'bandwidth': '10 Gbps',
+                            'dst_ip': '152.16.40.20',
+                            'local_mac': '00:00:00:00:00:02',
+                            'vld_id': 'downlink_0',
+                            'ifname': 'xe1',
+                        },
+                        'vnfd-connection-point-ref': 'xe1',
+                        'name': 'xe1'
+                    },
+                ],
+            },
+        ],
+        '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'
+    }
+
     def test___init__(self):
-        config_create = ConfigCreate([0], [1], 2)
-        self.assertEqual(config_create.priv_ports, [0])
-        self.assertEqual(config_create.pub_ports, [1])
+        vnfd_helper = VnfdHelper(self.VNFD_0)
+        config_create = ConfigCreate(vnfd_helper, 2)
+        self.assertEqual(config_create.uplink_ports, ['xe0'])
+        self.assertEqual(config_create.downlink_ports, ['xe1'])
         self.assertEqual(config_create.socket, 2)
 
+    def test_dpdk_port_to_link_id(self):
+        vnfd_helper = VnfdHelper(self.VNFD_0)
+        config_create = ConfigCreate(vnfd_helper, 2)
+        self.assertEqual(config_create.dpdk_port_to_link_id_map, {'xe0': 0, 'xe1': 1})
+
     def test_vpe_initialize(self):
-        config_create = ConfigCreate([0], [1], 2)
+        vnfd_helper = VnfdHelper(self.VNFD_0)
+        config_create = ConfigCreate(vnfd_helper, 2)
         config = configparser.ConfigParser()
         config_create.vpe_initialize(config)
         self.assertEqual(config.get('EAL', 'log_level'), '0')
@@ -72,14 +180,16 @@ class TestConfigCreate(unittest.TestCase):
         self.assertEqual(config.get('MEMPOOL1', 'pool_size'), '2M')
 
     def test_vpe_rxq(self):
-        config_create = ConfigCreate([0], [1, 2], 3)
+        vnfd_helper = VnfdHelper(self.VNFD_0)
+        config_create = ConfigCreate(vnfd_helper, 2)
         config = configparser.ConfigParser()
+        config_create.downlink_ports = ['xe0']
         config_create.vpe_rxq(config)
-        self.assertEqual(config.get('RXQ1.0', 'mempool'), 'MEMPOOL1')
-        self.assertEqual(config.get('RXQ2.0', 'mempool'), 'MEMPOOL1')
+        self.assertEqual(config.get('RXQ0.0', 'mempool'), 'MEMPOOL1')
 
     def test_get_sink_swq(self):
-        config_create = ConfigCreate([0], [1], 2)
+        vnfd_helper = VnfdHelper(self.VNFD_0)
+        config_create = ConfigCreate(vnfd_helper, 2)
         config = configparser.ConfigParser()
         config.add_section('PIPELINE0')
         config.set('PIPELINE0', 'key1', 'value1')
@@ -96,43 +206,35 @@ class TestConfigCreate(unittest.TestCase):
         self.assertEqual(config_create.get_sink_swq(config, 'PIPELINE0', 'key5', 5), 'SWQ0 SINK1')
 
     def test_generate_vpe_script(self):
-        vpe_config_vnf = ConfigCreate([0], [0], 0)
+        vnfd_helper = VnfdHelper(self.VNFD_0)
+        vpe_config_vnf = ConfigCreate(vnfd_helper, 2)
         intf = [
             {
+                "name": 'xe1',
+                "virtual-interface": {
+                    "dst_ip": "1.1.1.1",
+                    "dst_mac": "00:00:00:00:00:00:02",
+                },
+            },
+            {
+                "name": 'xe2',
                 "virtual-interface": {
                     "dst_ip": "1.1.1.1",
                     "dst_mac": "00:00:00:00:00:00:02",
                 },
             },
         ]
+        vpe_config_vnf.downlink_ports = ['xe1']
+        vpe_config_vnf.uplink_ports = ['xe2']
         result = vpe_config_vnf.generate_vpe_script(intf)
         self.assertIsInstance(result, str)
         self.assertNotEqual(result, '')
 
     def test_create_vpe_config(self):
-        priv_ports = [
-            {
-                'index': 0,
-                'dpdk_port_num': 1,
-                'peer_intf': {
-                    'dpdk_port_num': 2,
-                    'index': 3,
-                },
-            },
-        ]
-
-        pub_ports = [
-            {
-                'index': 2,
-                'dpdk_port_num': 3,
-                'peer_intf': {
-                    'dpdk_port_num': 0,
-                    'index': 1,
-                },
-            },
-        ]
-
-        config_create = ConfigCreate(priv_ports, pub_ports, 23)
+        vnfd_helper = VnfdHelper(self.VNFD_0)
+        config_create = ConfigCreate(vnfd_helper, 23)
+        config_create.downlink_ports = ['xe1']
+        config_create.uplink_ports = ['xe1']
         curr_path = os.path.dirname(os.path.abspath(__file__))
         vpe_cfg = "samples/vnf_samples/nsut/vpe/vpe_config"
         vnf_cfg = os.path.join(curr_path, "../../../../..", vpe_cfg)
@@ -140,7 +242,6 @@ class TestConfigCreate(unittest.TestCase):
         os.system("git checkout -- %s" % vnf_cfg)
 
 
-@mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.time')
 class TestVpeApproxVnf(unittest.TestCase):
 
     VNFD_0 = {
@@ -185,14 +286,15 @@ class TestVpeApproxVnf(unittest.TestCase):
                             'vpci': '0000:05:00.0',
                             'local_ip': '152.16.100.19',
                             'type': 'PCI-PASSTHROUGH',
-                            'vld_id': '',
                             'netmask': '255.255.255.0',
-                            'dpdk_port_num': '0',
+                            'dpdk_port_num': 0,
                             'bandwidth': '10 Gbps',
                             'driver': "i40e",
                             'dst_ip': '152.16.100.20',
                             'local_iface_name': 'xe0',
                             'local_mac': '00:00:00:00:00:02',
+                            'vld_id': 'uplink_0',
+                            'ifname': 'xe0',
                         },
                         'vnfd-connection-point-ref': 'xe0',
                         'name': 'xe0',
@@ -203,14 +305,15 @@ class TestVpeApproxVnf(unittest.TestCase):
                             'vpci': '0000:05:00.1',
                             'local_ip': '152.16.40.19',
                             'type': 'PCI-PASSTHROUGH',
-                            'vld_id': '',
                             'driver': "i40e",
                             'netmask': '255.255.255.0',
-                            'dpdk_port_num': '1',
+                            'dpdk_port_num': 1,
                             'bandwidth': '10 Gbps',
                             'dst_ip': '152.16.40.20',
                             'local_iface_name': 'xe1',
                             'local_mac': '00:00:00:00:00:01',
+                            'vld_id': 'downlink_0',
+                            'ifname': 'xe1',
                         },
                         'vnfd-connection-point-ref': 'xe1',
                         'name': 'xe1',
@@ -258,7 +361,7 @@ class TestVpeApproxVnf(unittest.TestCase):
     SCENARIO_CFG = {
         'options': {
             'packetsize': 64,
-            'traffic_type': 4 ,
+            'traffic_type': 4,
             'rfc2544': {
                 'allowed_drop_rate': '0.8 - 1',
             },
@@ -308,7 +411,7 @@ class TestVpeApproxVnf(unittest.TestCase):
                 'interfaces': {
                     'xe0': {
                         'local_iface_name': 'ens513f0',
-                        'vld_id': 'public',
+                        'vld_id': VpeApproxVnf.DOWNLINK,
                         'netmask': '255.255.255.0',
                         'local_ip': '152.16.40.20',
                         'dst_mac': '00:00:00:00:00:01',
@@ -342,7 +445,7 @@ class TestVpeApproxVnf(unittest.TestCase):
                 'interfaces': {
                     'xe0': {
                         'local_iface_name': 'ens785f0',
-                        'vld_id': 'private',
+                        'vld_id': VpeApproxVnf.UPLINK,
                         'netmask': '255.255.255.0',
                         'local_ip': '152.16.100.20',
                         'dst_mac': '00:00:00:00:00:02',
@@ -373,7 +476,7 @@ class TestVpeApproxVnf(unittest.TestCase):
                 'interfaces': {
                     'xe0': {
                         'local_iface_name': 'ens786f0',
-                        'vld_id': 'private',
+                        'vld_id': VpeApproxVnf.UPLINK,
                         'netmask': '255.255.255.0',
                         'local_ip': '152.16.100.19',
                         'dst_mac': '00:00:00:00:00:04',
@@ -385,7 +488,7 @@ class TestVpeApproxVnf(unittest.TestCase):
                     },
                     'xe1': {
                         'local_iface_name': 'ens786f1',
-                        'vld_id': 'public',
+                        'vld_id': VpeApproxVnf.DOWNLINK,
                         'netmask': '255.255.255.0',
                         'local_ip': '152.16.40.19',
                         'dst_mac': '00:00:00:00:00:03',
@@ -434,17 +537,21 @@ class TestVpeApproxVnf(unittest.TestCase):
         },
     }
 
-    def test___init__(self, _):
+    def setUp(self):
+        self.mock_sleep = mock.patch.object(time, 'sleep').start()
+
+    def test___init__(self):
         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
         self.assertIsNone(vpe_approx_vnf._vnf_process)
 
     @mock.patch(SSH_HELPER)
-    def test_collect_kpi_sa_not_running(self, ssh, _):
+    def test_collect_kpi_sa_not_running(self, ssh):
         mock_ssh(ssh)
 
         resource = mock.Mock(autospec=ResourceProfile)
-        resource.check_if_sa_running.return_value = False, 'error'
+        resource.check_if_system_agent_running.return_value = 1, ''
         resource.amqp_collect_nfvi_kpi.return_value = {'foo': 234}
+        resource.check_if_system_agent_running.return_value = (1, None)
 
         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
         vpe_approx_vnf.q_in = mock.MagicMock()
@@ -462,11 +569,11 @@ class TestVpeApproxVnf(unittest.TestCase):
         self.assertEqual(vpe_approx_vnf.collect_kpi(), expected)
 
     @mock.patch(SSH_HELPER)
-    def test_collect_kpi_sa_running(self, ssh, _):
+    def test_collect_kpi_sa_running(self, ssh):
         mock_ssh(ssh)
 
         resource = mock.Mock(autospec=ResourceProfile)
-        resource.check_if_sa_running.return_value = True, 'good'
+        resource.check_if_system_agent_running.return_value = 0, '1234'
         resource.amqp_collect_nfvi_kpi.return_value = {'foo': 234}
 
         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
@@ -485,7 +592,7 @@ class TestVpeApproxVnf(unittest.TestCase):
         self.assertEqual(vpe_approx_vnf.collect_kpi(), expected)
 
     @mock.patch(SSH_HELPER)
-    def test_vnf_execute(self, ssh, _):
+    def test_vnf_execute(self, ssh):
         mock_ssh(ssh)
         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
         vpe_approx_vnf.q_in = mock.MagicMock()
@@ -494,14 +601,11 @@ class TestVpeApproxVnf(unittest.TestCase):
         self.assertEqual(vpe_approx_vnf.vnf_execute("quit", 0), '')
 
     @mock.patch(SSH_HELPER)
-    def test_run_vpe(self, ssh, _):
+    def test_run_vpe(self, ssh):
         mock_ssh(ssh)
 
         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
         vpe_approx_vnf.tc_file_name = get_file_abspath(TEST_FILE_YAML)
-        vpe_approx_vnf.generate_port_pairs = mock.Mock()
-        vpe_approx_vnf.tg_port_pairs = [[[0], [1]]]
-        vpe_approx_vnf.vnf_port_pairs = [[[0], [1]]]
         vpe_approx_vnf.vnf_cfg = {
             'lb_config': 'SW',
             'lb_count': 1,
@@ -513,18 +617,58 @@ class TestVpeApproxVnf(unittest.TestCase):
                 NAME: {
                     'traffic_type': '4',
                     'topology': 'nsb_test_case.yaml',
+                    'vnf_config': 'vpe_config',
                 }
             }
         }
         vpe_approx_vnf.topology = "nsb_test_case.yaml"
         vpe_approx_vnf.nfvi_type = "baremetal"
         vpe_approx_vnf._provide_config_file = mock.Mock()
+        vpe_approx_vnf._build_config = mock.MagicMock()
 
+        self.assertIsInstance(vpe_approx_vnf.ssh_helper, mock.Mock)
         self.assertIsInstance(vpe_approx_vnf.ssh_helper, mock.Mock)
         self.assertIsNone(vpe_approx_vnf._run())
 
+    @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.MultiPortConfig")
+    @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
+    @mock.patch("yardstick.network_services.vnf_generic.vnf.vpe_vnf.ConfigCreate")
+    @mock.patch("yardstick.network_services.vnf_generic.vnf.vpe_vnf.open")
+    @mock.patch(SSH_HELPER)
+    def test_build_config(self, ssh, *args):
+        mock_ssh(ssh)
+        vpe_approx_vnf = VpeApproxSetupEnvHelper(mock.MagicMock(),
+                                                 mock.MagicMock(), mock.MagicMock())
+        vpe_approx_vnf.tc_file_name = get_file_abspath(TEST_FILE_YAML)
+        vpe_approx_vnf.generate_port_pairs = mock.Mock()
+        vpe_approx_vnf.vnf_cfg = {
+            'lb_config': 'SW',
+            'lb_count': 1,
+            'worker_config': '1C/1T',
+            'worker_threads': 1,
+        }
+        vpe_approx_vnf.scenario_helper.scenario_cfg = {
+            'options': {
+                NAME: {
+                    'traffic_type': '4',
+                    'topology': 'nsb_test_case.yaml',
+                    'vnf_config': 'vpe_config',
+                }
+            }
+        }
+        vpe_approx_vnf.topology = "nsb_test_case.yaml"
+        vpe_approx_vnf.nfvi_type = "baremetal"
+        vpe_approx_vnf._provide_config_file = mock.Mock()
+
+        vpe_approx_vnf.ssh_helper = mock.MagicMock()
+        vpe_approx_vnf.scenario_helper = mock.MagicMock()
+        vpe_approx_vnf.ssh_helper.bin_path = mock.Mock()
+        vpe_approx_vnf.ssh_helper.upload_config_file = mock.MagicMock()
+        self.assertIsNone(vpe_approx_vnf._build_vnf_ports())
+        self.assertIsNotNone(vpe_approx_vnf.build_config())
+
     @mock.patch(SSH_HELPER)
-    def test_wait_for_instantiate(self, ssh, _):
+    def test_wait_for_instantiate(self, ssh):
         mock_ssh(ssh)
 
         mock_process = mock.Mock(autospec=Process)
@@ -547,7 +691,7 @@ class TestVpeApproxVnf(unittest.TestCase):
         self.assertEqual(vpe_approx_vnf.wait_for_instantiate(), 432)
 
     @mock.patch(SSH_HELPER)
-    def test_wait_for_instantiate_fragmented(self, ssh, _):
+    def test_wait_for_instantiate_fragmented(self, ssh):
         mock_ssh(ssh)
 
         mock_process = mock.Mock(autospec=Process)
@@ -570,7 +714,7 @@ class TestVpeApproxVnf(unittest.TestCase):
         self.assertEqual(vpe_approx_vnf.wait_for_instantiate(), 432)
 
     @mock.patch(SSH_HELPER)
-    def test_wait_for_instantiate_crash(self, ssh, _):
+    def test_wait_for_instantiate_crash(self, ssh):
         mock_ssh(ssh, exec_result=(1, "", ""))
 
         mock_process = mock.Mock(autospec=Process)
@@ -589,7 +733,7 @@ class TestVpeApproxVnf(unittest.TestCase):
         self.assertIn('VNF process died', str(raised.exception))
 
     @mock.patch(SSH_HELPER)
-    def test_wait_for_instantiate_panic(self, ssh, _):
+    def test_wait_for_instantiate_panic(self, ssh):
         mock_ssh(ssh, exec_result=(1, "", ""))
 
         mock_process = mock.Mock(autospec=Process)
@@ -609,7 +753,7 @@ class TestVpeApproxVnf(unittest.TestCase):
         self.assertIn('Error starting', str(raised.exception))
 
     @mock.patch(SSH_HELPER)
-    def test_wait_for_instantiate_panic_fragmented(self, ssh, _):
+    def test_wait_for_instantiate_panic_fragmented(self, ssh):
         mock_ssh(ssh, exec_result=(1, "", ""))
 
         mock_process = mock.Mock(autospec=Process)
@@ -633,13 +777,8 @@ class TestVpeApproxVnf(unittest.TestCase):
 
         self.assertIn('Error starting', str(raised.exception))
 
-    def test_scale(self, _):
-        vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
-        with self.assertRaises(NotImplementedError):
-            vpe_approx_vnf.scale('')
-
     @mock.patch(SSH_HELPER)
-    def test_terminate(self, ssh, _):
+    def test_terminate(self, ssh):
         mock_ssh(ssh)
 
         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
@@ -648,7 +787,3 @@ class TestVpeApproxVnf(unittest.TestCase):
         vpe_approx_vnf.resource_helper = mock.MagicMock()
 
         self.assertIsNone(vpe_approx_vnf.terminate())
-
-
-if __name__ == '__main__':
-    unittest.main()