Add RFC2544 iteration status field
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_sample_vnf.py
index 7c22563..c9d42fb 100644 (file)
@@ -18,7 +18,6 @@ import unittest
 import mock
 import six
 
-from yardstick.benchmark.contexts.base import Context
 from yardstick.common import exceptions as y_exceptions
 from yardstick.common import utils
 from yardstick.network_services.nfvi.resource import ResourceProfile
@@ -35,6 +34,7 @@ from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF
 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFTrafficGen
 from yardstick.network_services.vnf_generic.vnf.sample_vnf import DpdkVnfSetupEnvHelper
 from yardstick.tests.unit.network_services.vnf_generic.vnf import test_base
+from yardstick.benchmark.contexts import base as ctx_base
 
 
 class MockError(Exception):
@@ -563,6 +563,7 @@ class TestDpdkVnfSetupEnvHelper(unittest.TestCase):
         ssh_helper = mock.Mock()
         scenario_helper = mock.Mock()
         scenario_helper.vnf_cfg = {}
+        scenario_helper.options = {}
         scenario_helper.all_options = {}
         dpdk_setup_helper = DpdkVnfSetupEnvHelper(vnfd_helper, ssh_helper, scenario_helper)
 
@@ -574,6 +575,7 @@ class TestDpdkVnfSetupEnvHelper(unittest.TestCase):
         mock_multi_port_config.generate_config.assert_called()
         mock_multi_port_config.generate_script.assert_called()
 
+        scenario_helper.options = {'rules': 'fake_file'}
         scenario_helper.vnf_cfg = {'file': 'fake_file'}
         dpdk_setup_helper = DpdkVnfSetupEnvHelper(vnfd_helper, ssh_helper, scenario_helper)
         mock_open_rf.side_effect = mock.mock_open(read_data='fake_data')
@@ -581,7 +583,7 @@ class TestDpdkVnfSetupEnvHelper(unittest.TestCase):
 
         result = dpdk_setup_helper.build_config()
 
-        mock_open_rf.assert_called_once()
+        mock_open_rf.assert_called()
         self.assertEqual(result, expected)
         self.assertGreaterEqual(ssh_helper.upload_config_file.call_count, 2)
         mock_find.assert_called()
@@ -1088,6 +1090,58 @@ class TestClientResourceHelper(unittest.TestCase):
 
         self.assertIs(client_resource_helper._connect(client), client)
 
+    @mock.patch.object(ClientResourceHelper, '_build_ports')
+    @mock.patch.object(ClientResourceHelper, '_run_traffic_once',
+                       return_value=(True, mock.ANY))
+    def test_run_traffic(self, mock_run_traffic_once, mock_build_ports):
+        client_resource_helper = ClientResourceHelper(mock.Mock())
+        client = mock.Mock()
+        traffic_profile = mock.Mock()
+        mq_producer = mock.Mock()
+        with mock.patch.object(client_resource_helper, '_connect') \
+                as mock_connect, \
+                mock.patch.object(client_resource_helper, '_terminated') \
+                as mock_terminated:
+            mock_connect.return_value = client
+            type(mock_terminated).value = mock.PropertyMock(
+                side_effect=[0, 1, 1, lambda x: x])
+            client_resource_helper.run_traffic(traffic_profile, mq_producer)
+
+        mock_build_ports.assert_called_once()
+        traffic_profile.register_generator.assert_called_once()
+        mq_producer.tg_method_started.assert_called_once()
+        mq_producer.tg_method_finished.assert_called_once()
+        mq_producer.tg_method_iteration.assert_called_once_with(1)
+        mock_run_traffic_once.assert_called_once_with(traffic_profile)
+
+    @mock.patch.object(ClientResourceHelper, '_build_ports')
+    @mock.patch.object(ClientResourceHelper, '_run_traffic_once',
+                       side_effect=Exception)
+    def test_run_traffic_exception(self, mock_run_traffic_once,
+                                   mock_build_ports):
+        client_resource_helper = ClientResourceHelper(mock.Mock())
+        client = mock.Mock()
+        traffic_profile = mock.Mock()
+        mq_producer = mock.Mock()
+        with mock.patch.object(client_resource_helper, '_connect') \
+                as mock_connect, \
+                mock.patch.object(client_resource_helper, '_terminated') \
+                as mock_terminated:
+            mock_connect.return_value = client
+            type(mock_terminated).value = mock.PropertyMock(return_value=0)
+            mq_producer.reset_mock()
+            # NOTE(ralonsoh): "trex_stl_exceptions.STLError" is mocked
+            with self.assertRaises(Exception):
+                client_resource_helper.run_traffic(traffic_profile,
+                                                   mq_producer)
+
+        mock_build_ports.assert_called_once()
+        traffic_profile.register_generator.assert_called_once()
+        mock_run_traffic_once.assert_called_once_with(traffic_profile)
+        mq_producer.tg_method_started.assert_called_once()
+        mq_producer.tg_method_finished.assert_not_called()
+        mq_producer.tg_method_iteration.assert_not_called()
+
 
 class TestRfc2544ResourceHelper(unittest.TestCase):
 
