Merge "Fixed document for Grafana Port usage"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_vpe_vnf.py
index 757109d..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'
@@ -39,9 +42,6 @@ if stl_patch:
     from yardstick.network_services.vnf_generic.vnf.vpe_vnf import \
         VpeApproxVnf, VpeApproxSetupEnvHelper
 
-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
-
 
 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.uplink_ports, [0])
-        self.assertEqual(config_create.downlink_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):
-        uplink_ports = [
-            {
-                'index': 0,
-                'dpdk_port_num': 1,
-                'peer_intf': {
-                    'dpdk_port_num': 2,
-                    'index': 3,
-                },
-            },
-        ]
-
-        downlink_ports = [
-            {
-                'index': 2,
-                'dpdk_port_num': 3,
-                'peer_intf': {
-                    'dpdk_port_num': 0,
-                    'index': 1,
-                },
-            },
-        ]
-
-        config_create = ConfigCreate(uplink_ports, downlink_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 = {
@@ -436,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()
@@ -464,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)
@@ -487,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()
@@ -496,7 +601,7 @@ 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)
@@ -530,10 +635,10 @@ class TestVpeApproxVnf(unittest.TestCase):
     @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, mock_mul, mock_context, mock_config, mock_open, ssh, _):
+    def test_build_config(self, ssh, *args):
         mock_ssh(ssh)
         vpe_approx_vnf = VpeApproxSetupEnvHelper(mock.MagicMock(),
-                                                 mock.MagicMock, 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 = {
@@ -563,7 +668,7 @@ class TestVpeApproxVnf(unittest.TestCase):
         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)
@@ -586,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)
@@ -609,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)
@@ -628,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)
@@ -648,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)
@@ -672,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)
@@ -687,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()