Fix role processing in Patrole
[functest.git] / functest / tests / unit / openstack / tempest / test_tempest.py
index 8600506..1fea4e4 100644 (file)
@@ -20,6 +20,7 @@ from functest.opnfv_tests.openstack.tempest import conf_utils
 
 
 class OSTempestTesting(unittest.TestCase):
+    # pylint: disable=too-many-public-methods
 
     def setUp(self):
         os_creds = OSCreds(
@@ -47,6 +48,7 @@ class OSTempestTesting(unittest.TestCase):
             self.tempestfull_parallel = tempest.TempestFullParallel()
             self.tempestcustom = tempest.TempestCustom()
             self.tempestdefcore = tempest.TempestDefcore()
+            self.tempestneutrontrunk = tempest.TempestNeutronTrunk()
 
     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.error')
     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
@@ -58,7 +60,7 @@ class OSTempestTesting(unittest.TestCase):
                         'os.path.isfile', return_value=False), \
                 self.assertRaises(Exception) as context:
             msg = "Tempest test list file %s NOT found."
-            self.tempestcommon.generate_test_list('test_verifier_repo_dir')
+            self.tempestcommon.generate_test_list()
             self.assertTrue(
                 (msg % conf_utils.TEMPEST_CUSTOM) in context.exception)
 
@@ -68,26 +70,23 @@ class OSTempestTesting(unittest.TestCase):
                         'shutil.copyfile') as mock_copyfile, \
             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
                        'os.path.isfile', return_value=True):
-            self.tempestcommon.generate_test_list('test_verifier_repo_dir')
+            self.tempestcommon.generate_test_list()
             self.assertTrue(mock_copyfile.called)
 
-    @mock.patch('functest.utils.functest_utils.execute_command')
+    @mock.patch('subprocess.check_output')
     def _test_gen_tl_mode_default(self, mode, mock_exec=None):
         self.tempestcommon.mode = mode
         if self.tempestcommon.mode == 'smoke':
-            testr_mode = r"'tempest\.(api|scenario).*\[.*\bsmoke\b.*\]'"
+            testr_mode = r"'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'"
         elif self.tempestcommon.mode == 'full':
             testr_mode = r"'^tempest\.'"
         else:
-            testr_mode = 'tempest.api.' + self.tempestcommon.mode
-        conf_utils.TEMPEST_RAW_LIST = 'raw_list'
+            testr_mode = self.tempestcommon.mode
         verifier_repo_dir = 'test_verifier_repo_dir'
-        cmd = ("cd {0};"
-               "testr list-tests {1} > {2};"
-               "cd -;".format(verifier_repo_dir, testr_mode,
-                              conf_utils.TEMPEST_RAW_LIST))
-        self.tempestcommon.generate_test_list('test_verifier_repo_dir')
-        mock_exec.assert_any_call(cmd)
+        cmd = "(cd {0}; testr list-tests {1} >{2} 2>/dev/null)".format(
+            verifier_repo_dir, testr_mode, self.tempestcommon.list)
+        self.tempestcommon.generate_test_list()
+        mock_exec.assert_called_once_with(cmd, shell=True)
 
     def test_gen_tl_smoke_mode(self):
         self._test_gen_tl_mode_default('smoke')
@@ -95,12 +94,33 @@ class OSTempestTesting(unittest.TestCase):
     def test_gen_tl_full_mode(self):
         self._test_gen_tl_mode_default('full')
 
+    def test_gen_tl_neutron_trunk_mode(self):
+        self._test_gen_tl_mode_default('neutron_trunk')
+
     def test_verif_res_missing_verif_id(self):
         self.tempestcommon.verification_id = None
         with self.assertRaises(Exception):
             self.tempestcommon.parse_verifier_result()
 
-    def test_apply_missing_blacklist(self):
+    def test_backup_config_default(self):
+        with mock.patch('os.path.exists', return_value=False), \
+                mock.patch('os.makedirs') as mock_makedirs, \
+                mock.patch('shutil.copyfile') as mock_copyfile:
+            self.tempestcommon.backup_tempest_config(
+                'test_conf_file', res_dir='test_dir')
+            self.assertTrue(mock_makedirs.called)
+            self.assertTrue(mock_copyfile.called)
+
+        with mock.patch('os.path.exists', return_value=True), \
+                mock.patch('shutil.copyfile') as mock_copyfile:
+            self.tempestcommon.backup_tempest_config(
+                'test_conf_file', res_dir='test_dir')
+            self.assertTrue(mock_copyfile.called)
+
+    @mock.patch("os.rename")
+    @mock.patch("os.remove")
+    @mock.patch("os.path.exists", return_value=True)
+    def test_apply_missing_blacklist(self, *args):
         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
             mock.patch.object(self.tempestcommon, 'read_file',
                               return_value=['test1', 'test2']):
@@ -111,8 +131,15 @@ class OSTempestTesting(unittest.TestCase):
             obj = mock_open()
             obj.write.assert_any_call('test1\n')
             obj.write.assert_any_call('test2\n')
+            args[0].assert_called_once_with(self.tempestcommon.raw_list)
+            args[1].assert_called_once_with(self.tempestcommon.raw_list)
+            args[2].assert_called_once_with(
+                self.tempestcommon.list, self.tempestcommon.raw_list)
 
-    def test_apply_blacklist_default(self):
+    @mock.patch("os.rename")
+    @mock.patch("os.remove")
+    @mock.patch("os.path.exists", return_value=True)
+    def test_apply_blacklist_default(self, *args):
         item_dict = {'scenarios': ['deploy_scenario'],
                      'installers': ['installer_type'],
                      'tests': ['test2']}
@@ -127,6 +154,10 @@ class OSTempestTesting(unittest.TestCase):
             obj = mock_open()
             obj.write.assert_any_call('test1\n')
             self.assertFalse(obj.write.assert_any_call('test2\n'))
+            args[0].assert_called_once_with(self.tempestcommon.raw_list)
+            args[1].assert_called_once_with(self.tempestcommon.raw_list)
+            args[2].assert_called_once_with(
+                self.tempestcommon.list, self.tempestcommon.raw_list)
 
     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
     def test_run_verifier_tests_default(self, mock_logger_info):
@@ -146,7 +177,7 @@ class OSTempestTesting(unittest.TestCase):
                 'subprocess.Popen')
     def test_generate_report(self, mock_popen):
         self.tempestcommon.verification_id = "1234"
-        html_file = os.path.join(conf_utils.TEMPEST_RESULTS_DIR,
+        html_file = os.path.join(tempest.TempestCommon.TEMPEST_RESULTS_DIR,
                                  "tempest-report.html")
         cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
                "1234", "--to", html_file]
@@ -193,7 +224,7 @@ class OSTempestTesting(unittest.TestCase):
     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
                 'get_active_compute_cnt', return_value=2)
     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                'conf_utils.configure_tempest', side_effect=Exception)
+                'TempestCommon.configure', side_effect=Exception)
     def test_run_configure_tempest_ko(self, *args):
         # pylint: disable=unused-argument
         self.assertEqual(self.tempestcommon.run(),
@@ -207,7 +238,7 @@ class OSTempestTesting(unittest.TestCase):
     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
                 'get_active_compute_cnt', return_value=2)
     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
-                'conf_utils.configure_tempest')
+                'TempestCommon.configure')
     def _test_run(self, status, *args):
         # pylint: disable=unused-argument
         self.assertEqual(self.tempestcommon.run(), status)