@@ -1152,6 +1206,7 @@ class TestRfc2544ResourceHelper(unittest.TestCase):
         self.assertIsNone(rfc2544_resource_helper._tolerance_high)
         self.assertEqual(rfc2544_resource_helper.tolerance_high, 0.15)
         self.assertEqual(rfc2544_resource_helper._tolerance_high, 0.15)
+        self.assertEqual(rfc2544_resource_helper._tolerance_precision, 2)
         scenario_helper.scenario_cfg = {}  # ensure that resource_helper caches
         self.assertEqual(rfc2544_resource_helper.tolerance_high, 0.15)
 
@@ -1186,6 +1241,7 @@ class TestRfc2544ResourceHelper(unittest.TestCase):
         rfc2544_resource_helper = Rfc2544ResourceHelper(scenario_helper)
 
         self.assertEqual(rfc2544_resource_helper.tolerance_high, 0.2)
+        self.assertEqual(rfc2544_resource_helper._tolerance_precision, 1)
 
     def test_property_tolerance_low_not_range(self):
         scenario_helper = ScenarioHelper('name1')
@@ -1490,7 +1546,7 @@ class TestSampleVnf(unittest.TestCase):
     }
 
     def test___init__(self):
-        sample_vnf = SampleVNF('vnf1', self.VNFD_0)
+        sample_vnf = SampleVNF('vnf1', self.VNFD_0, 'task_id')
 
         self.assertEqual(sample_vnf.name, 'vnf1')
         self.assertDictEqual(sample_vnf.vnfd_helper, self.VNFD_0)
@@ -1508,7 +1564,8 @@ class TestSampleVnf(unittest.TestCase):
         class MyResourceHelper(ResourceHelper):
             pass
 
-        sample_vnf = SampleVNF('vnf1', self.VNFD_0, MySetupEnvHelper, MyResourceHelper)
+        sample_vnf = SampleVNF('vnf1', self.VNFD_0, 'task_id',
+                               MySetupEnvHelper, MyResourceHelper)
 
         self.assertEqual(sample_vnf.name, 'vnf1')
         self.assertDictEqual(sample_vnf.vnfd_helper, self.VNFD_0)
@@ -1522,7 +1579,7 @@ class TestSampleVnf(unittest.TestCase):
     @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.Process')
     def test__start_vnf(self, *args):
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
         sample_vnf._run = mock.Mock()
 
         self.assertIsNone(sample_vnf.queue_wrapper)
@@ -1531,33 +1588,20 @@ class TestSampleVnf(unittest.TestCase):
         self.assertIsNotNone(sample_vnf.queue_wrapper)
         self.assertIsNotNone(sample_vnf._vnf_process)
 
