fix random test_task unittest fail due to os.environ mock 93/44493/1
authorRoss Brattain <ross.b.brattain@intel.com>
Tue, 19 Sep 2017 23:38:44 +0000 (16:38 -0700)
committerRoss Brattain <ross.b.brattain@intel.com>
Fri, 6 Oct 2017 20:20:55 +0000 (20:20 +0000)
sometime Jenkins fails due to what I guess are concurrency problems
in os.environ mock

======================================================================
FAIL: tests.unit.benchmark.core.test_task.TaskTestCase.test_parse_suite_with_constraint_no_args
tags: worker-10
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.5/unittest/mock.py", line 1157, in patched
    return func(*args, **keywargs)
  File "/home/jenkins/opnfv/slave_root/workspace/yardstick-verify-euphrates/tests/unit/benchmark/core/test_task.py", line 208, in test_parse_suite_with_constraint_no_args
    task_files, task_args, task_args_fnames = t.parse_suite()
  File "/home/jenkins/opnfv/slave_root/workspace/yardstick-verify-euphrates/yardstick/benchmark/core/task.py", line 455, in parse_suite
    cur_pod = os.environ.get('NODE_NAME', None)
  File "/usr/lib/python3.5/unittest/mock.py", line 917, in __call__
    return _mock_self._mock_call(*args, **kwargs)
  File "/usr/lib/python3.5/unittest/mock.py", line 976, in _mock_call
    result = next(effect)
StopIteration
Ran 1262 tests in 2.375s
FAILED (id=0, failures=1)
error: testr failed (1)
+ testr failing

Replace the mock decorator with a context manager to try to
reduce the scope and duration of the mock.

Change-Id: I342fe6c403e66c53ac4c39fd88fa9047cdfae5d9
Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
(cherry picked from commit 2fadf7aec9e2761c39d29d8af1ee7d69d154652d)

tests/unit/benchmark/core/test_task.py

index 737e705..bed0bb6 100644 (file)
@@ -165,12 +165,12 @@ class TaskTestCase(unittest.TestCase):
         result = t._check_precondition(cfg)
         self.assertTrue(result)
 
-    @mock.patch('yardstick.benchmark.core.task.os.environ')
-    def test_parse_suite_no_constraint_no_args(self, mock_environ):
+    def test_parse_suite_no_constraint_no_args(self):
         SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
-        mock_environ.get.side_effect = ['huawei-pod1', 'compass']
-        task_files, task_args, task_args_fnames = t.parse_suite()
+        with mock.patch('yardstick.benchmark.core.task.os.environ',
+                        new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
+            task_files, task_args, task_args_fnames = t.parse_suite()
         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
                                                 task_args_fnames))
         self.assertEqual(task_files[0], self.change_to_abspath(
@@ -186,8 +186,9 @@ class TaskTestCase(unittest.TestCase):
     def test_parse_suite_no_constraint_with_args(self, mock_environ):
         SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
-        mock_environ.get.side_effect = ['huawei-pod1', 'compass']
-        task_files, task_args, task_args_fnames = t.parse_suite()
+        with mock.patch('yardstick.benchmark.core.task.os.environ',
+                        new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
+            task_files, task_args, task_args_fnames = t.parse_suite()
         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
                                                 task_args_fnames))
         self.assertEqual(task_files[0], self.change_to_abspath(
@@ -204,8 +205,9 @@ class TaskTestCase(unittest.TestCase):
     def test_parse_suite_with_constraint_no_args(self, mock_environ):
         SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
-        mock_environ.get.side_effect = ['huawei-pod1', 'compass']
-        task_files, task_args, task_args_fnames = t.parse_suite()
+        with mock.patch('yardstick.benchmark.core.task.os.environ',
+                        new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
+            task_files, task_args, task_args_fnames = t.parse_suite()
         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
                                                 task_args_fnames))
         self.assertEqual(task_files[0], self.change_to_abspath(
@@ -221,8 +223,9 @@ class TaskTestCase(unittest.TestCase):
     def test_parse_suite_with_constraint_with_args(self, mock_environ):
         SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
-        mock_environ.get.side_effect = ['huawei-pod1', 'compass']
-        task_files, task_args, task_args_fnames = t.parse_suite()
+        with mock.patch('yardstick.benchmark.core.task.os.environ',
+                        new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
+            task_files, task_args, task_args_fnames = t.parse_suite()
         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
                                                 task_args_fnames))
         self.assertEqual(task_files[0], self.change_to_abspath(