diff options
Diffstat (limited to 'functest/tests/unit')
-rw-r--r-- | functest/tests/unit/ci/test_check_deployment.py | 176 | ||||
-rw-r--r-- | functest/tests/unit/energy/test_functest_energy.py | 5 | ||||
-rw-r--r-- | functest/tests/unit/openstack/rally/test_rally.py | 12 |
3 files changed, 186 insertions, 7 deletions
diff --git a/functest/tests/unit/ci/test_check_deployment.py b/functest/tests/unit/ci/test_check_deployment.py new file mode 100644 index 00000000..1f44d078 --- /dev/null +++ b/functest/tests/unit/ci/test_check_deployment.py @@ -0,0 +1,176 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Ericsson and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +import logging +import mock +import unittest + +from functest.ci import check_deployment + +__author__ = "Jose Lausuch <jose.lausuch@ericsson.com>" + + +class CheckDeploymentTesting(unittest.TestCase): + """The super class which testing classes could inherit.""" + # pylint: disable=missing-docstring + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.client_test = mock.Mock() + self.deployment = check_deployment.CheckDeployment() + self.service_test = 'compute' + self.rc_file = self.deployment.rc_file + self.endpoint_test = 'http://192.168.0.6:5000/v3' + creds_attr = {'auth_url': self.endpoint_test, + 'proxy_settings': ''} + proxy_attr = {'host': '192.168.0.1', 'port': '5000'} + proxy_settings = mock.Mock() + proxy_settings.configure_mock(**proxy_attr) + self.os_creds = mock.Mock() + self.os_creds.configure_mock(**creds_attr) + self.os_creds.proxy_settings = proxy_settings + self.deployment.os_creds = self.os_creds + + def test_check_rc(self): + with mock.patch('functest.ci.check_deployment.os.path.isfile', + returns=True) as m, \ + mock.patch('__builtin__.open', + mock.mock_open(read_data='OS_AUTH_URL')): + self.deployment.check_rc() + self.assertTrue(m.called) + + def test_check_rc_missing_file(self): + with mock.patch('functest.ci.check_deployment.os.path.isfile', + return_value=False), \ + self.assertRaises(Exception) as context: + msg = 'RC file {} does not exist!'.format(self.rc_file) + self.deployment.check_rc(self.rc_file) + self.assertTrue(msg in context) + + def test_check_rc_missing_os_auth(self): + with mock.patch('__builtin__.open', + mock.mock_open(read_data='test')), \ + self.assertRaises(Exception) as context: + msg = 'OS_AUTH_URL not defined in {}.'.format(self.rc_file) + self.assertTrue(msg in context) + + def test_check_auth_endpoint(self): + with mock.patch('functest.ci.check_deployment.verify_connectivity', + return_value=True) as m: + self.deployment.check_auth_endpoint() + self.assertTrue(m.called) + + def test_check_auth_endpoint_not_reachable(self): + with mock.patch('functest.ci.check_deployment.verify_connectivity', + return_value=False) as m, \ + self.assertRaises(Exception) as context: + endpoint = self.os_creds.auth_url + self.deployment.check_auth_endpoint() + msg = "OS_AUTH_URL {} is not reachable.".format(endpoint) + self.assertTrue(m.called) + self.assertTrue(msg in context) + + def test_check_public_endpoint(self): + with mock.patch('functest.ci.check_deployment.verify_connectivity', + return_value=True) as m, \ + mock.patch('functest.ci.check_deployment.keystone_utils.' + 'get_endpoint') as n: + self.deployment.check_public_endpoint() + self.assertTrue(m.called) + self.assertTrue(n.called) + + def test_check_public_endpoint_not_reachable(self): + with mock.patch('functest.ci.check_deployment.verify_connectivity', + return_value=False) as m, \ + mock.patch('functest.ci.check_deployment.keystone_utils.' + 'get_endpoint', + return_value=self.endpoint_test) as n, \ + self.assertRaises(Exception) as context: + self.deployment.check_public_endpoint() + msg = ("Public endpoint {} is not reachable." + .format(self.mock_endpoint)) + self.assertTrue(m.called) + self.assertTrue(n.called) + self.assertTrue(msg in context) + + def test_check_service_endpoint(self): + with mock.patch('functest.ci.check_deployment.verify_connectivity', + return_value=True) as m, \ + mock.patch('functest.ci.check_deployment.keystone_utils.' + 'get_endpoint') as n: + self.deployment.check_service_endpoint(self.service_test) + self.assertTrue(m.called) + self.assertTrue(n.called) + + def test_check_service_endpoint_not_reachable(self): + with mock.patch('functest.ci.check_deployment.verify_connectivity', + return_value=False) as m, \ + mock.patch('functest.ci.check_deployment.keystone_utils.' + 'get_endpoint', + return_value=self.endpoint_test) as n, \ + self.assertRaises(Exception) as context: + self.deployment.check_service_endpoint(self.service_test) + msg = "{} endpoint {} is not reachable.".format(self.service_test, + self.endpoint_test) + self.assertTrue(m.called) + self.assertTrue(n.called) + self.assertTrue(msg in context) + + def test_check_nova(self): + with mock.patch('functest.ci.check_deployment.nova_utils.nova_client', + return_value=self.client_test) as m: + self.deployment.check_nova() + self.assertTrue(m.called) + + def test_check_nova_fail(self): + with mock.patch('functest.ci.check_deployment.nova_utils.nova_client', + return_value=self.client_test) as m, \ + mock.patch.object(self.client_test, 'servers.list', + side_effect=Exception): + self.deployment.check_nova() + self.assertTrue(m.called) + self.assertRaises(Exception) + + def test_check_neutron(self): + with mock.patch('functest.ci.check_deployment.neutron_utils.' + 'neutron_client', return_value=self.client_test) as m: + self.deployment.check_neutron() + self.assertTrue(m.called) + + def test_check_neutron_fail(self): + with mock.patch('functest.ci.check_deployment.neutron_utils.' + 'neutron_client', + return_value=self.client_test) as m, \ + mock.patch.object(self.client_test, 'list_networks', + side_effect=Exception), \ + self.assertRaises(Exception): + self.deployment.check_neutron() + self.assertRaises(Exception) + self.assertTrue(m.called) + + def test_check_glance(self): + with mock.patch('functest.ci.check_deployment.glance_utils.' + 'glance_client', return_value=self.client_test) as m: + self.deployment.check_glance() + self.assertTrue(m.called) + + def test_check_glance_fail(self): + with mock.patch('functest.ci.check_deployment.glance_utils.' + 'glance_client', return_value=self.client_test) as m, \ + mock.patch.object(self.client_test, 'images.list', + side_effect=Exception): + self.deployment.check_glance() + self.assertRaises(Exception) + self.assertTrue(m.called) + + +if __name__ == "__main__": + logging.disable(logging.CRITICAL) + unittest.main(verbosity=2) diff --git a/functest/tests/unit/energy/test_functest_energy.py b/functest/tests/unit/energy/test_functest_energy.py index 177788bc..f8bb13c9 100644 --- a/functest/tests/unit/energy/test_functest_energy.py +++ b/functest/tests/unit/energy/test_functest_energy.py @@ -248,7 +248,9 @@ class EnergyRecorderTest(unittest.TestCase): self.__decorated_method() == self.returned_value_to_preserve ) - def test_decorator_preserve_ex(self): + @mock.patch( + "functest.energy.energy.finish_session") + def test_decorator_preserve_ex(self, finish_mock=None): """Test that decorator preserve method exceptions.""" self.test_load_config() with self.assertRaises(Exception) as context: @@ -256,6 +258,7 @@ class EnergyRecorderTest(unittest.TestCase): self.assertTrue( self.exception_message_to_preserve in context.exception ) + self.assertTrue(finish_mock.called) @mock.patch("functest.utils.functest_utils.get_functest_config", side_effect=config_loader_mock) diff --git a/functest/tests/unit/openstack/rally/test_rally.py b/functest/tests/unit/openstack/rally/test_rally.py index def9c93b..32cc1513 100644 --- a/functest/tests/unit/openstack/rally/test_rally.py +++ b/functest/tests/unit/openstack/rally/test_rally.py @@ -217,7 +217,7 @@ class OSRallyTesting(unittest.TestCase): self.assertRaises(Exception): self.rally_base._run_task('test_name') - @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.info') + @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info') def test_run_task_no_tests_for_scenario(self, mock_logger_info): with mock.patch('functest.opnfv_tests.openstack.rally.rally.' 'os.path.exists', @@ -227,10 +227,10 @@ class OSRallyTesting(unittest.TestCase): mock.patch.object(self.rally_base, 'file_is_empty', return_value=True): self.rally_base._run_task('test_name') - str = 'No tests for scenario "test_name"' - mock_logger_info.assert_any_call(str) + mock_logger_info.assert_any_call('No tests for scenario \"%s\"', + 'test_name') - @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.error') + @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error') def test_run_task_taskid_missing(self, mock_logger_error): with mock.patch('functest.opnfv_tests.openstack.rally.rally.' 'os.path.exists', @@ -253,8 +253,8 @@ class OSRallyTesting(unittest.TestCase): str = 'Failed to retrieve task_id, validating task...' mock_logger_error.assert_any_call(str) - @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.info') - @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.error') + @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info') + @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error') def test_run_task_default(self, mock_logger_error, mock_logger_info): popen = mock.Mock() |