+    @mock.patch.object(ctx_base.Context, 'get_context_from_server', return_value='fake_context')
     @mock.patch("yardstick.ssh.SSH")
-    def test_instantiate(self, ssh):
+    def test_instantiate(self, ssh, *args):
         test_base.mock_ssh(ssh)
-
         nodes = {
             'vnf1': 'name1',
             'vnf2': 'name2',
         }
 
-        context1 = mock.Mock()
-        context1._get_server.return_value = None
-        context2 = mock.Mock()
-        context2._get_server.return_value = context2
-
-        try:
-            Context.list.clear()
-        except AttributeError:
-            # clear() but works in Py2.7
-            Context.list[:] = []
-
-        Context.list.extend([
-            context1,
-            context2,
-        ])
-
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
+        sample_vnf.scenario_helper.scenario_cfg = {
+            'nodes': {sample_vnf.name: 'mock'}
+        }
         sample_vnf.APP_NAME = 'sample1'
         sample_vnf._start_server = mock.Mock(return_value=0)
         sample_vnf._vnf_process = mock.MagicMock()
@@ -1570,7 +1614,6 @@ class TestSampleVnf(unittest.TestCase):
         }
 
         self.assertIsNone(sample_vnf.instantiate(scenario_cfg, {}))
-        self.assertEqual(sample_vnf.nfvi_context, context2)
 
     def test__update_collectd_options(self):
         scenario_cfg = {'options':
@@ -1599,7 +1642,7 @@ class TestSampleVnf(unittest.TestCase):
                          'plugin1': {'param': 1}}}
 
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf__0', vnfd)
+        sample_vnf = SampleVNF('vnf__0', vnfd, 'task_id')
         sample_vnf._update_collectd_options(scenario_cfg, context_cfg)
         self.assertEqual(sample_vnf.setup_helper.collectd_options, expected)
 
@@ -1626,7 +1669,7 @@ class TestSampleVnf(unittest.TestCase):
                          'plugin1': {'param': 1}}}
 
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
         sample_vnf._update_options(options2, options1)
         self.assertEqual(options2, expected)
 
@@ -1648,7 +1691,7 @@ class TestSampleVnf(unittest.TestCase):
         ]
 
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
         sample_vnf.APP_NAME = 'sample1'
         sample_vnf.WAIT_TIME_FOR_SCRIPT = 0
         sample_vnf._start_server = mock.Mock(return_value=0)
@@ -1679,7 +1722,7 @@ class TestSampleVnf(unittest.TestCase):
         ]
 
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
         sample_vnf.APP_NAME = 'sample1'
         sample_vnf.q_out = mock.Mock()
         sample_vnf.q_out.qsize.side_effect = iter(queue_size_list)
@@ -1689,7 +1732,7 @@ class TestSampleVnf(unittest.TestCase):
 
     def test_terminate_without_vnf_process(self):
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
         sample_vnf.APP_NAME = 'sample1'
         sample_vnf.vnf_execute = mock.Mock()
         sample_vnf.ssh_helper = mock.Mock()
@@ -1700,16 +1743,20 @@ class TestSampleVnf(unittest.TestCase):
 
     def test_get_stats(self):
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
         sample_vnf.APP_NAME = 'sample1'
         sample_vnf.APP_WORD = 'sample1'
         sample_vnf.vnf_execute = mock.Mock(return_value='the stats')
 
         self.assertEqual(sample_vnf.get_stats(), 'the stats')
 
-    def test_collect_kpi(self):
+    @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
+    def test_collect_kpi(self, *args):
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
+        sample_vnf.scenario_helper.scenario_cfg = {
+            'nodes': {sample_vnf.name: "mock"}
+        }
         sample_vnf.APP_NAME = 'sample1'
         sample_vnf.COLLECT_KPI = r'\s(\d+)\D*(\d+)\D*(\d+)'
         sample_vnf.COLLECT_MAP = {
@@ -1726,18 +1773,24 @@ class TestSampleVnf(unittest.TestCase):
             'k2': 34,
             'k3': 91,
             'collect_stats': {},
+            'physical_node': 'mock_node'
         }
         result = sample_vnf.collect_kpi()
         self.assertDictEqual(result, expected)
 
