Merge "Re-Enable Promise testcases"
[functest.git] / functest / tests / unit / openstack / tempest / test_tempest.py
index e1653a4..b8b258b 100644 (file)
@@ -18,8 +18,6 @@ from functest.utils.constants import CONST
 
 class OSTempestTesting(unittest.TestCase):
 
-    logging.disable(logging.CRITICAL)
-
     def setUp(self):
         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
                         'conf_utils.get_verifier_id',
@@ -114,8 +112,8 @@ class OSTempestTesting(unittest.TestCase):
             mock.patch.object(self.tempestcommon, 'read_file',
                               return_value=['test1', 'test2']):
             conf_utils.TEMPEST_BLACKLIST = Exception
-            CONST.INSTALLER_TYPE = 'installer_type'
-            CONST.DEPLOY_SCENARIO = 'deploy_scenario'
+            CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
+            CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
             self.tempestcommon.apply_tempest_blacklist()
             obj = m()
             obj.write.assert_any_call('test1\n')
@@ -130,8 +128,8 @@ class OSTempestTesting(unittest.TestCase):
                               return_value=['test1', 'test2']), \
             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
                        'yaml.safe_load', return_value=item_dict):
-            CONST.INSTALLER_TYPE = 'installer_type'
-            CONST.DEPLOY_SCENARIO = 'deploy_scenario'
+            CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
+            CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
             self.tempestcommon.apply_tempest_blacklist()
             obj = m()
             obj.write.assert_any_call('test1\n')
@@ -151,119 +149,84 @@ class OSTempestTesting(unittest.TestCase):
                 assert_any_call("Starting Tempest test suite: '%s'."
                                 % cmd_line)
 
-    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.info')
-    def test_parse_verifier_result_default(self, mock_logger_info):
-        self.tempestcommon.VERIFICATION_ID = 'test_uuid'
-        self.tempestcommon.case_name = 'test_case_name'
-        stdout = ['Testscount||2', 'Success||2', 'Skipped||0', 'Failures||0']
-        with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                        'subprocess.Popen') as mock_popen, \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'ft_utils.check_success_rate') as mock_method, \
-                mock.patch('__builtin__.open', mock.mock_open()):
-            mock_stdout = mock.Mock()
-            attrs = {'stdout': stdout}
-            mock_stdout.configure_mock(**attrs)
-            mock_popen.return_value = mock_stdout
-
-            self.tempestcommon.parse_verifier_result()
-            mock_method.assert_any_call('test_case_name', 100)
-
-    def test_run_missing_create_tempest_dir(self):
-        ret = testcase.TestCase.EX_RUN_ERROR
-        with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                        'os.path.exists', return_value=False), \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'os.makedirs') as mock_os_makedirs, \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'conf_utils.create_tempest_resources',
-                       return_value="image_and_flavor"):
-            self.assertEqual(self.tempestcommon.run(),
-                             ret)
-            self.assertTrue(mock_os_makedirs.called)
-
-    def test_run_missing_configure_tempest(self):
-        ret = testcase.TestCase.EX_RUN_ERROR
-        ret_ok = testcase.TestCase.EX_OK
-        with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                        'os.path.exists', return_value=False), \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'os.makedirs') as mock_os_makedirs, \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'conf_utils.create_tempest_resources',
-                       return_value=ret_ok), \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'conf_utils.configure_tempest',
-                       return_value=ret):
-            self.assertEqual(self.tempestcommon.run(),
-                             ret)
-            self.assertTrue(mock_os_makedirs.called)
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
+                'os.path.exists', return_value=False)
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
+                side_effect=Exception)
+    def test_run_makedirs_ko(self, *args):
+        self.assertEqual(self.tempestcommon.run(),
+                         testcase.TestCase.EX_RUN_ERROR)
+
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
+                'os.path.exists', return_value=False)
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
+                'conf_utils.create_tempest_resources', side_effect=Exception)
+    def test_run_create_tempest_resources_ko(self, *args):
+        self.assertEqual(self.tempestcommon.run(),
+                         testcase.TestCase.EX_RUN_ERROR)
+
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
+                'os.path.exists', return_value=False)
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
+                'conf_utils.create_tempest_resources', return_value={})
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
+                'conf_utils.configure_tempest', side_effect=Exception)
+    def test_run_configure_tempest_ko(self, *args):
+        self.assertEqual(self.tempestcommon.run(),
+                         testcase.TestCase.EX_RUN_ERROR)
+
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
+                'os.path.exists', return_value=False)
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
+                'conf_utils.create_tempest_resources', return_value={})
+    @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
+                'conf_utils.configure_tempest')
+    def _test_run(self, status, *args):
+        self.assertEqual(self.tempestcommon.run(), status)
 
     def test_run_missing_generate_test_list(self):
