Merge "Import "traffic_profile" modules only once"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_tg_ixload.py
index cda4412..b93f9aa 100644 (file)
@@ -1,5 +1,3 @@
-#!/usr/bin/env python
-
 # Copyright (c) 2016-2017 Intel Corporation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # limitations under the License.
 #
 
-from __future__ import absolute_import
-import unittest
-import mock
 import subprocess
 
-STL_MOCKS = {
-    'stl': mock.MagicMock(),
-    'stl.trex_stl_lib': mock.MagicMock(),
-    'stl.trex_stl_lib.base64': mock.MagicMock(),
-    'stl.trex_stl_lib.binascii': mock.MagicMock(),
-    'stl.trex_stl_lib.collections': mock.MagicMock(),
-    'stl.trex_stl_lib.copy': mock.MagicMock(),
-    'stl.trex_stl_lib.datetime': mock.MagicMock(),
-    'stl.trex_stl_lib.functools': mock.MagicMock(),
-    'stl.trex_stl_lib.imp': mock.MagicMock(),
-    'stl.trex_stl_lib.inspect': mock.MagicMock(),
-    'stl.trex_stl_lib.json': mock.MagicMock(),
-    'stl.trex_stl_lib.linecache': mock.MagicMock(),
-    'stl.trex_stl_lib.math': mock.MagicMock(),
-    'stl.trex_stl_lib.os': mock.MagicMock(),
-    'stl.trex_stl_lib.platform': mock.MagicMock(),
-    'stl.trex_stl_lib.pprint': mock.MagicMock(),
-    'stl.trex_stl_lib.random': mock.MagicMock(),
-    'stl.trex_stl_lib.re': mock.MagicMock(),
-    'stl.trex_stl_lib.scapy': mock.MagicMock(),
-    'stl.trex_stl_lib.socket': mock.MagicMock(),
-    'stl.trex_stl_lib.string': mock.MagicMock(),
-    'stl.trex_stl_lib.struct': mock.MagicMock(),
-    'stl.trex_stl_lib.sys': mock.MagicMock(),
-    'stl.trex_stl_lib.threading': mock.MagicMock(),
-    'stl.trex_stl_lib.time': mock.MagicMock(),
-    'stl.trex_stl_lib.traceback': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
-    'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
-    'stl.trex_stl_lib.types': mock.MagicMock(),
-    'stl.trex_stl_lib.utils': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.common': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.json': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.os': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.random': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.re': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.string': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
-    'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
-    'stl.trex_stl_lib.warnings': mock.MagicMock(),
-    'stl.trex_stl_lib.yaml': mock.MagicMock(),
-    'stl.trex_stl_lib.zlib': mock.MagicMock(),
-    'stl.trex_stl_lib.zmq': mock.MagicMock(),
-}
+import mock
+import unittest
+import six
+
+from tests.unit import STL_MOCKS
+from yardstick import ssh
+from yardstick.common import utils
+
 
 STLClient = mock.MagicMock()
 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
@@ -126,7 +70,7 @@ class TestIxLoadTrafficGen(unittest.TestCase):
                     'local_ip': '152.16.100.19',
                     'type': 'PCI-PASSTHROUGH',
                     'netmask': '255.255.255.0',
-                    'dpdk_port_num': '0',
+                    'dpdk_port_num': 0,
                     'bandwidth': '10 Gbps',
                     'driver': "i40e",
                     'dst_ip': '152.16.100.20',
@@ -141,7 +85,7 @@ class TestIxLoadTrafficGen(unittest.TestCase):
                     'type': 'PCI-PASSTHROUGH',
                     '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',
@@ -179,7 +123,7 @@ class TestIxLoadTrafficGen(unittest.TestCase):
             ssh.from_node.return_value = ssh_mock
             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
             ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
-            self.assertIsNone(ixload_traffic_gen.data)
+            self.assertIsNone(ixload_traffic_gen.resource_helper.data)
 
     def test_collect_kpi(self):
         with mock.patch("yardstick.ssh.SSH") as ssh:
@@ -203,10 +147,11 @@ class TestIxLoadTrafficGen(unittest.TestCase):
             ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
             self.assertEqual(None, ixload_traffic_gen.listen_traffic({}))
 
-    @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.makedirs")
+    @mock.patch.object(utils, 'find_relative_file')
+    @mock.patch.object(utils, 'makedirs')
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
-    def test_instantiate(self, call, shutil, mock_makedirs):
+    def test_instantiate(self, shutil, *args):
         with mock.patch("yardstick.ssh.SSH") as ssh:
             ssh_mock = mock.Mock(autospec=ssh.SSH)
             ssh_mock.execute = \