-    def test_collect_kpi_default(self):
+    @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
+    def test_collect_kpi_default(self, *args):
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
+        sample_vnf.scenario_helper.scenario_cfg = {
+            'nodes': {sample_vnf.name: "mock"}
+        }
         sample_vnf.APP_NAME = 'sample1'
         sample_vnf.COLLECT_KPI = r'\s(\d+)\D*(\d+)\D*(\d+)'
         sample_vnf.get_stats = mock.Mock(return_value='')
 
         expected = {
+            'physical_node': 'mock_node',
             'packets_in': 0,
             'packets_fwd': 0,
             'packets_dropped': 0,
@@ -1747,7 +1800,7 @@ class TestSampleVnf(unittest.TestCase):
 
     def test_scale(self):
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
         self.assertRaises(y_exceptions.FunctionNotImplemented,
                           sample_vnf.scale)
 
@@ -1755,7 +1808,7 @@ class TestSampleVnf(unittest.TestCase):
         test_cmd = 'test cmd'
         run_kwargs = {'arg1': 'val1', 'arg2': 'val2'}
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf = SampleVNF('vnf1', vnfd, 'task_id')
         sample_vnf.ssh_helper = mock.Mock()
         sample_vnf.setup_helper = mock.Mock()
         with mock.patch.object(sample_vnf, '_build_config',
@@ -1891,30 +1944,30 @@ class TestSampleVNFTrafficGen(unittest.TestCase):
     }
 
     def test__check_status(self):
-        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0)
+        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0, 'task_id')
 
         with self.assertRaises(NotImplementedError):
             sample_vnf_tg._check_status()
 
     def test_listen_traffic(self):
-        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0)
+        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0, 'task_id')
 
         sample_vnf_tg.listen_traffic(mock.Mock())
 
     def test_verify_traffic(self):
-        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0)
+        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0, 'task_id')
 
         sample_vnf_tg.verify_traffic(mock.Mock())
 
     def test_terminate(self):
-        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0)
+        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0, 'task_id')
         sample_vnf_tg._traffic_process = mock.Mock()
         sample_vnf_tg._tg_process = mock.Mock()
 
         sample_vnf_tg.terminate()
 
     def test__wait_for_process(self):
-        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0)
+        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0, 'task_id')
         with mock.patch.object(sample_vnf_tg, '_check_status',
                                return_value=0) as mock_status, \
                 mock.patch.object(sample_vnf_tg, '_tg_process') as mock_proc:
@@ -1925,14 +1978,14 @@ class TestSampleVNFTrafficGen(unittest.TestCase):
             mock_status.assert_called_once()
 
     def test__wait_for_process_not_alive(self):
-        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0)
+        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0, 'task_id')
         with mock.patch.object(sample_vnf_tg, '_tg_process') as mock_proc:
             mock_proc.is_alive.return_value = False
             self.assertRaises(RuntimeError, sample_vnf_tg._wait_for_process)
             mock_proc.is_alive.assert_called_once()
 
     def test__wait_for_process_delayed(self):
-        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0)
+        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0, 'task_id')
         with mock.patch.object(sample_vnf_tg, '_check_status',
                                side_effect=[1, 0]) as mock_status, \
                 mock.patch.object(sample_vnf_tg,
@@ -1944,6 +1997,6 @@ class TestSampleVNFTrafficGen(unittest.TestCase):
             mock_status.assert_has_calls([mock.call(), mock.call()])
 
     def test_scale(self):
-        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0)
+        sample_vnf_tg = SampleVNFTrafficGen('tg1', self.VNFD_0, 'task_id')
         self.assertRaises(y_exceptions.FunctionNotImplemented,
                           sample_vnf_tg.scale)