Move rally and tempest out of functest-core
[functest-xtesting.git] / functest / tests / unit / openstack / tempest / test_conf_utils.py
index 7755808..f20a7e9 100644 (file)
@@ -88,8 +88,62 @@ class OSTempestConfUtilsTesting(unittest.TestCase):
         msg = 'Failed to create flavor'
         self.assertTrue(msg in context.exception, msg=str(context.exception))
 
-    def test_get_verifier_id_missing_verifier(self):
-        CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
+    def _get_rally_creds(self):
+        return {"type": "ExistingCloud",
+                "admin": {"username": 'test_user_name',
+                          "password": 'test_password',
+                          "tenant": 'test_tenant'}}
+
+    @mock.patch('functest.utils.openstack_utils.get_credentials_for_rally')
+    @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
+                '.logger.info')
+    @mock.patch('functest.utils.functest_utils.execute_command_raise')
+    @mock.patch('functest.utils.functest_utils.execute_command')
+    def test_create_rally_deployment(self, mock_exec, mock_exec_raise,
+                                     mock_logger_info, mock_os_utils):
+
+        mock_os_utils.return_value = self._get_rally_creds()
+
+        conf_utils.create_rally_deployment()
+
+        cmd = "rally deployment destroy opnfv-rally"
+        error_msg = "Deployment %s does not exist." % \
+                    CONST.__getattribute__('rally_deployment_name')
+        mock_logger_info.assert_any_call("Creating Rally environment...")
+        mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
+
+        cmd = "rally deployment create --file=rally_conf.json --name="
+        cmd += CONST.__getattribute__('rally_deployment_name')
+        error_msg = "Problem while creating Rally deployment"
+        mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
+
+        cmd = "rally deployment check"
+        error_msg = ("OpenStack not responding or "
+                     "faulty Rally deployment.")
+        mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
+
+    @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
+                '.logger.debug')
+    def test_create_verifier(self, mock_logger_debug):
+        mock_popen = mock.Mock()
+        attrs = {'poll.return_value': None,
+                 'stdout.readline.return_value': '0'}
+        mock_popen.configure_mock(**attrs)
+
+        CONST.__setattr__('tempest_verifier_name', 'test_veifier_name')
+        with mock.patch('functest.utils.functest_utils.execute_command_raise',
+                        side_effect=Exception), \
+                self.assertRaises(Exception):
+            conf_utils.create_verifier()
+            mock_logger_debug.assert_any_call("Tempest test_veifier_name"
+                                              " does not exist")
+
+    @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
+                'create_verifier', return_value=mock.Mock())
+    @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
+                'create_rally_deployment', return_value=mock.Mock())
+    def test_get_verifier_id_missing_verifier(self, mock_rally, mock_tempest):
+        CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
         with mock.patch('functest.opnfv_tests.openstack.tempest.'
                         'conf_utils.subprocess.Popen') as mock_popen, \
                 self.assertRaises(Exception):
@@ -97,10 +151,14 @@ class OSTempestConfUtilsTesting(unittest.TestCase):
             attrs = {'stdout.readline.return_value': ''}
             mock_stdout.configure_mock(**attrs)
             mock_popen.return_value = mock_stdout
-            conf_utils.get_verifier_id(),
-
-    def test_get_verifier_id_default(self):
-        CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
+            conf_utils.get_verifier_id()
+
+    @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
+                'create_verifier', return_value=mock.Mock())
+    @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
+                'create_rally_deployment', return_value=mock.Mock())
+    def test_get_verifier_id_default(self, mock_rally, mock_tempest):
+        CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
         with mock.patch('functest.opnfv_tests.openstack.tempest.'
                         'conf_utils.subprocess.Popen') as mock_popen:
             mock_stdout = mock.Mock()
@@ -112,7 +170,7 @@ class OSTempestConfUtilsTesting(unittest.TestCase):
                              'test_deploy_id')
 
     def test_get_verifier_deployment_id_missing_rally(self):
-        CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
+        CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
         with mock.patch('functest.opnfv_tests.openstack.tempest.'
                         'conf_utils.subprocess.Popen') as mock_popen, \
                 self.assertRaises(Exception):
@@ -123,7 +181,7 @@ class OSTempestConfUtilsTesting(unittest.TestCase):
             conf_utils.get_verifier_deployment_id(),
 
     def test_get_verifier_deployment_id_default(self):
-        CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
+        CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
         with mock.patch('functest.opnfv_tests.openstack.tempest.'
                         'conf_utils.subprocess.Popen') as mock_popen:
             mock_stdout = mock.Mock()
@@ -157,22 +215,25 @@ class OSTempestConfUtilsTesting(unittest.TestCase):
             self.assertTrue(m1.called)
             self.assertTrue(m2.called)
 
-    def test_get_repo_tag_default(self):
-        mock_popen = mock.Mock()
-        attrs = {'stdout.readline.return_value': 'test_tag'}
-        mock_popen.configure_mock(**attrs)
-
-        with mock.patch('functest.opnfv_tests.openstack.tempest.'
-                        'conf_utils.subprocess.Popen',
-                        return_value=mock_popen):
-            self.assertEqual(conf_utils.get_repo_tag('test_repo'),
-                             'test_tag')
-
     def test_backup_tempest_config_default(self):
         with mock.patch('functest.opnfv_tests.openstack.tempest.'
-                        'conf_utils.shutil.copyfile') as m1:
+                        'conf_utils.os.path.exists',
+                        return_value=False), \
+            mock.patch('functest.opnfv_tests.openstack.tempest.'
+                       'conf_utils.os.makedirs') as m1, \
+            mock.patch('functest.opnfv_tests.openstack.tempest.'
+                       'conf_utils.shutil.copyfile') as m2:
             conf_utils.backup_tempest_config('test_conf_file')
             self.assertTrue(m1.called)
+            self.assertTrue(m2.called)
+
+        with mock.patch('functest.opnfv_tests.openstack.tempest.'
+                        'conf_utils.os.path.exists',
+                        return_value=True), \
+            mock.patch('functest.opnfv_tests.openstack.tempest.'
+                       'conf_utils.shutil.copyfile') as m2:
+            conf_utils.backup_tempest_config('test_conf_file')
+            self.assertTrue(m2.called)
 
     def test_configure_tempest_default(self):
         with mock.patch('functest.opnfv_tests.openstack.tempest.'