@@ -217,7 +162,8 @@ class TestIxLoadTrafficGen(unittest.TestCase):
             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
             ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
             scenario_cfg = {'tc': "nsb_test_case",
-                            'ixia_profile': "ixload.cfg"}
+                            'ixia_profile': "ixload.cfg",
+                            'task_path': "/path/to/task"}
             ixload_traffic_gen.RESULTS_MOUNT = "/tmp/result"
             shutil.copy = mock.Mock()
             scenario_cfg.update({'options': {'packetsize': 64, 'traffic_type': 4,
@@ -229,16 +175,18 @@ class TestIxLoadTrafficGen(unittest.TestCase):
                                                                        '1C/1T',
                                                                        'worker_threads': 1}}
                                              }})
-            self.assertRaises(IOError,
-                              ixload_traffic_gen.instantiate(scenario_cfg, {}))
+            with mock.patch.object(six.moves.builtins, 'open',
+                                   create=True) as mock_open:
+                mock_open.return_value = mock.MagicMock()
+                ixload_traffic_gen.instantiate(scenario_cfg, {})
 
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
-    @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.open")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.min")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.max")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.len")
-    def test_run_traffic(self, call, shutil, main_open, min, max, len):
+    @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
+    def test_run_traffic(self, shutil, *args):
         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
         mock_traffic_profile.get_traffic_definition.return_value = "64"
         mock_traffic_profile.params = self.TRAFFIC_PROFILE
@@ -264,12 +212,12 @@ class TestIxLoadTrafficGen(unittest.TestCase):
             self.assertIsNone(result)
 
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
-    @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.open")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.min")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.max")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.len")
-    def test_run_traffic_csv(self, call, shutil, main_open, min, max, len):
+    @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
+    def test_run_traffic_csv(self, shutil, *args):
         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
         mock_traffic_profile.get_traffic_definition.return_value = "64"
         mock_traffic_profile.params = self.TRAFFIC_PROFILE
@@ -297,19 +245,15 @@ class TestIxLoadTrafficGen(unittest.TestCase):
             self.assertIsNone(result)
 
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
-    def test_terminate(self, call):
-        with mock.patch("yardstick.ssh.SSH") as ssh:
-            vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
-            ssh_mock = mock.Mock(autospec=ssh.SSH)
-            ssh_mock.execute = \
-                mock.Mock(return_value=(0, "", ""))
-            ssh.from_node.return_value = ssh_mock
-            ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
-            self.assertEqual(None, ixload_traffic_gen.terminate())
+    @mock.patch.object(ssh, 'SSH')
+    def test_terminate(self, *args):
+        vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
+        ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
+        self.assertEqual(None, ixload_traffic_gen.terminate())
 
-    @mock.patch("yardstick.ssh.SSH")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
-    def test_parse_csv_read(self, mock_call, mock_ssh):
+    @mock.patch.object(ssh, 'SSH')
+    def test_parse_csv_read(self, mock_ssh, *args):
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
         kpi_data = {
             'HTTP Total Throughput (Kbps)': 1,
@@ -331,9 +275,9 @@ class TestIxLoadTrafficGen(unittest.TestCase):
         for key_left, key_right in IxLoadResourceHelper.KPI_LIST.items():
             self.assertEqual(result[key_left][-1], int(kpi_data[key_right]))
 
-    @mock.patch("yardstick.ssh.SSH")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
-    def test_parse_csv_read_value_error(self, mock_call, mock_ssh):
+    @mock.patch.object(ssh, 'SSH')
+    def test_parse_csv_read_value_error(self, mock_ssh, *args):
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
         http_reader = [{
             'HTTP Total Throughput (Kbps)': 1,
@@ -353,9 +297,9 @@ class TestIxLoadTrafficGen(unittest.TestCase):
         ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
         self.assertDictEqual(ixload_traffic_gen.resource_helper.result, init_value)
 
-    @mock.patch("yardstick.ssh.SSH")
     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
-    def test_parse_csv_read_error(self, mock_call, mock_ssh):
+    @mock.patch.object(ssh, 'SSH')
+    def test_parse_csv_read_error(self, mock_ssh, *args):
         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
         http_reader = [{
             'HTTP Total Throughput (Kbps)': 1,