From b7513baa593bc9715abe652ef337c923619ead21 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Tue, 13 Feb 2018 17:17:22 +0000 Subject: [PATCH] Mock TRex STL libraries globally TRex Python libraries are not going to be available during unit testing execution. Those modules calling TRex libraries should be mocked individually. This patch will remove the need of mocking those libraries per module. JIRA: YARDSTICK-1010 Change-Id: I4aa11d43ecf32a3dad78f869541b4afea4ec1d28 Signed-off-by: Rodolfo Alonso Hernandez --- yardstick/tests/unit/__init__.py | 6 +++- .../tests/unit/benchmark/runner/test_search.py | 11 ++---- .../scenarios/networking/test_vnf_generic.py | 41 ++++++++-------------- 3 files changed, 21 insertions(+), 37 deletions(-) diff --git a/yardstick/tests/unit/__init__.py b/yardstick/tests/unit/__init__.py index a468b272b..59318aba7 100644 --- a/yardstick/tests/unit/__init__.py +++ b/yardstick/tests/unit/__init__.py @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import absolute_import +import sys + import mock @@ -74,3 +75,6 @@ STL_MOCKS = { 'trex_stl_lib.zlib': mock.MagicMock(), 'trex_stl_lib.zmq': mock.MagicMock(), } + +mock_stl = mock.patch.dict(sys.modules, STL_MOCKS) +mock_stl.start() diff --git a/yardstick/tests/unit/benchmark/runner/test_search.py b/yardstick/tests/unit/benchmark/runner/test_search.py index 1bc07448d..4e5b4fe77 100644 --- a/yardstick/tests/unit/benchmark/runner/test_search.py +++ b/yardstick/tests/unit/benchmark/runner/test_search.py @@ -17,15 +17,8 @@ import time import mock import unittest -from yardstick.tests.unit import STL_MOCKS - -STLClient = mock.MagicMock() -stl_patch = mock.patch.dict("sys.modules", STL_MOCKS) -stl_patch.start() - -if stl_patch: - from yardstick.benchmark.runners.search import SearchRunner - from yardstick.benchmark.runners.search import SearchRunnerHelper +from yardstick.benchmark.runners.search import SearchRunner +from yardstick.benchmark.runners.search import SearchRunnerHelper class TestSearchRunnerHelper(unittest.TestCase): diff --git a/yardstick/tests/unit/benchmark/scenarios/networking/test_vnf_generic.py b/yardstick/tests/unit/benchmark/scenarios/networking/test_vnf_generic.py index fb55b5ea0..64436eb4e 100644 --- a/yardstick/tests/unit/benchmark/scenarios/networking/test_vnf_generic.py +++ b/yardstick/tests/unit/benchmark/scenarios/networking/test_vnf_generic.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # Copyright (c) 2016-2017 Intel Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,10 +13,6 @@ # limitations under the License. # -# Unittest for yardstick.benchmark.scenarios.networking.test_vnf_generic - -from __future__ import absolute_import - import os import errno import unittest @@ -26,7 +20,6 @@ import mock from copy import deepcopy -from yardstick.tests.unit import STL_MOCKS from yardstick.benchmark.scenarios.networking.vnf_generic import \ SshManager, NetworkServiceTestCase, IncorrectConfig, \ open_relative_file @@ -35,10 +28,6 @@ from yardstick.network_services.vnf_generic.vnf.base import \ GenericTrafficGen, GenericVNF -# pylint: disable=unused-argument -# disable this for now because I keep forgetting mock patch arg ordering - - COMPLETE_TREX_VNFD = { 'vnfd:vnfd-catalog': { 'vnfd': [ @@ -425,16 +414,15 @@ class TestNetworkServiceTestCase(unittest.TestCase): def test_get_vnf_imp(self): vnfd = COMPLETE_TREX_VNFD['vnfd:vnfd-catalog']['vnfd'][0]['class-name'] - with mock.patch.dict("sys.modules", STL_MOCKS): - self.assertIsNotNone(self.s.get_vnf_impl(vnfd)) + self.assertIsNotNone(self.s.get_vnf_impl(vnfd)) - with self.assertRaises(IncorrectConfig) as raised: - self.s.get_vnf_impl('NonExistentClass') + with self.assertRaises(IncorrectConfig) as raised: + self.s.get_vnf_impl('NonExistentClass') - exc_str = str(raised.exception) - print(exc_str) - self.assertIn('No implementation', exc_str) - self.assertIn('found in', exc_str) + exc_str = str(raised.exception) + print(exc_str) + self.assertIn('No implementation', exc_str) + self.assertIn('found in', exc_str) def test_load_vnf_models_invalid(self): self.context_cfg["nodes"]['tg__1']['VNF model'] = \ @@ -626,14 +614,13 @@ class TestNetworkServiceTestCase(unittest.TestCase): self.s._get_traffic_imix()) def test__fill_traffic_profile(self): - with mock.patch.dict("sys.modules", STL_MOCKS): - self.scenario_cfg["traffic_profile"] = \ - self._get_file_abspath("ipv4_throughput_vpe.yaml") - self.scenario_cfg["traffic_options"]["flow"] = \ - self._get_file_abspath("ipv4_1flow_Packets_vpe.yaml") - self.scenario_cfg["traffic_options"]["imix"] = \ - self._get_file_abspath("imix_voice.yaml") - self.assertIsNotNone(self.s._fill_traffic_profile()) + self.scenario_cfg["traffic_profile"] = \ + self._get_file_abspath("ipv4_throughput_vpe.yaml") + self.scenario_cfg["traffic_options"]["flow"] = \ + self._get_file_abspath("ipv4_1flow_Packets_vpe.yaml") + self.scenario_cfg["traffic_options"]["imix"] = \ + self._get_file_abspath("imix_voice.yaml") + self.assertIsNotNone(self.s._fill_traffic_profile()) def test_teardown(self): vnf = mock.Mock(autospec=GenericVNF) -- 2.16.6