-        ret = testcase.TestCase.EX_RUN_ERROR
-        ret_ok = testcase.TestCase.EX_OK
-        with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                        'os.path.exists', return_value=False), \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'os.makedirs') as mock_os_makedirs, \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'conf_utils.create_tempest_resources',
-                       return_value=ret_ok), \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'conf_utils.configure_tempest',
-                       return_value=ret_ok), \
-            mock.patch.object(self.tempestcommon, 'generate_test_list',
-                              return_value=ret):
-            self.assertEqual(self.tempestcommon.run(),
-                             ret)
-            self.assertTrue(mock_os_makedirs.called)
-
-    def test_run_missing_apply_tempest_blacklist(self):
-        ret = testcase.TestCase.EX_RUN_ERROR
-        ret_ok = testcase.TestCase.EX_OK
-        with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                        'os.path.exists', return_value=False), \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'os.makedirs') as mock_os_makedirs, \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'conf_utils.create_tempest_resources',
-                       return_value=ret_ok), \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'conf_utils.configure_tempest',
-                       return_value=ret_ok), \
-            mock.patch.object(self.tempestcommon, 'generate_test_list',
-                              return_value=ret_ok), \
-            mock.patch.object(self.tempestcommon, 'apply_tempest_blacklist',
-                              return_value=ret):
-            self.assertEqual(self.tempestcommon.run(),
-                             ret)
-            self.assertTrue(mock_os_makedirs.called)
-
-    def test_run_missing_parse_verifier_result(self):
-        ret = testcase.TestCase.EX_RUN_ERROR
-        ret_ok = testcase.TestCase.EX_OK
-        with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                        'os.path.exists', return_value=False), \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'os.makedirs') as mock_os_makedirs, \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'conf_utils.create_tempest_resources',
-                       return_value=ret_ok), \
-            mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                       'conf_utils.configure_tempest',
-                       return_value=ret_ok), \
-            mock.patch.object(self.tempestcommon, 'generate_test_list',
-                              return_value=ret_ok), \
-            mock.patch.object(self.tempestcommon, 'apply_tempest_blacklist',
-                              return_value=ret_ok), \
-            mock.patch.object(self.tempestcommon, 'run_verifier_tests',
-                              return_value=ret_ok), \
-            mock.patch.object(self.tempestcommon, 'parse_verifier_result',
-                              return_value=ret):
-            self.assertEqual(self.tempestcommon.run(),
-                             ret)
-            self.assertTrue(mock_os_makedirs.called)
+        with mock.patch.object(self.tempestcommon, 'generate_test_list',
+                               side_effect=Exception):
+            self._test_run(testcase.TestCase.EX_RUN_ERROR)
+
+    def test_run_apply_tempest_blacklist_ko(self):
+        with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
+                    mock.patch.object(self.tempestcommon,
+                                      'apply_tempest_blacklist',
+                                      side_effect=Exception()):
+            self._test_run(testcase.TestCase.EX_RUN_ERROR)
+
+    def test_run_verifier_tests_ko(self, *args):
+        with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
+                mock.patch.object(self.tempestcommon,
+                                  'apply_tempest_blacklist'), \
+                mock.patch.object(self.tempestcommon, 'run_verifier_tests',
+                                  side_effect=Exception()), \
+                mock.patch.object(self.tempestcommon, 'parse_verifier_result',
+                                  side_effect=Exception):
+            self._test_run(testcase.TestCase.EX_RUN_ERROR)
+
+    def test_run_parse_verifier_result_ko(self, *args):
+        with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
+                mock.patch.object(self.tempestcommon,
+                                  'apply_tempest_blacklist'), \
+                mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
+                mock.patch.object(self.tempestcommon, 'parse_verifier_result',
+                                  side_effect=Exception):
+            self._test_run(testcase.TestCase.EX_RUN_ERROR)
+
+    def test_run(self, *args):
+        with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
+                mock.patch.object(self.tempestcommon,
+                                  'apply_tempest_blacklist'), \
+                mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
+                mock.patch.object(self.tempestcommon, 'parse_verifier_result'):
+            self._test_run(testcase.TestCase.EX_OK)
 
 
 if __name__ == "__main__":
+    logging.disable(logging.CRITICAL)
     unittest.main(verbosity=2)