Merge "Read env vars instead of using CONST in API"
[functest.git] / functest / tests / unit / core / test_feature.py
index 993da5a..3219c72 100644 (file)
@@ -17,18 +17,14 @@ import mock
 from functest.core import feature
 from functest.core import testcase
 
-# logging must be disabled else it calls time.time()
-# what will break these unit tests.
-logging.disable(logging.CRITICAL)
-
 
 class FeatureTestingBase(unittest.TestCase):
 
     _case_name = "foo"
     _project_name = "bar"
-    _repo = "dir_repo_copper"
-    _cmd = "cd /home/opnfv/repos/foo/tests && bash run.sh && cd -"
-    _output_file = '/home/opnfv/functest/results/bar.log'
+    _repo = "dir_repo_bar"
+    _cmd = "run_bar_tests.py"
+    _output_file = '/home/opnfv/functest/results/foo.log'
     feature = None
 
     @mock.patch('time.time', side_effect=[1, 2])
@@ -42,12 +38,29 @@ class FeatureTestingBase(unittest.TestCase):
         self.assertEqual(self.feature.start_time, 1)
         self.assertEqual(self.feature.stop_time, 2)
 
+    def test_logger_module_ko(self):
+        with mock.patch('six.moves.builtins.open'):
+            self.feature = feature.Feature(
+                project_name=self._project_name, case_name=self._case_name)
+            self.assertEqual(self.feature.logger.name, self._case_name)
+
+    def test_logger_module(self):
+        with mock.patch('six.moves.builtins.open'):
+            self.feature = feature.Feature(
+                project_name=self._project_name, case_name=self._case_name,
+                run={'module': 'bar'})
+            self.assertEqual(self.feature.logger.name, 'bar')
+
 
 class FeatureTesting(FeatureTestingBase):
 
     def setUp(self):
-        self.feature = feature.Feature(
-            project_name=self._project_name, case_name=self._case_name)
+        # logging must be disabled else it calls time.time()
+        # what will break these unit tests.
+        logging.disable(logging.CRITICAL)
+        with mock.patch('six.moves.builtins.open'):
+            self.feature = feature.Feature(
+                project_name=self._project_name, case_name=self._case_name)
 
     def test_run_exc(self):
         # pylint: disable=bad-continuation
@@ -64,34 +77,40 @@ class FeatureTesting(FeatureTestingBase):
 class BashFeatureTesting(FeatureTestingBase):
 
     def setUp(self):
-        self.feature = feature.BashFeature(
-            project_name=self._project_name, case_name=self._case_name)
-
-    @mock.patch("functest.utils.functest_utils.execute_command")
-    def test_run_no_cmd(self, mock_method=None):
-        self.assertEqual(self.feature.run(), testcase.TestCase.EX_RUN_ERROR)
-        mock_method.assert_not_called()
-
-    @mock.patch("functest.utils.functest_utils.execute_command",
-                return_value=1)
-    def test_run_ko(self, mock_method=None):
-        self._test_run(testcase.TestCase.EX_RUN_ERROR)
-        mock_method.assert_called_once_with(
-            self._cmd, output_file=self._output_file)
-
-    @mock.patch("functest.utils.functest_utils.execute_command",
-                side_effect=Exception)
-    def test_run_exc(self, mock_method=None):
-        self._test_run(testcase.TestCase.EX_RUN_ERROR)
-        mock_method.assert_called_once_with(
-            self._cmd, output_file=self._output_file)
-
-    @mock.patch("functest.utils.functest_utils.execute_command",
-                return_value=0)
-    def test_run(self, mock_method):
-        self._test_run(testcase.TestCase.EX_OK)
-        mock_method.assert_called_once_with(
-            self._cmd, output_file=self._output_file)
+        # logging must be disabled else it calls time.time()
+        # what will break these unit tests.
+        logging.disable(logging.CRITICAL)
+        with mock.patch('six.moves.builtins.open'):
+            self.feature = feature.BashFeature(
+                project_name=self._project_name, case_name=self._case_name)
+
+    @mock.patch('subprocess.Popen')
+    def test_run_no_cmd(self, mock_subproc):
+        self.assertEqual(
+            self.feature.run(), testcase.TestCase.EX_RUN_ERROR)
+        mock_subproc.assert_not_called()
+
+    @mock.patch('subprocess.Popen')
+    def test_run_ko(self, mock_subproc):
+        with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen:
+            mock_obj = mock.Mock()
+            attrs = {'wait.return_value': 1}
+            mock_obj.configure_mock(**attrs)
+
+            mock_subproc.return_value = mock_obj
+            self._test_run(testcase.TestCase.EX_RUN_ERROR)
+            mopen.assert_called_once_with(self._output_file, "w+")
+
+    @mock.patch('subprocess.Popen')
+    def test_run(self, mock_subproc):
+        with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen:
+            mock_obj = mock.Mock()
+            attrs = {'wait.return_value': 0}
+            mock_obj.configure_mock(**attrs)
+
+            mock_subproc.return_value = mock_obj
+            self._test_run(testcase.TestCase.EX_OK)
+            mopen.assert_called_once_with(self._output_file, "w+")
 
 
 if __name__ == "__main__":